Skip to content

Commit

Permalink
feat: add order by to units
Browse files Browse the repository at this point in the history
  • Loading branch information
ec2sw committed May 24, 2022
1 parent d73fab3 commit bebe6af
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/controllers/units.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export const create = async (req, res) => {

export const findAll = async (req, res) => {
try {
let { page, limit, columns, orgUid, search, xls } = req.query;
let { page, limit, columns, orgUid, search, xls, order } = req.query;
let where = orgUid != null && orgUid !== 'all' ? { orgUid } : undefined;

const includes = Unit.getAssociatedModels();
Expand Down Expand Up @@ -160,11 +160,20 @@ export const findAll = async (req, res) => {
};
}

// default to DESC
let resultOrder = [['timeStaged', 'DESC']];

if (order && order === 'SERIALNUMBER') {
resultOrder = [['serialNumberBlock', 'ASC']];
} else if (order && order === 'ASC') {
resultOrder = [['timeStaged', 'ASC']];
}

if (!results) {
results = await Unit.findAndCountAll({
where,
distinct: true,
order: [['timeStaged', 'DESC']],
order: resultOrder,
...columnsToInclude(columns, includes),
...paginationParams(page, limit),
});
Expand Down
1 change: 1 addition & 0 deletions src/validations/units.validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export const unitsGetQuerySchema = Joi.object()
warehouseUnitId: Joi.string(),
columns: Joi.array().items(Joi.string()).single(),
orgUid: Joi.string(),
order: Joi.string().valid('SERIALNUMBER', 'ASC', 'DESC'),
xls: Joi.boolean(),
})
.with('page', 'limit');
Expand Down
27 changes: 27 additions & 0 deletions tests/resources/units.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import _ from 'lodash';
import supertest from 'supertest';
import { expect } from 'chai';
import sinon from 'sinon';
Expand Down Expand Up @@ -47,6 +48,7 @@ describe('Units Resource CRUD', function () {

expect(result.body.length).to.not.equal(0);
}).timeout(TEST_WAIT_TIME * 10);

it('gets all the units filtered by orgUid', async function () {
const result = await supertest(app)
.get('/v1/units')
Expand All @@ -55,6 +57,31 @@ describe('Units Resource CRUD', function () {
expect(result.body.length).to.not.equal(1);
// ?orgUid=XXXX
}).timeout(TEST_WAIT_TIME * 10);

it('orders by serial number', async function () {
const newUnit1 = _.cloneDeep(newUnit);
newUnit1.unitBlockStart = 'A1';
newUnit1.unitBlockEnd = 'A2';
await testFixtures.createNewUnit(newUnit1);

const newUnit2 = _.cloneDeep(newUnit);
newUnit2.unitBlockStart = 'B1';
newUnit2.unitBlockEnd = 'B2';
await testFixtures.createNewUnit(newUnit2);
await testFixtures.commitStagingRecords();
await testFixtures.waitForDataLayerSync();

const result = await supertest(app)
.get('/v1/units')
.query({ order: 'SERIALNUMBER' });

expect(result.body[0].serialNumberBlock).to.equal('A1-A2');
expect(result.body[1].serialNumberBlock).to.equal(
'AXJJFSLGHSHEJ1000-AXJJFSLGHSHEJ1010',
);
expect(result.body[2].serialNumberBlock).to.equal('B1-B2');
}).timeout(TEST_WAIT_TIME * 10);

it('gets all the units for a search term', async function () {
// ?search=XXXX
const result = await supertest(app)
Expand Down

0 comments on commit bebe6af

Please sign in to comment.