-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from panoratech/feat/add-contact-post
Feat/add contact post
- Loading branch information
Showing
12 changed files
with
582 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 32 additions & 35 deletions
67
packages/api/src/crm/contact/services/freshsales/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,52 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
import { HttpStatus, Injectable } from '@nestjs/common'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { ApiResponse } from '../../types'; | ||
import axios, { AxiosResponse } from 'axios'; | ||
import axios from 'axios'; | ||
import { | ||
CrmObject, | ||
FreshsalesContactInput, | ||
FreshsalesContactOutput, | ||
} from 'src/crm/@types'; | ||
import { PrismaService } from 'src/@core/prisma/prisma.service'; | ||
import { LoggerService } from 'src/@core/logger/logger.service'; | ||
import { handleServiceError } from 'src/@core/utils/errors'; | ||
|
||
@Injectable() | ||
export class FreshSalesService { | ||
constructor(private prisma: PrismaService, private logger: LoggerService) { | ||
this.logger.setContext(FreshSalesService.name); | ||
} | ||
|
||
async addContact( | ||
contactData: FreshsalesContactInput, | ||
linkedUserId: string, | ||
): Promise<ApiResponse<FreshsalesContactOutput>> { | ||
const mobile = contactData.phone_numbers[0]; | ||
const url = 'https://domain.freshsales.io/api/contacts'; | ||
const data = { | ||
contact: { | ||
first_name: contactData.first_name, | ||
last_name: contactData.last_name, | ||
mobile_number: mobile, | ||
}, | ||
}; | ||
const token = process.env.FRESHSALES_API_KEY; | ||
const headers = { | ||
Authorization: `Token token=${token}`, | ||
'Content-Type': 'application/json', | ||
}; | ||
|
||
try { | ||
const response: AxiosResponse<FreshsalesContactOutput> = await axios.post( | ||
url, | ||
data, | ||
{ headers: headers }, | ||
const connection = await this.prisma.connections.findFirst({ | ||
where: { | ||
id_linked_user: BigInt(linkedUserId), | ||
}, | ||
}); | ||
const dataBody = { | ||
contact: contactData, | ||
}; | ||
const resp = await axios.post( | ||
'https://domain.freshsales.io/api/contacts', | ||
JSON.stringify(dataBody), | ||
{ | ||
headers: { | ||
Authorization: `Token token=${connection.access_token}`, | ||
'Content-Type': 'application/json', | ||
}, | ||
}, | ||
); | ||
console.log(response.data); | ||
return { | ||
data: response.data, | ||
message: 'Contact created successfully.', | ||
statusCode: HttpStatus.OK, | ||
data: resp.data, | ||
message: 'Freshsales contact created', | ||
statusCode: 201, | ||
}; | ||
} catch (error) { | ||
console.error(error.response ? error.response.data : error.message); | ||
const status: number = error.response | ||
? error.response.status | ||
: HttpStatus.INTERNAL_SERVER_ERROR; | ||
return { | ||
data: null, | ||
error: error.message, | ||
message: 'Failed to create contact.', | ||
statusCode: status, | ||
}; | ||
handleServiceError(error, this.logger, 'Freshsales', CrmObject.contact); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,52 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { ApiResponse } from '../../types'; | ||
import { HubspotContactInput, HubspotContactOutput } from 'src/crm/@types'; | ||
import { | ||
CrmObject, | ||
HubspotContactInput, | ||
HubspotContactOutput, | ||
} from 'src/crm/@types'; | ||
import axios from 'axios'; | ||
import { PrismaService } from 'src/@core/prisma/prisma.service'; | ||
import { LoggerService } from 'src/@core/logger/logger.service'; | ||
import { handleServiceError } from 'src/@core/utils/errors'; | ||
|
||
@Injectable() | ||
export class HubspotService { | ||
constructor(private prisma: PrismaService, private logger: LoggerService) { | ||
this.logger.setContext(HubspotService.name); | ||
} | ||
async addContact( | ||
contactData: HubspotContactInput, | ||
linkedUserId: string, | ||
): Promise<ApiResponse<HubspotContactOutput>> { | ||
try { | ||
//TODO: check required scope => crm.objects.contacts.write | ||
const connection = await this.prisma.connections.findFirst({ | ||
where: { | ||
id_linked_user: BigInt(linkedUserId), | ||
}, | ||
}); | ||
const dataBody = { | ||
properties: contactData, | ||
}; | ||
const resp = await axios.post( | ||
`https://api.hubapi.com/crm/v3/objects/contacts/`, | ||
JSON.stringify(dataBody), | ||
{ | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${connection.access_token}`, | ||
}, | ||
}, | ||
); | ||
return { | ||
data: resp.data, | ||
message: 'Hubspot contact created', | ||
statusCode: 201, | ||
}; | ||
} catch (error) { | ||
handleServiceError(error, this.logger, 'Hubspot', CrmObject.contact); | ||
} | ||
return; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,27 @@ | ||
export interface HubspotContactInput { | ||
company_size: string; | ||
email?: string; | ||
firstname?: string; | ||
phone?: string; | ||
lastname?: string; | ||
city?: string; | ||
country?: string; | ||
zip?: string; | ||
state?: string; | ||
address?: string; | ||
mobilephone?: string; | ||
hubspot_owner_id?: string; | ||
associatedcompanyid?: string; | ||
fax?: string; | ||
jobtitle?: string; | ||
} | ||
|
||
export interface HubspotContactOutput { | ||
id: number; | ||
company: string; | ||
createdate: string; // Use 'Date' if you prefer a Date object | ||
email: string; | ||
firstname: string; | ||
lastmodifieddate: string; // Use 'Date' if you prefer a Date object | ||
lastname: string; | ||
phone: string; | ||
website: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,50 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { ApiResponse } from '../../types'; | ||
import { PipedriveContactInput, PipedriveContactOutput } from 'src/crm/@types'; | ||
import { | ||
CrmObject, | ||
PipedriveContactInput, | ||
PipedriveContactOutput, | ||
} from 'src/crm/@types'; | ||
import axios from 'axios'; | ||
import { PrismaService } from 'src/@core/prisma/prisma.service'; | ||
import { LoggerService } from 'src/@core/logger/logger.service'; | ||
import { handleServiceError } from 'src/@core/utils/errors'; | ||
|
||
@Injectable() | ||
export class PipedriveService { | ||
constructor(private prisma: PrismaService, private logger: LoggerService) { | ||
this.logger.setContext(PipedriveService.name); | ||
} | ||
|
||
async addContact( | ||
contactData: PipedriveContactInput, | ||
linkedUserId: string, | ||
): Promise<ApiResponse<PipedriveContactOutput>> { | ||
try { | ||
//TODO: check required scope => crm.objects.contacts.write | ||
const connection = await this.prisma.connections.findFirst({ | ||
where: { | ||
id_linked_user: BigInt(linkedUserId), | ||
}, | ||
}); | ||
const resp = await axios.post( | ||
`https://api.pipedrive.com/v1/persons`, | ||
JSON.stringify(contactData), | ||
{ | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${connection.access_token}`, | ||
}, | ||
}, | ||
); | ||
return { | ||
data: resp.data, | ||
message: 'Pipedrive contact created', | ||
statusCode: 201, | ||
}; | ||
} catch (error) { | ||
handleServiceError(error, this.logger, 'Pipedrive', CrmObject.contact); | ||
} | ||
return; | ||
} | ||
} |
Oops, something went wrong.