Skip to content

Commit

Permalink
feat(contact-service): Implement search endpoint and fix name field i…
Browse files Browse the repository at this point in the history
…ssue in Close CRM
  • Loading branch information
ashutosh-revert committed Nov 15, 2023
1 parent 1066484 commit 8eb5bcd
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 27 deletions.
2 changes: 0 additions & 2 deletions packages/backend/helpers/crm/closecrm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ export default function handleCloseCRMDisunify<T extends Record<string, any>>({
obj: T;
transformedObj: any;
}) {
console.log('DEBUG', 'OG obj......', obj);
// const names = transformedObj['name'].split(' ');
if (obj.firstName && obj.lastName) {
transformedObj['name'] = `${obj.firstName} ${obj.lastName}`;
} else if (obj.firstName) {
Expand Down
21 changes: 1 addition & 20 deletions packages/backend/helpers/crm/transform/preprocess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ export const preprocessUnifyObject = <T extends Record<string, any>>({
firstName: names[0],
lastName: names[1],
};
console.log(
'DEBUG',
'lalsdjaslkdjlsjdlasdlasjdlasdlasjdlasdkaljdlajsdlaksdjlasjdlajsdlajad',
modifiedObj
);
return modifiedObj;
}
return { ...obj };
Expand Down Expand Up @@ -143,21 +138,7 @@ export const postprocessDisUnifyObject = <T extends Record<string, any>>({
};
},
},
[TP_ID.closecrm]: {
// [StandardObjects.note]: (obj: T) => {
// return {
// ...obj,
// lead_id: 'lead_u1ETw8SoeXST0FXO8V7OdxEwD67q3SwGjROpbUHy6sV',
// };
// },
// [StandardObjects.contact]: (obj: T) => {
// console.log('DEBUG', 'From preprocess closecrm', obj);
// return {
// ...obj,
// name: `${obj.firstName} ${obj.lastName}`,
// };
// },
},
[TP_ID.closecrm]: {},
};
const transformFn = (preprocessMap[tpId] || {})[objType];
return transformFn ? transformFn(obj) : obj;
Expand Down
15 changes: 13 additions & 2 deletions packages/backend/helpers/crm/transform/unify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,30 @@ export async function unifyObject<T extends Record<string, any>, K>({
tenantSchemaMappingId,
accountFieldMappingConfig,
});
const unifiedObject = {
const unifiedObject: {
additional: any;
associations: any | undefined;
} = {
...transformedObject,
additional: {
...(transformedObject.additional || {}),
},
associations: {},
};

// Map additional fields
Object.keys(obj).forEach((key) => {
if (!(key in unifiedObject) && key !== 'properties') {
unifiedObject['additional'][key] = obj[key];
if (unifiedObject.additional.lead_id) {
unifiedObject['associations']['leadId'] = unifiedObject.additional.lead_id;
}
}
});

// Check if associations object is empty and set it to undefined
if (Object.keys(unifiedObject.associations || {}).length === 0) {
unifiedObject.associations = undefined;
}

return unifiedObject as K;
}
37 changes: 34 additions & 3 deletions packages/backend/services/crm/contact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ const contactService = new ContactService(
const pagingString = `${pageSize ? `&limit=${pageSize}` : ''}${
cursor ? `&after=${cursor}` : ''
}`;
console.log('DEBUG', formattedFields);
let contacts: any = await axios({
method: 'get',
url: `https://api.hubapi.com/crm/v3/objects/contacts?properties=${formattedFields}&${pagingString}`,
Expand Down Expand Up @@ -522,7 +521,9 @@ const contactService = new ContactService(
}
case TP_ID.closecrm: {
// Manually setting the contact name since it couldn't be retrieved from fieldMappings

if (!contactData.lastName || !contactData.firstName) {
throw new Error('Both "firstName" and "lastName" fields are required.');
}
const response = await axios({
method: 'post',
url: 'https://api.close.com/api/v1/contact/',
Expand Down Expand Up @@ -635,6 +636,11 @@ const contactService = new ContactService(
break;
}
case TP_ID.closecrm: {
// checks for name field
if ((contact.lastName || contact.firstName) && (!contact.firstName || !contact.lastName)) {
throw new Error('Both firstName and lastName fields are required for Close CRM.');
}
console.log('Yooooooo');
const response = await axios({
method: 'put',
url: `https://api.close.com/api/v1/contact/${contactId}`,
Expand All @@ -658,7 +664,7 @@ const contactService = new ContactService(
}
} catch (error: any) {
logError(error);
console.error('Could not update lead', error);
console.error('Could not update contact', error);
if (isStandardError(error)) {
throw error;
}
Expand Down Expand Up @@ -811,6 +817,31 @@ const contactService = new ContactService(
}
// @TODO
case TP_ID.closecrm: {
const fields = ['id', 'date_created', 'date_updated', 'name', 'phones', 'emails', 'lead_id'];
const response: any = await axios({
method: 'post',
url: 'https://api.close.com/api/v1/data/search',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${thirdPartyToken}`,
},
data: { ...searchCriteria, _fields: { contact: fields } },
});

const contacts = await Promise.all(
response.data.data.map(
async (l: any) =>
await unifyObject<any, UnifiedContact>({
obj: l,
tpId: thirdPartyId,
objType,
tenantSchemaMappingId: connection.schema_mapping_id,
accountFieldMappingConfig: account.accountFieldMappingConfig,
})
)
);

res.send({ status: 'ok', results: contacts });
break;
}
default: {
Expand Down

0 comments on commit 8eb5bcd

Please sign in to comment.