Skip to content

Commit 80324ae

Browse files
authored
fix: imap email comments not pulling through (#447)
1 parent 54e932e commit 80324ae

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

apps/api/src/lib/services/imap.service.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,31 @@ export class ImapService {
6161
): Promise<void> {
6262
const { from, subject, text, html, textAsHtml } = parsed;
6363

64+
console.log("isReply", isReply);
65+
6466
if (isReply) {
65-
const ticketIdMatch = subject.match(/#(\d+)/);
66-
if (!ticketIdMatch) {
67+
// First try to match UUID format
68+
const uuidMatch = subject.match(
69+
/(?:ref:|#)([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/i
70+
);
71+
console.log("UUID MATCH", uuidMatch);
72+
73+
const ticketId = uuidMatch?.[1];
74+
75+
console.log("TICKET ID", ticketId);
76+
77+
if (!ticketId) {
6778
throw new Error(`Could not extract ticket ID from subject: ${subject}`);
6879
}
6980

70-
const ticketId = ticketIdMatch[1];
7181
const ticket = await prisma.ticket.findFirst({
72-
where: { Number: Number(ticketId) },
82+
where: {
83+
id: ticketId,
84+
},
7385
});
7486

87+
console.log("TICKET", ticket);
88+
7589
if (!ticket) {
7690
throw new Error(`Ticket not found: ${ticketId}`);
7791
}
@@ -152,7 +166,10 @@ export class ImapService {
152166
msg.on("body", (stream) => {
153167
simpleParser(stream, async (err, parsed) => {
154168
if (err) throw err;
155-
const isReply = parsed.subject?.includes("Re:");
169+
const subjectLower = parsed.subject?.toLowerCase() || "";
170+
const isReply =
171+
subjectLower.includes("re:") ||
172+
subjectLower.includes("ref:");
156173
await this.processEmail(parsed, isReply || false);
157174
});
158175
});

0 commit comments

Comments
 (0)