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

@@ -2,6 +2,7 @@ import {
Controller,
Get,
Put,
Delete,
Param,
Patch,
Body,
@@ -176,6 +177,19 @@ export class UsersController {
return this.usersService.updatePrivacyPreferences(user.id, data);
}
// =============================================
// DELETE ACCOUNT
// =============================================
@Delete('account')
@Roles(UserRole.USER)
@ApiOperation({ summary: 'Delete user account permanently (Regular users only)' })
@ApiResponse({ status: 200, description: 'Account deleted successfully' })
@ApiResponse({ status: 404, description: 'User not found' })
async deleteAccount(@CurrentUser() user: { id: string }) {
return this.usersService.deleteAccount(user.id);
}
// =============================================
// ADMIN ENDPOINTS
// =============================================

View File

@@ -485,4 +485,38 @@ export class UsersService {
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 !== UserRole.USER) {
throw new BadRequestException('This endpoint is for regular users only');
}
// Delete in a transaction to ensure data consistency
await this.prisma.$transaction(async (tx) => {
// Delete user profile if it exists
await tx.userProfile.deleteMany({
where: { userId },
});
// Delete the user account
await tx.user.delete({
where: { id: userId },
});
});
return { message: 'Account deleted successfully' };
}
}