Skip to content

Commit a3290ab

Browse files
Merge pull request #51 from wednesday-solutions/feat/referenced-orders-refactor
Refactor referenced and unsharded referenced orders
2 parents 7bee315 + f9037a7 commit a3290ab

File tree

4 files changed

+70
-13
lines changed

4 files changed

+70
-13
lines changed

server/api/referencedOrders/index.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
import { apiFailure, apiSuccess } from 'utils/apiUtils';
2+
import { fetchAllPurchasedProducts } from '../utils';
23

3-
export const fetchAllReferencedOrders = (router, model, validator) => {
4+
export const fetchAllReferencedOrders = (router, model, _validator) => {
45
router.use('/', async (req, res, next) => {
56
try {
6-
const items = await model
7-
.find()
8-
.select('purchasedProducts')
9-
.populate('purchasedProducts')
10-
.skip(req.query.page * req.query.limit)
11-
.limit(req.query.limit);
7+
const items = await fetchAllPurchasedProducts(model, req.query);
128
return apiSuccess(res, items);
139
} catch (err) {
1410
return apiFailure(res, err.message);

server/api/tests/utils.test.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
createItem,
33
createUser,
44
deleteItem,
5+
fetchAllPurchasedProducts,
56
fetchItem,
67
fetchItems,
78
updateItem
@@ -178,4 +179,60 @@ describe('utils tests', () => {
178179
expect(spy).toHaveBeenCalledWith({ err: error });
179180
});
180181
});
182+
describe('fetchAllPurchasedProducts tests', () => {
183+
let items;
184+
let model;
185+
186+
beforeAll(() => {
187+
items = [
188+
{
189+
name: 'Product 1'
190+
},
191+
{
192+
name: 'Product 2'
193+
}
194+
];
195+
});
196+
197+
beforeEach(() => {
198+
model = {};
199+
model.find = jest.fn().mockReturnValue(model);
200+
model.select = jest.fn().mockReturnValue(model);
201+
model.populate = jest.fn().mockReturnValue(model);
202+
model.skip = jest.fn().mockReturnValue(model);
203+
model.limit = jest.fn().mockResolvedValue(items);
204+
});
205+
206+
it('should fetch all purchased products with no limit and page passed', async () => {
207+
const resItems = await fetchAllPurchasedProducts(model, {});
208+
expect(model.find).toBeCalled();
209+
expect(model.select).toBeCalledWith('purchasedProducts');
210+
expect(model.populate).toBeCalledWith('purchasedProducts');
211+
expect(resItems).toEqual(items);
212+
});
213+
214+
it('should fetch all purchased products with correct page and limit parameters', async () => {
215+
const resItems = await fetchAllPurchasedProducts(model, {
216+
limit: 1,
217+
page: 1
218+
});
219+
expect(model.find).toBeCalled();
220+
expect(model.select).toBeCalledWith('purchasedProducts');
221+
expect(model.populate).toBeCalledWith('purchasedProducts');
222+
expect(model.skip).toBeCalledWith(1);
223+
expect(model.limit).toBeCalledWith(1);
224+
expect(resItems).toEqual(items);
225+
});
226+
227+
it('should throw error when fetchAllPurchasedProducts fail to fetch and return items', async () => {
228+
const error = new Error('unable to fetch items');
229+
const model = {
230+
find: jest.fn(() => {
231+
throw error;
232+
})
233+
};
234+
expect(() => fetchItems(model, {})).rejects.toThrow(error);
235+
expect(spy).toHaveBeenCalledWith({ err: error });
236+
});
237+
});
181238
});

server/api/unshardedReferencedOrders/index.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
import { apiFailure, apiSuccess } from 'utils/apiUtils';
2+
import { fetchAllPurchasedProducts } from '../utils';
23

34
export const fetchAllUnshardedReferencedOrders = (app, model, name) => {
45
app.use('/', async (req, res, next) => {
56
try {
6-
const items = await model
7-
.find()
8-
.select('purchasedProducts')
9-
.populate('purchasedProducts')
10-
.skip(req.query.page * req.query.limit)
11-
.limit(req.query.limit);
7+
const items = await fetchAllPurchasedProducts(model, req.query);
128
return apiSuccess(res, items);
139
} catch (err) {
1410
return apiFailure(res, err.message);

server/api/utils.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,11 @@ export const createUser = async (model, args) => {
6262
throw err;
6363
}
6464
};
65+
66+
export const fetchAllPurchasedProducts = async (model, query) =>
67+
model
68+
.find()
69+
.select('purchasedProducts')
70+
.populate('purchasedProducts')
71+
.skip(query.page * query.limit)
72+
.limit(query.limit);

0 commit comments

Comments
 (0)