Skip to content

refactor: getContacts#18137

Merged
joeauyeung merged 4 commits intomainfrom
refactor/crmService
Dec 16, 2024
Merged

refactor: getContacts#18137
joeauyeung merged 4 commits intomainfrom
refactor/crmService

Conversation

@Udit-takkar
Copy link
Copy Markdown
Contributor

@Udit-takkar Udit-takkar commented Dec 12, 2024

What does this PR do?

  • Fixes #XXXX (GitHub issue number)
  • Fixes CAL-XXXX (Linear issue number - should be visible at the bottom of the GitHub issue description)

For better debugging and prevent Type error

Screenshot 2024-12-13 at 1 08 58 AM

https://app.campsite.com/cal/posts/vwtobznl53bv#comment-yze7du8uorc9

Follow up Steps

  • Split big functions in crmService into small functions

Mandatory Tasks (DO NOT REMOVE)

  • I have self-reviewed the code (A decent size PR without self-review might be rejected).
  • N/A I have updated the developer docs in /docs if this PR makes changes that would require a documentation change. If N/A, write N/A here and check the checkbox.
  • I confirm automated tests are in place that prove my fix is effective or that my feature works.

How should this be tested?

@keithwillcode keithwillcode added core area: core, team members only enterprise area: enterprise, audit log, organisation, SAML, SSO labels Dec 12, 2024
@vercel
Copy link
Copy Markdown

vercel bot commented Dec 12, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

2 Skipped Deployments
Name Status Preview Comments Updated (UTC)
cal ⬜️ Ignored (Inspect) Visit Preview Dec 13, 2024 9:57am
calcom-web-canary ⬜️ Ignored (Inspect) Visit Preview Dec 13, 2024 9:57am

Comment on lines +246 to +254
expect(querySpy).toHaveBeenNthCalledWith(
1,
"SELECT Id, Email, OwnerId, AccountId FROM Contact WHERE Email = 'test@example.com' AND AccountId != null"
);
expect(querySpy).toHaveBeenNthCalledWith(2, "SELECT Id, OwnerId FROM Account WHERE Id = 'acc001'");
expect(querySpy).toHaveBeenNthCalledWith(
3,
expect.stringContaining("SELECT Id, Email, Name FROM User WHERE Id = 'owner001'")
);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am also checking the exact query in every test

const accountId = await this.getAccountIdBasedOnEmailDomainOfContacts(attendeeEmail);
if (accountId) {
soql = `SELECT Id, OwnerId FROM Account WHERE Id = '${accountId}'`;
const log = logger.getSubLogger({ prefix: [`[getContacts]:${emails}`] });
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For better logging and debugging.

@Udit-takkar Udit-takkar added this to the v4.8 milestone Dec 12, 2024
@Udit-takkar Udit-takkar added the High priority Created by Linear-GitHub Sync label Dec 12, 2024
@Udit-takkar Udit-takkar marked this pull request as ready for review December 12, 2024 19:41
@graphite-app graphite-app bot requested a review from a team December 12, 2024 19:41
@graphite-app
Copy link
Copy Markdown

graphite-app bot commented Dec 12, 2024

Graphite Automations

"Add consumer team as reviewer" took an action on this PR • (12/12/24)

1 reviewer was added to this PR based on Keith Williams's automation.

@hariombalhara
Copy link
Copy Markdown
Member

@Udit-takkar tests are failing

if (accountId) {
soql = `SELECT Id, OwnerId FROM Account WHERE Id = '${accountId}'`;
const log = logger.getSubLogger({ prefix: [`[getContacts]:${emails}`] });
try {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a followup, we should add try-catch at a higher level so that no CRM app can crash the booking page and not just salesforce.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK CrmManager prevents a crash to block bookings doesn't it?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fn is called while loading the booking page. There it isn't handled

Comment thread packages/app-store/salesforce/lib/CrmService.ts Outdated

// If this is for a round robin skip then we need to return the account record
if (forRoundRobinSkip) {
const results = await conn.query(soql);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Followup:
It is best to have type assertion here that considers records as possibly nullish and records. Items of records should be asserted as nullish.
In this way if we accidentally miss an optional chaining operator, TS will complain.

Right now records is unknown[], so TS won't complain if we miss a ? after records.

Comment on lines 358 to 379
log.info("Account contact search results", { resultCount: results.records.length });

if (results.records?.length) {
const contact = results.records[0] as { AccountId?: string };
if (contact?.AccountId) {
soql = `SELECT Id, OwnerId FROM Account WHERE Id = '${contact.AccountId}'`;
}
} else {
// If we can't find the exact contact, then we need to search for an account where the contacts share the same email domain
const accountId = await this.getAccountIdBasedOnEmailDomainOfContacts(attendeeEmail);
if (accountId) {
soql = `SELECT Id, OwnerId FROM Account WHERE Id = '${accountId}'`;
}
}
}
// If creating events on contacts or leads
} else {
// Handle Contact/Lead record types
soql = `SELECT Id, Email, OwnerId FROM ${recordToSearch} WHERE Email IN ('${emailArray.join(
"','"
)}')`;
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This entire function is quite complex now. As a followup we should break it down into smaller functions but after adding good amount of unit tests for this,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah i have mentioned this in the PR description. we would have to split all the bug functions into smaller ones.

Copy link
Copy Markdown
Member

@hariombalhara hariombalhara left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are failing unit tests and logs need to be improved.

@github-actions github-actions bot marked this pull request as draft December 13, 2024 03:36
@Udit-takkar Udit-takkar marked this pull request as ready for review December 13, 2024 10:10
@dosubot dosubot bot added the crm-apps area: crm apps, salesforce, hubspot, close.com, sendgrid label Dec 13, 2024
@github-actions
Copy link
Copy Markdown
Contributor

E2E results are ready!

@keithwillcode keithwillcode modified the milestones: v4.8, v4.9 Dec 15, 2024
@joeauyeung joeauyeung merged commit 423b17a into main Dec 16, 2024
@joeauyeung joeauyeung deleted the refactor/crmService branch December 16, 2024 15:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core area: core, team members only crm-apps area: crm apps, salesforce, hubspot, close.com, sendgrid enterprise area: enterprise, audit log, organisation, SAML, SSO High priority Created by Linear-GitHub Sync ready-for-e2e 💻 refactor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants