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

@ -642,6 +642,79 @@ export const importPrismaSnapshot = mutation({
},
})
export const backfillTicketCommentAuthorSnapshots = mutation({
args: {
limit: v.optional(v.number()),
dryRun: v.optional(v.boolean()),
},
handler: async (ctx, { limit, dryRun }) => {
const effectiveDryRun = Boolean(dryRun)
const maxUpdates = limit && limit > 0 ? limit : null
const comments = await ctx.db.query("ticketComments").collect()
let updated = 0
let skippedExisting = 0
let missingAuthors = 0
for (const comment of comments) {
if (comment.authorSnapshot) {
skippedExisting += 1
continue
}
if (maxUpdates !== null && updated >= maxUpdates) {
break
}
const author = await ctx.db.get(comment.authorId)
let name: string | null = author?.name ?? null
const email: string | null = author?.email ?? null
let avatarUrl: string | null = author?.avatarUrl ?? null
const teams: string[] | undefined = (author?.teams ?? undefined) as string[] | undefined
if (!author) {
missingAuthors += 1
const events = await ctx.db
.query("ticketEvents")
.withIndex("by_ticket", (q) => q.eq("ticketId", comment.ticketId))
.collect()
const matchingEvent = events.find(
(event) => event.type === "COMMENT_ADDED" && event.createdAt === comment.createdAt,
)
if (matchingEvent && matchingEvent.payload && typeof matchingEvent.payload === "object") {
const payload = matchingEvent.payload as { authorName?: string; authorAvatar?: string }
if (typeof payload.authorName === "string" && payload.authorName.trim().length > 0) {
name = payload.authorName.trim()
}
if (typeof payload.authorAvatar === "string" && payload.authorAvatar.trim().length > 0) {
avatarUrl = payload.authorAvatar
}
}
}
const snapshot = pruneUndefined({
name: name && name.trim().length > 0 ? name : "Usuário removido",
email: email ?? undefined,
avatarUrl: avatarUrl ?? undefined,
teams: teams && teams.length > 0 ? teams : undefined,
})
if (!effectiveDryRun) {
await ctx.db.patch(comment._id, { authorSnapshot: snapshot })
}
updated += 1
}
return {
dryRun: effectiveDryRun,
totalComments: comments.length,
updated,
skippedExisting,
missingAuthors,
limit: maxUpdates,
}
},
})
export const syncMachineCompanyReferences = mutation({
args: {
tenantId: v.optional(v.string()),

View file

@ -125,6 +125,14 @@ export default defineSchema({
authorId: v.id("users"),
visibility: v.string(), // PUBLIC | INTERNAL
body: v.string(),
authorSnapshot: v.optional(
v.object({
name: v.string(),
email: v.optional(v.string()),
avatarUrl: v.optional(v.string()),
teams: v.optional(v.array(v.string())),
})
),
attachments: v.optional(
v.array(
v.object({
@ -137,7 +145,9 @@ export default defineSchema({
),
createdAt: v.number(),
updatedAt: v.number(),
}).index("by_ticket", ["ticketId"]),
})
.index("by_ticket", ["ticketId"])
.index("by_author", ["authorId"]),
ticketEvents: defineTable({
ticketId: v.id("tickets"),

View file

@ -33,6 +33,9 @@ const LEGACY_STATUS_MAP: Record<string, TicketStatusNormalized> = {
CLOSED: "RESOLVED",
};
const missingRequesterLogCache = new Set<string>();
const missingCommentAuthorLogCache = new Set<string>();
function normalizeStatus(status: string | null | undefined): TicketStatusNormalized {
if (!status) return "PENDING";
const normalized = LEGACY_STATUS_MAP[status.toUpperCase()];
@ -140,9 +143,13 @@ function buildRequesterSummary(
if (process.env.NODE_ENV !== "test") {
const ticketInfo = context?.ticketId ? ` (ticket ${String(context.ticketId)})` : "";
console.warn(
`[tickets] requester ${idString} ausente ao hidratar resposta${ticketInfo}; usando placeholders.`,
);
const cacheKey = `${idString}:${context?.ticketId ? String(context.ticketId) : "unknown"}`;
if (!missingRequesterLogCache.has(cacheKey)) {
missingRequesterLogCache.add(cacheKey);
console.warn(
`[tickets] requester ${idString} ausente ao hidratar resposta${ticketInfo}; usando placeholders.`,
);
}
}
return {
@ -153,6 +160,76 @@ function buildRequesterSummary(
};
}
type CommentAuthorFallbackContext = {
ticketId?: Id<"tickets">;
commentId?: Id<"ticketComments">;
};
type CommentAuthorSnapshot = {
name: string;
email?: string;
avatarUrl?: string;
teams?: string[];
};
export function buildCommentAuthorSummary(
comment: Doc<"ticketComments">,
author: Doc<"users"> | null,
context?: CommentAuthorFallbackContext,
) {
if (author) {
return {
id: author._id,
name: author.name,
email: author.email,
avatarUrl: author.avatarUrl,
teams: normalizeTeams(author.teams),
};
}
if (process.env.NODE_ENV !== "test") {
const ticketInfo = context?.ticketId ? ` (ticket ${String(context.ticketId)})` : "";
const commentInfo = context?.commentId ? ` (comentário ${String(context.commentId)})` : "";
const cacheKeyParts = [String(comment.authorId), context?.ticketId ? String(context.ticketId) : "unknown"];
if (context?.commentId) cacheKeyParts.push(String(context.commentId));
const cacheKey = cacheKeyParts.join(":");
if (!missingCommentAuthorLogCache.has(cacheKey)) {
missingCommentAuthorLogCache.add(cacheKey);
console.warn(
`[tickets] autor ${String(comment.authorId)} ausente ao hidratar comentário${ticketInfo}${commentInfo}; usando placeholders.`,
);
}
}
const idString = String(comment.authorId);
const fallbackName = "Usuário removido";
const fallbackEmail = `author-${idString}@example.invalid`;
const snapshot = comment.authorSnapshot as CommentAuthorSnapshot | undefined;
if (snapshot) {
const name =
typeof snapshot.name === "string" && snapshot.name.trim().length > 0
? snapshot.name.trim()
: fallbackName;
const emailCandidate =
typeof snapshot.email === "string" && snapshot.email.includes("@") ? snapshot.email : null;
const email = emailCandidate ?? fallbackEmail;
return {
id: comment.authorId,
name,
email,
avatarUrl: snapshot.avatarUrl ?? undefined,
teams: normalizeTeams(snapshot.teams ?? []),
};
}
return {
id: comment.authorId,
name: fallbackName,
email: fallbackEmail,
teams: [],
};
}
type CustomFieldInput = {
fieldId: Id<"ticketFields">;
value: unknown;
@ -536,15 +613,13 @@ export const getById = query({
url: await ctx.storage.getUrl(att.storageId),
}))
);
const authorSummary = buildCommentAuthorSummary(c, author, {
ticketId: t._id,
commentId: c._id,
});
return {
id: c._id,
author: {
id: author!._id,
name: author!.name,
email: author!.email,
avatarUrl: author!.avatarUrl,
teams: author!.teams ?? [],
},
author: authorSummary,
visibility: c.visibility,
body: c.body,
attachments,
@ -842,12 +917,20 @@ export const addComment = mutation({
}
}
const authorSnapshot: CommentAuthorSnapshot = {
name: author.name,
email: author.email,
avatarUrl: author.avatarUrl ?? undefined,
teams: author.teams ?? undefined,
};
const now = Date.now();
const id = await ctx.db.insert("ticketComments", {
ticketId: args.ticketId,
authorId: args.authorId,
visibility: requestedVisibility,
body: args.body,
authorSnapshot,
attachments,
createdAt: now,
updatedAt: now,

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" };
},