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

Commit

Permalink
poscCustomers query fix
Browse files Browse the repository at this point in the history
  • Loading branch information
munkhsaikhan committed Oct 11, 2023
1 parent c4c99e0 commit ba85977
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 67 deletions.
6 changes: 3 additions & 3 deletions packages/plugin-posclient-api/src/graphql/resolvers/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ export default {
_id: company._id,
code: company.code,
primaryPhone: company.primaryPhone,
firstName: company.primaryName,
primaryEmail: company.primaryEmail,
firstName: company.primaryName,
lastName: ''
};
}
Expand All @@ -52,8 +52,8 @@ export default {
_id: user._id,
code: user.code,
primaryPhone: (user.details && user.details.operatorPhone) || '',
firstName: `${user.firstName || ''} ${user.lastName || ''}`,
primaryEmail: user.email,
firstName: `${user.firstName || ''} ${user.lastName || ''}`,
lastName: user.username
};
}
Expand All @@ -70,8 +70,8 @@ export default {
_id: customer._id,
code: customer.code,
primaryPhone: customer.primaryPhone,
firstName: customer.firstName,
primaryEmail: customer.primaryEmail,
firstName: customer.firstName,
lastName: customer.lastName
};
},
Expand Down
163 changes: 125 additions & 38 deletions packages/plugin-posclient-api/src/graphql/resolvers/queries/bridges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,146 @@ import { sendContactsMessage, sendCoreMessage } from '../../../messageBroker';
import { IContext } from '../../types';

interface IListArgs {
searchValue?: string;
searchValue: string;
type?: string;
perPage?: number;
page?: number;
}

const bridgesQueries = {
async poscCustomers(
_root,
{ searchValue }: IListArgs,
{ searchValue, type, perPage, page }: IListArgs,
{ subdomain }: IContext
) {
const filter: any = {};
const limit = perPage || 20;
const skip = ((page || 1) - 1) * limit;

if (searchValue) {
const regex = new RegExp(`${searchValue}`, 'i');
if (type === 'company') {
const companies = await sendContactsMessage({
subdomain,
action: 'companies.findActiveCompanies',
data: {
selector: {
$or: [
{ _id: searchValue },
{ code: searchValue },
{ primaryName: { $regex: searchValue, $options: 'i' } },
{ primaryEmail: { $regex: searchValue, $options: 'i' } },
{ primaryPhone: { $regex: searchValue, $options: 'i' } }
]
},
fields: {
_id: 1,
code: 1,
primaryPhone: 1,
primaryEmail: 1,
primaryName: 1
},
skip,
limit
},
isRPC: true
});
console.log(companies.length);
if (companies) {
return companies.map(company => ({
_id: company._id,
code: company.code,
primaryPhone: company.primaryPhone,
primaryEmail: company.primaryEmail,
firstName: company.primaryName,
lastName: ''
}));
}
return [];
}

filter.$or = [
{ primaryEmail: regex },
{ firstName: regex },
{ primaryPhone: regex },
{ lastName: regex }
];
if (type === 'user') {
const users = await sendCoreMessage({
subdomain,
action: 'users.find',
data: {
query: {
$or: [
{ _id: searchValue },
{ code: searchValue },
{ employeeId: searchValue },
{ email: { $regex: searchValue, $options: 'i' } },
{ username: { $regex: searchValue, $options: 'i' } },
{
'details.operatorPhone': { $regex: searchValue, $options: 'i' }
}
]
},
fields: {
_id: 1,
code: 1,
details: 1,
email: 1,
firstName: 1,
lastName: 1,
userName: 1
},
skip,
limit
},
isRPC: true
});

if (users) {
return users.map(user => ({
_id: user._id,
code: user.code,
primaryPhone: (user.details && user.details.operatorPhone) || '',
primaryEmail: user.email,
firstName: `${user.firstName || ''} ${user.lastName || ''}`,
lastName: user.username
}));
}
return [];
}

return sendContactsMessage({
const customers = await sendContactsMessage({
subdomain,
action: 'customers.find',
action: 'customers.findActiveCustomers',
data: {
selector: filter,
selector: {
$or: [
{ _id: searchValue },
{ code: searchValue },
{ primaryPhone: { $regex: searchValue, $options: 'i' } },
{ primaryEmail: { $regex: searchValue, $options: 'i' } },
{ firstName: { $regex: searchValue, $options: 'i' } },
{ lastName: { $regex: searchValue, $options: 'i' } },
{ middleName: { $regex: searchValue, $options: 'i' } }
]
},
fields: {
_id: 1,
state: 1,
createdAt: 1,
modifiedAt: 1,
avatar: 1,
firstName: 1,
lastName: 1,
middleName: 1,
birthDate: 1,
sex: 1,
email: 1,
primaryEmail: 1,
emails: 1,
primaryPhone: 1,
phones: 1,
phone: 1,
tagIds: 1,
code: 1,
primaryAddress: 1,
addresses: 1
}
primaryPhone: 1,
primaryEmail: 1,
firstName: 1,
lastName: 1
},
skip,
limit
},
isRPC: true,
defaultValue: []
isRPC: true
});

if (customers) {
return customers.map(customer => ({
_id: customer._id,
code: customer.code,
primaryPhone: customer.primaryPhone,
primaryEmail: customer.primaryEmail,
firstName: customer.firstName,
lastName: customer.lastName
}));
}
return;
},

async poscCustomerDetail(
Expand Down Expand Up @@ -84,8 +171,8 @@ const bridgesQueries = {
_id: company._id,
code: company.code,
primaryPhone: company.primaryPhone,
firstName: company.primaryName,
primaryEmail: company.primaryEmail,
firstName: company.primaryName,
lastName: ''
};
}
Expand Down Expand Up @@ -114,8 +201,8 @@ const bridgesQueries = {
_id: user._id,
code: user.code,
primaryPhone: (user.details && user.details.operatorPhone) || '',
firstName: `${user.firstName || ''} ${user.lastName || ''}`,
primaryEmail: user.email,
firstName: `${user.firstName || ''} ${user.lastName || ''}`,
lastName: user.username
};
}
Expand All @@ -139,8 +226,8 @@ const bridgesQueries = {
_id: customer._id,
code: customer.code,
primaryPhone: customer.primaryPhone,
firstName: customer.firstName,
primaryEmail: customer.primaryEmail,
firstName: customer.firstName,
lastName: customer.lastName
};
}
Expand Down
27 changes: 2 additions & 25 deletions packages/plugin-posclient-api/src/graphql/schema/bridges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,16 @@ export const types = `
_id: String!
code: String
primaryPhone: String
firstName: String
primaryEmail: String
firstName: String
lastName: String
primaryAddress: JSON
addresses: [JSON]
}
`;

const queryParams = `
page: Int
perPage: Int
segment: String
type: String
tag: String
ids: [String]
excludeIds: Boolean
searchValue: String
autoCompletion: Boolean
autoCompletionType: String
brand: String
integration: String
form: String
startDate: String
endDate: String
leadStatus: String
sortField: String
sortDirection: Int
sex: Int
birthDate: Date
`;

export const queries = `
poscCustomers(${queryParams}): [PosCustomer]
poscCustomers(searchValue: String!, type: String, perPage: Int, page: Int): [PosCustomer]
poscCustomerDetail(_id: String!, type: String): PosCustomer
`;

Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-posclient-api/src/subscriptions/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ export const types = `
_id: String!
code: String
primaryPhone: String
firstName: String
primaryEmail: String
firstName: String
lastName: String
primaryAddress: JSON
addresses: [JSON]
Expand Down

0 comments on commit ba85977

Please sign in to comment.