153 lines
4.5 KiB
JavaScript
153 lines
4.5 KiB
JavaScript
// Data mapping/transformation utilities for Zoho integration
|
|
class ZohoMapper {
|
|
// Map Zoho CRM Lead to standardized format
|
|
static mapLead(zohoLead) {
|
|
return {
|
|
id: zohoLead.id,
|
|
name: zohoLead.Full_Name || zohoLead.Lead_Name,
|
|
email: zohoLead.Email,
|
|
phone: zohoLead.Phone,
|
|
company: zohoLead.Company,
|
|
status: zohoLead.Lead_Status,
|
|
source: zohoLead.Lead_Source,
|
|
createdTime: zohoLead.Created_Time,
|
|
modifiedTime: zohoLead.Modified_Time,
|
|
owner: zohoLead.Owner?.name,
|
|
// Map custom fields if they exist
|
|
customFields: this.mapCustomFields(zohoLead)
|
|
};
|
|
}
|
|
|
|
// Map Zoho CRM Contact to standardized format
|
|
static mapContact(zohoContact) {
|
|
return {
|
|
id: zohoContact.id,
|
|
firstName: zohoContact.First_Name,
|
|
lastName: zohoContact.Last_Name,
|
|
email: zohoContact.Email,
|
|
phone: zohoContact.Phone,
|
|
account: zohoContact.Account_Name,
|
|
title: zohoContact.Title,
|
|
department: zohoContact.Department,
|
|
createdTime: zohoContact.Created_Time,
|
|
modifiedTime: zohoContact.Modified_Time,
|
|
owner: zohoContact.Owner?.name,
|
|
customFields: this.mapCustomFields(zohoContact)
|
|
};
|
|
}
|
|
|
|
// Map Zoho CRM Deal to standardized format
|
|
static mapDeal(zohoDeal) {
|
|
return {
|
|
id: zohoDeal.id,
|
|
name: zohoDeal.Deal_Name,
|
|
amount: zohoDeal.Amount,
|
|
stage: zohoDeal.Stage,
|
|
probability: zohoDeal.Probability,
|
|
closeDate: zohoDeal.Closing_Date,
|
|
account: zohoDeal.Account_Name,
|
|
contact: zohoDeal.Contact_Name,
|
|
createdTime: zohoDeal.Created_Time,
|
|
modifiedTime: zohoDeal.Modified_Time,
|
|
owner: zohoDeal.Owner?.name,
|
|
customFields: this.mapCustomFields(zohoDeal)
|
|
};
|
|
}
|
|
|
|
// Map Zoho People Employee to standardized format
|
|
static mapEmployee(zohoEmployee) {
|
|
return {
|
|
id: zohoEmployee.id,
|
|
employeeId: zohoEmployee.employeeId,
|
|
firstName: zohoEmployee.firstName,
|
|
lastName: zohoEmployee.lastName,
|
|
email: zohoEmployee.email,
|
|
phone: zohoEmployee.phone,
|
|
department: zohoEmployee.department,
|
|
designation: zohoEmployee.designation,
|
|
reportingTo: zohoEmployee.reportingTo,
|
|
joiningDate: zohoEmployee.joiningDate,
|
|
status: zohoEmployee.status,
|
|
customFields: this.mapCustomFields(zohoEmployee)
|
|
};
|
|
}
|
|
|
|
// Map Zoho Projects Project to standardized format
|
|
static mapProject(zohoProject) {
|
|
return {
|
|
id: zohoProject.id,
|
|
name: zohoProject.name,
|
|
description: zohoProject.description,
|
|
status: zohoProject.status,
|
|
startDate: zohoProject.start_date,
|
|
endDate: zohoProject.end_date,
|
|
owner: zohoProject.owner?.name,
|
|
createdTime: zohoProject.created_time,
|
|
customFields: this.mapCustomFields(zohoProject)
|
|
};
|
|
}
|
|
|
|
// Map Zoho Projects Task to standardized format
|
|
static mapTask(zohoTask) {
|
|
return {
|
|
id: zohoTask.id,
|
|
name: zohoTask.name,
|
|
description: zohoTask.description,
|
|
status: zohoTask.status,
|
|
priority: zohoTask.priority,
|
|
startDate: zohoTask.start_date,
|
|
endDate: zohoTask.end_date,
|
|
assignee: zohoTask.assignee?.name,
|
|
project: zohoTask.project?.name,
|
|
customFields: this.mapCustomFields(zohoTask)
|
|
};
|
|
}
|
|
|
|
// Map custom fields from Zoho response
|
|
static mapCustomFields(zohoRecord) {
|
|
const customFields = {};
|
|
if (zohoRecord.Custom_Fields) {
|
|
zohoRecord.Custom_Fields.forEach(field => {
|
|
customFields[field.api_name] = field.value;
|
|
});
|
|
}
|
|
return customFields;
|
|
}
|
|
|
|
// Map multiple records using appropriate mapper
|
|
static mapRecords(records, recordType) {
|
|
const mapperMap = {
|
|
'leads': this.mapLead,
|
|
'contacts': this.mapContact,
|
|
'deals': this.mapDeal,
|
|
'employees': this.mapEmployee,
|
|
'projects': this.mapProject,
|
|
'tasks': this.mapTask
|
|
};
|
|
|
|
const mapper = mapperMap[recordType];
|
|
if (!mapper) {
|
|
throw new Error(`No mapper found for record type: ${recordType}`);
|
|
}
|
|
|
|
return records.map(record => mapper(record));
|
|
}
|
|
|
|
// Map Zoho API response to standardized format
|
|
static mapApiResponse(zohoResponse, recordType) {
|
|
const records = zohoResponse.data || [];
|
|
// const mappedRecords = this.mapRecords(records, recordType);
|
|
|
|
return {
|
|
data: records,
|
|
info: {
|
|
count: zohoResponse.info?.count || mappedRecords.length,
|
|
moreRecords: zohoResponse.info?.more_records || false,
|
|
page: zohoResponse.info?.page || 1
|
|
}
|
|
};
|
|
}
|
|
}
|
|
|
|
module.exports = ZohoMapper;
|