Skip to content

Commit

Permalink
feat(planners): implement remove planner item
Browse files Browse the repository at this point in the history
  • Loading branch information
inhibitor1217 committed Mar 20, 2024
1 parent cc0d5e7 commit 757ba1f
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/modules/planners/planners.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,14 @@ export class PlannersController {

@Post(':plannerId/remove-item')
async removePlanner(
@Body() dto: PlannerRemoveItemDto,
@Body() removeItem: PlannerRemoveItemDto,
@Param('plannerId') plannerId: number,
@GetUser() user: session_userprofile,
): Promise<PlannerResponseDto> {
throw new Error('Not implemented');
return await this.plannersService.removePlannerItem(
plannerId,
removeItem,
user,
);
}
}
50 changes: 49 additions & 1 deletion src/modules/planners/planners.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { Injectable } from '@nestjs/common';
import {
BadRequestException,
Injectable,
NotFoundException,
} from '@nestjs/common';
import { session_userprofile } from '@prisma/client';
import {
PlannerBodyDto,
PlannerQueryDto,
PlannerRemoveItemDto,
} from 'src/common/interfaces/dto/planner/planner.request.dto';
import { PlannerResponseDto } from 'src/common/interfaces/dto/planner/planner.response.dto';
import { toJsonPlanner } from 'src/common/interfaces/serializer/planner.serializer';
Expand Down Expand Up @@ -93,4 +98,47 @@ export class PlannersService {

return toJsonPlanner(planner);
}

public async removePlannerItem(
plannerId: number,
removeItem: PlannerRemoveItemDto,
user: session_userprofile,
): Promise<PlannerResponseDto> {
switch (removeItem.item_type) {
case 'TAKEN':
throw new BadRequestException(
'Planner item with type "taken" can\'t be deleted',
);
case 'FUTURE': {
const futureItem =
await this.PlannerRepository.getFuturePlannerItemById(
user,
removeItem.item,
);
if (futureItem.planner_id !== plannerId) {
throw new NotFoundException();
}
await this.PlannerRepository.deleteFuturePlannerItem(futureItem);
break;
}
case 'ARBITRARY': {
const arbitraryItem =
await this.PlannerRepository.getArbitraryPlannerItemById(
user,
removeItem.item,
);
if (arbitraryItem.planner_id !== plannerId) {
throw new NotFoundException();
}
await this.PlannerRepository.deleteArbitraryPlannerItem(arbitraryItem);
break;
}
}

const planner = await this.PlannerRepository.getPlannerById(
user,
plannerId,
);
return toJsonPlanner(planner);
}
}
33 changes: 33 additions & 0 deletions src/prisma/repositories/planner.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,23 @@ export class PlannerRepository {
});
}

public async getPlannerById(
user: session_userprofile,
id: number,
): Promise<PlannerDetails> {
const planner = await this.prisma.planner_planner.findFirst({
...plannerDetails,
where: {
user_id: user.id,
id: id,
},
});
if (!planner) {
throw new NotFoundException();
}
return planner;
}

public async getRelatedPlanner(
user: session_userprofile,
): Promise<PlannerBasic[]> {
Expand Down Expand Up @@ -190,6 +207,14 @@ export class PlannerRepository {
});
}

public async deleteFuturePlannerItem(target_item: FuturePlannerItem) {
return await this.prisma.planner_futureplanneritem.delete({
where: {
id: target_item.id,
},
});
}

public async getArbitraryPlannerItemById(
user: session_userprofile,
id: number,
Expand Down Expand Up @@ -247,4 +272,12 @@ export class PlannerRepository {
},
});
}

public async deleteArbitraryPlannerItem(target_item: ArbitraryPlannerItem) {
return await this.prisma.planner_arbitraryplanneritem.delete({
where: {
id: target_item.id,
},
});
}
}

0 comments on commit 757ba1f

Please sign in to comment.