diff --git a/server/api/referencedOrders/index.js b/server/api/referencedOrders/index.js index deac6c7..c41136f 100644 --- a/server/api/referencedOrders/index.js +++ b/server/api/referencedOrders/index.js @@ -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); diff --git a/server/api/tests/utils.test.js b/server/api/tests/utils.test.js index 57753e7..d89cd04 100644 --- a/server/api/tests/utils.test.js +++ b/server/api/tests/utils.test.js @@ -2,6 +2,7 @@ import { createItem, createUser, deleteItem, + fetchAllPurchasedProducts, fetchItem, fetchItems, updateItem @@ -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 }); + }); + }); }); diff --git a/server/api/unshardedReferencedOrders/index.js b/server/api/unshardedReferencedOrders/index.js index eecee39..883d286 100644 --- a/server/api/unshardedReferencedOrders/index.js +++ b/server/api/unshardedReferencedOrders/index.js @@ -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); diff --git a/server/api/utils.js b/server/api/utils.js index 26497be..41276a3 100644 --- a/server/api/utils.js +++ b/server/api/utils.js @@ -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);