diff --git a/src/common/dto/index.ts b/src/common/dto/index.ts index fa41966..66d9cbc 100644 --- a/src/common/dto/index.ts +++ b/src/common/dto/index.ts @@ -1,3 +1,4 @@ export * from './oauth.dto'; export * from './jwt.dto'; export * from './token.dto'; +export * from './response.dto'; diff --git a/src/common/dto/response.dto.ts b/src/common/dto/response.dto.ts new file mode 100644 index 0000000..ae37c32 --- /dev/null +++ b/src/common/dto/response.dto.ts @@ -0,0 +1,11 @@ +import { ApiProperty } from '@nestjs/swagger'; + +type StatusType = 'success' | 'error'; + +export abstract class ResponseDTO { + @ApiProperty() + status: StatusType; + + @ApiProperty() + data: T; +} diff --git a/src/modules/plan/dto/plan-reponse.dto.ts b/src/modules/plan/dto/plan-response.dto.ts similarity index 86% rename from src/modules/plan/dto/plan-reponse.dto.ts rename to src/modules/plan/dto/plan-response.dto.ts index ea229c6..1e733a0 100644 --- a/src/modules/plan/dto/plan-reponse.dto.ts +++ b/src/modules/plan/dto/plan-response.dto.ts @@ -2,7 +2,7 @@ import { Plan, User } from '@prisma/client'; import { ApiProperty } from '@nestjs/swagger'; -import { UserResponseDTO } from '../../user/dto/user.dto'; +import { UserResponseDTO } from 'src/modules/user/dto/user.dto'; export class PlanResponseDTO implements Plan { @ApiProperty() diff --git a/src/modules/plan/plan.controller.ts b/src/modules/plan/plan.controller.ts index 3fb68f9..3d2d1b6 100644 --- a/src/modules/plan/plan.controller.ts +++ b/src/modules/plan/plan.controller.ts @@ -7,7 +7,7 @@ import { ApiBearerAuth, ApiBody, ApiOkResponse, ApiOperation, ApiTags, ApiUnauth import { CurrentUser } from 'src/common'; import { CreatePlanDTO } from './dto/create-plan.dto'; -import { PlanResponseDTO } from './dto/plan-reponse.dto'; +import { PlanResponseDTO } from './dto/plan-response.dto'; import { PlanService } from './plan.service'; @ApiTags('plan') @@ -38,6 +38,16 @@ export class PlanController { return await this.planService.createPlan(user, dto); } + @Delete() + @ApiBearerAuth() + @UseGuards(AuthGuard('access')) + @ApiOperation({ summary: 'Delete a plan' }) + @ApiOkResponse({ description: 'Plan deleted', type: PlanResponseDTO }) + @ApiUnauthorizedResponse({ description: 'Unauthorized' }) + async deletePlan(@CurrentUser() user: User): Promise { + return await this.planService.deletePlan(user); + } + @Get('invite') @ApiBearerAuth() @UseGuards(AuthGuard('access')) diff --git a/src/modules/plan/plan.service.ts b/src/modules/plan/plan.service.ts index 4a0a0dc..d088685 100644 --- a/src/modules/plan/plan.service.ts +++ b/src/modules/plan/plan.service.ts @@ -7,6 +7,7 @@ import { FirebaseService } from 'src/common/modules/firebase/firebase.service'; import { PrismaService } from 'src/common/modules/prisma/prisma.service'; import { CreatePlanDTO } from './dto/create-plan.dto'; +import { PlanResponseDTO } from './dto/plan-response.dto'; @Injectable() export class PlanService { @@ -69,6 +70,58 @@ export class PlanService { }); } + async deletePlan({ id }: User): Promise { + const user = await this.prisma.user.findUnique({ + where: { + id, + }, + }); + + const plan = await this.prisma.plan.findFirst({ + where: { + users: { + some: { + id, + }, + }, + }, + include: { + users: true, + }, + }); + + if (!user) throw new HttpException('User not found', HttpStatus.NOT_FOUND); + if (!plan) throw new HttpException('User does not have a plan', HttpStatus.BAD_GATEWAY); + + const res = + plan.users.length === 1 + ? await this.prisma.plan.delete({ + where: { + id: plan.id, + }, + include: { + users: true, + }, + }) + : await this.prisma.plan.update({ + where: { + id: plan.id, + }, + data: { + users: { + disconnect: { + id, + }, + }, + }, + include: { + users: true, + }, + }); + + return res; + } + async getInvite({ id }: User) { const invites = await this.prisma.invite.findMany({ where: {