Skip to content

Commit

Permalink
⚡ Improve providers
Browse files Browse the repository at this point in the history
:
  • Loading branch information
naelob committed Dec 12, 2023
1 parent 0fb02d2 commit ecfba32
Show file tree
Hide file tree
Showing 9 changed files with 262 additions and 187 deletions.
6 changes: 5 additions & 1 deletion packages/api/src/@core/prisma/prisma.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { PrismaClient } from '@prisma/client';
@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit {
async onModuleInit() {
await this.$connect();
try {
await this.$connect();
} catch (error) {
throw new Error(error);
}
}
}
3 changes: 3 additions & 0 deletions packages/api/src/crm/contact/services/hubspot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export class HubspotService {
const connection = await this.prisma.connections.findFirst({
where: {
id_linked_user: linkedUserId,
provider_slug: 'hubspot',
},
});
const dataBody = {
Expand Down Expand Up @@ -59,6 +60,7 @@ export class HubspotService {
}
return;
}

async getContacts(
linkedUserId: string,
custom_properties?: string[],
Expand All @@ -68,6 +70,7 @@ export class HubspotService {
const connection = await this.prisma.connections.findFirst({
where: {
id_linked_user: linkedUserId,
provider_slug: 'hubspot',
},
});

Expand Down
7 changes: 5 additions & 2 deletions packages/api/src/crm/contact/services/pipedrive/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export class PipedriveService {
const connection = await this.prisma.connections.findFirst({
where: {
id_linked_user: linkedUserId,
provider_slug: 'pipedrive',
},
});
const resp = await axios.post(
Expand All @@ -41,7 +42,7 @@ export class PipedriveService {
},
);
return {
data: resp.data,
data: resp.data.data,
message: 'Pipedrive contact created',
statusCode: 201,
};
Expand All @@ -65,6 +66,7 @@ export class PipedriveService {
const connection = await this.prisma.connections.findFirst({
where: {
id_linked_user: linkedUserId,
provider_slug: 'pipedrive',
},
});
const resp = await axios.get(`https://api.pipedrive.com/v1/persons`, {
Expand All @@ -73,8 +75,9 @@ export class PipedriveService {
Authorization: `Bearer ${decrypt(connection.access_token)}`,
},
});

return {
data: resp.data,
data: resp.data.data,
message: 'Pipedrive contacts retrieved',
statusCode: 200,
};
Expand Down
10 changes: 4 additions & 6 deletions packages/api/src/crm/contact/services/pipedrive/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,16 @@ export function mapToContact_Pipedrive(

// Convert to Pipedrive format if needed
const emailObject = primaryEmail
? [{ value: primaryEmail, primary: true }]
? [{ value: primaryEmail, primary: true, label: '' }]
: [];
const phoneObject = primaryPhone
? [{ value: primaryPhone, primary: true }]
? [{ value: primaryPhone, primary: true, label: '' }]
: [];

return {
name: `${source.first_name} ${source.last_name}`,
email: emailObject,
phone: phoneObject,
// Map other optional fields as needed
// label, visible_to, marketing_status, add_time, etc.
};
}

Expand All @@ -47,11 +45,11 @@ function _mapSinglePipedriveContact(
last_name: contact.last_name,
email_addresses: contact.email.map((e) => ({
email_address: e.value,
email_address_type: e.label,
email_address_type: e.label ? e.label : '',
})), // Map each email
phone_numbers: contact.phone.map((p) => ({
phone_number: p.value,
phone_type: p.label,
phone_type: p.label ? p.label : '',
})), // Map each phone number
};
}
153 changes: 67 additions & 86 deletions packages/api/src/crm/contact/services/pipedrive/types.ts
Original file line number Diff line number Diff line change
@@ -1,97 +1,78 @@
export interface PipedriveContactInput {
name: string;
owner_id?: number;
org_id?: number;
email: string | EmailObject[];
phone: string | PhoneObject[];
label?: number;
visible_to?: VisibleTo;
marketing_status?: MarketingStatus;
add_time?: string;
}

export interface PipedriveContactOutput {
id: number;
export interface PipedriveContact {
id: string;
company_id: number;
owner_id: User;
org_id: OrgId;
owner_id: {
id: number;
name: string;
email: string;
has_pic: number;
pic_hash: string;
active_flag: boolean;
value: number;
};
org_id: {
name: string;
people_count: number;
owner_id: number;
address: string;
active_flag: boolean;
cc_email: string;
value: number;
};
name: string;
first_name: string;
last_name: string;
// ... other properties
phone: Phone[];
email: Email[];
open_deals_count: number;
related_open_deals_count: number;
closed_deals_count: number;
related_closed_deals_count: number;
participant_open_deals_count: number;
participant_closed_deals_count: number;
email_messages_count: number;
activities_count: number;
done_activities_count: number;
undone_activities_count: number;
files_count: number;
notes_count: number;
followers_count: number;
won_deals_count: number;
related_won_deals_count: number;
lost_deals_count: number;
related_lost_deals_count: number;
active_flag: boolean;
phone: { value: string; primary: boolean; label: string }[];
email: { value: string; primary: boolean; label: string }[];
primary_email: string;
// ... other properties
picture_id: PictureId;
// ... other properties
first_char: string;
update_time: Date;
add_time: Date;
visible_to: string;
marketing_status: string;
picture_id: {
item_type: string;
item_id: number;
active_flag: boolean;
add_time: string;
update_time: string;
added_by_user_id: number;
pictures: {
'128': string;
'512': string;
};
value: number;
};
next_activity_date: string;
next_activity_time: string;
next_activity_id: number;
last_activity_id: number;
last_activity_date: string;
last_incoming_mail_time: string;
last_outgoing_mail_time: string;
label: number;
org_name: string;
owner_name: string;
cc_email: string;
}

interface User {
id: number;
name: string;
email: string;
has_pic: number;
pic_hash: string;
active_flag: boolean;
}

interface OrgId {
name: string;
people_count: number;
owner_id: number;
address: string;
active_flag: boolean;
cc_email: string;
value: number;
}

//OUTPUT
interface Phone {
value: string;
primary: boolean;
label: string;
}

//OUTPUT
interface Email {
value: string;
primary: boolean;
label: string;
}

//OUTPUT
interface PictureId {
item_type: string;
item_id: number;
active_flag: boolean;
add_time: string;
update_time: string;
added_by_user_id: number;
pictures: Record<string, string>;
value: number;
}

//INPUT
interface EmailObject {
value: string;
primary?: boolean;
label?: string;
}

//INPUT
type PhoneObject = EmailObject;

//INPUT
type MarketingStatus =
| 'no_consent'
| 'unsubscribed'
| 'subscribed'
| 'archived';

//INPUT
type VisibleTo = 1 | 3 | 5 | 7;
export type PipedriveContactInput = Partial<PipedriveContact>;
export type PipedriveContactOutput = PipedriveContactInput;
19 changes: 13 additions & 6 deletions packages/api/src/crm/contact/services/zendesk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,23 @@ export class ZendeskService {
const connection = await this.prisma.connections.findFirst({
where: {
id_linked_user: linkedUserId,
provider_slug: 'zendesk',
},
});
const resp = await axios.get(`https://api.getbase.com/v2/contacts`, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${decrypt(connection.access_token)}`,
const resp = await axios.get(
`https://api.getbase.com/v3/contacts/stream`,
{
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${decrypt(connection.access_token)}`,
},
},
});
);
this.logger.log(
'zendesk contacts are ' + JSON.stringify(resp.data.items),
);
return {
data: resp.data,
data: resp.data.items,
message: 'Zendesk contacts retrieved',
statusCode: 200,
};
Expand Down
Loading

0 comments on commit ecfba32

Please sign in to comment.