refactor: circular deps between app store and lib [6] (#23971)

* move delegation credential repository to features

* mv credential repository to features

* update imports

* mv

* fix

* fix

* fix

* fix

* fix

* update imports

* update imports

* update eslint rule

* fix

* fix

* mv getConnectedDestinationCalendars

* fix import errors

* mv getCalendarsEvents

* remove getUsersCredentials

* wip

* revert eslint rule change for now

* fix type checks

* fix

* format

* cleanup

* fix

* fix

* fix

* fix

* fix tests

* migrate getUserAvailability

* migrate

* fix tests

* fix type checks

* fix

* fix

* migrate crmManager

* update imports

* migrate raqbUtils to appstore

* migrate getLuckyUser to features

* migrate findTeamMembersMatchingAttributeLogic to appstore

* update imports

* fix

* fix

* fix test

* fix unit tests

* fix

* fix

* add eslint config
This commit is contained in:
Benny Joo authored and GitHub committed 2025-10-09 14:02:12 +00:00
1 parent 445b307972
commit bb68cd73ef
114 files changed
+263 -245

No files matched your search

@@ -0,0 +1,98 @@
import { mockCrmApp } from "@calcom/web/test/utils/bookingScenario/bookingScenario";
import type { TFunction } from "i18next";
import { describe, expect, test, vi } from "vitest";
import { getCrm } from "@calcom/app-store/_utils/getCrm";
import CrmManager from "./crmManager";
// vi.mock("@calcom/app-store/_utils/getCrm");
describe.skip("crmManager tests", () => {
test("Set crmService if not set", async () => {
const spy = vi.spyOn(CrmManager.prototype as any, "getCrmService");
const crmManager = new CrmManager({
id: 1,
type: "credential_crm",
key: {},
userId: 1,
teamId: null,
appId: "crm-app",
invalid: false,
user: { email: "[email protected]" },
});
expect(crmManager.crmService).toBe(null);
crmManager.getContacts(["[email protected]"]);
expect(spy).toBeCalledTimes(1);
});
describe("creating events", () => {
test("If the contact exists, create the event", async () => {
const tFunc = vi.fn(() => "foo");
vi.spyOn(getCrm).mockReturnValue({
getContacts: () => [
{
id: "contact-id",
email: "[email protected]",
},
],
createContacts: [{ id: "contact-id", email: "[email protected]" }],
});
// This mock is defaulting to non implemented mock return
const mockedCrmApp = mockCrmApp("salesforce", {
getContacts: [
{
id: "contact-id",
email: "[email protected]",
},
],
createContacts: [{ id: "contact-id", email: "[email protected]" }],
});
const crmManager = new CrmManager({
id: 1,
type: "salesforce_crm",
key: {
clientId: "test-client-id",
},
userId: 1,
teamId: null,
appId: "salesforce",
invalid: false,
user: { email: "[email protected]" },
});
crmManager.createEvent({
title: "Test Meeting",
type: "test-meeting",
description: "Test Description",
startTime: Date(),
endTime: Date(),
organizer: {
email: "[email protected]",
name: "Organizer",
timeZone: "America/New_York",
language: {
locale: "en",
translate: tFunc as TFunction,
},
},
attendees: [
{
email: "[email protected]",
name: "Test",
timeZone: "America/New_York",
language: {
locale: "en",
translate: tFunc as TFunction,
},
},
],
});
console.log(mockedCrmApp);
});
});
});
@@ -0,0 +1,92 @@
import getCrm from "@calcom/app-store/_utils/getCrm";
import logger from "@calcom/lib/logger";
import type { CalendarEvent, CalEventResponses } from "@calcom/types/Calendar";
import type { CredentialPayload } from "@calcom/types/Credential";
import type { CRM, ContactCreateInput } from "@calcom/types/CrmService";
const log = logger.getSubLogger({ prefix: ["CrmManager"] });
export default class CrmManager {
crmService: CRM | null | undefined = null;
credential: CredentialPayload;
appOptions: any;
constructor(credential: CredentialPayload, appOptions?: any) {
this.credential = credential;
this.appOptions = appOptions;
}
private async getCrmService(credential: CredentialPayload) {
if (this.crmService) return this.crmService;
const crmService = await getCrm(credential, this.appOptions);
this.crmService = crmService;
if (!this.crmService) {
console.log("💀 Error initializing CRM service");
log.error("CRM service initialization failed");
}
return crmService;
}
public async createEvent(event: CalendarEvent) {
const crmService = await this.getCrmService(this.credential);
if (!crmService) return;
const { skipContactCreation = false, ignoreGuests = false } = crmService.getAppOptions() || {};
const eventAttendees = ignoreGuests ? [event.attendees[0]] : event.attendees;
// First see if the attendees already exist in the crm
let contacts = (await this.getContacts({ emails: eventAttendees.map((a) => a.email) })) || [];
// Ensure that all attendees are in the crm
if (contacts.length == eventAttendees.length) {
return await crmService.createEvent(event, contacts);
}
if (skipContactCreation) return;
const contactSet = new Set(contacts.map((c: { email: string }) => c.email));
// Figure out which contacts to create
const contactsToCreate = eventAttendees.filter((attendee) => !contactSet.has(attendee.email));
const createdContacts = await this.createContacts(
contactsToCreate,
event.organizer?.email,
event.responses
);
contacts = contacts.concat(createdContacts);
return await crmService.createEvent(event, contacts);
}
public async updateEvent(uid: string, event: CalendarEvent) {
const crmService = await this.getCrmService(this.credential);
return await crmService?.updateEvent(uid, event);
}
public async deleteEvent(uid: string, event: CalendarEvent) {
const crmService = await this.getCrmService(this.credential);
return await crmService?.deleteEvent(uid, event);
}
public async getContacts(params: {
emails: string | string[];
includeOwner?: boolean;
forRoundRobinSkip?: boolean;
}) {
const crmService = await this.getCrmService(this.credential);
const contacts = await crmService?.getContacts(params);
return contacts;
}
public async createContacts(
contactsToCreate: ContactCreateInput[],
organizerEmail?: string,
calEventResponses?: CalEventResponses | null
) {
const crmService = await this.getCrmService(this.credential);
const createdContacts =
(await crmService?.createContacts(contactsToCreate, organizerEmail, calEventResponses)) || [];
return createdContacts;
}
public async handleAttendeeNoShow(bookingUid: string, attendees: { email: string; noShow: boolean }[]) {
const crmService = await this.getCrmService(this.credential);
if (crmService?.handleAttendeeNoShow) {
await crmService.handleAttendeeNoShow(bookingUid, attendees);
}
}
}