From 0656c42412a2ef58427913454c5d055a007030d0 Mon Sep 17 00:00:00 2001 From: meshde Date: Sun, 5 Apr 2020 23:33:49 +0530 Subject: [PATCH 1/2] Add endpoint to list distinct device models --- docs/swagger.yaml | 20 +++++++- docs/tc-lookup-api.postman_collection.json | 53 +++++++++++++++++++++- src/controllers/DeviceController.js | 13 +++++- src/routes.js | 7 +++ src/services/DeviceService.js | 17 ++++++- 5 files changed, 105 insertions(+), 5 deletions(-) diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 1a51c77..b030495 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -456,6 +456,24 @@ paths: description: Server error schema: $ref: '#/definitions/ErrorModel' + '/lookups/devices/models': + get: + tags: + - Device + description: | + List distinct device models. + + responses: + '200': + description: OK + schema: + type: array + items: + type: string + '500': + description: Server error + schema: + $ref: '#/definitions/ErrorModel' '/lookups/countries': get: tags: @@ -1241,4 +1259,4 @@ definitions: operatingSystem: type: string operatingSystemVersion: - type: string \ No newline at end of file + type: string diff --git a/docs/tc-lookup-api.postman_collection.json b/docs/tc-lookup-api.postman_collection.json index cdcc4f5..ba052b6 100644 --- a/docs/tc-lookup-api.postman_collection.json +++ b/docs/tc-lookup-api.postman_collection.json @@ -1,7 +1,7 @@ { "info": { - "_postman_id": "4802b022-4a66-43f5-b223-cbbcfd91ad9e", - "name": "tc-lookup-api", + "_postman_id": "b3587de7-1b41-4422-bb81-0f18a4efd0d3", + "name": "tc-lookup-api copy", "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" }, "item": [ @@ -1235,6 +1235,55 @@ }, "response": [] }, + { + "name": "get device models", + "event": [ + { + "listen": "test", + "script": { + "id": "fb283d16-ca1b-4b47-8e95-ed76324d2174", + "exec": [ + "pm.test(\"Status code is 200\", function () {", + " pm.response.to.have.status(200);", + "});" + ], + "type": "text/javascript" + } + } + ], + "protocolProfileBehavior": { + "disableBodyPruning": true + }, + "request": { + "method": "GET", + "header": [ + { + "key": "Accept", + "value": "application/json" + }, + { + "key": "Content-Type", + "value": "application/json" + } + ], + "body": { + "mode": "raw", + "raw": "" + }, + "url": { + "raw": "{{URL}}/lookups/devices/models", + "host": [ + "{{URL}}" + ], + "path": [ + "lookups", + "devices", + "models" + ] + } + }, + "response": [] + }, { "name": "get device manufacturers", "event": [ diff --git a/src/controllers/DeviceController.js b/src/controllers/DeviceController.js index d137114..0077316 100644 --- a/src/controllers/DeviceController.js +++ b/src/controllers/DeviceController.js @@ -107,6 +107,16 @@ async function getManufacturers (req, res) { res.send(result) } +/** + * Get distinct device models + * @param {Object} req the request + * @param {Object} res the response + */ +async function getDeviceModels (req, res) { + const result = await service.getDeviceModels() + res.send(result) +} + module.exports = { list, listHead, @@ -117,5 +127,6 @@ module.exports = { partiallyUpdate, remove, getTypes, - getManufacturers + getManufacturers, + getDeviceModels } diff --git a/src/routes.js b/src/routes.js index 745dfd1..5bd086b 100644 --- a/src/routes.js +++ b/src/routes.js @@ -146,6 +146,13 @@ module.exports = { // any role / scope is allowed } }, + '/lookups/devices/models': { + get: { + controller: 'DeviceController', + method: 'getDeviceModels' + // any role / scope is allowed + } + }, '/lookups/devices/:id': { get: { controller: 'DeviceController', diff --git a/src/services/DeviceService.js b/src/services/DeviceService.js index dc8df86..05e4ae1 100644 --- a/src/services/DeviceService.js +++ b/src/services/DeviceService.js @@ -351,6 +351,20 @@ getManufacturers.schema = { }).required() } +/** + * Get distinct device models. + * @returns {Array} the distinct device models + */ +async function getDeviceModels () { + const result = [] + await iterateDevices({}, (device) => { + if (!_.includes(result, device.model)) { + result.push(device.model) + } + }) + return result +} + module.exports = { list, getEntity, @@ -359,7 +373,8 @@ module.exports = { update, remove, getTypes, - getManufacturers + getManufacturers, + getDeviceModels } logger.buildService(module.exports) From b69e298bd1044d42817e8307edc6dfb329eaebee Mon Sep 17 00:00:00 2001 From: meshde Date: Sun, 5 Apr 2020 23:41:21 +0530 Subject: [PATCH 2/2] Add support for type filters --- docs/swagger.yaml | 6 ++++++ src/controllers/DeviceController.js | 2 +- src/services/DeviceService.js | 11 +++++++++-- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/docs/swagger.yaml b/docs/swagger.yaml index b030495..b2b1ddd 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -462,6 +462,12 @@ paths: - Device description: | List distinct device models. + parameters: + - name: type + in: query + description: Filter by device type, case insensitive, partial match is used + required: false + type: string responses: '200': diff --git a/src/controllers/DeviceController.js b/src/controllers/DeviceController.js index 0077316..f540b57 100644 --- a/src/controllers/DeviceController.js +++ b/src/controllers/DeviceController.js @@ -113,7 +113,7 @@ async function getManufacturers (req, res) { * @param {Object} res the response */ async function getDeviceModels (req, res) { - const result = await service.getDeviceModels() + const result = await service.getDeviceModels(req.query) res.send(result) } diff --git a/src/services/DeviceService.js b/src/services/DeviceService.js index 05e4ae1..38fd3d0 100644 --- a/src/services/DeviceService.js +++ b/src/services/DeviceService.js @@ -353,11 +353,12 @@ getManufacturers.schema = { /** * Get distinct device models. + * @param {Object} criteria the search criteria * @returns {Array} the distinct device models */ -async function getDeviceModels () { +async function getDeviceModels (criteria) { const result = [] - await iterateDevices({}, (device) => { + await iterateDevices(criteria, (device) => { if (!_.includes(result, device.model)) { result.push(device.model) } @@ -365,6 +366,12 @@ async function getDeviceModels () { return result } +getDeviceModels.schema = { + criteria: Joi.object().keys({ + type: Joi.string() + }) +} + module.exports = { list, getEntity,