app/services/catering/category.js   A
last analyzed

Complexity

Total Complexity 10
Complexity/F 2.5

Size

Lines of Code 66
Function Count 4

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 0
wmc 10
eloc 39
c 0
b 0
f 0
nc 4
mnd 1
bc 7
fnc 4
dl 0
loc 66
rs 10
bpm 1.75
cpm 2.5
noi 2

4 Functions

Rating   Name   Duplication   Size   Complexity  
A module.exports.edit 0 16 3
A module.exports.detail 0 16 4
A module.exports.add 0 19 2
A module.exports.list 0 4 1
1
const ModelCategory = require('../../model/catering/category')
2
const ApiError = require('../../util/api_error')
3
const Validate = require('request-validate')
4
const _ = require('underscore')
5
6
module.exports = {
7
    list: async function (storeId, filter = {}) {
8
        let result = await ModelCategory.list(storeId, filter)
9
        return result
10
    },
11
12
    detail: async function (storeId, categoryId) {
13
        let result = await ModelCategory.first(categoryId)
14
        if (_.isEmpty(result)) {
15
            throw new ApiError('common.notExist', 'category')
16
        }
17
18
        if (result.store_id != storeId) {
19
            throw new ApiError('common.notExist', 'category')
20
        }
21
22
        if (result.status == -1) {
23
            throw new ApiError('common.notExist', 'category')
24
        }
25
26
        return result
27
    },
28
29
    add: async function (storeId, data) {
30
        Validate(data, {
31
            name: 'required',
32
        })
33
34
        let maxSortData = await ModelCategory.getMaxSort(storeId)
35
        let maxSort = 0
36
        if (!_.isEmpty(maxSortData)) maxSort = parseInt(maxSortData.sort)
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
37
38
        let insertData = {
39
            store_id: storeId,
40
            name: data.name,
41
            sort: maxSort + 1,
42
        }
43
        let insertResult = await ModelCategory.add(insertData)
44
        insertData.id = insertResult.insertId
45
46
        return insertData
47
    },
48
49
    edit: async function (storeId, id, data) {
50
        let detail = await this.detail(storeId, id)
51
52
        let where = {
53
            id: id
54
        }
55
56
        let updateData = {
57
            name: _.has(data, 'name') ? data.name : detail.name,
58
            status: _.has(data, 'status') ? data.status : detail.status,
59
        }
60
61
        let editResult = await ModelCategory.edit(updateData, where)
0 ignored issues
show
Unused Code introduced by
The variable editResult seems to be never used. Consider removing it.
Loading history...
62
63
        return updateData
64
    },
65
66
}
67