47f345c4bf
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
76 lines
2.0 KiB
TypeScript
76 lines
2.0 KiB
TypeScript
export interface DigestResult {
|
|
text: string;
|
|
messageIds: string[];
|
|
}
|
|
|
|
const MAX_MESSAGES = 20;
|
|
const PREVIEW_LENGTH = 120;
|
|
|
|
function formatDate(date: Date): string {
|
|
return date.toLocaleDateString('en-US', {
|
|
year: 'numeric',
|
|
month: 'long',
|
|
day: 'numeric',
|
|
timeZone: 'UTC',
|
|
});
|
|
}
|
|
|
|
export async function buildTemplateDigest(
|
|
tenantId: string,
|
|
tenantName: string,
|
|
prisma: any,
|
|
digestDate: Date,
|
|
): Promise<DigestResult | null> {
|
|
const from = new Date(digestDate);
|
|
const to = new Date(digestDate.getTime() + 24 * 60 * 60 * 1000);
|
|
|
|
const messages = await prisma.message.findMany({
|
|
where: {
|
|
tenantId,
|
|
status: { in: ['APPROVED', 'DISTRIBUTED'] },
|
|
updatedAt: { gte: from, lt: to },
|
|
},
|
|
include: {
|
|
sourceGroup: { select: { name: true } },
|
|
},
|
|
orderBy: { updatedAt: 'asc' },
|
|
take: MAX_MESSAGES,
|
|
});
|
|
|
|
if (messages.length === 0) return null;
|
|
|
|
// Group messages by first tag; untagged go under "General"
|
|
const groups = new Map<string, typeof messages>();
|
|
for (const msg of messages) {
|
|
const tag = msg.tags?.[0] ?? 'General';
|
|
if (!groups.has(tag)) groups.set(tag, []);
|
|
groups.get(tag)!.push(msg);
|
|
}
|
|
|
|
const dateLabel = formatDate(digestDate);
|
|
const lines: string[] = [
|
|
`📋 *Daily Digest — ${tenantName}*`,
|
|
`_${dateLabel}_`,
|
|
'',
|
|
];
|
|
|
|
for (const [tag, msgs] of groups) {
|
|
const sectionHeader = tag === 'General' ? '*📌 General*' : `*🏷️ ${tag}*`;
|
|
lines.push(sectionHeader);
|
|
for (const msg of msgs) {
|
|
const preview = msg.content.slice(0, PREVIEW_LENGTH).replace(/\n+/g, ' ');
|
|
const groupName = msg.sourceGroup?.name ?? 'Unknown Group';
|
|
lines.push(`• ${preview} _(${groupName})_`);
|
|
}
|
|
lines.push('');
|
|
}
|
|
|
|
const groupCount = new Set(messages.map((m: any) => m.sourceGroupId)).size;
|
|
lines.push(`_${messages.length} message${messages.length !== 1 ? 's' : ''} from ${groupCount} group${groupCount !== 1 ? 's' : ''} | TOWER_`);
|
|
|
|
return {
|
|
text: lines.join('\n'),
|
|
messageIds: messages.map((m: any) => m.id),
|
|
};
|
|
}
|