Skip to content
This repository has been archived by the owner on Nov 21, 2020. It is now read-only.

Commit

Permalink
feat(customer/company): merge customFieldsData
Browse files Browse the repository at this point in the history
  • Loading branch information
batamar committed Nov 11, 2019
1 parent d36ff05 commit b4f3e46
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/db/models/Companies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ export const loadClass = () => {
await this.checkDuplication(companyFields, companyIds);

let scopeBrandIds: string[] = [];
let customFieldsData = {};
let tagIds: string[] = [];
let names: string[] = [];
let emails: string[] = [];
Expand All @@ -180,6 +181,9 @@ export const loadClass = () => {
// Merging scopeBrandIds
scopeBrandIds = [...scopeBrandIds, ...(companyObj.scopeBrandIds || [])];

// merge custom fields data
customFieldsData = { ...customFieldsData, ...(companyObj.customFieldsData || {}) };

// Merging company's tag into 1 array
tagIds = tagIds.concat(companyTags);

Expand Down Expand Up @@ -208,6 +212,7 @@ export const loadClass = () => {
const company = await Companies.createCompany({
...companyFields,
scopeBrandIds,
customFieldsData,
tagIds,
mergedIds: companyIds,
names,
Expand Down
6 changes: 5 additions & 1 deletion src/db/models/Customers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ export const loadClass = () => {

let scopeBrandIds: string[] = [];
let tagIds: string[] = [];
let customFieldsData = {};

let emails: string[] = [];
let phones: string[] = [];
Expand All @@ -273,14 +274,16 @@ export const loadClass = () => {
phones.push(customerFields.primaryPhone);
}

// Merging customer tags and companies
for (const customerId of customerIds) {
const customerObj = await Customers.findOne({ _id: customerId });

if (customerObj) {
// get last customer's integrationId
customerFields.integrationId = customerObj.integrationId;

// merge custom fields data
customFieldsData = { ...customFieldsData, ...(customerObj.customFieldsData || {}) };

// Merging scopeBrandIds
scopeBrandIds = [...scopeBrandIds, ...(customerObj.scopeBrandIds || [])];

Expand Down Expand Up @@ -309,6 +312,7 @@ export const loadClass = () => {
const customer = await this.createCustomer({
...customerFields,
scopeBrandIds,
customFieldsData,
tagIds,
mergedIds: customerIds,
emails,
Expand Down

0 comments on commit b4f3e46

Please sign in to comment.