Skip to content

Commit

Permalink
Merge pull request #51 from wednesday-solutions/feat/referenced-order…
Browse files Browse the repository at this point in the history
…s-refactor

Refactor referenced and unsharded referenced orders
  • Loading branch information
praveenkumar1798 authored Jun 1, 2022
2 parents 7bee315 + f9037a7 commit a3290ab
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 13 deletions.
10 changes: 3 additions & 7 deletions server/api/referencedOrders/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { apiFailure, apiSuccess } from 'utils/apiUtils';
import { fetchAllPurchasedProducts } from '../utils';

export const fetchAllReferencedOrders = (router, model, validator) => {
export const fetchAllReferencedOrders = (router, model, _validator) => {
router.use('/', async (req, res, next) => {
try {
const items = await model
.find()
.select('purchasedProducts')
.populate('purchasedProducts')
.skip(req.query.page * req.query.limit)
.limit(req.query.limit);
const items = await fetchAllPurchasedProducts(model, req.query);
return apiSuccess(res, items);
} catch (err) {
return apiFailure(res, err.message);
Expand Down
57 changes: 57 additions & 0 deletions server/api/tests/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
createItem,
createUser,
deleteItem,
fetchAllPurchasedProducts,
fetchItem,
fetchItems,
updateItem
Expand Down Expand Up @@ -178,4 +179,60 @@ describe('utils tests', () => {
expect(spy).toHaveBeenCalledWith({ err: error });
});
});
describe('fetchAllPurchasedProducts tests', () => {
let items;
let model;

beforeAll(() => {
items = [
{
name: 'Product 1'
},
{
name: 'Product 2'
}
];
});

beforeEach(() => {
model = {};
model.find = jest.fn().mockReturnValue(model);
model.select = jest.fn().mockReturnValue(model);
model.populate = jest.fn().mockReturnValue(model);
model.skip = jest.fn().mockReturnValue(model);
model.limit = jest.fn().mockResolvedValue(items);
});

it('should fetch all purchased products with no limit and page passed', async () => {
const resItems = await fetchAllPurchasedProducts(model, {});
expect(model.find).toBeCalled();
expect(model.select).toBeCalledWith('purchasedProducts');
expect(model.populate).toBeCalledWith('purchasedProducts');
expect(resItems).toEqual(items);
});

it('should fetch all purchased products with correct page and limit parameters', async () => {
const resItems = await fetchAllPurchasedProducts(model, {
limit: 1,
page: 1
});
expect(model.find).toBeCalled();
expect(model.select).toBeCalledWith('purchasedProducts');
expect(model.populate).toBeCalledWith('purchasedProducts');
expect(model.skip).toBeCalledWith(1);
expect(model.limit).toBeCalledWith(1);
expect(resItems).toEqual(items);
});

it('should throw error when fetchAllPurchasedProducts fail to fetch and return items', async () => {
const error = new Error('unable to fetch items');
const model = {
find: jest.fn(() => {
throw error;
})
};
expect(() => fetchItems(model, {})).rejects.toThrow(error);
expect(spy).toHaveBeenCalledWith({ err: error });
});
});
});
8 changes: 2 additions & 6 deletions server/api/unshardedReferencedOrders/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { apiFailure, apiSuccess } from 'utils/apiUtils';
import { fetchAllPurchasedProducts } from '../utils';

export const fetchAllUnshardedReferencedOrders = (app, model, name) => {
app.use('/', async (req, res, next) => {
try {
const items = await model
.find()
.select('purchasedProducts')
.populate('purchasedProducts')
.skip(req.query.page * req.query.limit)
.limit(req.query.limit);
const items = await fetchAllPurchasedProducts(model, req.query);
return apiSuccess(res, items);
} catch (err) {
return apiFailure(res, err.message);
Expand Down
8 changes: 8 additions & 0 deletions server/api/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,11 @@ export const createUser = async (model, args) => {
throw err;
}
};

export const fetchAllPurchasedProducts = async (model, query) =>
model
.find()
.select('purchasedProducts')
.populate('purchasedProducts')
.skip(query.page * query.limit)
.limit(query.limit);

0 comments on commit a3290ab

Please sign in to comment.