Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions prisma/migrations/20250402170145_add_cascade_delete/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- DropForeignKey
ALTER TABLE "UserAction" DROP CONSTRAINT "UserAction_pollId_fkey";

-- DropForeignKey
ALTER TABLE "Vote" DROP CONSTRAINT "Vote_pollId_fkey";

-- AddForeignKey
ALTER TABLE "UserAction" ADD CONSTRAINT "UserAction_pollId_fkey" FOREIGN KEY ("pollId") REFERENCES "Poll"("pollId") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Vote" ADD CONSTRAINT "Vote_pollId_fkey" FOREIGN KEY ("pollId") REFERENCES "Poll"("pollId") ON DELETE CASCADE ON UPDATE CASCADE;
4 changes: 2 additions & 2 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ model UserAction {
pollId Int
type ActionType
user User @relation(fields: [userId], references: [id])
poll Poll @relation(fields: [pollId], references: [pollId])
poll Poll @relation(fields: [pollId], references: [pollId],onDelete: Cascade)

}

Expand Down Expand Up @@ -62,7 +62,7 @@ model Vote {
weightDistribution Json
proof String
user User @relation(fields: [userId], references: [id])
poll Poll @relation(fields: [pollId], references: [pollId])
poll Poll @relation(fields: [pollId], references: [pollId],onDelete: Cascade)
}

enum ActionType {
Expand Down
14 changes: 14 additions & 0 deletions src/poll/poll.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,18 @@ export class PollController {
.json({ message: 'Internal server error', error: error.message });
}
}

@Delete(':id')
async deletePoll(@Param('id') id: number, @Res() res: Response) {
const userId = 1; // need to implement Auth
try {
const poll = await this.pollService.deletePoll(userId, Number(id));

return res.status(200).json({ message: 'Poll deleted', poll: poll });
} catch (error) {
return res
.status(500)
.json({ message: 'Internal server error', error: error.message });
}
}
}
33 changes: 33 additions & 0 deletions src/poll/poll.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,37 @@ export class PollService {
throw new Error('Database query failed');
}
}

async deletePoll(userId: number, pollId: number) {
const poll = await this.databaseService.poll.findUnique({
where: { pollId },
});

if (!poll) {
throw new Error('Poll not found');
}
if (poll.authorUserId !== userId) {
throw new Error('User Not Authorized');
}

return this.databaseService.$transaction(async (tx) => {
const deleted = await tx.poll.delete({
where: {
pollId,
},
});

// Update user's pollsCreatedCount
await tx.user.update({
where: { id: deleted.authorUserId },
data: {
pollsCreatedCount: {
decrement: 1,
},
},
});

return deleted;
});
}
Comment on lines +137 to +169
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve error handling by using NestJS exception classes.

The method uses generic Error class for exceptions instead of NestJS's built-in exception classes. This is inconsistent with the rest of the service which uses BadRequestException.

  async deletePoll(userId: number, pollId: number) {
    const poll = await this.databaseService.poll.findUnique({
      where: { pollId },
    });

    if (!poll) {
-      throw new Error('Poll not found');
+      throw new BadRequestException('Poll not found');
    }
    if (poll.authorUserId !== userId) {
-      throw new Error('User Not Authorized');
+      throw new BadRequestException('User Not Authorized');
    }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
async deletePoll(userId: number, pollId: number) {
const poll = await this.databaseService.poll.findUnique({
where: { pollId },
});
if (!poll) {
throw new Error('Poll not found');
}
if (poll.authorUserId !== userId) {
throw new Error('User Not Authorized');
}
return this.databaseService.$transaction(async (tx) => {
const deleted = await tx.poll.delete({
where: {
pollId,
},
});
// Update user's pollsCreatedCount
await tx.user.update({
where: { id: deleted.authorUserId },
data: {
pollsCreatedCount: {
decrement: 1,
},
},
});
return deleted;
});
}
async deletePoll(userId: number, pollId: number) {
const poll = await this.databaseService.poll.findUnique({
where: { pollId },
});
if (!poll) {
throw new BadRequestException('Poll not found');
}
if (poll.authorUserId !== userId) {
throw new BadRequestException('User Not Authorized');
}
return this.databaseService.$transaction(async (tx) => {
const deleted = await tx.poll.delete({
where: {
pollId,
},
});
// Update user's pollsCreatedCount
await tx.user.update({
where: { id: deleted.authorUserId },
data: {
pollsCreatedCount: {
decrement: 1,
},
},
});
return deleted;
});
}

}