Files
cal.diy/packages/app-store/_utils/CRMRoundRobinSkip.ts
T
Joe Au-YeungandUdit Takkar b1d9e2b84c feat: Salesforce choose how to save attendees & other options (#17009)
* Refactor get SF user and create event

* Add new Salesforce options

* Pass app options to CrmService

* Add record enum

* Add creating new lead and contact under an account

* Handle if the contact already exists but not connected to account

* Create a lead if an account doesn't exist

* Merge fix

* Refactor - add generateCreateRecordBody

* Type fix

* Clean up

* Add getAppOptions to other CRM services

* Type fixes

* Fix typo

* feat: Salesforce round robin skip - choose which entity to search on (#17038)

* Add option to search which entity for the owner

* Add logic to search entity for skipping RR assignment

* Type fixes

* Disable license

* Skip license check in component

* Revert license changes

---------

Co-authored-by: Udit Takkar <[email protected]>
2024-10-12 10:02:27 +00:00

62 lines
1.8 KiB
TypeScript

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({ emails: bookerEmail, forRoundRobinSkip: 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, crmRoundRobinLeadSkip);
}