Skip to content

Commit

Permalink
✅ Health Test for company, contact and deals
Browse files Browse the repository at this point in the history
  • Loading branch information
naelob committed Jul 16, 2024
1 parent f16b759 commit a014a4e
Show file tree
Hide file tree
Showing 23 changed files with 214 additions and 142 deletions.
1 change: 1 addition & 0 deletions packages/api/src/crm/@lib/@types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ export class Email {
})
@IsString()
@IsOptional()
@IsIn(['COMPANY', 'CONTACT'])
owner_type?: string;
}

Expand Down
15 changes: 15 additions & 0 deletions packages/api/src/crm/@lib/@utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,21 @@ export class Utils {
}
}

async getEmailFromUserUuid(uuid: string) {
try {
const res = await this.prisma.crm_users.findFirst({
where: {
id_crm_user: uuid,
},
});
// if (!res) throw new Error(`crm_user not found for uuid ${uuid}`);
if (!res) return;
return res.email;
} catch (error) {
throw error;
}
}

async getUser(uuid: string) {
try {
const res = await this.prisma.crm_users.findFirst({
Expand Down
28 changes: 11 additions & 17 deletions packages/api/src/crm/company/services/attio/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ICompanyMapper } from '@crm/company/types';
import { Utils } from '@crm/@lib/@utils';
import { Injectable } from '@nestjs/common';
import { MappersRegistry } from '@@core/@core-services/registries/mappers.registry';
import { getCountryCode, getCountryName } from '@@core/utils/types';

@Injectable()
export class AttioCompanyMapper implements ICompanyMapper {
Expand All @@ -29,31 +30,29 @@ export class AttioCompanyMapper implements ICompanyMapper {
],
},
};
if (source.industry) {
/*todo if (source.industry) {
result.values.categories = [
{
option: source.industry,
},
];
}
}*/

if (source.addresses) {
const address = source.addresses[0];
if (address) {
// result.city = address.city;
// result.state = address.state;
result.values.primary_location = [
{
locality: address.city,
line_1: address.street_1,
line_2: address.street_2,
line_2: address.street_2 ?? null,
line_3: null,
line_4: null,
region: address.state + ',' + address.country,
region: address.state,
postcode: address.postal_code,
latitude: null,
longitude: null,
country_code: null,
country_code: getCountryCode(address.country),
},
];
}
Expand Down Expand Up @@ -158,16 +157,11 @@ export class AttioCompanyMapper implements ICompanyMapper {
city: company.values.primary_location[0]?.locality,
state: company.values.primary_location[0]?.region,
postal_code: company.values.primary_location[0]?.postcode,
country: company.values.primary_location[0]?.country_code,
address_type: 'primary',
owner_type: 'company',
},
],
phone_numbers: [
{
phone_number: null,
phone_type: 'primary',
owner_type: 'company',
country: getCountryName(
company.values.primary_location[0]?.country_code,
),
address_type: 'WORK',
owner_type: 'COMPANY',
},
],
field_mappings,
Expand Down
7 changes: 4 additions & 3 deletions packages/api/src/crm/company/services/close/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ICompanyMapper } from '@crm/company/types';
import { Utils } from '@crm/@lib/@utils';
import { Injectable } from '@nestjs/common';
import { MappersRegistry } from '@@core/@core-services/registries/mappers.registry';
import { getCountryCode, getCountryName } from '@@core/utils/types';

@Injectable()
export class CloseCompanyMapper implements ICompanyMapper {
Expand All @@ -28,6 +29,7 @@ export class CloseCompanyMapper implements ICompanyMapper {
address_2: address.street_2,
city: address.city,
state: address.state,
country: getCountryCode(address.country),
zipcode: address.postal_code,
label: address.address_type,
})) as CloseCompanyInput['addresses'],
Expand Down Expand Up @@ -109,11 +111,10 @@ export class CloseCompanyMapper implements ICompanyMapper {
city: address.city,
state: address.state,
postal_code: address.zipcode,
country: address.country,
country: getCountryName(address.country) || address.country,
address_type: address.label,
owner_type: 'company',
owner_type: 'COMPANY',
})),
phone_numbers: null,
field_mappings,
...opts,
};
Expand Down
21 changes: 15 additions & 6 deletions packages/api/src/crm/company/services/company.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ export class CompanyService {
data: {
...email,
id_crm_company: companyId,
id_connection: connectionId,
},
});
}
Expand All @@ -249,6 +250,7 @@ export class CompanyService {
data: {
...phone,
id_crm_company: companyId,
id_connection: connectionId,
},
});
}
Expand Down Expand Up @@ -295,6 +297,7 @@ export class CompanyService {
data: {
...email,
id_crm_company: companyId,
id_connection: connectionId,
},
}),
),
Expand All @@ -308,6 +311,7 @@ export class CompanyService {
data: {
...phone,
id_crm_company: companyId,
id_connection: connectionId,
},
}),
),
Expand Down Expand Up @@ -406,10 +410,12 @@ export class CompanyService {
});
const remote_data = JSON.parse(resp.data);

