From 6dda87511494ebe9ff77c2760af2fd8dba373342 Mon Sep 17 00:00:00 2001 From: Ryan Liu Date: Tue, 12 Dec 2023 14:28:48 -0800 Subject: [PATCH] chore: inject all CRM field mappings into CRM clients (for expand to work) (#2055) This makes it so that expanded association objects follow whatever schema is relevant for that object. --- packages/core/remotes/categories/crm/base.ts | 10 +- packages/core/remotes/impl/hubspot/index.ts | 98 +++++++++++-------- .../impl/ms_dynamics_365_sales/index.ts | 77 +++++++++------ packages/core/remotes/impl/pipedrive/index.ts | 80 ++++++++------- .../core/remotes/impl/salesforce/index.ts | 69 ++++++++----- .../crm/common_object_service.ts | 8 +- packages/core/services/connection_service.ts | 24 ++++- packages/types/field_mapping_config.ts | 4 + 8 files changed, 233 insertions(+), 137 deletions(-) diff --git a/packages/core/remotes/categories/crm/base.ts b/packages/core/remotes/categories/crm/base.ts index 6def412d2..a20d6c73b 100644 --- a/packages/core/remotes/categories/crm/base.ts +++ b/packages/core/remotes/categories/crm/base.ts @@ -6,7 +6,7 @@ import type { ListCRMCommonObjectTypeMap, ListMetadata, } from '@supaglue/types/crm'; -import type { FieldMappingConfig } from '@supaglue/types/field_mapping_config'; +import type { AllCrmFieldMappingConfigs, FieldMappingConfig } from '@supaglue/types/field_mapping_config'; import type { Readable } from 'stream'; import { NotImplementedError } from '../../../errors'; import type { PaginatedSupaglueRecords } from '../../../lib'; @@ -24,7 +24,7 @@ export interface CrmRemoteClient extends RemoteClient { getCommonObjectRecord( commonObjectType: T, id: string, - fieldMappingConfig: FieldMappingConfig, + allFieldMappingConfigs: AllCrmFieldMappingConfigs, params: CRMCommonObjectTypeMap['getParams'] ): Promise['object']>; createCommonObjectRecord( @@ -46,7 +46,7 @@ export interface CrmRemoteClient extends RemoteClient { ): Promise['object']>>; listCommonObjectRecords( commonObjectType: CRMCommonObjectType, - fieldMappingConfig: FieldMappingConfig, + allFieldMappingConfigs: AllCrmFieldMappingConfigs, params: CRMCommonObjectTypeMap['listParams'] ): Promise['object']>>; @@ -80,7 +80,7 @@ export abstract class AbstractCrmRemoteClient extends AbstractRemoteClient imple public async getCommonObjectRecord( commonObjectType: T, id: string, - fieldMappingConfig: FieldMappingConfig, + allFieldMappingConfigs: AllCrmFieldMappingConfigs, params: CRMCommonObjectTypeMap['getParams'] ): Promise['object']> { throw new NotImplementedError(); @@ -114,7 +114,7 @@ export abstract class AbstractCrmRemoteClient extends AbstractRemoteClient imple listCommonObjectRecords( commonObjectType: CRMCommonObjectType, - fieldMappingConfig: FieldMappingConfig, + allFieldMappingConfigs: AllCrmFieldMappingConfigs, params: CRMCommonObjectTypeMap['listParams'] ): Promise['object']>> { throw new NotImplementedError(); diff --git a/packages/core/remotes/impl/hubspot/index.ts b/packages/core/remotes/impl/hubspot/index.ts index e42b12c39..bb67b488d 100644 --- a/packages/core/remotes/impl/hubspot/index.ts +++ b/packages/core/remotes/impl/hubspot/index.ts @@ -61,7 +61,7 @@ import type { SimpleCustomObjectSchemaDeprecated, } from '@supaglue/types/custom_object'; import type { FieldsToFetch } from '@supaglue/types/fields_to_fetch'; -import type { FieldMappingConfig } from '@supaglue/types/field_mapping_config'; +import type { AllCrmFieldMappingConfigs, FieldMappingConfig } from '@supaglue/types/field_mapping_config'; import type { FormField } from '@supaglue/types/marketing_automation/form_field'; import type { FormMetadata } from '@supaglue/types/marketing_automation/form_metadata'; import type { SubmitFormData, SubmitFormResult } from '@supaglue/types/marketing_automation/submit_form'; @@ -1135,20 +1135,20 @@ class HubSpotClient extends AbstractCrmRemoteClient implements MarketingAutomati public override async getCommonObjectRecord( commonObjectType: T, id: string, - fieldMappingConfig: FieldMappingConfig, + allFieldMappingConfigs: AllCrmFieldMappingConfigs, params: CRMCommonObjectTypeMap['getParams'] ): Promise['object']> { switch (commonObjectType) { case 'account': - return this.getAccount(id, fieldMappingConfig, params); + return this.getAccount(id, allFieldMappingConfigs, params); case 'contact': - return this.getContact(id, fieldMappingConfig, params); + return this.getContact(id, allFieldMappingConfigs, params); case 'lead': throw new Error('Cannot get leads in HubSpot'); case 'opportunity': - return this.getOpportunity(id, fieldMappingConfig, params); + return this.getOpportunity(id, allFieldMappingConfigs, params); case 'user': - return this.getUser(id, fieldMappingConfig, params); + return this.getUser(id, allFieldMappingConfigs, params); default: throw new Error(`Unsupported common object type: ${commonObjectType}`); } @@ -1205,7 +1205,7 @@ class HubSpotClient extends AbstractCrmRemoteClient implements MarketingAutomati public override async listCommonObjectRecords( commonObjectType: CRMCommonObjectType, - fieldMappingConfig: FieldMappingConfig, + allFieldMappingConfigs: AllCrmFieldMappingConfigs, params: CRMCommonObjectTypeMap['listParams'] ): Promise['object']>> { // TODO: Implement expand for lists @@ -1214,15 +1214,15 @@ class HubSpotClient extends AbstractCrmRemoteClient implements MarketingAutomati } switch (commonObjectType) { case 'contact': - return await this.listContacts(params, fieldMappingConfig); + return await this.listContacts(params, allFieldMappingConfigs); case 'lead': - return await this.listLeads(params, fieldMappingConfig); + return await this.listLeads(params, allFieldMappingConfigs); case 'account': - return await this.listAccounts(params, fieldMappingConfig); + return await this.listAccounts(params, allFieldMappingConfigs); case 'opportunity': - return await this.listOpportunities(params, fieldMappingConfig); + return await this.listOpportunities(params, allFieldMappingConfigs); case 'user': - return await this.listUsers(params, fieldMappingConfig); + return await this.listUsers(params, allFieldMappingConfigs); default: throw new Error(`Unsupported common object type: ${commonObjectType}`); } @@ -1521,10 +1521,10 @@ class HubSpotClient extends AbstractCrmRemoteClient implements MarketingAutomati public async listAccounts( params: CrmListParams, - fieldMappingConfig: FieldMappingConfig + allFieldMappingConfigs: AllCrmFieldMappingConfigs ): Promise> { await this.maybeRefreshAccessToken(); - const properties = await this.getCommonObjectPropertyIdsToFetch('company', fieldMappingConfig); + const properties = await this.getCommonObjectPropertyIdsToFetch('company', allFieldMappingConfigs.account); const { standardObjectTypes: associatedStandardObjectTypes, customObjectSchemas: associatedCustomObjectSchemas } = await this.#getAssociatedObjectTypesForObjectTypeFeatureFlagged('company', params.associationsToFetch); const response = params.modifiedAfter @@ -1553,7 +1553,7 @@ class HubSpotClient extends AbstractCrmRemoteClient implements MarketingAutomati ...fromHubSpotCompanyToAccount(result.rawData), rawData: params.includeRawData ? { - ...toMappedProperties(result.rawData.properties, fieldMappingConfig), + ...toMappedProperties(result.rawData.properties, allFieldMappingConfigs.account), _associations: result.rawData.associations, } : undefined, @@ -1569,8 +1569,12 @@ class HubSpotClient extends AbstractCrmRemoteClient implements MarketingAutomati }; } - public async getAccount(id: string, fieldMappingConfig: FieldMappingConfig, params: CrmGetParams): Promise { - const properties = await this.getCommonObjectPropertyIdsToFetch('company', fieldMappingConfig); + public async getAccount( + id: string, + allFieldMappingConfigs: AllCrmFieldMappingConfigs, + params: CrmGetParams + ): Promise { + const properties = await this.getCommonObjectPropertyIdsToFetch('company', allFieldMappingConfigs.account); const { standardObjectTypes: associatedStandardObjectTypes, customObjectSchemas: associatedCustomObjectSchemas } = await this.#getAssociatedObjectTypesForObjectTypeFeatureFlagged('company', params.associationsToFetch); const associations = [ @@ -1590,11 +1594,14 @@ class HubSpotClient extends AbstractCrmRemoteClient implements MarketingAutomati associations: flattenedAssociations, } as unknown as RecordWithFlattenedAssociations), rawData: params.includeRawData - ? { ...toMappedProperties(company.properties, fieldMappingConfig), _associations: flattenedAssociations } + ? { + ...toMappedProperties(company.properties, allFieldMappingConfigs.account), + _associations: flattenedAssociations, + } : undefined, }; if (account.ownerId && (params.expand?.includes('user') || params.expand?.includes('owner'))) { - account.owner = await this.getUser(account.ownerId, fieldMappingConfig, { + account.owner = await this.getUser(account.ownerId, allFieldMappingConfigs, { includeRawData: params.includeRawData, }); } @@ -1738,11 +1745,11 @@ class HubSpotClient extends AbstractCrmRemoteClient implements MarketingAutomati public async listOpportunities( params: CrmListParams, - fieldMappingConfig: FieldMappingConfig + allFieldMappingConfigs: AllCrmFieldMappingConfigs ): Promise> { await this.maybeRefreshAccessToken(); const pipelineStageMapping = await this.#getPipelineStageMapping(); - const properties = await this.getCommonObjectPropertyIdsToFetch('deal', fieldMappingConfig); + const properties = await this.getCommonObjectPropertyIdsToFetch('deal', allFieldMappingConfigs.opportunity); const { standardObjectTypes: associatedStandardObjectTypes, customObjectSchemas: associatedCustomObjectSchemas } = await this.#getAssociatedObjectTypesForObjectTypeFeatureFlagged('deal', params.associationsToFetch); const response = params.modifiedAfter @@ -1771,7 +1778,7 @@ class HubSpotClient extends AbstractCrmRemoteClient implements MarketingAutomati ...fromHubSpotDealToOpportunity(result.rawData, pipelineStageMapping), rawData: params.includeRawData ? { - ...toMappedProperties(result.rawData.properties, fieldMappingConfig), + ...toMappedProperties(result.rawData.properties, allFieldMappingConfigs.opportunity), _associations: result.rawData.associations, } : undefined, @@ -1789,11 +1796,11 @@ class HubSpotClient extends AbstractCrmRemoteClient implements MarketingAutomati public async getOpportunity( id: string, - fieldMappingConfig: FieldMappingConfig, + allFieldMappingConfigs: AllCrmFieldMappingConfigs, params: CrmGetParams ): Promise { const pipelineStageMapping = await this.#getPipelineStageMapping(); - const properties = await this.getCommonObjectPropertyIdsToFetch('deal', fieldMappingConfig); + const properties = await this.getCommonObjectPropertyIdsToFetch('deal', allFieldMappingConfigs.opportunity); const { standardObjectTypes: associatedStandardObjectTypes, customObjectSchemas: associatedCustomObjectSchemas } = await this.#getAssociatedObjectTypesForObjectTypeFeatureFlagged('deal', params.associationsToFetch); const associations = [ @@ -1816,16 +1823,19 @@ class HubSpotClient extends AbstractCrmRemoteClient implements MarketingAutomati pipelineStageMapping ), rawData: params.includeRawData - ? { ...toMappedProperties(deal.properties, fieldMappingConfig), _associations: flattenedAssociations } + ? { + ...toMappedProperties(deal.properties, allFieldMappingConfigs.opportunity), + _associations: flattenedAssociations, + } : undefined, }; if (opportunity.ownerId && (params.expand?.includes('user') || params.expand?.includes('owner'))) { - opportunity.owner = await this.getUser(opportunity.ownerId, fieldMappingConfig, { + opportunity.owner = await this.getUser(opportunity.ownerId, allFieldMappingConfigs, { includeRawData: params.includeRawData, }); } if (opportunity.accountId && params.expand?.includes('account')) { - opportunity.account = await this.getAccount(opportunity.accountId, fieldMappingConfig, { + opportunity.account = await this.getAccount(opportunity.accountId, allFieldMappingConfigs, { includeRawData: params.includeRawData, }); } @@ -1946,8 +1956,12 @@ class HubSpotClient extends AbstractCrmRemoteClient implements MarketingAutomati ]); } - public async getContact(id: string, fieldMappingConfig: FieldMappingConfig, params: CrmGetParams): Promise { - const properties = await this.getCommonObjectPropertyIdsToFetch('contact', fieldMappingConfig); + public async getContact( + id: string, + allFieldMappingConfigs: AllCrmFieldMappingConfigs, + params: CrmGetParams + ): Promise { + const properties = await this.getCommonObjectPropertyIdsToFetch('contact', allFieldMappingConfigs.contact); const { standardObjectTypes: associatedStandardObjectTypes, customObjectSchemas: associatedCustomObjectSchemas } = await this.#getAssociatedObjectTypesForObjectTypeFeatureFlagged('contact', params.associationsToFetch); @@ -1969,18 +1983,18 @@ class HubSpotClient extends AbstractCrmRemoteClient implements MarketingAutomati } as unknown as RecordWithFlattenedAssociations), rawData: params.includeRawData ? { - ...toMappedProperties(hubspotContact.properties, fieldMappingConfig), + ...toMappedProperties(hubspotContact.properties, allFieldMappingConfigs.contact), _associations: flattenedAssociations, } : undefined, }; if (contact.accountId && params.expand?.includes('account')) { - contact.account = await this.getAccount(contact.accountId, fieldMappingConfig, { + contact.account = await this.getAccount(contact.accountId, allFieldMappingConfigs, { includeRawData: params.includeRawData, }); } if (contact.ownerId && (params.expand?.includes('user') || params.expand?.includes('owner'))) { - contact.owner = await this.getUser(contact.ownerId, fieldMappingConfig, { + contact.owner = await this.getUser(contact.ownerId, allFieldMappingConfigs, { includeRawData: params.includeRawData, }); } @@ -2012,10 +2026,10 @@ class HubSpotClient extends AbstractCrmRemoteClient implements MarketingAutomati public async listContacts( params: CrmListParams, - fieldMappingConfig: FieldMappingConfig + allFieldMappingConfigs: AllCrmFieldMappingConfigs ): Promise> { await this.maybeRefreshAccessToken(); - const properties = await this.getCommonObjectPropertyIdsToFetch('contact', fieldMappingConfig); + const properties = await this.getCommonObjectPropertyIdsToFetch('contact', allFieldMappingConfigs.contact); const { standardObjectTypes: associatedStandardObjectTypes, customObjectSchemas: associatedCustomObjectSchemas } = await this.#getAssociatedObjectTypesForObjectTypeFeatureFlagged('contact', params.associationsToFetch); const response = params.modifiedAfter @@ -2044,7 +2058,7 @@ class HubSpotClient extends AbstractCrmRemoteClient implements MarketingAutomati ...fromHubSpotContactToContact(result.rawData), rawData: params.includeRawData ? { - ...toMappedProperties(result.rawData.properties, fieldMappingConfig), + ...toMappedProperties(result.rawData.properties, allFieldMappingConfigs.contact), _associations: result.rawData.associations, } : undefined, @@ -2165,7 +2179,7 @@ class HubSpotClient extends AbstractCrmRemoteClient implements MarketingAutomati public async listLeads( params: CrmListParams, - fieldMappingConfig: FieldMappingConfig + allFieldMappingConfigs: AllCrmFieldMappingConfigs ): Promise> { throw new BadRequestError('Listing leads is not supported for hubspot'); } @@ -2193,17 +2207,21 @@ class HubSpotClient extends AbstractCrmRemoteClient implements MarketingAutomati throw new BadRequestError('Not supported'); } - public async getUser(id: string, fieldMappingConfig: FieldMappingConfig, params: CrmGetParams): Promise { + public async getUser( + id: string, + allFieldMappingConfigs: AllCrmFieldMappingConfigs, + params: CrmGetParams + ): Promise { const owner = await this.#client.crm.owners.ownersApi.getById(parseInt(id)); return { ...fromHubspotOwnerToUser(owner), - rawData: params.includeRawData ? toMappedProperties(owner, fieldMappingConfig) : undefined, + rawData: params.includeRawData ? toMappedProperties(owner, allFieldMappingConfigs.user) : undefined, }; } public async listUsers( params: CrmListParams, - fieldMappingConfig: FieldMappingConfig + allFieldMappingConfigs: AllCrmFieldMappingConfigs ): Promise> { let response = await this.#listUsersFull( /* archived */ false, @@ -2217,7 +2235,7 @@ class HubSpotClient extends AbstractCrmRemoteClient implements MarketingAutomati return { ...fromHubspotOwnerToUser(result), rawData: params.includeRawData - ? toMappedProperties(fromHubspotOwnerToUser(result).rawData, fieldMappingConfig) + ? toMappedProperties(fromHubspotOwnerToUser(result).rawData, allFieldMappingConfigs.user) : undefined, }; }); diff --git a/packages/core/remotes/impl/ms_dynamics_365_sales/index.ts b/packages/core/remotes/impl/ms_dynamics_365_sales/index.ts index 5b2f6c5be..c92fe862f 100644 --- a/packages/core/remotes/impl/ms_dynamics_365_sales/index.ts +++ b/packages/core/remotes/impl/ms_dynamics_365_sales/index.ts @@ -26,7 +26,7 @@ import type { User, } from '@supaglue/types/crm'; import type { FieldsToFetch } from '@supaglue/types/fields_to_fetch'; -import type { FieldMappingConfig } from '@supaglue/types/field_mapping_config'; +import type { AllCrmFieldMappingConfigs, FieldMappingConfig } from '@supaglue/types/field_mapping_config'; import type { StandardOrCustomObject } from '@supaglue/types/standard_or_custom_object'; import { MS_DYNAMICS_365_SALES_STANDARD_OBJECTS } from '@supaglue/utils'; import type { OHandler } from 'odata'; @@ -355,7 +355,7 @@ class MsDynamics365Sales extends AbstractCrmRemoteClient { public override async listCommonObjectRecords( commonObjectType: CRMCommonObjectType, - fieldMappingConfig: FieldMappingConfig, + allFieldMappingConfigs: AllCrmFieldMappingConfigs, params: CRMCommonObjectTypeMap['listParams'] ): Promise['object']>> { if (params.expand?.length) { @@ -363,15 +363,15 @@ class MsDynamics365Sales extends AbstractCrmRemoteClient { } switch (commonObjectType) { case 'contact': - return await this.listContacts(fieldMappingConfig, params); + return await this.listContacts(allFieldMappingConfigs, params); case 'lead': - return await this.listLeads(fieldMappingConfig, params); + return await this.listLeads(allFieldMappingConfigs, params); case 'opportunity': - return await this.listOpportunities(fieldMappingConfig, params); + return await this.listOpportunities(allFieldMappingConfigs, params); case 'account': - return await this.listAccounts(fieldMappingConfig, params); + return await this.listAccounts(allFieldMappingConfigs, params); case 'user': - return await this.listUsers(fieldMappingConfig, params); + return await this.listUsers(allFieldMappingConfigs, params); default: throw new Error(`Unsupported common object type: ${commonObjectType}`); } @@ -380,7 +380,7 @@ class MsDynamics365Sales extends AbstractCrmRemoteClient { public override async getCommonObjectRecord( commonObjectType: T, id: string, - fieldMappingConfig: FieldMappingConfig, + allFieldMappingConfigs: AllCrmFieldMappingConfigs, params: CRMCommonObjectTypeMap['getParams'] ): Promise['object']> { if (params.expand?.length) { @@ -388,15 +388,15 @@ class MsDynamics365Sales extends AbstractCrmRemoteClient { } switch (commonObjectType) { case 'account': - return await this.getAccount(id, fieldMappingConfig, params); + return await this.getAccount(id, allFieldMappingConfigs, params); case 'contact': - return await this.getContact(id, fieldMappingConfig, params); + return await this.getContact(id, allFieldMappingConfigs, params); case 'lead': - return await this.getLead(id, fieldMappingConfig, params); + return await this.getLead(id, allFieldMappingConfigs, params); case 'opportunity': - return await this.getOpportunity(id, fieldMappingConfig, params); + return await this.getOpportunity(id, allFieldMappingConfigs, params); case 'user': - return await this.getUser(id, fieldMappingConfig, params); + return await this.getUser(id, allFieldMappingConfigs, params); default: throw new Error(`Unsupported common object type: ${commonObjectType}`); } @@ -497,51 +497,67 @@ class MsDynamics365Sales extends AbstractCrmRemoteClient { return response[0]?.systemuserid; } - private async getAccount(id: string, fieldMappingConfig: FieldMappingConfig, params: CrmGetParams): Promise { + private async getAccount( + id: string, + allFieldMappingConfigs: AllCrmFieldMappingConfigs, + params: CrmGetParams + ): Promise { await this.maybeRefreshAccessToken(); return fromDynamicsAccountToRemoteAccount( (await this.#odata.get('accounts').query({ $filter: `accountid eq '${id}'` }))[0], - fieldMappingConfig, + allFieldMappingConfigs.account, params.includeRawData ?? false ); } - private async getContact(id: string, fieldMappingConfig: FieldMappingConfig, params: CrmGetParams): Promise { + private async getContact( + id: string, + allFieldMappingConfigs: AllCrmFieldMappingConfigs, + params: CrmGetParams + ): Promise { await this.maybeRefreshAccessToken(); return fromDynamicsContactToRemoteContact( (await this.#odata.get('contacts').query({ $filter: `contactid eq '${id}'` }))[0], - fieldMappingConfig, + allFieldMappingConfigs.contact, params.includeRawData ?? false ); } - private async getLead(id: string, fieldMappingConfig: FieldMappingConfig, params: CrmGetParams): Promise { + private async getLead( + id: string, + allFieldMappingConfigs: AllCrmFieldMappingConfigs, + params: CrmGetParams + ): Promise { await this.maybeRefreshAccessToken(); return fromDynamicsLeadToRemoteLead( (await this.#odata.get('leads').query({ $filter: `leadid eq '${id}'` }))[0], - fieldMappingConfig, + allFieldMappingConfigs.lead, params.includeRawData ?? false ); } private async getOpportunity( id: string, - fieldMappingConfig: FieldMappingConfig, + allFieldMappingConfigs: AllCrmFieldMappingConfigs, params: CrmGetParams ): Promise { await this.maybeRefreshAccessToken(); return fromDynamicsOpportunityToRemoteOpportunity( (await this.#odata.get('opportunities').query({ $filter: `opportunityid eq '${id}'` }))[0], - fieldMappingConfig, + allFieldMappingConfigs.opportunity, params.includeRawData ?? false ); } - private async getUser(id: string, fieldMappingConfig: FieldMappingConfig, params: CrmGetParams): Promise { + private async getUser( + id: string, + allFieldMappingConfigs: AllCrmFieldMappingConfigs, + params: CrmGetParams + ): Promise { await this.maybeRefreshAccessToken(); return fromDynamicsUserToRemoteUser( (await this.#odata.get('systemusers').query({ $filter: `systemuserid eq '${id}'` }))[0], - fieldMappingConfig, + allFieldMappingConfigs.user, params.includeRawData ?? false ); } @@ -633,45 +649,50 @@ class MsDynamics365Sales extends AbstractCrmRemoteClient { } private async listAccounts( - fieldMappingConfig: FieldMappingConfig, + allFieldMappingConfigs: AllCrmFieldMappingConfigs, params: CrmListParams ): Promise> { + const fieldMappingConfig = allFieldMappingConfigs.account; return this.listImpl('account', fieldMappingConfig, params, (result) => fromDynamicsAccountToRemoteAccount(result, fieldMappingConfig, params.includeRawData ?? false) ); } private async listContacts( - fieldMappingConfig: FieldMappingConfig, + allFieldMappingConfigs: AllCrmFieldMappingConfigs, params: CrmListParams ): Promise> { + const fieldMappingConfig = allFieldMappingConfigs.contact; return this.listImpl('contact', fieldMappingConfig, params, (result) => fromDynamicsContactToRemoteContact(result, fieldMappingConfig, params.includeRawData ?? false) ); } private async listLeads( - fieldMappingConfig: FieldMappingConfig, + allFieldMappingConfigs: AllCrmFieldMappingConfigs, params: CrmListParams ): Promise> { + const fieldMappingConfig = allFieldMappingConfigs.lead; return this.listImpl('lead', fieldMappingConfig, params, (result) => fromDynamicsLeadToRemoteLead(result, fieldMappingConfig, params.includeRawData ?? false) ); } private async listOpportunities( - fieldMappingConfig: FieldMappingConfig, + allFieldMappingConfigs: AllCrmFieldMappingConfigs, params: CrmListParams ): Promise> { + const fieldMappingConfig = allFieldMappingConfigs.opportunity; return this.listImpl('opportunity', fieldMappingConfig, params, (result) => fromDynamicsOpportunityToRemoteOpportunity(result, fieldMappingConfig, params.includeRawData ?? false) ); } private async listUsers( - fieldMappingConfig: FieldMappingConfig, + allFieldMappingConfigs: AllCrmFieldMappingConfigs, params: CrmListParams ): Promise> { + const fieldMappingConfig = allFieldMappingConfigs.user; return this.listImpl('user', fieldMappingConfig, params, (result) => fromDynamicsUserToRemoteUser(result, fieldMappingConfig, params.includeRawData ?? false) ); diff --git a/packages/core/remotes/impl/pipedrive/index.ts b/packages/core/remotes/impl/pipedrive/index.ts index 2d9977bcf..6950edeaf 100644 --- a/packages/core/remotes/impl/pipedrive/index.ts +++ b/packages/core/remotes/impl/pipedrive/index.ts @@ -29,7 +29,7 @@ import type { OpportunityUpdateParams, User, } from '@supaglue/types/crm'; -import type { FieldMappingConfig } from '@supaglue/types/field_mapping_config'; +import type { AllCrmFieldMappingConfigs, FieldMappingConfig } from '@supaglue/types/field_mapping_config'; import { Readable } from 'stream'; import { BadRequestError, @@ -203,7 +203,7 @@ class PipedriveClient extends AbstractCrmRemoteClient { public override async listCommonObjectRecords( commonObjectType: CRMCommonObjectType, - fieldMappingConfig: FieldMappingConfig, + allFieldMappingConfigs: AllCrmFieldMappingConfigs, params: CRMCommonObjectTypeMap['listParams'] ): Promise['object']>> { if (params.expand?.length) { @@ -211,15 +211,15 @@ class PipedriveClient extends AbstractCrmRemoteClient { } switch (commonObjectType) { case 'contact': - return await this.listContacts(fieldMappingConfig, params); + return await this.listContacts(allFieldMappingConfigs, params); case 'lead': - return await this.listLeads(fieldMappingConfig, params); + return await this.listLeads(allFieldMappingConfigs, params); case 'opportunity': - return await this.listOpportunities(fieldMappingConfig, params); + return await this.listOpportunities(allFieldMappingConfigs, params); case 'account': - return await this.listAccounts(fieldMappingConfig, params); + return await this.listAccounts(allFieldMappingConfigs, params); case 'user': - return await this.listUsers(fieldMappingConfig, params); + return await this.listUsers(allFieldMappingConfigs, params); default: throw new Error(`Unsupported common object type: ${commonObjectType}`); } @@ -259,7 +259,7 @@ class PipedriveClient extends AbstractCrmRemoteClient { } private async listContacts( - fieldMappingConfig: FieldMappingConfig, + allFieldMappingConfigs: AllCrmFieldMappingConfigs, params: CrmListParams ): Promise> { const normalPageFetcher = this.#getListRecordsFetcher( @@ -284,7 +284,7 @@ class PipedriveClient extends AbstractCrmRemoteClient { if (!response.data) { throw new Error('Unexpected response from Pipedrive'); } - const records = response.data.map((result) => mapper(result, fields, fieldMappingConfig)); + const records = response.data.map((result) => mapper(result, fields, allFieldMappingConfigs.contact)); const nextCursor = response.additional_data.pagination?.next_start?.toString(); return { records, @@ -358,7 +358,7 @@ class PipedriveClient extends AbstractCrmRemoteClient { } private async listLeads( - fieldMappingConfig: FieldMappingConfig, + allFieldMappingConfigs: AllCrmFieldMappingConfigs, params: CrmListParams ): Promise> { const normalPageFetcher = this.#getListRecordsFetcher( @@ -383,7 +383,7 @@ class PipedriveClient extends AbstractCrmRemoteClient { if (!response.data) { throw new Error('Unexpected response from Pipedrive'); } - const records = response.data.map((result) => mapper(result, fields, fieldMappingConfig)); + const records = response.data.map((result) => mapper(result, fields, allFieldMappingConfigs.lead)); const nextCursor = response.additional_data.pagination?.next_start?.toString(); return { records, @@ -459,7 +459,7 @@ class PipedriveClient extends AbstractCrmRemoteClient { } private async listOpportunities( - fieldMappingConfig: FieldMappingConfig, + allFieldMappingConfigs: AllCrmFieldMappingConfigs, params: CrmListParams ): Promise> { const pipelineStageMapping = await this.#getPipelineStageMapping(); @@ -485,7 +485,7 @@ class PipedriveClient extends AbstractCrmRemoteClient { if (!response.data) { throw new Error('Unexpected response from Pipedrive'); } - const records = response.data.map((result) => mapper(result, fields, fieldMappingConfig)); + const records = response.data.map((result) => mapper(result, fields, allFieldMappingConfigs.opportunity)); const nextCursor = response.additional_data.pagination?.next_start?.toString(); return { records, @@ -528,7 +528,7 @@ class PipedriveClient extends AbstractCrmRemoteClient { } private async listAccounts( - fieldMappingConfig: FieldMappingConfig, + allFieldMappingConfigs: AllCrmFieldMappingConfigs, params: CrmListParams ): Promise> { const normalPageFetcher = this.#getListRecordsFetcher( @@ -553,7 +553,7 @@ class PipedriveClient extends AbstractCrmRemoteClient { if (!response.data) { throw new Error('Unexpected response from Pipedrive'); } - const records = response.data.map((result) => mapper(result, fields, fieldMappingConfig)); + const records = response.data.map((result) => mapper(result, fields, allFieldMappingConfigs.user)); const nextCursor = response.additional_data.pagination?.next_start?.toString(); return { records, @@ -591,7 +591,7 @@ class PipedriveClient extends AbstractCrmRemoteClient { } private async listUsers( - fieldMappingConfig: FieldMappingConfig, + allFieldMappingConfigs: AllCrmFieldMappingConfigs, params: CrmListParams ): Promise> { const normalPageFetcher = this.#getListRecordsFetcher( @@ -611,7 +611,7 @@ class PipedriveClient extends AbstractCrmRemoteClient { if (!response.data) { throw new Error('Unexpected response from Pipedrive'); } - const records = response.data.map((result) => mapper(result, fieldMappingConfig)); + const records = response.data.map((result) => mapper(result, allFieldMappingConfigs.user)); const nextCursor = response.additional_data.pagination?.next_start?.toString(); return { records, @@ -625,7 +625,7 @@ class PipedriveClient extends AbstractCrmRemoteClient { public override async getCommonObjectRecord( commonObjectType: T, id: string, - fieldMappingConfig: FieldMappingConfig, + allFieldMappingConfigs: AllCrmFieldMappingConfigs, params: CRMCommonObjectTypeMap['getParams'] ): Promise['object']> { if (params.expand?.length) { @@ -633,21 +633,25 @@ class PipedriveClient extends AbstractCrmRemoteClient { } switch (commonObjectType) { case 'contact': - return await this.getContact(id, fieldMappingConfig, params); + return await this.getContact(id, allFieldMappingConfigs, params); case 'lead': - return await this.getLead(id, fieldMappingConfig, params); + return await this.getLead(id, allFieldMappingConfigs, params); case 'opportunity': - return await this.getOpportunity(id, fieldMappingConfig, params); + return await this.getOpportunity(id, allFieldMappingConfigs, params); case 'account': - return await this.getAccount(id, fieldMappingConfig, params); + return await this.getAccount(id, allFieldMappingConfigs, params); case 'user': - return await this.getUser(id, fieldMappingConfig, params); + return await this.getUser(id, allFieldMappingConfigs, params); default: throw new BadRequestError(`Common object ${commonObjectType} not supported`); } } - async getContact(id: string, fieldMappingConfig: FieldMappingConfig, params: CrmGetParams): Promise { + async getContact( + id: string, + allFieldMappingConfigs: AllCrmFieldMappingConfigs, + params: CrmGetParams + ): Promise { await this.maybeRefreshAccessToken(); const fields = await this.#getCustomFieldsForObject('person'); const response = await axios.get(`${this.#credentials.instanceUrl}/api/v1/persons/${id}`, { @@ -656,11 +660,11 @@ class PipedriveClient extends AbstractCrmRemoteClient { const contact = fromPipedrivePersonToContact(response.data.data, fields); return { ...contact, - rawData: params.includeRawData ? toMappedProperties(contact.rawData, fieldMappingConfig) : undefined, + rawData: params.includeRawData ? toMappedProperties(contact.rawData, allFieldMappingConfigs.contact) : undefined, }; } - async getLead(id: string, fieldMappingConfig: FieldMappingConfig, params: CrmGetParams): Promise { + async getLead(id: string, allFieldMappingConfigs: AllCrmFieldMappingConfigs, params: CrmGetParams): Promise { await this.maybeRefreshAccessToken(); const fields = await this.#getCustomFieldsForObject('lead'); const response = await axios.get(`${this.#credentials.instanceUrl}/api/v1/leads/${id}`, { @@ -669,11 +673,15 @@ class PipedriveClient extends AbstractCrmRemoteClient { const lead = fromPipedriveLeadToLead(response.data.data, fields); return { ...lead, - rawData: params.includeRawData ? toMappedProperties(lead.rawData, fieldMappingConfig) : undefined, + rawData: params.includeRawData ? toMappedProperties(lead.rawData, allFieldMappingConfigs.lead) : undefined, }; } - async getOpportunity(id: string, fieldMappingConfig: FieldMappingConfig, params: CrmGetParams): Promise { + async getOpportunity( + id: string, + allFieldMappingConfigs: AllCrmFieldMappingConfigs, + params: CrmGetParams + ): Promise { await this.maybeRefreshAccessToken(); const fields = await this.#getCustomFieldsForObject('deal'); const response = await axios.get(`${this.#credentials.instanceUrl}/api/v1/deals/${id}`, { @@ -683,11 +691,17 @@ class PipedriveClient extends AbstractCrmRemoteClient { const opportunity = fromPipedriveDealToOpportunity(response.data.data, pipelineStageMapping, fields); return { ...opportunity, - rawData: params.includeRawData ? toMappedProperties(opportunity.rawData, fieldMappingConfig) : undefined, + rawData: params.includeRawData + ? toMappedProperties(opportunity.rawData, allFieldMappingConfigs.opportunity) + : undefined, }; } - async getAccount(id: string, fieldMappingConfig: FieldMappingConfig, params: CrmGetParams): Promise { + async getAccount( + id: string, + allFieldMappingConfigs: AllCrmFieldMappingConfigs, + params: CrmGetParams + ): Promise { await this.maybeRefreshAccessToken(); const fields = await this.#getCustomFieldsForObject('organization'); const response = await axios.get(`${this.#credentials.instanceUrl}/api/v1/organizations/${id}`, { @@ -696,11 +710,11 @@ class PipedriveClient extends AbstractCrmRemoteClient { const account = fromPipedriveOrganizationToAccount(response.data.data, fields); return { ...account, - rawData: params.includeRawData ? toMappedProperties(account.rawData, fieldMappingConfig) : undefined, + rawData: params.includeRawData ? toMappedProperties(account.rawData, allFieldMappingConfigs.account) : undefined, }; } - async getUser(id: string, fieldMappingConfig: FieldMappingConfig, params: CrmGetParams): Promise { + async getUser(id: string, allFieldMappingConfigs: AllCrmFieldMappingConfigs, params: CrmGetParams): Promise { await this.maybeRefreshAccessToken(); const response = await axios.get(`${this.#credentials.instanceUrl}/api/v1/users/${id}`, { headers: this.#headers, @@ -708,7 +722,7 @@ class PipedriveClient extends AbstractCrmRemoteClient { const user = fromPipedriveUserToUser(response.data.data); return { ...user, - rawData: params.includeRawData ? toMappedProperties(user.rawData, fieldMappingConfig) : undefined, + rawData: params.includeRawData ? toMappedProperties(user.rawData, allFieldMappingConfigs.user) : undefined, }; } diff --git a/packages/core/remotes/impl/salesforce/index.ts b/packages/core/remotes/impl/salesforce/index.ts index 89afaa257..c172e2ecc 100644 --- a/packages/core/remotes/impl/salesforce/index.ts +++ b/packages/core/remotes/impl/salesforce/index.ts @@ -55,7 +55,7 @@ import type { SimpleCustomObjectSchemaDeprecated, } from '@supaglue/types/custom_object'; import type { FieldsToFetch } from '@supaglue/types/fields_to_fetch'; -import type { FieldMappingConfig } from '@supaglue/types/field_mapping_config'; +import type { AllCrmFieldMappingConfigs, FieldMappingConfig } from '@supaglue/types/field_mapping_config'; import type { StandardOrCustomObject } from '@supaglue/types/standard_or_custom_object'; import { SALESFORCE_OBJECTS } from '@supaglue/utils'; import retry from 'async-retry'; @@ -473,7 +473,7 @@ ${modifiedAfter ? `WHERE SystemModstamp > ${modifiedAfter.toISOString()} ORDER B public override async listCommonObjectRecords( commonObjectType: T, - fieldMappingConfig: FieldMappingConfig, + allFieldMappingConfigs: AllCrmFieldMappingConfigs, params: CRMCommonObjectTypeMap['listParams'] ): Promise['object']>> { // TODO: Implement expand for lists @@ -481,6 +481,7 @@ ${modifiedAfter ? `WHERE SystemModstamp > ${modifiedAfter.toISOString()} ORDER B throw new BadRequestError('Expand is not yet supported for list operations'); } const sobject = capitalizeString(commonObjectType); + const fieldMappingConfig = allFieldMappingConfigs[commonObjectType]; const propertiesToFetch = await this.getCommonPropertiesToFetch(commonObjectType, fieldMappingConfig); const limit = params.pageSize ?? DEFAULT_PAGE_SIZE; const offset = (decodeCursor(params.cursor)?.id as number | undefined) ?? 0; @@ -514,20 +515,20 @@ ${modifiedAfter ? `WHERE SystemModstamp > ${modifiedAfter.toISOString()} ORDER B public override async getCommonObjectRecord( commonObjectType: T, id: string, - fieldMappingConfig: FieldMappingConfig, + allFieldMappingConfigs: AllCrmFieldMappingConfigs, params: CRMCommonObjectTypeMap['getParams'] ): Promise['object']> { switch (commonObjectType) { case 'account': - return this.getAccount(id, fieldMappingConfig, params); + return this.getAccount(id, allFieldMappingConfigs, params); case 'contact': - return this.getContact(id, fieldMappingConfig, params); + return this.getContact(id, allFieldMappingConfigs, params); case 'lead': - return this.getLead(id, fieldMappingConfig, params); + return this.getLead(id, allFieldMappingConfigs, params); case 'opportunity': - return this.getOpportunity(id, fieldMappingConfig, params); + return this.getOpportunity(id, allFieldMappingConfigs, params); case 'user': - return this.getUser(id, fieldMappingConfig, params); + return this.getUser(id, allFieldMappingConfigs, params); default: throw new Error(`Unsupported common object type: ${commonObjectType}`); } @@ -1370,16 +1371,20 @@ ${modifiedAfter ? `WHERE SystemModstamp > ${modifiedAfter.toISOString()} ORDER B })); } - public async getAccount(id: string, fieldMappingConfig: FieldMappingConfig, params: CrmGetParams): Promise { + public async getAccount( + id: string, + allFieldMappingConfigs: AllCrmFieldMappingConfigs, + params: CrmGetParams + ): Promise { const account = await this.#client.retrieve('Account', id); if (account.ownerId && (params.expand?.includes('user') || params.expand?.includes('owner'))) { - account.owner = await this.getUser(account.ownerId, fieldMappingConfig, { + account.owner = await this.getUser(account.ownerId, allFieldMappingConfigs, { includeRawData: params.includeRawData, }); } return { ...fromSalesforceAccountToAccount(account), - rawData: params.includeRawData ? toMappedProperties(account, fieldMappingConfig) : undefined, + rawData: params.includeRawData ? toMappedProperties(account, allFieldMappingConfigs.account) : undefined, }; } @@ -1416,22 +1421,26 @@ ${modifiedAfter ? `WHERE SystemModstamp > ${modifiedAfter.toISOString()} ORDER B return response.id; } - public async getContact(id: string, fieldMappingConfig: FieldMappingConfig, params: CrmGetParams): Promise { + public async getContact( + id: string, + allFieldMappingConfigs: AllCrmFieldMappingConfigs, + params: CrmGetParams + ): Promise { const sfContact = await this.#client.retrieve('Contact', id); const contact = fromSalesforceContactToContact(sfContact); if (contact.accountId && params.expand?.includes('account')) { - contact.account = await this.getAccount(contact.accountId, fieldMappingConfig, { + contact.account = await this.getAccount(contact.accountId, allFieldMappingConfigs, { includeRawData: params.includeRawData, }); } if (contact.ownerId && (params.expand?.includes('user') || params.expand?.includes('owner'))) { - contact.owner = await this.getUser(contact.ownerId, fieldMappingConfig, { + contact.owner = await this.getUser(contact.ownerId, allFieldMappingConfigs, { includeRawData: params.includeRawData, }); } return { ...contact, - rawData: params.includeRawData ? toMappedProperties(sfContact, fieldMappingConfig) : undefined, + rawData: params.includeRawData ? toMappedProperties(sfContact, allFieldMappingConfigs.contact) : undefined, }; } @@ -1493,25 +1502,25 @@ ${modifiedAfter ? `WHERE SystemModstamp > ${modifiedAfter.toISOString()} ORDER B public async getOpportunity( id: string, - fieldMappingConfig: FieldMappingConfig, + allFieldMappingConfigs: AllCrmFieldMappingConfigs, params: CrmGetParams ): Promise { const sfOpportunity = await this.#client.retrieve('Opportunity', id); const opportunity = fromSalesforceOpportunityToOpportunity(sfOpportunity); if (opportunity.accountId && params.expand?.includes('account')) { - opportunity.account = await this.getAccount(opportunity.accountId, fieldMappingConfig, { + opportunity.account = await this.getAccount(opportunity.accountId, allFieldMappingConfigs, { includeRawData: params.includeRawData, }); } if (opportunity.ownerId && (params.expand?.includes('user') || params.expand?.includes('owner'))) { - opportunity.owner = await this.getUser(opportunity.ownerId, fieldMappingConfig, { + opportunity.owner = await this.getUser(opportunity.ownerId, allFieldMappingConfigs, { includeRawData: params.includeRawData, }); } return { ...opportunity, - rawData: params.includeRawData ? toMappedProperties(opportunity, fieldMappingConfig) : undefined, + rawData: params.includeRawData ? toMappedProperties(opportunity, allFieldMappingConfigs.opportunity) : undefined, }; } @@ -1531,28 +1540,32 @@ ${modifiedAfter ? `WHERE SystemModstamp > ${modifiedAfter.toISOString()} ORDER B return response.id; } - public async getLead(id: string, fieldMappingConfig: FieldMappingConfig, params: CrmGetParams): Promise { + public async getLead( + id: string, + allFieldMappingConfigs: AllCrmFieldMappingConfigs, + params: CrmGetParams + ): Promise { const sfLead = await this.#client.retrieve('Lead', id); const lead = fromSalesforceLeadToLead(sfLead); if (lead.convertedAccountId && params.expand?.includes('account')) { - lead.convertedAccount = await this.getAccount(lead.convertedAccountId, fieldMappingConfig, { + lead.convertedAccount = await this.getAccount(lead.convertedAccountId, allFieldMappingConfigs, { includeRawData: params.includeRawData, }); } if (lead.convertedContactId && params.expand?.includes('contact')) { - lead.convertedContact = await this.getContact(lead.convertedContactId, fieldMappingConfig, { + lead.convertedContact = await this.getContact(lead.convertedContactId, allFieldMappingConfigs, { includeRawData: params.includeRawData, }); } if (lead.ownerId && (params.expand?.includes('user') || params.expand?.includes('owner'))) { - lead.owner = await this.getUser(lead.ownerId, fieldMappingConfig, { + lead.owner = await this.getUser(lead.ownerId, allFieldMappingConfigs, { includeRawData: params.includeRawData, }); } return { ...fromSalesforceLeadToLead(lead), - rawData: params.includeRawData ? toMappedProperties(lead, fieldMappingConfig) : undefined, + rawData: params.includeRawData ? toMappedProperties(lead, allFieldMappingConfigs.lead) : undefined, }; } @@ -1612,11 +1625,15 @@ ${modifiedAfter ? `WHERE SystemModstamp > ${modifiedAfter.toISOString()} ORDER B return response.id; } - public async getUser(id: string, fieldMappingConfig: FieldMappingConfig, params: CrmGetParams): Promise { + public async getUser( + id: string, + allFieldMappingConfigs: AllCrmFieldMappingConfigs, + params: CrmGetParams + ): Promise { const user = await this.#client.retrieve('User', id); return { ...fromSalesforceUserToUser(user), - rawData: params.includeRawData ? toMappedProperties(user, fieldMappingConfig) : undefined, + rawData: params.includeRawData ? toMappedProperties(user, allFieldMappingConfigs.user) : undefined, }; } diff --git a/packages/core/services/common_objects/crm/common_object_service.ts b/packages/core/services/common_objects/crm/common_object_service.ts index fd965e349..a123b7f68 100644 --- a/packages/core/services/common_objects/crm/common_object_service.ts +++ b/packages/core/services/common_objects/crm/common_object_service.ts @@ -41,10 +41,10 @@ export class CrmCommonObjectService { params: CRMCommonObjectTypeMap['getParams'] = {} ): Promise['object']> { const [remoteClient, providerName] = await this.#remoteService.getCrmRemoteClient(connection.id); - const fieldMappingConfig = await this.#connectionService.getFieldMappingConfig(connection.id, 'common', objectName); + const allFieldMappings = await this.#connectionService.getAllCrmCommonFieldMappingConfigs(connection.id); const end = remoteDuration.startTimer({ operation: 'get', remote_name: providerName }); - const obj = await remoteClient.getCommonObjectRecord(objectName, id, fieldMappingConfig, params); + const obj = await remoteClient.getCommonObjectRecord(objectName, id, allFieldMappings, params); end(); return obj; @@ -56,10 +56,10 @@ export class CrmCommonObjectService { params: CRMCommonObjectTypeMap['listParams'] ): Promise['object']>> { const [remoteClient, providerName] = await this.#remoteService.getCrmRemoteClient(connection.id); - const fieldMappingConfig = await this.#connectionService.getFieldMappingConfig(connection.id, 'common', objectName); + const allFieldMappings = await this.#connectionService.getAllCrmCommonFieldMappingConfigs(connection.id); const end = remoteDuration.startTimer({ operation: 'get', remote_name: providerName }); - const records = await remoteClient.listCommonObjectRecords(objectName, fieldMappingConfig, params); + const records = await remoteClient.listCommonObjectRecords(objectName, allFieldMappings, params); end(); return records; diff --git a/packages/core/services/connection_service.ts b/packages/core/services/connection_service.ts index 5a6b6f5ab..9f90e83d5 100644 --- a/packages/core/services/connection_service.ts +++ b/packages/core/services/connection_service.ts @@ -9,6 +9,7 @@ import type { OauthConnectionCredentialsDecrypted, ObjectFieldMappingInfo, ObjectFieldMappingUpdateParams, + Provider, ProviderName, ProviderObject, Schema, @@ -17,7 +18,7 @@ import type { } from '@supaglue/types'; import type { CRMProviderName } from '@supaglue/types/crm'; import type { ConnectionEntityMapping, MergedEntityMapping } from '@supaglue/types/entity_mapping'; -import type { FieldMappingConfig } from '@supaglue/types/field_mapping_config'; +import type { AllCrmFieldMappingConfigs, FieldMappingConfig } from '@supaglue/types/field_mapping_config'; import type { StandardOrCustomObject } from '@supaglue/types/standard_or_custom_object'; import retry from 'async-retry'; import type { ProviderService, SchemaService, WebhookService } from '.'; @@ -644,6 +645,15 @@ export class ConnectionService { ): Promise { const connection = await this.getSafeById(connectionId); const provider = await this.#providerService.getById(connection.providerId); + return await this.#getFieldMappingConfigImpl(connection, provider, objectType, objectName); + } + + async #getFieldMappingConfigImpl( + connection: ConnectionSafeAny, + provider: Provider, + objectType: 'common' | 'standard', + objectName: string + ): Promise { const schemaId = (provider.objects?.[objectType] as ProviderObject[] | undefined)?.find( (o) => o.name === objectName )?.schemaId; @@ -658,6 +668,18 @@ export class ConnectionService { } return createFieldMappingConfig(schema?.config, customerFieldMapping); } + + public async getAllCrmCommonFieldMappingConfigs(connectionId: string): Promise { + const connection = await this.getSafeById(connectionId); + const provider = await this.#providerService.getById(connection.providerId); + return { + account: await this.#getFieldMappingConfigImpl(connection, provider, 'common', 'account'), + contact: await this.#getFieldMappingConfigImpl(connection, provider, 'common', 'contact'), + lead: await this.#getFieldMappingConfigImpl(connection, provider, 'common', 'lead'), + opportunity: await this.#getFieldMappingConfigImpl(connection, provider, 'common', 'opportunity'), + user: await this.#getFieldMappingConfigImpl(connection, provider, 'common', 'user'), + }; + } } export const getFieldMappingInfo = ( diff --git a/packages/types/field_mapping_config.ts b/packages/types/field_mapping_config.ts index 11e8ccc57..00099477f 100644 --- a/packages/types/field_mapping_config.ts +++ b/packages/types/field_mapping_config.ts @@ -1,3 +1,5 @@ +import type { CRMCommonObjectType } from './crm/index'; + export type FieldMapping = { schemaField: string; mappedField: string; @@ -14,3 +16,5 @@ export type DefinedFieldMappingConfig = { }; export type FieldMappingConfig = InheritedFieldMappingConfig | DefinedFieldMappingConfig; + +export type AllCrmFieldMappingConfigs = Record;