diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 1a51c77..b2b1ddd 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -456,6 +456,30 @@ paths: description: Server error schema: $ref: '#/definitions/ErrorModel' + '/lookups/devices/models': + get: + tags: + - 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': + description: OK + schema: + type: array + items: + type: string + '500': + description: Server error + schema: + $ref: '#/definitions/ErrorModel' '/lookups/countries': get: tags: @@ -1241,4 +1265,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..f540b57 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(req.query) + 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..38fd3d0 100644 --- a/src/services/DeviceService.js +++ b/src/services/DeviceService.js @@ -351,6 +351,27 @@ getManufacturers.schema = { }).required() } +/** + * Get distinct device models. + * @param {Object} criteria the search criteria + * @returns {Array} the distinct device models + */ +async function getDeviceModels (criteria) { + const result = [] + await iterateDevices(criteria, (device) => { + if (!_.includes(result, device.model)) { + result.push(device.model) + } + }) + return result +} + +getDeviceModels.schema = { + criteria: Joi.object().keys({ + type: Joi.string() + }) +} + module.exports = { list, getEntity, @@ -359,7 +380,8 @@ module.exports = { update, remove, getTypes, - getManufacturers + getManufacturers, + getDeviceModels } logger.buildService(module.exports)