Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved synchronization service for all entities. #336

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/api/src/crm/company/services/attio/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ export class AttioCompanyMapper implements ICompanyMapper {

let opts: any = {};


//TODO - Logic needs to be reconsider
Copy link
Contributor

Choose a reason for hiding this comment

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

The TODO comment indicates a need for reconsidering logic related to company.values.team[0]?.target_record_id.

Would you like assistance in addressing this TODO? It's important to ensure that the logic aligns with the intended functionality and data integrity requirements.

if (company.values.team[0]?.target_record_id) {
const owner_id = await this.utils.getUserUuidFromRemoteId(
company.values.team[0].target_record_id,
Expand All @@ -139,6 +141,7 @@ export class AttioCompanyMapper implements ICompanyMapper {
}

return {
remote_id: company.id?.record_id,
name: company.values.name[0]?.value,
industry: typeof company.values.categories[0]?.option === "string" ? company.values.categories[0]?.option : company.values.categories[0]?.option.title,
number_of_employees: 0, // Placeholder, as there's no direct mapping provided
Expand Down
2 changes: 2 additions & 0 deletions packages/api/src/crm/company/services/hubspot/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export class HubspotCompanyMapper implements ICompanyMapper {
})) || [];

let opts: any = {};
//TODO - Logic needs to be reconsider
Copy link
Contributor

Choose a reason for hiding this comment

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

The TODO comment about reconsidering logic indicates an area that may need further attention or refinement. It's important to ensure that any logic related to owner ID mapping is correctly implemented and does not introduce inconsistencies.

Would you like me to help refine this logic or open a GitHub issue to track this task?

if (company.properties.hubspot_owner_id) {
const owner_id = await this.utils.getUserUuidFromRemoteId(
company.properties.hubspot_owner_id,
Expand All @@ -108,6 +109,7 @@ export class HubspotCompanyMapper implements ICompanyMapper {
}

return {
remote_id: company.id,
name: company.properties.name,
industry: company.properties.industry,
number_of_employees: 0, // Placeholder, as there's no direct mapping provided
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/crm/company/services/pipedrive/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export class PipedriveCompanyMapper implements ICompanyMapper {
})) || [];

let res = {
remote_id: company.id,
name: company.name,
industry: '', // Pipedrive may not directly provide this, need custom mapping
number_of_employees: 0, // Placeholder, as there's no direct mapping provided
Expand Down
4 changes: 4 additions & 0 deletions packages/api/src/crm/company/services/zendesk/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import {
import { ICompanyMapper } from '@crm/company/types';
import { Utils } from '@crm/contact/utils';


//TODO - Zendesk does not have Company object

export class ZendeskCompanyMapper implements ICompanyMapper {
private readonly utils: Utils;

Expand Down Expand Up @@ -134,6 +137,7 @@ export class ZendeskCompanyMapper implements ICompanyMapper {
}

return {
remote_id: company.id,
name: company.name,
email_addresses,
phone_numbers,
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/crm/company/services/zoho/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export class ZohoCompanyMapper implements ICompanyMapper {
})) || [];

return {
remote_id: company.id,
name: company.Account_Name,
phone_numbers: [
{
Expand Down
9 changes: 2 additions & 7 deletions packages/api/src/crm/company/sync/sync.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,12 @@ export class SyncService implements OnModuleInit {
customFieldMappings,
})) as UnifiedCompanyOutput[];

//TODO
const companyIds = sourceObject.map((company) =>
'id' in company ? String(company.id) : undefined,
);


//insert the data in the DB with the fieldMappings (value table)
const companies_data = await this.saveCompanysInDb(
linkedUserId,
unifiedObject,
companyIds,
integrationId,
sourceObject,
);
Expand Down Expand Up @@ -178,15 +174,14 @@ export class SyncService implements OnModuleInit {
async saveCompanysInDb(
linkedUserId: string,
companies: UnifiedCompanyOutput[],
originIds: string[],
originSource: string,
remote_data: Record<string, any>[],
): Promise<CrmCompany[]> {
try {
let companies_results: CrmCompany[] = [];
for (let i = 0; i < companies.length; i++) {
const company = companies[i];
const originId = originIds[i];
const originId = company.remote_id;

if (!originId || originId == '') {
throw new NotFoundError(`Origin id not there, found ${originId}`);
Expand Down
3 changes: 3 additions & 0 deletions packages/api/src/crm/contact/services/attio/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,10 @@ export class AttioContactMapper implements IContactMapper {
};
const opts: any = {};

console.log(JSON.stringify(contact.id))

return {
remote_id: contact.id?.record_id,
first_name: contact.values.name[0]?.first_name,
last_name: contact.values.name[0]?.last_name,
// user_id: contact.values.created_by[0]?.referenced_actor_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export class FreshsalesContactMapper implements IContactMapper {
};

return {
remote_id: contact.id + "",
first_name: contact.first_name,
last_name: contact.last_name,
email_addresses,
Expand Down
2 changes: 2 additions & 0 deletions packages/api/src/crm/contact/services/hubspot/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ export class HubspotContactMapper implements IContactMapper {
};*/

return {
// Setting remote_id field
remote_id: contact.id,
first_name: contact.properties.firstname,
last_name: contact.properties.lastname,
email_addresses: [
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/crm/contact/services/pipedrive/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ export class PipedriveContactMapper implements IContactMapper {
}

return {
remote_id: contact.id,
first_name: contact.first_name,
last_name: contact.last_name,
email_addresses: contact.email.map((e) => ({
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/crm/contact/services/zendesk/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export class ZendeskContactMapper implements IContactMapper {
};

return {
remote_id: contact.id + "",
first_name: contact.first_name,
last_name: contact.last_name,
email_addresses,
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/crm/contact/services/zoho/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export class ZohoContactMapper implements IContactMapper {
};

return {
remote_id: contact.id,
first_name: contact.First_Name ? contact.First_Name : '',
last_name: contact.Last_Name ? contact.Last_Name : '',
email_addresses,
Expand Down
25 changes: 14 additions & 11 deletions packages/api/src/crm/contact/sync/sync.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,20 +138,23 @@ export class SyncContactsService implements OnModuleInit {
customFieldMappings,
})) as UnifiedContactOutput[];

//TODO
const contactIds = sourceObject.map((contact) =>
'id' in contact
? String(contact.id)
: 'contact_id' in contact
? String(contact.contact_id)
: undefined,
);

// Removing contactsIds

// //TODO
// const contactIds = sourceObject.map((contact) =>
// 'id' in contact
// ? String(contact.id)
// : 'contact_id' in contact
// ? String(contact.contact_id)
// : undefined,
// );

//insert the data in the DB with the fieldMappings (value table)
const contacts_data = await this.saveContactsInDb(
linkedUserId,
unifiedObject,
contactIds,
// contactIds,
integrationId,
sourceObject,
);
Expand Down Expand Up @@ -182,15 +185,15 @@ export class SyncContactsService implements OnModuleInit {
async saveContactsInDb(
linkedUserId: string,
contacts: UnifiedContactOutput[],
originIds: string[],
// originIds: string[],
originSource: string,
remote_data: Record<string, any>[],
): Promise<CrmContact[]> {
try {
let contacts_results: CrmContact[] = [];
for (let i = 0; i < contacts.length; i++) {
const contact = contacts[i];
const originId = originIds[i];
const originId = contact.remote_id;

if (!originId || originId == '') {
throw new NotFoundError(`Origin id not there, found ${originId}`);
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/crm/deal/services/hubspot/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export class HubspotDealMapper implements IDealMapper {
}

return {
remote_id: deal.id,
name: deal.properties.dealname,
description: deal.properties.dealname, // Placeholder if there's no direct mapping
amount: parseFloat(deal.properties.amount),
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/crm/deal/services/pipedrive/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export class PipedriveDealMapper implements IDealMapper {
}

return {
remote_id: deal.id,
name: deal.title,
amount: deal.value,
description: '',
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/crm/deal/services/zendesk/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export class ZendeskDealMapper implements IDealMapper {
}

return {
remote_id: deal.id,
name: deal.name,
amount:
typeof deal.value === 'string' ? parseFloat(deal.value) : deal.value,
Expand Down
2 changes: 2 additions & 0 deletions packages/api/src/crm/deal/services/zoho/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ export class ZohoDealMapper implements IDealMapper {
})) || [];

return {
//TODO - might be wrong deal.Owner
remote_id: deal.Owner,
name: deal.Title,
description: deal.Description,
amount: 0, //todo;
Expand Down
9 changes: 2 additions & 7 deletions packages/api/src/crm/deal/sync/sync.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,12 @@ export class SyncService implements OnModuleInit {
customFieldMappings,
})) as UnifiedDealOutput[];

//TODO
const dealIds = sourceObject.map((deal) =>
'id' in deal ? String(deal.id) : undefined,
);


//insert the data in the DB with the fieldMappings (value table)
const deals_data = await this.saveDealsInDb(
linkedUserId,
unifiedObject,
dealIds,
integrationId,
sourceObject,
);
Expand Down Expand Up @@ -175,15 +171,14 @@ export class SyncService implements OnModuleInit {
async saveDealsInDb(
linkedUserId: string,
deals: UnifiedDealOutput[],
originIds: string[],
originSource: string,
remote_data: Record<string, any>[],
): Promise<CrmDeal[]> {
try {
let deals_results: CrmDeal[] = [];
for (let i = 0; i < deals.length; i++) {
const deal = deals[i];
const originId = originIds[i];
const originId = deal.remote_id;

if (!originId || originId == '') {
throw new NotFoundError(`Origin id not there, found ${originId}`);
Expand Down
17 changes: 9 additions & 8 deletions packages/api/src/crm/engagement/services/hubspot/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ export class HubspotEngagementMapper implements IEngagementMapper {
source.direction === 'INBOUND'
? 'INCOMING_EMAIL'
: source.direction === 'OUTBOUND'
? 'FORWARDED_EMAIL'
: '',
? 'FORWARDED_EMAIL'
: '',
Comment on lines +146 to +147
Copy link
Contributor

Choose a reason for hiding this comment

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

The ternary operator for hs_email_direction has been updated to map source.direction to 'FORWARDED_EMAIL' for 'OUTBOUND' emails. However, this seems to be a logical error since outbound emails should not be mapped to 'FORWARDED_EMAIL'. Instead, they should likely be mapped to a different HubSpot-specific value that accurately represents outbound emails.

-            ? 'FORWARDED_EMAIL'
+            ? 'OUTBOUND_EMAIL' // Assuming 'OUTBOUND_EMAIL' is the correct mapping, please replace with the correct HubSpot value.

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
? 'FORWARDED_EMAIL'
: '',
? 'OUTBOUND_EMAIL' // Assuming 'OUTBOUND_EMAIL' is the correct mapping, please replace with the correct HubSpot value.
: '',

hs_email_to_lastname: '', // Placeholder, needs appropriate mapping
hs_email_sender_email: '', // Placeholder, needs appropriate mapping
hs_email_to_firstname: '', // Placeholder, needs appropriate mapping
Expand Down Expand Up @@ -191,15 +191,15 @@ export class HubspotEngagementMapper implements IEngagementMapper {
case 'MEETING':
return await this.unifyMeeting(
source as
| HubspotEngagementMeetingOutput
| HubspotEngagementMeetingOutput[],
| HubspotEngagementMeetingOutput
| HubspotEngagementMeetingOutput[],
customFieldMappings,
);
case 'EMAIL':
return await this.unifyEmail(
source as
| HubspotEngagementEmailOutput
| HubspotEngagementEmailOutput[],
| HubspotEngagementEmailOutput
| HubspotEngagementEmailOutput[],
customFieldMappings,
);
default:
Expand Down Expand Up @@ -296,6 +296,7 @@ export class HubspotEngagementMapper implements IEngagementMapper {
}

return {
remote_id: engagement.id,
content: engagement.properties.hs_call_body,
subject: engagement.properties.hs_call_title,
start_at: new Date(engagement.properties.createdate),
Expand Down Expand Up @@ -378,8 +379,8 @@ export class HubspotEngagementMapper implements IEngagementMapper {
engagement.properties.hs_email_direction === 'INCOMING_EMAIL'
? 'INBOUND'
: engagement.properties.hs_email_direction === 'FORWARDED_EMAIL'
? 'OUTBOUND'
: '',
? 'OUTBOUND'
: '',
field_mappings,
...opts,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ export class PipedriveEngagementMapper implements IEngagementMapper {
}

return {
remote_id: engagement.id,
content: engagement.note,
subject: engagement.subject,
start_at: new Date(engagement.due_date + ' ' + engagement.due_time), // Combine due_date and due_time
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export class ZendeskEngagementMapper implements IEngagementMapper {
const direction = engagement.incoming ? 'incoming' : 'outgoing';

return {
remote_id: engagement.id,
content: engagement.summary,
subject: '',
start_at: new Date(engagement.made_at),
Expand Down
9 changes: 2 additions & 7 deletions packages/api/src/crm/engagement/sync/sync.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,12 @@ export class SyncService implements OnModuleInit {
customFieldMappings,
})) as UnifiedEngagementOutput[];

//TODO
const engagementIds = sourceObject.map((engagement) =>
'id' in engagement ? String(engagement.id) : undefined,
);


//insert the data in the DB with the fieldMappings (value table)
const engagements_data = await this.saveEngagementsInDb(
linkedUserId,
unifiedObject,
engagementIds,
integrationId,
sourceObject,
);
Expand Down Expand Up @@ -183,15 +179,14 @@ export class SyncService implements OnModuleInit {
async saveEngagementsInDb(
linkedUserId: string,
engagements: UnifiedEngagementOutput[],
originIds: string[],
originSource: string,
remote_data: Record<string, any>[],
): Promise<CrmEngagement[]> {
try {
let engagements_results: CrmEngagement[] = [];
for (let i = 0; i < engagements.length; i++) {
const engagement = engagements[i];
const originId = originIds[i];
const originId = engagement.remote_id;

if (!originId || originId == '') {
throw new NotFoundError(`Origin id not there, found ${originId}`);
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/crm/note/services/hubspot/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export class HubspotNoteMapper implements INoteMapper {
}

return {
remote_id: note.id,
content: note.properties.hs_note_body,
field_mappings,
...opts,
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/crm/note/services/pipedrive/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export class PipedriveNoteMapper implements INoteMapper {
}

return {
remote_id: note.id,
content: note.content,
field_mappings,
...opts,
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/crm/note/services/zendesk/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export class ZendeskNoteMapper implements INoteMapper {
}

return {
remote_id: note.id,
content: note.content,
field_mappings,
...opts,
Expand Down
Loading
Loading