import { type Ticket, db, type ticketAttachment, ticketAttachments, ticketMessages, } from "@/database"; import { getPublicUrl } from "@/utils/s3"; import { asc, eq } from "drizzle-orm"; export async function generateTranscript( ticket: Ticket, ): Promise<[string, ticketAttachment[]]> { const messages = await db .select() .from(ticketMessages) .where(eq(ticketMessages.ticketId, ticket.id)) .orderBy(asc(ticketMessages.createdAt)); const attachments = await db .select() .from(ticketAttachments) .where(eq(ticketAttachments.ticketId, ticket.id)); const attachmentMap = new Map(); for (const att of attachments) { const existing = attachmentMap.get(att.messageId ?? 0) ?? []; attachmentMap.set(att.messageId ?? 0, [...existing, att]); } const lines = messages.map((msg) => { const msgAttachments = attachmentMap.get(msg.id) ?? []; const time = new Date(msg.createdAt).toISOString(); const author = msg.authorType === "user" ? "reporter" : `<@${msg.authorId}>`; let text = `[${time}] ${author}: ${msg.content}`; if (msgAttachments.length > 0) { const attLines = msgAttachments .map((att) => { return `\n (Attachment) ${att.fileName}: ${getPublicUrl(att.key)}`; }) .join(""); text += attLines; } return text; }); const separator = "=================================================="; const header = [ separator, ` TRANSCRIPT FOR TICKET #${ticket.anonymousId}`, ` CREATED : ${new Date(ticket.createdAt).toISOString()}`, ` CLOSED : ${ticket.closedAt ? new Date(ticket.closedAt).toISOString() : new Date().toISOString()}`, "", " Times are displayed in UTC.", " All attachments are listed with their public URLs.", separator, "\n", ].join("\n"); return [header + lines.join("\n"), attachments]; }