From 530edf5b3653e1a26786ee7944a55ab23fc5e045 Mon Sep 17 00:00:00 2001 From: potts99 Date: Sun, 3 Dec 2023 16:54:54 +0000 Subject: [PATCH] replyto ticket --- apps/api/src/lib/imap.ts | 56 +++++++++++-------- apps/api/src/lib/nodemailer/ticket/create.ts | 2 +- .../migrations/20231203164241_/migration.sql | 8 +++ apps/api/src/prisma/schema.prisma | 6 +- 4 files changed, 45 insertions(+), 27 deletions(-) create mode 100644 apps/api/src/prisma/migrations/20231203164241_/migration.sql diff --git a/apps/api/src/lib/imap.ts b/apps/api/src/lib/imap.ts index 159083fd1..55122e145 100644 --- a/apps/api/src/lib/imap.ts +++ b/apps/api/src/lib/imap.ts @@ -87,29 +87,39 @@ export const getEmails = async () => { const parsedData = parseEmailContent(textAsHtml); - const imap = await client.imap_Email.create({ - data: { - from: from.text, - subject: subject ? subject : "No Subject", - body: text ? text : "No Body", - html: html ? html : "", - text: textAsHtml, - }, - }); - - const ticket = await client.ticket.create({ - data: { - email: imap.from, - name: imap.from, - title: imap.subject ? imap.subject : "-", - isComplete: Boolean(false), - priority: "Low", - fromImap: Boolean(true), - detail: textAsHtml, - }, - }); - - console.log(imap, ticket); + if (subject.includes("Re:")) { + return await client.comment.create({ + data: { + text: text ? text : "No Body", + userId: null, + ticketId: subject.split(" ")[1].split("#")[1], + }, + }); + } else { + const imap = await client.imap_Email.create({ + data: { + from: from.text, + subject: subject ? subject : "No Subject", + body: text ? text : "No Body", + html: html ? html : "", + text: textAsHtml, + }, + }); + + const ticket = await client.ticket.create({ + data: { + email: imap.from, + name: imap.from, + title: imap.subject ? imap.subject : "-", + isComplete: Boolean(false), + priority: "Low", + fromImap: Boolean(true), + detail: textAsHtml, + }, + }); + + console.log(imap, ticket); + } }); }); msg.once("attributes", (attrs: any) => { diff --git a/apps/api/src/lib/nodemailer/ticket/create.ts b/apps/api/src/lib/nodemailer/ticket/create.ts index 5aa15c2b4..cca6e9d86 100644 --- a/apps/api/src/lib/nodemailer/ticket/create.ts +++ b/apps/api/src/lib/nodemailer/ticket/create.ts @@ -7,7 +7,7 @@ export async function sendTicketCreate(ticket: any) { const emails = await prisma.email.findMany(); - if (emails.length > 0) { + if (emails.length === 0) { if (process.env.ENVIRONMENT === "development") { let testAccount = await nodeMailer.createTestAccount(); mail = nodeMailer.createTransport({ diff --git a/apps/api/src/prisma/migrations/20231203164241_/migration.sql b/apps/api/src/prisma/migrations/20231203164241_/migration.sql new file mode 100644 index 000000000..5e32c2625 --- /dev/null +++ b/apps/api/src/prisma/migrations/20231203164241_/migration.sql @@ -0,0 +1,8 @@ +-- DropForeignKey +ALTER TABLE "Comment" DROP CONSTRAINT "Comment_userId_fkey"; + +-- AlterTable +ALTER TABLE "Comment" ALTER COLUMN "userId" DROP NOT NULL; + +-- AddForeignKey +ALTER TABLE "Comment" ADD CONSTRAINT "Comment_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE; diff --git a/apps/api/src/prisma/schema.prisma b/apps/api/src/prisma/schema.prisma index 98d5c51b9..06965bf3a 100644 --- a/apps/api/src/prisma/schema.prisma +++ b/apps/api/src/prisma/schema.prisma @@ -118,10 +118,10 @@ model Comment { text String public Boolean @default(false) - userId String - user User @relation(fields: [userId], references: [id]) + userId String? + user User? @relation(fields: [userId], references: [id]) ticketId String - ticket Ticket @relation(fields: [ticketId], references: [id]) + ticket Ticket @relation(fields: [ticketId], references: [id]) } model Client {