Skip to content

Commit

Permalink
✨ Fixed all oauth providers for contact
Browse files Browse the repository at this point in the history
  • Loading branch information
naelob committed Dec 12, 2023
1 parent ecfba32 commit 69b7083
Show file tree
Hide file tree
Showing 9 changed files with 7,396 additions and 14,340 deletions.
2 changes: 1 addition & 1 deletion apps/frontend-snippet/src/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const providersConfig: ProvidersConfig = {
},
'zoho': {
clientId: '1000.CWBWAO0XK6QNROXMA2Y0RUZYMGJIGT',
scopes: 'AaaServer.profile.Read', //todo
scopes: 'ZohoCRM.modules.ALL',
authBaseUrl: 'https://accounts.zoho.eu/oauth/v2/auth',
logoPath: 'assets/crm/zoho_logo.png',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export class ZohoConnectionService {
redirect_uri: REDIRECT_URI,
code: code,
});
//no refresh token
const domain = ZOHOLocations[zohoLocation];
const res = await axios.post(
`${domain}/oauth/v2/token`,
Expand Down Expand Up @@ -105,14 +106,14 @@ export class ZohoConnectionService {
) {
try {
const REDIRECT_URI = `${config.OAUTH_REDIRECT_BASE}/connections/oauth/callback`;

const formData = new URLSearchParams({
grant_type: 'refresh_token',
client_id: config.HUBSPOT_CLIENT_ID,
client_secret: config.HUBSPOT_CLIENT_SECRET,
client_id: config.ZOHOCRM_CLIENT_ID,
client_secret: config.ZOHOCRM_CLIENT_SECRET,
redirect_uri: REDIRECT_URI,
refresh_token: decrypt(refresh_token),
});

const res = await axios.post(
`${domain}/oauth/v2/token`,
formData.toString(),
Expand Down
29 changes: 15 additions & 14 deletions packages/api/src/crm/contact/services/zendesk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,24 @@ export class ZendeskService {
const connection = await this.prisma.connections.findFirst({
where: {
id_linked_user: linkedUserId,
provider_slug: 'zendesk',
},
});
const resp = await axios.post(
`https://api.getbase.com/v2/contacts`,
JSON.stringify(contactData),
{
data: contactData,
},
{
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${decrypt(connection.access_token)}`,
},
},
);

return {
data: resp.data,
data: resp.data.data,
message: 'Zendesk contact created',
statusCode: 201,
};
Expand All @@ -67,20 +71,17 @@ export class ZendeskService {
provider_slug: 'zendesk',
},
});
const resp = await axios.get(
`https://api.getbase.com/v3/contacts/stream`,
{
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${decrypt(connection.access_token)}`,
},
const resp = await axios.get(`https://api.getbase.com/v2/contacts`, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${decrypt(connection.access_token)}`,
},
);
this.logger.log(
'zendesk contacts are ' + JSON.stringify(resp.data.items),
);
});
const finalData = resp.data.items.map((item) => {
return item.data;
});
return {
data: resp.data.items,
data: finalData,
message: 'Zendesk contacts retrieved',
statusCode: 200,
};
Expand Down
30 changes: 20 additions & 10 deletions packages/api/src/crm/contact/services/zoho/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ export class ZohoService {
const connection = await this.prisma.connections.findFirst({
where: {
id_linked_user: linkedUserId,
provider_slug: 'zoho',
},
});
const resp = await axios.post(
//TODO
`https://www.zohoapis.com/crm/v3/Contacts`,
JSON.stringify(contactData),
`https://www.zohoapis.eu/crm/v3/Contacts`,
{ data: [contactData] },
{
headers: {
'Content-Type': 'application/json',
Expand All @@ -39,8 +39,9 @@ export class ZohoService {
},
},
);
//this.logger.log('zoho resp is ' + JSON.stringify(resp));
return {
data: resp.data,
data: resp.data.data,
message: 'Zoho contact created',
statusCode: 201,
};
Expand All @@ -64,16 +65,25 @@ export class ZohoService {
const connection = await this.prisma.connections.findFirst({
where: {
id_linked_user: linkedUserId,
provider_slug: 'zoho',
},
});
const resp = await axios.get(`https://www.zohoapis.com/crm/v3/Contacts`, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${decrypt(connection.access_token)}`,
//TODO: handle fields
const fields = 'First_Name,Last_Name,Full_Name,Email,Phone';
const resp = await axios.get(
`https://www.zohoapis.eu/crm/v3/Contacts?fields=${fields}`,
{
headers: {
'Content-Type': 'application/json',
Authorization: `Zoho-oauthtoken ${decrypt(
connection.access_token,
)}`,
},
},
});
);
this.logger.log('CONTACTS ZOHO ' + JSON.stringify(resp.data.data));
return {
data: resp.data,
data: resp.data.data,
message: 'Zoho contacts retrieved',
statusCode: 200,
};
Expand Down
51 changes: 28 additions & 23 deletions packages/api/src/crm/contact/services/zoho/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,14 @@ export function mapToContact_Zoho(
const primaryEmail = source.email_addresses?.[0]?.email_address;
const primaryPhone = source.phone_numbers?.[0]?.phone_number;

//TODO zoho input weird ???
return; /*{
firstname: source_.first_name,
lastname: source_.last_name,
email: primaryEmail,
phone: primaryPhone,
// Map other fields as needed
// If there are fields such as city, country, etc., in your UnifiedContactInput, map them here
};*/
return {
First_Name: source.first_name,
Last_Name: source.last_name,
Email: primaryEmail,
Phone: primaryPhone,
};
}
//TODO

export function mapToUnifiedContact_Zoho(
source: ZohoContactOutput | ZohoContactOutput[],
): UnifiedContactOutput | UnifiedContactOutput[] {
Expand All @@ -36,34 +33,42 @@ export function mapToUnifiedContact_Zoho(
function _mapSingleZohoContact(
contact: ZohoContactOutput,
): UnifiedContactOutput {
// Finding the primary contact person
const primaryContact = contact.contact_persons.find(
(p) => p.is_primary_contact,
);

// Constructing email and phone details
const email_addresses =
primaryContact && primaryContact.email
? [{ email_address: primaryContact.email, email_address_type: 'primary' }]
contact && contact.Email
? [{ email_address: contact.Email, email_address_type: 'primary' }]
: [];

const phone_numbers = [];

if (primaryContact && primaryContact.phone) {
if (contact && contact.Phone) {
phone_numbers.push({
phone_number: primaryContact.phone,
phone_number: contact.Phone,
phone_type: 'work',
});
}
if (primaryContact && primaryContact.mobile) {
if (contact && contact.Mobile) {
phone_numbers.push({
phone_number: primaryContact.mobile,
phone_number: contact.Mobile,
phone_type: 'mobile',
});
}
if (contact && contact.Fax) {
phone_numbers.push({
phone_number: contact.Fax,
phone_type: 'fax',
});
}
if (contact && contact.Home_Phone) {
phone_numbers.push({
phone_number: contact.Home_Phone,
phone_type: 'home',
});
}

return {
first_name: primaryContact ? primaryContact.first_name : '',
last_name: primaryContact ? primaryContact.last_name : '',
first_name: contact.First_Name ? contact.First_Name : '',
last_name: contact.Last_Name ? contact.Last_Name : '',
email_addresses,
phone_numbers,
};
Expand Down
Loading

0 comments on commit 69b7083

Please sign in to comment.