Skip to content

Commit

Permalink
feat: Create delete plan api
Browse files Browse the repository at this point in the history
  • Loading branch information
PleBea committed Jul 13, 2024
1 parent 27e35c2 commit 049b7e7
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/common/dto/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './oauth.dto';
export * from './jwt.dto';
export * from './token.dto';
export * from './response.dto';
11 changes: 11 additions & 0 deletions src/common/dto/response.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ApiProperty } from '@nestjs/swagger';

type StatusType = 'success' | 'error';

export abstract class ResponseDTO<T> {
@ApiProperty()
status: StatusType;

@ApiProperty()
data: T;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
12 changes: 11 additions & 1 deletion src/modules/plan/plan.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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<PlanResponseDTO> {
return await this.planService.deletePlan(user);
}

@Get('invite')
@ApiBearerAuth()
@UseGuards(AuthGuard('access'))
Expand Down
53 changes: 53 additions & 0 deletions src/modules/plan/plan.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -69,6 +70,58 @@ export class PlanService {
});
}

async deletePlan({ id }: User): Promise<PlanResponseDTO> {
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: {
Expand Down

0 comments on commit 049b7e7

Please sign in to comment.