Skip to content

Commit

Permalink
refactor: Update User model relation to set planId to null on deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
PleBea committed Jul 19, 2024
1 parent 9d19143 commit d06fad2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 22 deletions.
14 changes: 7 additions & 7 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ model User {
profileImage String?
UserProfileImage UserProfileImage[]
planId String?
Plan Plan? @relation(fields: [planId], references: [id], onDelete: SetNull)
plan Plan? @relation(fields: [planId], references: [id], onDelete: SetNull)
FCMToken String?
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
Invite Invite?
invite Invite?
}

model UserProfileImage {
url String @id
userId String
User User @relation(fields: [userId], references: [id])
user User @relation(fields: [userId], references: [id])
createdAt DateTime @default(now())
}

Expand All @@ -47,7 +47,7 @@ model Plan {
place Place?
status PlanStatus @default(WAITING)
users User[]
Invite Invite[]
invite Invite[]
createdAt DateTime @default(now())
}

Expand All @@ -58,14 +58,14 @@ model Place {
x String
y String
planId String @unique
Plan Plan @relation(fields: [planId], references: [id], onDelete: Cascade)
plan Plan @relation(fields: [planId], references: [id], onDelete: Cascade)
}

model Invite {
id String @id @default(uuid())
planId String
Plan Plan @relation(fields: [planId], references: [id], onDelete: Cascade)
plan Plan @relation(fields: [planId], references: [id], onDelete: Cascade)
userId String @unique
User User @relation(fields: [userId], references: [id])
user User @relation(fields: [userId], references: [id])
createdAt DateTime @default(now())
}
4 changes: 2 additions & 2 deletions src/modules/plan/dto/invite-response.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ export class InviteResponseDTO implements Invite {
@ApiProperty({
type: UserResponseDTO,
})
User: User;
user: User;

@ApiProperty()
userId: string;

@ApiProperty({
type: PlanDTO,
})
Plan: Plan;
plan: Plan;

@ApiProperty()
createdAt: Date;
Expand Down
28 changes: 16 additions & 12 deletions src/modules/plan/plan.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,17 @@ export class PlanService {
id: inviteId,
},
include: {
Plan: true,
User: true,
plan: {
include: {
place: true,
},
},
user: true,
},
});

if (!invite) throw new HttpException('Invite not found', HttpStatus.NOT_FOUND);
if (!invite.Plan) throw new HttpException('Plan not found', HttpStatus.NOT_FOUND);
if (!invite.plan) throw new HttpException('Plan not found', HttpStatus.NOT_FOUND);

return invite;
}
Expand All @@ -68,23 +72,23 @@ export class PlanService {
id,
},
include: {
Plan: true,
Invite: true,
plan: true,
invite: true,
},
});

if (!user) throw new HttpException('User not found', HttpStatus.NOT_FOUND);
if (!user.Plan) throw new HttpException('User does not have a plan', HttpStatus.BAD_GATEWAY);
if (user.Invite) return this.getInviteLink(user.Invite.id);
if (!user.plan) throw new HttpException('User does not have a plan', HttpStatus.BAD_GATEWAY);
if (user.invite) return this.getInviteLink(user.invite.id);

const invite = await this.prisma.invite.create({
data: {
Plan: {
plan: {
connect: {
id: user.Plan.id,
id: user.plan.id,
},
},
User: {
user: {
connect: {
id,
},
Expand All @@ -104,12 +108,12 @@ export class PlanService {
id,
},
select: {
Plan: true,
plan: true,
},
});

if (!user) return;
if (user.Plan) throw new HttpException('User already has a plan', HttpStatus.BAD_GATEWAY);
if (user.plan) throw new HttpException('User already has a plan', HttpStatus.BAD_GATEWAY);

const place = await this.addressService.getAddressByKeyword(dto.address);

Expand Down
2 changes: 1 addition & 1 deletion src/modules/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class UserService {
.create({
data: {
url,
User: { connect: { id } },
user: { connect: { id } },
},
})
.then(() =>
Expand Down

0 comments on commit d06fad2

Please sign in to comment.