diff --git a/src/modules/planners/planners.controller.ts b/src/modules/planners/planners.controller.ts index 25346df0..04771ec3 100644 --- a/src/modules/planners/planners.controller.ts +++ b/src/modules/planners/planners.controller.ts @@ -68,10 +68,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 { - throw new Error('Not implemented'); + return await this.plannersService.removePlannerItem( + plannerId, + removeItem, + user, + ); } } diff --git a/src/modules/planners/planners.service.ts b/src/modules/planners/planners.service.ts index 866d2fdf..178a22cd 100644 --- a/src/modules/planners/planners.service.ts +++ b/src/modules/planners/planners.service.ts @@ -1,4 +1,5 @@ import { + BadRequestException, Injectable, NotFoundException, UnauthorizedException, @@ -8,6 +9,7 @@ import { IPlanner } from 'src/common/interfaces/IPlanner'; 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 { toJsonArbitraryItem } from 'src/common/interfaces/serializer/planner.item.serializer'; @@ -128,4 +130,47 @@ export class PlannersService { }); return toJsonArbitraryItem(arbitraryItem); } + + public async removePlannerItem( + plannerId: number, + removeItem: PlannerRemoveItemDto, + user: session_userprofile, + ): Promise { + 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); + } } diff --git a/src/prisma/repositories/planner.repository.ts b/src/prisma/repositories/planner.repository.ts index 9a6d8577..41405a73 100644 --- a/src/prisma/repositories/planner.repository.ts +++ b/src/prisma/repositories/planner.repository.ts @@ -88,6 +88,23 @@ export class PlannerRepository { }); } + public async getPlannerById( + user: session_userprofile, + id: number, + ): Promise { + 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 { @@ -199,6 +216,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, @@ -260,4 +285,12 @@ export class PlannerRepository { include: EArbitraryPlannerItem.Details.include, }); } + + public async deleteArbitraryPlannerItem(target_item: ArbitraryPlannerItem) { + return await this.prisma.planner_arbitraryplanneritem.delete({ + where: { + id: target_item.id, + }, + }); + } }