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 27, 2024
1 parent 7b92a9a commit a86a3b9
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 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 @@ -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<PlannerResponseDto> {
throw new Error('Not implemented');
return await this.plannersService.removePlannerItem(
plannerId,
removeItem,
user,
);
}
}
45 changes: 45 additions & 0 deletions src/modules/planners/planners.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
BadRequestException,
Injectable,
NotFoundException,
UnauthorizedException,
Expand All @@ -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';
Expand Down Expand Up @@ -128,4 +130,47 @@ export class PlannersService {
});
return toJsonArbitraryItem(arbitraryItem);
}

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 @@ -88,6 +88,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 @@ -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,
Expand Down Expand Up @@ -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,
},
});
}
}

0 comments on commit a86a3b9

Please sign in to comment.