fix(notifications): tell staff about every new comment

Only the shot's assigned artist was notified of player comments, and
client-link comments and pasted WhatsApp feedback notified no one.
notifyCommentsAdded now notifies all active staff plus the shot/task
artist, never the author, from all three paths. A pasted round becomes
one notification per version.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
twotalesanimation
2026-10-17 09:06:26 +02:00
parent 7a7088f2ad
commit 41cf79abbe
5 changed files with 223 additions and 38 deletions
+94 -23
View File
@@ -78,29 +78,6 @@ export async function notifyNewVersionUploaded(params: {
);
}
/**
* Notify the shot's artist about new feedback.
*/
export async function notifyFeedbackAdded(params: {
artistId: string;
shotCode: string;
frameNumber: number;
versionId: string;
authorName: string;
}) {
await createNotification({
userId: params.artistId,
type: NotificationType.FEEDBACK_ADDED,
title: "New feedback on your shot",
message: `${params.authorName} commented on ${params.shotCode} at frame ${params.frameNumber}`,
data: {
versionId: params.versionId,
shotCode: params.shotCode,
frameNumber: params.frameNumber,
},
});
}
/**
* Notify relevant users of an approval status change.
*/
@@ -250,3 +227,97 @@ export async function notifyCommentReply(params: {
},
});
}
/** One new comment, as the recipients of its notification need it. */
export interface NewCommentNote {
versionId: string;
shotCode: string;
frameNumber: number;
/** The shot's artist and, for a task version, the task's artist. */
artistIds: (string | null | undefined)[];
}
/**
* Who hears about new comments, and what they read. Pure so it can be tested:
* every active staff member plus each note's artist, never the author.
* Notes are grouped per version so a pasted round of client feedback is one
* notification per shot rather than one per line.
*/
export function buildCommentNotifications(params: {
notes: NewCommentNote[];
authorId: string;
authorName: string;
/** "client" = typed on a client review link; "whatsapp" = pasted by staff. */
origin?: "client" | "whatsapp";
staffIds: string[];
}): CreateNotificationPayload[] {
const byVersion = new Map<string, NewCommentNote[]>();
for (const n of params.notes) {
byVersion.set(n.versionId, [...(byVersion.get(n.versionId) ?? []), n]);
}
const who =
params.origin === "client"
? `Client ${params.authorName}`
: params.origin === "whatsapp"
? `Client feedback (logged by ${params.authorName})`
: params.authorName;
const title = params.origin ? "New client feedback" : "New feedback";
const payloads: CreateNotificationPayload[] = [];
for (const [versionId, notes] of byVersion) {
const { shotCode, frameNumber } = notes[0];
const message =
notes.length === 1
? `${who} commented on ${shotCode} at frame ${frameNumber}`
: `${who} left ${notes.length} notes on ${shotCode}`;
const recipients = new Set([
...params.staffIds,
...notes.flatMap((n) => n.artistIds).filter((id): id is string => !!id),
]);
recipients.delete(params.authorId);
for (const userId of recipients) {
payloads.push({
userId,
type: NotificationType.FEEDBACK_ADDED,
title,
message,
data: { versionId, shotCode, frameNumber },
});
}
}
return payloads;
}
/**
* Notify staff and the shot's artist about new comments. Artists who are not
* staff are only told when they are still active.
*/
export async function notifyCommentsAdded(params: {
notes: NewCommentNote[];
authorId: string;
authorName: string;
origin?: "client" | "whatsapp";
}) {
if (params.notes.length === 0) return;
const artistIds = [
...new Set(params.notes.flatMap((n) => n.artistIds).filter((id): id is string => !!id)),
];
const [staff, activeArtists] = await Promise.all([
db.user.findMany({
where: { role: { in: ["ADMIN", "PRODUCER", "SUPERVISOR"] }, isActive: true },
select: { id: true },
}),
db.user.findMany({
where: { id: { in: artistIds }, isActive: true, role: { not: "CLIENT" } },
select: { id: true },
}),
]);
const active = new Set(activeArtists.map((u) => u.id));
const payloads = buildCommentNotifications({
...params,
notes: params.notes.map((n) => ({ ...n, artistIds: n.artistIds.filter((id) => id && active.has(id)) })),
staffIds: staff.map((u) => u.id),
});
if (payloads.length) await createNotifications(payloads);
}