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

Commit

Permalink
feat(users): filter by brand
Browse files Browse the repository at this point in the history
close #681
  • Loading branch information
Buyantogtokh authored and batamar committed Jan 13, 2020
1 parent b5f4160 commit 9dca98e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
35 changes: 31 additions & 4 deletions src/__tests__/userQueries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,39 @@ describe('userQueries', () => {
await Conversations.deleteMany({});
});

test('Users', async () => {
test('Test users()', async () => {
// Creating test data
const user1 = await userFactory({ email: 'example@email.com' });
const brand = await brandFactory();
const user1 = await userFactory({ email: 'example@email.com', brandIds: [brand._id] });
const user2 = await userFactory();
const user3 = await userFactory({ isActive: false });
await userFactory({ registrationToken: 'token' });

const paramDefs = `
$page: Int,
$perPage: Int,
$searchValue: String,
$requireUsername: Boolean,
$isActive: Boolean,
$ids: [String],
$status: String,
$brandIds: [String]
`;

const paramValues = `
page: $page,
perPage: $perPage,
searchValue: $searchValue,
requireUsername: $requireUsername,
isActive: $isActive,
ids: $ids,
status: $status,
brandIds: $brandIds
`;

const qry = `
query users($page: Int $perPage: Int $searchValue: String $requireUsername: Boolean $isActive: Boolean $ids: [String] $status: String) {
users(page: $page perPage: $perPage searchValue: $searchValue requireUsername: $requireUsername isActive: $isActive ids: $ids status: $status) {
query users(${paramDefs}) {
users(${paramValues}) {
_id
}
}
Expand Down Expand Up @@ -52,6 +75,10 @@ describe('userQueries', () => {

// 6 in graphRequest + above 2
expect(response.length).toBe(8);

response = await graphqlRequest(qry, 'users', { brandIds: [brand._id] });

expect(response.length).toBe(1);
});

test('All users', async () => {
Expand Down
7 changes: 6 additions & 1 deletion src/data/resolvers/queries/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ interface IListArgs {
ids?: string[];
email?: string;
status?: string;
brandIds?: string[];
}

const queryBuilder = async (params: IListArgs) => {
const { searchValue, isActive, requireUsername, ids, status } = params;
const { searchValue, isActive, requireUsername, ids, status, brandIds } = params;

const selector: any = {
isActive,
Expand Down Expand Up @@ -47,6 +48,10 @@ const queryBuilder = async (params: IListArgs) => {
selector.registrationToken = { $eq: null };
}

if (brandIds && brandIds.length > 0) {
selector.brandIds = { $in: brandIds };
}

return selector;
};

Expand Down
3 changes: 2 additions & 1 deletion src/data/schema/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ const commonSelector = `
searchValue: String,
isActive: Boolean,
requireUsername: Boolean,
ids: [String]
ids: [String],
brandIds: [String]
`;

export const queries = `
Expand Down

0 comments on commit 9dca98e

Please sign in to comment.