feat: Add account deletion functionality for both agents and regular users.

This commit is contained in:
pradeepkumar
2026-02-09 01:15:45 +05:30
parent 789551e443
commit bbbd83084a
4 changed files with 107 additions and 0 deletions

View File

@@ -893,4 +893,50 @@ export class AgentsService {
return updatedUser.privacyPreferences;
}
// =============================================
// DELETE ACCOUNT
// =============================================
async deleteAccount(userId: string) {
const user = await this.prisma.user.findUnique({
where: { id: userId },
select: { id: true, role: true },
});
if (!user) {
throw new NotFoundException('User not found');
}
if (user.role !== 'AGENT') {
throw new BadRequestException('This endpoint is for agents only');
}
// Delete in a transaction to ensure data consistency
await this.prisma.$transaction(async (tx) => {
// First, delete the agent profile and related data
const agentProfile = await tx.agentProfile.findUnique({
where: { userId },
});
if (agentProfile) {
// Delete agent profile field values
await tx.agentProfileFieldValue.deleteMany({
where: { agentProfileId: agentProfile.id },
});
// Delete the agent profile
await tx.agentProfile.delete({
where: { userId },
});
}
// Finally, delete the user account
await tx.user.delete({
where: { id: userId },
});
});
return { message: 'Account deleted successfully' };
}
}