res = {
...res,
remote_data: remote_data,
};
if (resp && resp.data) {
res = {
...res,
remote_data: remote_data,
};
}
}
if (linkedUserId && integrationId) {
await this.prisma.events.create({
Expand Down Expand Up @@ -552,8 +558,11 @@ export class CompanyService {
ressource_owner_id: company.id,
},
});
const remote_data = JSON.parse(resp.data);
return { ...company, remote_data };
if (resp && resp.data) {
const remote_data = JSON.parse(resp.data);
return { ...company, remote_data };
}
return company;
}),
);
res = remote_array_data;
Expand Down
31 changes: 17 additions & 14 deletions packages/api/src/crm/company/services/hubspot/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ export class HubspotCompanyMapper implements ICompanyMapper {
remote_id: string;
}[],
): Promise<HubspotCompanyInput> {
const result: HubspotCompanyInput = {
city: null,
const result: any = {
name: source.name,
phone: null,
state: null,
domain: null,
industry: source.industry || null,
};

if (source.number_of_employees) {
result.numberofemployees = source.number_of_employees;
}

// Assuming 'phone_numbers' array contains at least one phone number
const primaryPhone = source.phone_numbers?.[0]?.phone_number;
if (primaryPhone) {
Expand All @@ -39,6 +39,9 @@ export class HubspotCompanyMapper implements ICompanyMapper {
if (address) {
result.city = address.city;
result.state = address.state;
result.zip = address.postal_code;
result.address = address.street_1;
result.country = address.country;
}
}

Expand Down Expand Up @@ -121,23 +124,23 @@ export class HubspotCompanyMapper implements ICompanyMapper {
remote_id: company.id,
name: company.properties.name,
industry: company.properties.industry,
number_of_employees: null,
number_of_employees: company.properties.numberofemployees,
addresses: [
{
street_1: null,
street_1: company.properties.address,
city: company.properties.city,
state: company.properties.state,
postal_code: null,
country: null,
address_type: 'primary',
owner_type: 'company',
postal_code: company.properties.zip,
country: company.properties.country,
address_type: 'WORK',
owner_type: 'COMPANY',
},
], // Assuming 'street', 'city', 'state', 'postal_code', 'country' are properties in company.properties
],
phone_numbers: [
{
phone_number: company.properties.phone,
phone_type: 'primary',
owner_type: 'company',
phone_type: 'WORK',
owner_type: 'COMPANY',
},
],
field_mappings,
Expand Down
14 changes: 13 additions & 1 deletion packages/api/src/crm/company/services/hubspot/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ export interface HubspotCompanyInput {
city: string;
name: string;
phone: string;
state: string;
domain: string;
industry: string;
state: string;
country: string;
address: string;
numberofemployees: string;
zip: string;
hubspot_owner_id?: string;
[key: string]: any;
}
Expand All @@ -21,6 +25,10 @@ export interface HubspotCompanyOutput {
name: string;
phone: string;
state: string;
country: string;
zip: string;
address: string;
numberofemployees: string;
[key: string]: string;
};
createdAt: string;
Expand All @@ -36,5 +44,9 @@ export const commonCompanyHubspotProperties = {
name: '',
phone: '',
state: '',
country: '',
zip: '',
address: '',
hubspot_owner_id: '',
numberofemployees: '',
};
37 changes: 23 additions & 14 deletions packages/api/src/crm/company/services/pipedrive/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ export class PipedriveCompanyMapper implements ICompanyMapper {
};

if (source.addresses && source.addresses[0]) {
result.address = source.addresses[0].street_1;
result.address_locality = source.addresses[0].city;
result.address_country = source.addresses[0].country;
result.address_postal_code = source.addresses[0].postal_code;
result.address = `${source.addresses[0].street_1}, ${source.addresses[0].postal_code} ${source.addresses[0].city}, ${source.addresses[0].country}`;
}

if (source.user_id) {
Expand Down Expand Up @@ -115,20 +112,32 @@ export class PipedriveCompanyMapper implements ICompanyMapper {
}
}
if (company.address) {
opts.addresses[0] = {
street_1: company.address,
city: company.address_locality,
country: company.address_country,
postal_code: company.address_postal_code,
};
const addressRegex = /^(.*?), (\d{5}) (.*?), (.*)$/;
const match = company.address.match(addressRegex);

const [, street, postalCode, city, country] = match;
opts.addresses = [
{
street_1: street,
city: city,
country: country,
postal_code: postalCode,
},
];
}
if (company.cc_email) {
opts.email_adresses = [
{
owner_type: 'COMPANY',
email_address: company.cc_email,
email_address_type: 'WORK',
},
];
}
return {
name: company.name,
industry: null,
number_of_employees: null,
email_addresses: null,
phone_numbers: null,
addresses: null,
number_of_employees: company.people_count ?? null,
field_mappings,
remote_id: String(company.id),
...opts,
Expand Down
6 changes: 3 additions & 3 deletions packages/api/src/crm/company/services/zendesk/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,17 @@ export class ZendeskCompanyMapper implements ICompanyMapper {

// Constructing the email and phone details
const email_addresses = company.email
? [{ email_address: company.email, email_address_type: 'primary' }]
? [{ email_address: company.email, email_address_type: 'WORK' }]
: null;
const phone_numbers = [];

if (company.phone) {
phone_numbers.push({ phone_number: company.phone, phone_type: 'work' });
phone_numbers.push({ phone_number: company.phone, phone_type: 'WORK' });
}
if (company.mobile) {
phone_numbers.push({
phone_number: company.mobile,
phone_type: 'mobile',
phone_type: 'MOBILE',
});
}

Expand Down
14 changes: 12 additions & 2 deletions packages/api/src/crm/company/services/zoho/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,19 @@ export class ZohoService implements ICompanyService {
},
},
);
//this.logger.log('zoho resp is ' + JSON.stringify(resp));
const final_res = await axios.get(
`${connection.account_url}/Accounts/${resp.data.data[0].details.id}`,
{
headers: {
'Content-Type': 'application/json',
Authorization: `Zoho-oauthtoken ${this.cryptoService.decrypt(
connection.access_token,
)}`,
},
},
);
return {
data: resp.data.data,
data: final_res.data.data[0],
message: 'Zoho company created',
statusCode: 201,
};
Expand Down
Loading

0 comments on commit a014a4e

Please sign in to comment.