Skip to content

Commit 82c78e3

Browse files
committed
Patched datastream after hook to handle null uom field
1 parent 8227890 commit 82c78e3

File tree

4 files changed

+75
-37
lines changed

4 files changed

+75
-37
lines changed

dist/server/services/datastream/hooks/index.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,25 @@ exports.before = {
326326
remove: [auth.hooks.authenticate('jwt'), authHooks.restrictToRoles({
327327
roles: ['sys-admin']
328328
})]
329-
};
329+
330+
/**
331+
* HACK: Ensure uom is not null since populate({schema: convertibleToUomsSchema}) blows chunks.
332+
*/
333+
};function discardIfFalse(field) {
334+
return hook => {
335+
const items = commonHooks.getItems(hook);
336+
337+
if (Array.isArray(items)) {
338+
items.forEach(item => {
339+
if (!item[field]) delete item[field];
340+
});
341+
} else if (typeof items === 'object') {
342+
if (!items[field]) delete items[field];
343+
}
344+
345+
return hook;
346+
};
347+
}
330348

331349
const uomSchema = {
332350
include: {
@@ -358,7 +376,7 @@ const preferredUomsSchema = {
358376
};
359377

360378
exports.after = {
361-
all: [commonHooks.populate({ schema: uomSchema }), commonHooks.populate({ schema: convertibleToUomsSchema }), commonHooks.populate({ schema: preferredUomsSchema })]
379+
all: [commonHooks.populate({ schema: uomSchema }), discardIfFalse('uom'), commonHooks.populate({ schema: convertibleToUomsSchema }), commonHooks.populate({ schema: preferredUomsSchema })]
362380

363381
// find: [],
364382
// get: [],

package-lock.json

Lines changed: 31 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dendra-web-api",
3-
"version": "0.0.12",
3+
"version": "0.0.13",
44
"description": "Dendra public facing REST API.",
55
"license": "BSD-2-Clause-FreeBSD",
66
"author": "J. Scott Smith <[email protected]>",
@@ -35,7 +35,7 @@
3535
"@dendra-science/api-hooks-common": "0.0.1",
3636
"@dendra-science/math": "0.0.1",
3737
"@dendra-science/utils": "0.0.1",
38-
"ajv": "^5.5.0",
38+
"ajv": "^5.5.1",
3939
"babel-runtime": "^6.26.0",
4040
"body-parser": "^1.18.2",
4141
"compression": "^1.7.1",
@@ -60,15 +60,15 @@
6060
"passport": "^0.4.0",
6161
"pug": "^2.0.0-rc.4",
6262
"request": "^2.83.0",
63-
"sequelize": "^4.23.1",
63+
"sequelize": "^4.27.0",
6464
"winston": "^2.4.0"
6565
},
6666
"devDependencies": {
6767
"babel-cli": "^6.26.0",
6868
"babel-core": "^6.26.0",
6969
"babel-preset-es2015-node6": "^0.4.0",
7070
"chai": "^4.1.2",
71-
"eslint": "^4.12.0",
71+
"eslint": "^4.13.0",
7272
"eslint-config-standard": "^10.2.1",
7373
"eslint-plugin-import": "^2.8.0",
7474
"eslint-plugin-node": "^5.2.1",

src/server/services/datastream/hooks/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,25 @@ exports.before = {
380380
]
381381
}
382382

383+
/**
384+
* HACK: Ensure uom is not null since populate({schema: convertibleToUomsSchema}) blows chunks.
385+
*/
386+
function discardIfFalse (field) {
387+
return (hook) => {
388+
const items = commonHooks.getItems(hook)
389+
390+
if (Array.isArray(items)) {
391+
items.forEach(item => {
392+
if (!item[field]) delete item[field]
393+
})
394+
} else if (typeof items === 'object') {
395+
if (!items[field]) delete items[field]
396+
}
397+
398+
return hook
399+
}
400+
}
401+
383402
const uomSchema = {
384403
include: {
385404
service: 'uoms',
@@ -412,6 +431,7 @@ const preferredUomsSchema = {
412431
exports.after = {
413432
all: [
414433
commonHooks.populate({schema: uomSchema}),
434+
discardIfFalse('uom'),
415435
commonHooks.populate({schema: convertibleToUomsSchema}),
416436
commonHooks.populate({schema: preferredUomsSchema})
417437
]

0 commit comments

Comments
 (0)