Skip to content

Commit

Permalink
Merge branch 'hotfix/fix_sf_locked_row'
Browse files Browse the repository at this point in the history
  • Loading branch information
emile-bex committed Oct 30, 2023
2 parents 3868016 + 2998b86 commit edbf945
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 33 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "linkedout-backend",
"version": "2.15.4",
"version": "2.15.5",
"license": "ISC",
"engines": {
"node": "16.x"
Expand Down
101 changes: 69 additions & 32 deletions src/external-services/salesforce/salesforce.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,21 +396,42 @@ export class SalesforceService {
}

async createOrUpdateTask(params: TaskProps | TaskProps[]) {
let records: SalesforceTask | SalesforceTask[];
if (Array.isArray(params)) {
records = params.map((singleParams) => {
const records = params.map((singleParams) => {
return mapSalesforceTaskFields(singleParams);
});

return this.upsertRecord(
ObjectNames.TASK,
records,
'ID_Externe__c',
'findTaskById'
);
} else {
records = mapSalesforceTaskFields(params);
const record = mapSalesforceTaskFields(params);
try {
return (await this.upsertRecord(
ObjectNames.TASK,
record,
'ID_Externe__c',
'findTaskById'
)) as string;
} catch (err) {
if (
(err as SalesforceError).errorCode ===
ErrorCodes.CANNOT_UPDATE_CONVERTED_LEAD
) {
return (await this.upsertRecord(
ObjectNames.TASK,
{ ...record, WhoId: params.contactSfId },
'ID_Externe__c',
'findTaskById'
)) as string;
}
console.error(err);
throw err;
}
}

return this.upsertRecord(
ObjectNames.TASK,
records,
'ID_Externe__c',
'findTaskById'
);
}

async searchAccountByName(search: string, recordType: AccountRecordType) {
Expand Down Expand Up @@ -983,10 +1004,10 @@ export class SalesforceService {
zone,
autreSource: 'Formulaire_Sourcing_Page_Travailler',
} as const;
const leadId = (await this.createCandidateLead(leadToCreate)) as string;

if (infoCo) {
try {
const leadId = (await this.createCandidateLead(leadToCreate)) as string;
await this.createCampaignMemberInfoCo({ leadId }, infoCo);
return leadId;
} catch (err) {
Expand All @@ -996,48 +1017,61 @@ export class SalesforceService {
(err as SalesforceError).errorCode ===
ErrorCodes.FIELD_INTEGRITY_EXCEPTION
) {
const contactId = await this.findContact(email);
const contactSfId = await this.findContact(email);
await this.createCampaignMemberInfoCo(
{ contactId: contactId },
{ contactId: contactSfId },
infoCo
);
return leadId;
return contactSfId;
}
console.error(err);
throw err;
}
}
}

async createCampaignMemberInfoCo(
async findOrCreateCampaignMember(
leadOrContactId: { leadId?: string; contactId?: string },
infoCoId: string
) {
try {
let campaignMemberId = await this.findCampaignMember(
const campaignMemberSfId = await this.findCampaignMember(
leadOrContactId,
infoCoId
);
const { leadId, contactId } = leadOrContactId;
if (!campaignMemberId) {
campaignMemberId = (await this.createRecord(
ObjectNames.CAMPAIGN_MEMBER,
{
...(leadId
? {
LeadId: leadId,
}
: { ContactId: contactId }),
CampaignId: infoCoId,
Status: 'Inscrit',
}
)) as string;
if (!campaignMemberSfId) {
await this.createRecord(ObjectNames.CAMPAIGN_MEMBER, {
...(leadId
? {
LeadId: leadId,
}
: { ContactId: contactId }),
CampaignId: infoCoId,
Status: 'Inscrit',
});
}
return campaignMemberId;
} catch (err) {
// Case where the flow is not as fast as the api call
if ((err as SalesforceError).errorCode === ErrorCodes.DUPLICATE_VALUE) {
return this.findCampaignMember(leadOrContactId, infoCoId);
return;
}
console.error(err);
throw err;
}
}

async createCampaignMemberInfoCo(
leadOrContactId: { leadId?: string; contactId?: string },
infoCoId: string
) {
try {
await this.findOrCreateCampaignMember(leadOrContactId, infoCoId);
} catch (err) {
if (
(err as SalesforceError).errorCode !== ErrorCodes.UNABLE_TO_LOCK_ROW
) {
await asyncTimeout(1000);
await this.findOrCreateCampaignMember(leadOrContactId, infoCoId);
}
console.error(err);
throw err;
Expand Down Expand Up @@ -1281,11 +1315,14 @@ export class SalesforceService {

const ownerSfId = await this.findOwnerByLeadSfId(leadSfId);

const contactSfId = await this.findContact(email);

return {
binomeSfId,
externalMessageId,
ownerSfId,
leadSfId,
contactSfId,
subject: `Message envoyé à ${candidateFirstName} ${candidateLastName} LinkedOut via le site`,
zone,
};
Expand Down
2 changes: 2 additions & 0 deletions src/external-services/salesforce/salesforce.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export const ErrorCodes = {
FIELD_INTEGRITY_EXCEPTION: 'FIELD_INTEGRITY_EXCEPTION',
FIELD_FILTER_VALIDATION_EXCEPTION: 'FIELD_FILTER_VALIDATION_EXCEPTION',
NOT_FOUND: 'NOT_FOUND',
UNABLE_TO_LOCK_ROW: 'UNABLE_TO_LOCK_ROW',
} as const;

export type ErrorCode = typeof ErrorCodes[keyof typeof ErrorCodes];
Expand Down Expand Up @@ -423,6 +424,7 @@ export interface TaskProps {
subject: string;
ownerSfId: string;
leadSfId: string;
contactSfId?: string;
binomeSfId: string;
zone: CompanyZone;
}
Expand Down
1 change: 1 addition & 0 deletions tests/mocks.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export const SalesforceMocks: ProviderMock<SalesforceService> = {
setIsWorker: jest.fn(),
updateLead: jest.fn(),
findCampaignMember: jest.fn(),
findOrCreateCampaignMember: jest.fn(),
findOrCreateLeadFromInscriptionCandidateForm: jest.fn(),
findOrCreateLeadFromExternalMessage: jest.fn(),
createCampaignMemberInfoCo: jest.fn(),
Expand Down

0 comments on commit edbf945

Please sign in to comment.