chore: snapshot comment authors before user deletion

This commit is contained in:
Esdras Renan 2025-10-19 14:30:59 -03:00
parent 846e575637
commit 63d6a65334
6 changed files with 319 additions and 5976 deletions

View file

@ -118,6 +118,33 @@ export const deleteUser = mutation({
throw new ConvexError("Usuário ainda está atribuído a tickets");
}
const comments = await ctx.db
.query("ticketComments")
.withIndex("by_author", (q) => q.eq("authorId", userId))
.collect();
if (comments.length > 0) {
const authorSnapshot = {
name: user.name,
email: user.email,
avatarUrl: user.avatarUrl ?? undefined,
teams: user.teams ?? undefined,
};
await Promise.all(
comments.map(async (comment) => {
const existingSnapshot = comment.authorSnapshot;
const shouldUpdate =
!existingSnapshot ||
existingSnapshot.name !== authorSnapshot.name ||
existingSnapshot.email !== authorSnapshot.email ||
existingSnapshot.avatarUrl !== authorSnapshot.avatarUrl ||
JSON.stringify(existingSnapshot.teams ?? []) !== JSON.stringify(authorSnapshot.teams ?? []);
if (shouldUpdate) {
await ctx.db.patch(comment._id, { authorSnapshot });
}
}),
);
}
await ctx.db.delete(userId);
return { status: "deleted" };
},