-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Set
teamMemberEmail
server side for CRM RR Skip (#16367)
Co-authored-by: Keith Williams <keithwillcode@gmail.com> Co-authored-by: zomars <zomars@me.com>
- Loading branch information
1 parent
45c3724
commit af7347e
Showing
17 changed files
with
140 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import type { z } from "zod"; | ||
|
||
import CrmManager from "@calcom/core/crmManager/crmManager"; | ||
import { prisma } from "@calcom/prisma"; | ||
import type { EventTypeAppMetadataSchema } from "@calcom/prisma/zod-utils"; | ||
import { EventTypeMetaDataSchema } from "@calcom/prisma/zod-utils"; | ||
|
||
export async function getCRMContactOwnerForRRLeadSkip( | ||
bookerEmail: string, | ||
eventTypeId: number | ||
): Promise<string | undefined> { | ||
const eventTypeMetadataQuery = await prisma.eventType.findUnique({ | ||
where: { | ||
id: eventTypeId, | ||
}, | ||
select: { metadata: true }, | ||
}); | ||
|
||
const eventTypeMetadata = EventTypeMetaDataSchema.safeParse(eventTypeMetadataQuery?.metadata); | ||
|
||
if (!eventTypeMetadata.success || !eventTypeMetadata.data?.apps) return; | ||
|
||
const crm = await getCRMManagerWithRRLeadSkip(eventTypeMetadata.data.apps); | ||
|
||
if (!crm) return; | ||
|
||
const contact = await crm.getContacts(bookerEmail, true); | ||
if (!contact?.length) return; | ||
return contact[0].ownerEmail; | ||
} | ||
|
||
async function getCRMManagerWithRRLeadSkip(apps: z.infer<typeof EventTypeAppMetadataSchema>) { | ||
let crmRoundRobinLeadSkip; | ||
for (const appKey in apps) { | ||
const app = apps[appKey as keyof typeof apps]; | ||
if ( | ||
app.enabled && | ||
typeof app.appCategories === "object" && | ||
app.appCategories.some((category: string) => category === "crm") && | ||
app.roundRobinLeadSkip | ||
) { | ||
crmRoundRobinLeadSkip = app; | ||
break; | ||
} | ||
} | ||
if (!crmRoundRobinLeadSkip) return; | ||
const crmCredential = await prisma.credential.findUnique({ | ||
where: { | ||
id: crmRoundRobinLeadSkip.credentialId, | ||
}, | ||
include: { | ||
user: { | ||
select: { | ||
email: true, | ||
}, | ||
}, | ||
}, | ||
}); | ||
if (!crmCredential) return; | ||
return new CrmManager(crmCredential); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.