From be2f0f986e819521a707c6f48293ea6c368fa2f6 Mon Sep 17 00:00:00 2001 From: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com> Date: Thu, 28 Nov 2024 14:31:06 -0500 Subject: [PATCH] fix: `getAttributeRoutingConfig` find the route based on the chosenRouteId (#17867) ## What does this PR do? We were parsing routes based on `eventTypeId` but routes could be pointing to the same event type. This was causing misroutings. - Fixes #XXXX (GitHub issue number) - Fixes CAL-XXXX (Linear issue number - should be visible at the bottom of the GitHub issue description) ## Mandatory Tasks (DO NOT REMOVE) - [x] I have self-reviewed the code (A decent size PR without self-review might be rejected). - [ ] I have updated the developer docs in /docs if this PR makes changes that would require a [documentation change](https://cal.com/docs). If N/A, write N/A here and check the checkbox. - [ ] I confirm automated tests are in place that prove my fix is effective or that my feature works. --- .../getTeamMemberEmailFromCrm.test.ts | 5 +++- apps/web/lib/getTeamMemberEmailFromCrm.ts | 23 ++++++++++--------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/apps/web/lib/__tests__/getTeamMemberEmailFromCrm.test.ts b/apps/web/lib/__tests__/getTeamMemberEmailFromCrm.test.ts index aded368c30..a1337a6c04 100644 --- a/apps/web/lib/__tests__/getTeamMemberEmailFromCrm.test.ts +++ b/apps/web/lib/__tests__/getTeamMemberEmailFromCrm.test.ts @@ -79,6 +79,8 @@ async function createRoutingFormWithResponse({ }, }); + const routeId = v4(); + const form = await prismock.app_RoutingForms_Form.create({ data: { ...formData, @@ -88,7 +90,7 @@ async function createRoutingFormWithResponse({ id: v4(), type: "group", }, - id: v4(), + id: routeId, ...route, })), user: { @@ -101,6 +103,7 @@ async function createRoutingFormWithResponse({ const responseRecord = await prismock.app_RoutingForms_FormResponse.create({ data: { + chosenRouteId: routeId, response: {}, form: { connect: { diff --git a/apps/web/lib/getTeamMemberEmailFromCrm.ts b/apps/web/lib/getTeamMemberEmailFromCrm.ts index 3237cbcc0a..0abc54e904 100644 --- a/apps/web/lib/getTeamMemberEmailFromCrm.ts +++ b/apps/web/lib/getTeamMemberEmailFromCrm.ts @@ -58,28 +58,29 @@ async function getAttributeRoutingConfig( return data.route.attributeRoutingConfig ?? null; } const { routingFormResponseId, eventTypeId } = data; - const routingFormQuery = await prisma.app_RoutingForms_Form.findFirst({ + + const routingFormResponseQuery = await prisma.app_RoutingForms_FormResponse.findFirst({ where: { - responses: { - some: { - id: routingFormResponseId, + id: routingFormResponseId, + }, + include: { + form: { + select: { + routes: true, }, }, }, - select: { - routes: true, - }, }); - if (!routingFormQuery || !routingFormQuery?.routes) return null; - const parsedRoutes = routesSchema.safeParse(routingFormQuery.routes); + + if (!routingFormResponseQuery || !routingFormResponseQuery?.form.routes) return null; + const parsedRoutes = routesSchema.safeParse(routingFormResponseQuery?.form.routes); if (!parsedRoutes.success || !parsedRoutes.data) return null; // Find the route with the attributeRoutingConfig - // FIXME: There could be multiple routes with same action.eventTypeId, we should actually ensure we have the chosenRouteId in here and use that route. const route = parsedRoutes.data.find((route) => { if ("action" in route) { - return route.action.eventTypeId === eventTypeId; + return route.id === routingFormResponseQuery.chosenRouteId; } });