2
0

first commit

This commit is contained in:
2024-08-09 00:39:27 +02:00
commit 79688abe2e
5698 changed files with 497838 additions and 0 deletions

View File

@@ -0,0 +1,98 @@
import { mockCrmApp } from "@calcom/web/test/utils/bookingScenario/bookingScenario";
import type { TFunction } from "next-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: "test@test.com" },
});
expect(crmManager.crmService).toBe(null);
crmManager.getContacts(["test@test.com"]);
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: "test@test.com",
},
],
createContacts: [{ id: "contact-id", email: "test@test.com" }],
});
// This mock is defaulting to non implemented mock return
const mockedCrmApp = mockCrmApp("salesforce", {
getContacts: [
{
id: "contact-id",
email: "test@test.com",
},
],
createContacts: [{ id: "contact-id", email: "test@test.com" }],
});
const crmManager = new CrmManager({
id: 1,
type: "salesforce_crm",
key: {
clientId: "test-client-id",
},
userId: 1,
teamId: null,
appId: "salesforce",
invalid: false,
user: { email: "test@test.com" },
});
crmManager.createEvent({
title: "Test Meeting",
type: "test-meeting",
description: "Test Description",
startTime: Date(),
endTime: Date(),
organizer: {
email: "organizer@test.com",
name: "Organizer",
timeZone: "America/New_York",
language: {
locale: "en",
translate: tFunc as TFunction,
},
},
attendees: [
{
email: "test@test.com",
name: "Test",
timeZone: "America/New_York",
language: {
locale: "en",
translate: tFunc as TFunction,
},
},
],
});
console.log(mockedCrmApp);
});
});
});

View File

@@ -0,0 +1,68 @@
import getCrm from "@calcom/app-store/_utils/getCrm";
import logger from "@calcom/lib/logger";
import type { CalendarEvent } 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;
constructor(credential: CredentialPayload) {
this.credential = credential;
}
private async getCrmService(credential: CredentialPayload) {
if (this.crmService) return this.crmService;
const crmService = await getCrm(credential);
this.crmService = crmService;
if (this.crmService === null) {
console.log("💀 Error initializing CRM service");
log.error("CRM service initialization failed");
}
return crmService;
}
public async createEvent(event: CalendarEvent, skipContactCreation?: boolean) {
const crmService = await this.getCrmService(this.credential);
// First see if the attendees already exist in the crm
let contacts = (await this.getContacts(event.attendees.map((a) => a.email))) || [];
// Ensure that all attendees are in the crm
if (contacts.length == event.attendees.length) {
return await crmService?.createEvent(event, contacts);
}
if (skipContactCreation) return;
// Figure out which contacts to create
const contactsToCreate = event.attendees.filter(
(attendee) => !contacts.some((contact) => contact.email === attendee.email)
);
const createdContacts = await this.createContacts(contactsToCreate);
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) {
const crmService = await this.getCrmService(this.credential);
return await crmService?.deleteEvent(uid);
}
public async getContacts(emailOrEmails: string | string[], includeOwner?: boolean) {
const crmService = await this.getCrmService(this.credential);
const contacts = await crmService?.getContacts(emailOrEmails, includeOwner);
return contacts;
}
public async createContacts(contactsToCreate: ContactCreateInput[]) {
const crmService = await this.getCrmService(this.credential);
const createdContacts = (await crmService?.createContacts(contactsToCreate)) || [];
return createdContacts;
}
}