Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add distinct device models endpoint #23

Merged
merged 2 commits into from
Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -1241,4 +1265,4 @@ definitions:
operatingSystem:
type: string
operatingSystemVersion:
type: string
type: string
53 changes: 51 additions & 2 deletions docs/tc-lookup-api.postman_collection.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down Expand Up @@ -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": [
Expand Down
13 changes: 12 additions & 1 deletion src/controllers/DeviceController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -117,5 +127,6 @@ module.exports = {
partiallyUpdate,
remove,
getTypes,
getManufacturers
getManufacturers,
getDeviceModels
}
7 changes: 7 additions & 0 deletions src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
24 changes: 23 additions & 1 deletion src/services/DeviceService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -359,7 +380,8 @@ module.exports = {
update,
remove,
getTypes,
getManufacturers
getManufacturers,
getDeviceModels
}

logger.buildService(module.exports)