Passed
Push — master ( a8bdc8...2feb85 )
by
unknown
01:06
created

src/router/device-type-api-router.js   A

Complexity

Total Complexity 22
Complexity/F 1.38

Size

Lines of Code 96
Function Count 16

Duplication

Duplicated Lines 96
Ratio 100 %

Test Coverage

Coverage 93.55%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
nc 6
dl 96
loc 96
ccs 29
cts 31
cp 0.9355
crap 0
rs 10
c 1
b 0
f 0
noi 2
wmc 22
mnd 1
bc 10
fnc 16
bpm 0.625
cpm 1.375

1 Function

Rating   Name   Duplication   Size   Complexity  
A device-type-api-router.js ➔ ??? 8 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
/**
2
 * deviceType Service API router
3
 *
4
 * @since 1.0.0
5
 */
6
7 4 View Code Duplication
const Joi = require('joi');
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
8 4
const DeviceType = require('./../repository/DeviceType');
9 4
const config = require('../config/server.config').url;
10 4
const dateUtil = require('../common/date-util');
11 4
const NotifierError = require('../common/Error');
12 4
const logger = require('winston');
0 ignored issues
show
Unused Code introduced by
The constant logger seems to be never used. Consider removing it.
Loading history...
13
14 4
module.exports = [
15
  {
16
    method: 'GET',
17
    path: `${config.deviceTypeApiPrefix}`,
18
    handler: (request, reply) => {
19 1
      DeviceType.find().then((list) => {
20 1
        reply({
21
          data: dateUtil.formatDates(list),
22
          totalCount: list.length,
23
        });
24
      }).catch(err => reply(err));
25
    },
26
  },
27
  {
28
    method: 'POST',
29
    path: `${config.deviceTypeApiPrefix}`,
30
    handler: (request, reply) => {
31 3
      const deviceType = Object.assign({}, request.payload);
32 3
      DeviceType.find({ value: deviceType.value })
33
        .then((result) => {
34 3
          if (result && result.length > 0) {
35 1
            throw new NotifierError(NotifierError.Types.CONFLICT, { value: deviceType.value });
36
          }
37 2
          return true;
38
        })
39 2
        .then(() => DeviceType.add(deviceType))
40 2
        .then(result => reply(result))
41 1
        .catch(err => reply(err));
42
    },
43
    config: {
44
      validate: {
45
        payload: {
46
          label: Joi.string().required(),
47
          value: Joi.string().required(),
48
          template: Joi.string(),
49
        },
50
      },
51
    },
52
  },
53
  {
54
    method: 'PUT',
55
    path: `${config.deviceTypeApiPrefix}/{deviceTypeId}`,
56
    handler: (request, reply) => {
57 2
      const deviceType = Object.assign({}, request.payload);
58
      let unset;
59 2
      if (!deviceType.template) {
60 2
        unset = { template: 1 };
61
      }
62 2
      DeviceType.find({ value: deviceType.value })
63
        .then((result) => {
64 2
          if (result && result.length > 0 && request.params.deviceTypeId !== result[0]._id) {
65 1
            throw new NotifierError(NotifierError.Types.CONFLICT, { value: deviceType.value });
66
          }
67 1
          return true;
68
        })
69 1
        .then(() => DeviceType.update(request.params.deviceTypeId, deviceType, unset))
0 ignored issues
show
Bug introduced by
The variable unset does not seem to be initialized in case !deviceType.template on line 59 is false. Are you sure the function update handles undefined variables?
Loading history...
70 1
        .then(result => reply(result))
71 1
        .catch(err => reply(err));
72
    },
73
    config: {
74
      validate: {
75
        params: {
76
          deviceTypeId: Joi.string().required(),
77
        },
78
        payload: {
79
          label: Joi.string().required(),
80
          value: Joi.string().required(),
81
          template: Joi.string(),
82
        },
83
      },
84
    },
85
  },
86
  {
87
    method: 'DELETE',
88
    path: `${config.deviceTypeApiPrefix}/{deviceTypeId}`,
89
    handler: (request, reply) => {
90 2
      DeviceType.remove(request.params.deviceTypeId)
91 2
        .then(result => reply(result))
92
        .catch(err => reply(err));
93
    },
94
    config: {
95
      validate: {
96
        params: {
97
          deviceTypeId: Joi.string().required(),
98
        },
99
      },
100
    },
101
  },
102
];
103