feat: Salesforce - write assignment reason to record (#22223)

* Create `assignmentReasonRepository`

* Write `assignmentReason` to booking

* Type fix

* Clean up

---------

Co-authored-by: Udit Takkar <53316345+Udit-takkar@users.noreply.github.com>
This commit is contained in:
Joe Au-Yeung
2025-07-03 20:56:36 +00:00
committed by GitHub
co-authored by Udit Takkar
parent eb66fbe054
commit 7f891adff2
2 changed files with 32 additions and 0 deletions
@@ -10,6 +10,7 @@ import { RetryableError } from "@calcom/lib/crmManager/errors";
import { checkIfFreeEmailDomain } from "@calcom/lib/freeEmailDomainCheck/checkIfFreeEmailDomain";
import logger from "@calcom/lib/logger";
import { safeStringify } from "@calcom/lib/safeStringify";
import { AssignmentReasonRepository } from "@calcom/lib/server/repository/assignmentReason";
import { prisma } from "@calcom/prisma";
import type { CalendarEvent, CalEventResponses } from "@calcom/types/Calendar";
import type { CredentialPayload } from "@calcom/types/Credential";
@@ -1244,6 +1245,16 @@ export default class SalesforceCRMService implements CRM {
return;
}
valueToWrite = await this.getTextValueFromBookingTracking(fieldValue, bookingUid);
} else if (fieldValue === "{assignmentReason}") {
if (!bookingUid) {
log.error(`BookingUid not passed. Cannot get assignment reason without it`);
return;
}
valueToWrite = await this.getAssignmentReason(bookingUid);
if (!valueToWrite) {
log.error(`No assignment reason found for bookingUid ${bookingUid}`);
return;
}
} else {
// Get the value from the booking response
if (!calEventResponses) {
@@ -1336,6 +1347,11 @@ export default class SalesforceCRMService implements CRM {
return tracking[`utm_${utmParam}` as keyof typeof tracking]?.toString() ?? "";
}
private async getAssignmentReason(bookingId: string) {
const assignmentReason = await AssignmentReasonRepository.findLatestReasonFromBookingUid(bookingId);
return assignmentReason?.reasonString ?? "";
}
private getTextValueFromBookingResponse(fieldValue: string, calEventResponses: CalEventResponses) {
const regexValueToReplace = /\{(.*?)\}/g;
return fieldValue.replace(regexValueToReplace, (match, captured) => {
@@ -0,0 +1,16 @@
import prisma from "@calcom/prisma";
export class AssignmentReasonRepository {
static async findLatestReasonFromBookingUid(bookingUid: string) {
return await prisma.assignmentReason.findFirst({
where: {
booking: {
uid: bookingUid,
},
},
orderBy: {
createdAt: "desc",
},
});
}
}