feat: expand admin and data sync workflows
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Delete,
|
||||
Get,
|
||||
HttpCode,
|
||||
Param,
|
||||
Patch,
|
||||
Post,
|
||||
@@ -69,4 +71,12 @@ export class UsersController {
|
||||
void this.audit.log(this.actingId(req), "user.reset_password", { userId: id });
|
||||
return user;
|
||||
}
|
||||
|
||||
@Delete(":id")
|
||||
@HttpCode(204)
|
||||
async remove(@Param("id") id: string, @Req() req: Request) {
|
||||
const actingId = this.actingId(req);
|
||||
await this.users.remove(id, actingId);
|
||||
void this.audit.log(actingId, "user.delete", { userId: id });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,6 +111,30 @@ export class UsersService {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Hard-delete a user. The schema's ActivityLog.userId FK would otherwise
|
||||
* block the row (default `Restrict`), so null it out in the same
|
||||
* transaction. Rows + the actor id captured in the `message` JSON stay
|
||||
* intact for the audit trail.
|
||||
*/
|
||||
async remove(id: string, actingUserId: string): Promise<void> {
|
||||
if (id === actingUserId) {
|
||||
throw new BadRequestException("No puede eliminar su propia cuenta");
|
||||
}
|
||||
await this.ensureExists(id);
|
||||
try {
|
||||
await this.prisma.$transaction([
|
||||
this.prisma.activityLog.updateMany({
|
||||
where: { userId: id },
|
||||
data: { userId: null },
|
||||
}),
|
||||
this.prisma.user.delete({ where: { id } }),
|
||||
]);
|
||||
} catch (e) {
|
||||
throw this.mapError(e);
|
||||
}
|
||||
}
|
||||
|
||||
private async ensureExists(id: string): Promise<void> {
|
||||
const found = await this.prisma.user.findUnique({ where: { id }, select: { id: true } });
|
||||
if (!found) throw new NotFoundException(`Usuario ${id} no encontrado`);
|
||||
|
||||
Reference in New Issue
Block a user