fix: Salesforce - fallback to creating lead (#20137)
* Fallback creating lead * Add tests * Type fix * Type fixes * Type fix * Fix tests
This commit is contained in:
1 parent
7cb9e2b99b
commit
e9f20b1a0b
4 files changed
+210
-9
No files matched your search
@@ -80,13 +80,14 @@ const accountQueryResponse = {
|
||||
|
||||
describe("SalesforceCRMService", () => {
|
||||
let service: SalesforceCRMService;
|
||||
let mockConnection: { query: any };
|
||||
let mockConnection: { query: any; sobject: any };
|
||||
|
||||
setupAndTeardown();
|
||||
|
||||
beforeEach(() => {
|
||||
mockConnection = {
|
||||
query: vi.fn(),
|
||||
sobject: vi.fn(),
|
||||
};
|
||||
|
||||
const mockCredential: CredentialPayload = {
|
||||
@@ -512,6 +513,57 @@ describe("SalesforceCRMService", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("createContacts", () => {
|
||||
describe("createEventOn lead", () => {
|
||||
describe("createNewContactUnderAccount enabled", () => {
|
||||
it("when attendee has an account", async () => {
|
||||
mockAppOptions({
|
||||
createNewContactUnderAccount: true,
|
||||
createEventOn: SalesforceRecordEnum.LEAD,
|
||||
});
|
||||
|
||||
const querySpy = vi.spyOn(mockConnection, "query");
|
||||
querySpy.mockResolvedValueOnce(accountQueryResponse);
|
||||
querySpy.mockResolvedValueOnce({ records: [] });
|
||||
|
||||
mockConnection.sobject.mockReturnValue({
|
||||
create: vi.fn().mockResolvedValue({
|
||||
success: true,
|
||||
id: "newContactId",
|
||||
name: "New Contact",
|
||||
email: "[email protected]",
|
||||
}),
|
||||
});
|
||||
|
||||
const result = await service.createContacts([{ name: "New Contact", email: "[email protected]" }]);
|
||||
expect(result).toEqual([{ id: "newContactId", email: "[email protected]" }]);
|
||||
});
|
||||
it("attendee has no account", async () => {
|
||||
mockAppOptions({
|
||||
createNewContactUnderAccount: true,
|
||||
createEventOn: SalesforceRecordEnum.LEAD,
|
||||
});
|
||||
|
||||
const querySpy = vi.spyOn(mockConnection, "query");
|
||||
querySpy.mockResolvedValueOnce({ records: [] });
|
||||
querySpy.mockResolvedValueOnce({ records: [] });
|
||||
|
||||
mockConnection.sobject.mockReturnValue({
|
||||
create: vi.fn().mockResolvedValue({
|
||||
success: true,
|
||||
id: "newLeadId",
|
||||
name: "New Lead",
|
||||
email: "[email protected]",
|
||||
}),
|
||||
});
|
||||
|
||||
const result = await service.createContacts([{ name: "New Lead", email: "[email protected]" }]);
|
||||
expect(result).toEqual([{ id: "newLeadId", email: "[email protected]" }]);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("getAllPossibleAccountWebsiteFromEmailDomain", () => {
|
||||
it("should return all possible account websites from email domain", () => {
|
||||
const result = service.getAllPossibleAccountWebsiteFromEmailDomain("example.com");
|
||||
|
||||
@@ -526,6 +526,7 @@ export default class SalesforceCRMService implements CRM {
|
||||
if (appOptions.createNewContactUnderAccount) {
|
||||
// Check for an account
|
||||
const accountId = await this.getAccountIdBasedOnEmailDomainOfContacts(attendee.email);
|
||||
|
||||
if (accountId) {
|
||||
const createdAccountContacts = await this.createNewContactUnderAnAccount({
|
||||
attendee,
|
||||
@@ -536,10 +537,14 @@ export default class SalesforceCRMService implements CRM {
|
||||
createdContacts.push(...createdAccountContacts);
|
||||
}
|
||||
} else {
|
||||
log.info(
|
||||
"createEventOn=LEAD, No account found for attendee, not creating a contact",
|
||||
safeStringify({ attendeeEmail: attendee.email })
|
||||
);
|
||||
await this.createAttendeeRecord({
|
||||
attendee,
|
||||
recordType: SalesforceRecordEnum.LEAD,
|
||||
organizerId,
|
||||
calEventResponses,
|
||||
}).then((result) => {
|
||||
createdContacts.push(...result);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
await this.createAttendeeRecord({
|
||||
|
||||
@@ -68,6 +68,7 @@ const salesforceSettingScenario = {
|
||||
appOptions: {
|
||||
createEventOn: SalesforceRecordEnum.LEAD,
|
||||
createEventOnLeadCheckForContact: true,
|
||||
createNewContactUnderAccount: true,
|
||||
roundRobinSkipCheckRecordOn: SalesforceRecordEnum.ACCOUNT,
|
||||
},
|
||||
credential: createMockCredential(),
|
||||
@@ -99,7 +100,8 @@ describe("SalesforceCRMService", () => {
|
||||
)
|
||||
);
|
||||
// Override jsforce mock with our custom mock
|
||||
// @ts-expect-error - Not full implementation of jsforce.Connection
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore - Not full implementation of jsforce.Connection
|
||||
vi.mocked(jsforce.Connection).mockImplementation(() => ({
|
||||
...salesforceMock.mockConnection,
|
||||
version: "1.0",
|
||||
@@ -336,5 +338,64 @@ describe("SalesforceCRMService", () => {
|
||||
expect(contactsOrLeadsOrAccounts[0].recordType).toBe(SalesforceRecordEnum.CONTACT);
|
||||
});
|
||||
});
|
||||
|
||||
describe("createContact", () => {
|
||||
it("should create a contact under an account if the attendee has an account", async () => {
|
||||
const crmService = new SalesforceCRMService(credential, appOptions);
|
||||
const attendeeEmail = "[email protected]";
|
||||
const account = salesforceMock.addAccount({
|
||||
Id: "test-account-id",
|
||||
Owner: {
|
||||
Email: "[email protected]",
|
||||
},
|
||||
Website: "https://booker.com",
|
||||
});
|
||||
|
||||
expect(salesforceMock.getContacts()).toHaveLength(0);
|
||||
|
||||
const result = await crmService.createContacts([
|
||||
{
|
||||
name: "Test User",
|
||||
email: attendeeEmail,
|
||||
},
|
||||
]);
|
||||
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result[0].id).toBeDefined();
|
||||
expect(result[0].email).toBe(attendeeEmail);
|
||||
|
||||
const allContactsInSalesforce = salesforceMock.getContacts();
|
||||
expect(allContactsInSalesforce).toHaveLength(1);
|
||||
expect(allContactsInSalesforce[0].Id).toBeDefined();
|
||||
expect(allContactsInSalesforce[0].Email).toBe(attendeeEmail);
|
||||
expect(allContactsInSalesforce[0].AccountId).toBe(account.Id);
|
||||
|
||||
const allLeadsInSalesforce = salesforceMock.getLeads();
|
||||
expect(allLeadsInSalesforce).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("should create a lead if the attendee has no account", async () => {
|
||||
const crmService = new SalesforceCRMService(credential, appOptions);
|
||||
const attendeeEmail = "[email protected]";
|
||||
const result = await crmService.createContacts([
|
||||
{
|
||||
name: "Test User",
|
||||
email: attendeeEmail,
|
||||
},
|
||||
]);
|
||||
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result[0].id).toBeDefined();
|
||||
expect(result[0].email).toBe(attendeeEmail);
|
||||
|
||||
const allLeadsInSalesforce = salesforceMock.getLeads();
|
||||
expect(allLeadsInSalesforce).toHaveLength(1);
|
||||
expect(allLeadsInSalesforce[0].Id).toBeDefined();
|
||||
expect(allLeadsInSalesforce[0].Email).toBe(attendeeEmail);
|
||||
|
||||
const allContactsInSalesforce = salesforceMock.getContacts();
|
||||
expect(allContactsInSalesforce).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -259,12 +259,80 @@ export const createSalesforceMock = () => {
|
||||
};
|
||||
|
||||
// Mock connection interface
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
const mockConnection = {
|
||||
query: vi.fn().mockImplementation(handleQuery),
|
||||
sobject: vi.fn().mockReturnValue({
|
||||
create: vi.fn().mockImplementation(() => ({ success: true, id: `id_${Math.random()}` })),
|
||||
update: vi.fn().mockImplementation(() => ({ success: true })),
|
||||
delete: vi.fn().mockImplementation(() => ({ success: true })),
|
||||
create: vi.fn().mockImplementation(async (data) => {
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
const objectType = mockConnection.sobject.mock.calls[mockConnection.sobject.mock.calls.length - 1][0];
|
||||
let result;
|
||||
|
||||
switch (objectType) {
|
||||
case "Contact":
|
||||
result = addContact(data);
|
||||
break;
|
||||
case "Lead":
|
||||
result = addLead(data);
|
||||
break;
|
||||
case "Account":
|
||||
result = addAccount(data);
|
||||
break;
|
||||
case "User":
|
||||
result = addUser(data);
|
||||
break;
|
||||
case "Event":
|
||||
records.events.push({ ...data, Id: `evt${Math.random().toString().slice(2, 10)}` });
|
||||
result = { Id: records.events[records.events.length - 1].Id };
|
||||
break;
|
||||
default:
|
||||
result = { Id: `gen${Math.random().toString().slice(2, 10)}` };
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
id: result.Id,
|
||||
...result,
|
||||
};
|
||||
}),
|
||||
update: vi.fn().mockImplementation(async (data) => {
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
const objectType = mockConnection.sobject.mock.calls[mockConnection.sobject.mock.calls.length - 1][0];
|
||||
const id = data.Id;
|
||||
let record;
|
||||
|
||||
switch (objectType) {
|
||||
case "Contact":
|
||||
record = records.contacts.find((r) => r.Id === id);
|
||||
if (record) Object.assign(record, data);
|
||||
break;
|
||||
case "Lead":
|
||||
record = records.leads.find((r) => r.Id === id);
|
||||
if (record) Object.assign(record, data);
|
||||
break;
|
||||
case "Account":
|
||||
record = records.accounts.find((r) => r.Id === id);
|
||||
if (record) Object.assign(record, data);
|
||||
break;
|
||||
case "User":
|
||||
record = records.users.find((r) => r.Id === id);
|
||||
if (record) Object.assign(record, data);
|
||||
break;
|
||||
case "Event":
|
||||
record = records.events.find((r) => r.Id === id);
|
||||
if (record) Object.assign(record, data);
|
||||
break;
|
||||
}
|
||||
|
||||
return {
|
||||
success: !!record,
|
||||
id: record?.Id,
|
||||
};
|
||||
}),
|
||||
delete: vi.fn().mockImplementation(async () => ({ success: true })),
|
||||
}),
|
||||
describe: vi.fn().mockImplementation(() => {
|
||||
return {
|
||||
@@ -293,11 +361,26 @@ export const createSalesforceMock = () => {
|
||||
},
|
||||
};
|
||||
|
||||
const getContacts = () => {
|
||||
return records.contacts;
|
||||
};
|
||||
|
||||
const getLeads = () => {
|
||||
return records.leads;
|
||||
};
|
||||
|
||||
const getAccounts = () => {
|
||||
return records.accounts;
|
||||
};
|
||||
|
||||
return {
|
||||
mockConnection,
|
||||
addContact,
|
||||
getContacts,
|
||||
addLead,
|
||||
getLeads,
|
||||
addAccount,
|
||||
getAccounts,
|
||||
addUser,
|
||||
getRecords: () => records,
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user