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

Commit

Permalink
feat(customers): added code field
Browse files Browse the repository at this point in the history
close #631
  • Loading branch information
Buyantogtokh authored and batamar committed Nov 19, 2019
1 parent 09c5f99 commit 7a696d4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/data/schema/customer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export const types = `
hasAuthority: String
description: String
doNotDisturb: String
code: String
integration: Integration
links: CustomerLinks
Expand Down Expand Up @@ -112,6 +113,7 @@ const fields = `
doNotDisturb: String
links: JSON
customFieldsData: JSON
code: String
`;

export const mutations = `
Expand Down
2 changes: 2 additions & 0 deletions src/db/factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ interface ICustomerFactoryInput {
ownerId?: string;
hasValidEmail?: boolean;
profileScore?: number;
code?: string;
}

export const customerFactory = (params: ICustomerFactoryInput = {}, useModelMethod = false) => {
Expand All @@ -450,6 +451,7 @@ export const customerFactory = (params: ICustomerFactoryInput = {}, useModelMeth
ownerId: params.ownerId || Random.id(),
hasValidEmail: params.hasValidEmail || false,
profileScore: params.profileScore || 0,
code: params.code || faker.random.word(),
};

if (useModelMethod) {
Expand Down
30 changes: 24 additions & 6 deletions src/db/models/Customers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { IUserDocument } from './definitions/users';
interface ICustomerFieldsInput {
primaryEmail?: string;
primaryPhone?: string;
code?: string;
}

export interface ICustomerModel extends Model<ICustomerDocument> {
Expand Down Expand Up @@ -80,6 +81,18 @@ export const loadClass = () => {
throw new Error('Duplicated phone');
}
}

if (customerFields.code) {
// check duplication from code
previousEntry = await Customers.find({
...query,
code: customerFields.code,
});

if (previousEntry.length > 0) {
throw new Error('Duplicated code');
}
}
}

/**
Expand Down Expand Up @@ -195,21 +208,26 @@ export const loadClass = () => {
let score = 0;
let searchText = (customer.emails || []).join(' ').concat(' ', (customer.phones || []).join(' '));

if (customer.firstName && !nullValues.includes(customer.firstName || '')) {
if (!nullValues.includes(customer.firstName || '')) {
score += 10;
searchText = searchText.concat(' ', customer.firstName);
searchText = searchText.concat(' ', customer.firstName || '');
}

if (customer.lastName && !nullValues.includes(customer.lastName || '')) {
if (!nullValues.includes(customer.lastName || '')) {
score += 5;
searchText = searchText.concat(' ', customer.lastName);
searchText = searchText.concat(' ', customer.lastName || '');
}

if (!nullValues.includes(customer.code || '')) {
score += 10;
searchText = searchText.concat(' ', customer.code || '');
}

if (customer.primaryEmail && !nullValues.includes(customer.primaryEmail || '')) {
if (!nullValues.includes(customer.primaryEmail || '')) {
score += 15;
}

if (customer.primaryPhone && !nullValues.includes(customer.primaryPhone || '')) {
if (!nullValues.includes(customer.primaryPhone || '')) {
score += 10;
}

Expand Down
2 changes: 2 additions & 0 deletions src/db/models/definitions/customers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export interface ICustomer {
visitorContactInfo?: IVisitorContact;
urlVisits?: any;
deviceTokens?: string[];
code?: string;
}

export interface ICustomerDocument extends ICustomer, Document {
Expand Down Expand Up @@ -230,5 +231,6 @@ export const customerSchema = schemaWrapper(

deviceTokens: field({ type: [String], default: [] }),
searchText: field({ type: String, optional: true, index: true }),
code: field({ type: String, label: 'Code', optional: true }),
}),
);

0 comments on commit 7a696d4

Please sign in to comment.