Skip to content

Commit

Permalink
[EN-6531] chore(salesforce): better management of salesforce authenti…
Browse files Browse the repository at this point in the history
…fication
  • Loading branch information
emile-bex committed Nov 24, 2023
1 parent 80cf01c commit b46044f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 19 deletions.
40 changes: 25 additions & 15 deletions src/external-services/salesforce/salesforce.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ export class SalesforceService {
);
}

async checkIfConnected() {
try {
await this.salesforce
.sobject(ObjectNames.USER)
.find({ Email: process.env.SALESFORCE_USERNAME });
} catch (error) {
await this.refreshSalesforceInstance();
}
}

async refreshSalesforceInstance(retries?: number) {
const remainingRetries = retries || retries === 0 ? retries : RETRY_NUMBER;
try {
Expand All @@ -131,7 +141,7 @@ export class SalesforceService {
name: T,
params: SalesforceObject<T, K> | SalesforceObject<T, K>[]
): Promise<string | string[]> {
await this.refreshSalesforceInstance();
await this.checkIfConnected();

try {
if (Array.isArray(params)) {
Expand Down Expand Up @@ -175,7 +185,7 @@ export class SalesforceService {
name: T,
params: SalesforceObject<T, K> | SalesforceObject<T, K>[]
): Promise<string | string[]> {
await this.refreshSalesforceInstance();
await this.checkIfConnected();

try {
if (Array.isArray(params)) {
Expand Down Expand Up @@ -234,7 +244,7 @@ export class SalesforceService {
| 'findEventById'
| 'findTaskById'
): Promise<string | string[]> {
await this.refreshSalesforceInstance();
await this.checkIfConnected();

try {
if (Array.isArray(params)) {
Expand Down Expand Up @@ -437,7 +447,7 @@ export class SalesforceService {

async searchAccountByName(search: string, recordType: AccountRecordType) {
const escapedSearch = search?.replace(/[?&|!{}[\]()^~*:\\"'+-]/gi, '\\$&');
await this.refreshSalesforceInstance();
await this.checkIfConnected();
if (escapedSearch.length === 1) {
const { records }: { records: Partial<SalesforceAccount>[] } =
await this.salesforce.query(
Expand Down Expand Up @@ -476,7 +486,7 @@ export class SalesforceService {
}

async findContact(email: string, recordType?: ContactRecordType) {
await this.refreshSalesforceInstance();
await this.checkIfConnected();
const { records }: { records: Partial<SalesforceContact>[] } =
await this.salesforce.query(
`SELECT Id
Expand All @@ -489,7 +499,7 @@ export class SalesforceService {
}

async findLead<T extends LeadRecordType>(email: string, recordType: T) {
await this.refreshSalesforceInstance();
await this.checkIfConnected();
const { records }: { records: Partial<SalesforceLead<T>>[] } =
await this.salesforce.query(
`SELECT Id
Expand All @@ -502,7 +512,7 @@ export class SalesforceService {
}

async findOwnerByLeadSfId<T extends LeadRecordType>(id: string) {
await this.refreshSalesforceInstance();
await this.checkIfConnected();
const { records }: { records: Partial<SalesforceLead<T>>[] } =
await this.salesforce.query(
`SELECT OwnerId
Expand All @@ -517,7 +527,7 @@ export class SalesforceService {
{ leadId, contactId }: { leadId?: string; contactId?: string },
infoCoId: string
) {
await this.refreshSalesforceInstance();
await this.checkIfConnected();
const { records }: { records: Partial<SalesforceCampaignMember>[] } =
await this.salesforce.query(
`SELECT Id
Expand All @@ -529,7 +539,7 @@ export class SalesforceService {
}

async findBinomeByCandidateSfId<T>(id: T) {
await this.refreshSalesforceInstance();
await this.checkIfConnected();
const { records }: { records: Partial<SalesforceBinome>[] } =
await this.salesforce.query(
`SELECT Id
Expand All @@ -540,7 +550,7 @@ export class SalesforceService {
}

async findOfferById<T>(id: T): Promise<string> {
await this.refreshSalesforceInstance();
await this.checkIfConnected();
const { records }: { records: Partial<SalesforceOffer>[] } =
await this.salesforce.query(
`SELECT Id
Expand All @@ -551,7 +561,7 @@ export class SalesforceService {
}

async findEventById<T>(id: T): Promise<string> {
await this.refreshSalesforceInstance();
await this.checkIfConnected();
const { records }: { records: Partial<SalesforceEvent>[] } =
await this.salesforce.query(
`SELECT Id
Expand All @@ -563,7 +573,7 @@ export class SalesforceService {
}

async findTaskById<T>(id: T): Promise<string> {
await this.refreshSalesforceInstance();
await this.checkIfConnected();
const { records }: { records: Partial<SalesforceTask>[] } =
await this.salesforce.query(
`SELECT Id
Expand All @@ -574,7 +584,7 @@ export class SalesforceService {
}

async findOfferRelationsById<T>(id: T) {
await this.refreshSalesforceInstance();
await this.checkIfConnected();
const { records }: { records: Partial<SalesforceOffer>[] } =
await this.salesforce.query(
`SELECT Entreprise_Recruteuse__c, Prenom_Nom_du_recruteur__c
Expand All @@ -588,7 +598,7 @@ export class SalesforceService {
}

async findProcessById<T>(id: T) {
await this.refreshSalesforceInstance();
await this.checkIfConnected();
const { records }: { records: Partial<SalesforceProcess>[] } =
await this.salesforce.query(
`SELECT Id
Expand Down Expand Up @@ -1526,7 +1536,7 @@ export class SalesforceService {

async getCampaigns() {
this.setIsWorker(false);
await this.refreshSalesforceInstance();
await this.checkIfConnected();

const {
records: timeZoneRecords,
Expand Down
11 changes: 7 additions & 4 deletions src/external-services/salesforce/salesforce.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export const ObjectNames = {
CAMPAIGN: 'Campaign',
CAMPAIGN_MEMBER: 'CampaignMember',
TASK: 'Task',
USER: 'User',
} as const;

export type ObjectName = typeof ObjectNames[keyof typeof ObjectNames];
Expand All @@ -89,6 +90,7 @@ type SalesforceObjects<K extends LeadRecordType> = {
[ObjectNames.CAMPAIGN]: SalesforceCampaign;
[ObjectNames.CAMPAIGN_MEMBER]: SalesforceCampaignMember;
[ObjectNames.TASK]: SalesforceTask;
[ObjectNames.USER]: SalesforceUser;
};

export type SalesforceObject<
Expand Down Expand Up @@ -472,10 +474,6 @@ export interface SalesforceCampaign {
Heure_de_d_but__c: string;
Adresse_de_l_v_nement__c: string;
Code_postal__c: string;
attributes: {
type: string;
url: string;
};
}

export interface SalesforceCampaignMember {
Expand All @@ -486,6 +484,11 @@ export interface SalesforceCampaignMember {
Status: string; // Inscrit
}

export interface SalesforceUser {
Id?: string;
Email: string;
}

export interface ExternalMessageProps {
firstName: string;
lastName: string;
Expand Down
1 change: 1 addition & 0 deletions tests/mocks.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export const SalesforceMocks: ProviderMock<SalesforceService> = {
createCampaignMemberInfoCo: jest.fn(),
createOrUpdateInscriptionCandidateSalesforceLead: jest.fn(),
getCampaigns: jest.fn(),
checkIfConnected: jest.fn(),
} as const;

export const MailjetMock: ProviderMock<MailjetService> = {
Expand Down

0 comments on commit b46044f

Please sign in to comment.