Skip to content

Commit

Permalink
🐛 Fix remote_id
Browse files Browse the repository at this point in the history
  • Loading branch information
mit-27 committed May 3, 2024
1 parent 519e970 commit cdd43a4
Show file tree
Hide file tree
Showing 75 changed files with 230 additions and 270 deletions.
1 change: 1 addition & 0 deletions packages/api/src/crm/company/services/attio/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,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'
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/crm/company/services/hubspot/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,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
7 changes: 5 additions & 2 deletions packages/api/src/crm/company/services/pipedrive/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ export class PipedriveCompanyMapper implements ICompanyMapper {
postal_code: company.address_postal_code,
};
}
res = { ...res, ...opts };
return res;
return {
remote_id: company.id,
...res,
...opts,
};
}
}
1 change: 1 addition & 0 deletions packages/api/src/crm/company/services/zendesk/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ export class ZendeskCompanyMapper implements ICompanyMapper {
}

return {
remote_id: String(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 @@ -89,6 +89,7 @@ export class ZohoCompanyMapper implements ICompanyMapper {
}

return {
remote_id: company.id,
name: company.Account_Name,
phone_numbers: [
{
Expand Down
23 changes: 9 additions & 14 deletions packages/api/src/crm/company/sync/sync.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class SyncService implements OnModuleInit {

//function used by sync worker which populate our crm_companies table
//its role is to fetch all companies from providers 3rd parties and save the info inside our db
//@Cron('*/2 * * * *') // every 2 minutes (for testing)
// @Cron('*/2 * * * *') // every 2 minutes (for testing)
@Cron('0 */8 * * *') // every 8 hours
async syncCompanies(user_id?: string) {
try {
Expand All @@ -83,12 +83,12 @@ export class SyncService implements OnModuleInit {
*/
const users = user_id
? [
await this.prisma.users.findUnique({
where: {
id_user: user_id,
},
}),
]
await this.prisma.users.findUnique({
where: {
id_user: user_id,
},
}),
]
: await this.prisma.users.findMany();
if (users && users.length > 0) {
for (const user of users) {
Expand Down Expand Up @@ -183,16 +183,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 @@ -223,15 +219,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
1 change: 1 addition & 0 deletions packages/api/src/crm/contact/services/attio/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export class AttioContactMapper implements IContactMapper {
const opts: any = {};

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
1 change: 1 addition & 0 deletions packages/api/src/crm/contact/services/hubspot/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export class HubspotContactMapper implements IContactMapper {
};*/

return {
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 @@ -119,6 +119,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 @@ -138,6 +138,7 @@ export class ZendeskContactMapper implements IContactMapper {
};

return {
remote_id: String(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/zoho/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ export class ZohoContactMapper implements IContactMapper {
country: contact.Mailing_Country,
};

// TODO - Set remote_id with correct attribute
return {
remote_id: '',
first_name: contact.First_Name ? contact.First_Name : '',
last_name: contact.Last_Name ? contact.Last_Name : '',
email_addresses,
Expand Down
27 changes: 8 additions & 19 deletions packages/api/src/crm/contact/sync/sync.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,20 @@ export class SyncService implements OnModuleInit {

//function used by sync worker which populate our crm_contacts table
//its role is to fetch all contacts from providers 3rd parties and save the info inside our db
//@Cron('*/2 * * * *') // every 2 minutes (for testing)
// @Cron('*/2 * * * *') // every 2 minutes (for testing)
@Cron('0 */8 * * *') // every 8 hours
async syncContacts(user_id?: string) {
try {
this.logger.log(`Syncing contacts....`);

const users = user_id
? [
await this.prisma.users.findUnique({
where: {
id_user: user_id,
},
}),
]
await this.prisma.users.findUnique({
where: {
id_user: user_id,
},
}),
]
: await this.prisma.users.findMany();
if (users && users.length > 0) {
for (const user of users) {
Expand Down Expand Up @@ -185,20 +185,10 @@ export class SyncService 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,
);

//insert the data in the DB with the fieldMappings (value table)
const contacts_data = await this.saveContactsInDb(
linkedUserId,
unifiedObject,
contactIds,
integrationId,
sourceObject,
);
Expand Down Expand Up @@ -229,15 +219,14 @@ export class SyncService implements OnModuleInit {
async saveContactsInDb(
linkedUserId: string,
contacts: UnifiedContactOutput[],
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 @@ -102,6 +102,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 @@ -112,6 +112,7 @@ export class PipedriveDealMapper implements IDealMapper {
}

return {
remote_id: String(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 @@ -129,6 +129,7 @@ export class ZendeskDealMapper implements IDealMapper {
}

return {
remote_id: String(deal.id),
name: deal.name,
amount:
typeof deal.value === 'string' ? parseFloat(deal.value) : deal.value,
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/crm/deal/services/zoho/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export class ZohoDealMapper implements IDealMapper {
}
}
const res: UnifiedDealOutput = {
remote_id: deal.id,
name: deal.Deal_Name,
description: deal.Description,
amount: deal.Amount,
Expand Down
21 changes: 7 additions & 14 deletions packages/api/src/crm/deal/sync/sync.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ export class SyncService implements OnModuleInit {
this.logger.log(`Syncing deals....`);
const users = user_id
? [
await this.prisma.users.findUnique({
where: {
id_user: user_id,
},
}),
]
await this.prisma.users.findUnique({
where: {
id_user: user_id,
},
}),
]
: await this.prisma.users.findMany();
if (users && users.length > 0) {
for (const user of users) {
Expand Down Expand Up @@ -169,16 +169,10 @@ 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 @@ -209,15 +203,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 @@ -145,8 +145,8 @@ export class HubspotEngagementMapper implements IEngagementMapper {
source.direction === 'INBOUND'
? 'INCOMING_EMAIL'
: source.direction === 'OUTBOUND'
? 'FORWARDED_EMAIL'
: '',
? 'FORWARDED_EMAIL'
: '',
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 @@ -194,15 +194,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 @@ -378,6 +378,7 @@ export class HubspotEngagementMapper implements IEngagementMapper {
}

return {
remote_id: engagement.id,
content: engagement.properties.hs_email_text,
subject: engagement.properties.hs_email_subject,
start_at: new Date(engagement.properties.createdate),
Expand All @@ -387,8 +388,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 @@ -217,6 +217,7 @@ export class PipedriveEngagementMapper implements IEngagementMapper {
}

return {
remote_id: String(engagement.id),
content: engagement.note, // Assuming email content is stored in 'note'
subject: engagement.subject,
start_at: new Date(engagement.add_time), // Using 'add_time' as the start time
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export class ZendeskEngagementMapper implements IEngagementMapper {
const direction = engagement.incoming ? 'incoming' : 'outgoing';

return {
remote_id: String(engagement.id),
content: engagement.summary,
subject: '',
start_at: new Date(engagement.made_at),
Expand Down
Loading

0 comments on commit cdd43a4

Please sign in to comment.