From 7c8d3bb050fb0c35e0296eeb71d516f0c37329a2 Mon Sep 17 00:00:00 2001 From: Nik Lampe Date: Mon, 9 Sep 2024 16:44:58 +0200 Subject: [PATCH] fix: move updating the contact to contacts controller --- packages/api/src/controllers/v1/Contacts.ts | 8 ++++---- packages/api/src/services/ActionService.ts | 7 ------- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/packages/api/src/controllers/v1/Contacts.ts b/packages/api/src/controllers/v1/Contacts.ts index 76a124a..7389975 100644 --- a/packages/api/src/controllers/v1/Contacts.ts +++ b/packages/api/src/controllers/v1/Contacts.ts @@ -103,13 +103,13 @@ export class Contacts { const { id, email } = ContactSchemas.manage.parse(req.body); - const contact = id ? await ContactService.id(id) : await ContactService.email(project.id, email as string); + let contact = id ? await ContactService.id(id) : await ContactService.email(project.id, email as string); if (!contact || contact.projectId !== project.id) { throw new NotFound("contact"); } - await prisma.contact.update({ + contact = await prisma.contact.update({ where: { id: contact.id }, data: { subscribed: false }, }); @@ -154,13 +154,13 @@ export class Contacts { const { id, email } = ContactSchemas.manage.parse(req.body); - const contact = id ? await ContactService.id(id) : await ContactService.email(project.id, email as string); + let contact = id ? await ContactService.id(id) : await ContactService.email(project.id, email as string); if (!contact || contact.projectId !== project.id) { throw new NotFound("contact"); } - await prisma.contact.update({ + contact = await prisma.contact.update({ where: { id: contact.id }, data: { subscribed: true }, }); diff --git a/packages/api/src/services/ActionService.ts b/packages/api/src/services/ActionService.ts index fe485c6..26a88fa 100644 --- a/packages/api/src/services/ActionService.ts +++ b/packages/api/src/services/ActionService.ts @@ -71,13 +71,6 @@ export class ActionService { const triggers = await ContactService.triggers(contact.id); - // Refetch the contact, as it may have subscribed now - contact = await prisma.contact.findUniqueOrThrow({ - where: { - id: contact.id, - }, - }); - for (const action of actions) { const hasTriggeredAction = !!triggers.find((t) => t.actionId === action.id);