1 | /** |
||
2 | * Status Service API router |
||
3 | * |
||
4 | * @since 1.0.0 |
||
5 | */ |
||
6 | |||
7 | 4 | const Joi = require('joi'); |
|
8 | 4 | const Status = require('./../repository/Status'); |
|
9 | 4 | const config = require('../config/server.config').url; |
|
10 | 4 | const dateUtil = require('../common/date-util'); |
|
11 | 4 | const logger = require('winston'); |
|
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
12 | |||
13 | 4 | module.exports = [ |
|
14 | { |
||
15 | method: 'GET', |
||
16 | path: `${config.statusApiPrefix}`, |
||
17 | handler: (request, reply) => { |
||
18 | let filter = {}; |
||
19 | 2 | if (request.query.filter === 'current') { // 미래 포함 |
|
20 | filter = { |
||
21 | $or: [ |
||
22 | { endTime: { $gt: new Date() } }, |
||
23 | { startTime: { $exists: false }, endTime: { $exists: false } }, |
||
24 | ], |
||
25 | }; |
||
26 | 2 | } else if (request.query.filter === 'expired') { |
|
27 | filter = { endTime: { $lte: new Date() } }; |
||
28 | } |
||
29 | Promise.all([ |
||
30 | Status.find(filter, { isActivated: -1, startTime: 1, endTime: 1, createTime: 1 }, request.query.skip, request.query.limit), |
||
31 | Status.count(filter), |
||
32 | ]).then(([list, totalCount]) => { |
||
33 | reply({ |
||
34 | data: dateUtil.formatDates(list), |
||
35 | totalCount, |
||
36 | }); |
||
37 | }).catch(err => reply(err)); |
||
38 | }, |
||
39 | config: { |
||
40 | validate: { |
||
41 | query: { |
||
42 | filter: Joi.any().valid('current', 'expired'), |
||
43 | skip: Joi.number(), |
||
44 | limit: Joi.number(), |
||
45 | }, |
||
46 | }, |
||
47 | }, |
||
48 | }, |
||
49 | { |
||
50 | method: 'GET', |
||
51 | path: `${config.statusApiPrefix}/check`, |
||
52 | handler: (request, reply) => { |
||
53 | 6 | Status.findWithComparators(request.query.deviceType || '*', request.query.deviceVersion || '*', request.query.appVersion || '*', { startTime: 1 }) |
|
54 | 4 | .then(result => dateUtil.formatDates(result)) |
|
55 | 4 | .then(result => reply({ data: result })) |
|
56 | 1 | .catch(err => reply(err)); |
|
57 | }, |
||
58 | config: { |
||
59 | validate: { |
||
60 | query: { |
||
61 | deviceType: Joi.string(), |
||
62 | deviceVersion: Joi.string().regex(/^[0-9A-Za-z.*-]+$/), |
||
63 | appVersion: Joi.string().regex(/^[0-9A-Za-z.*-]+$/), |
||
64 | }, |
||
65 | }, |
||
66 | auth: false, |
||
67 | }, |
||
68 | }, |
||
69 | { |
||
70 | method: 'POST', |
||
71 | path: `${config.statusApiPrefix}`, |
||
72 | handler: (request, reply) => { |
||
73 | 1 | const status = Object.assign({}, request.payload); |
|
74 | 4 | if (status.startTime && status.endTime) { |
|
75 | 1 | status.startTime = new Date(Date.parse(status.startTime)); |
|
76 | 1 | status.endTime = new Date(Date.parse(status.endTime)); |
|
77 | } |
||
78 | 1 | Status.add(status) |
|
79 | 1 | .then(result => reply(result)) |
|
80 | .catch(err => reply(err)); |
||
81 | }, |
||
82 | config: { |
||
83 | validate: { |
||
84 | payload: { |
||
85 | startTime: Joi.string().isoDate(), |
||
86 | endTime: Joi.string().isoDate(), |
||
87 | deviceTypes: Joi.array().items(Joi.string()).required(), |
||
88 | deviceSemVersion: Joi.string().regex(/^(([*>=<]{1,2}[0-9A-Za-z.-]*[\s]*)[\s|]*)+$/).required(), |
||
89 | appSemVersion: Joi.string().regex(/^(([*>=<]{1,2}[0-9A-Za-z.-]*[\s]*)[\s|]*)+$/).required(), |
||
90 | type: Joi.string().required(), |
||
91 | url: Joi.string().uri().allow(''), |
||
92 | title: Joi.string().required(), |
||
93 | contents: Joi.string(), |
||
94 | isActivated: Joi.boolean().required(), |
||
95 | }, |
||
96 | }, |
||
97 | }, |
||
98 | }, |
||
99 | { |
||
100 | method: 'PUT', |
||
101 | path: `${config.statusApiPrefix}/{statusId}`, |
||
102 | handler: (request, reply) => { |
||
103 | 2 | const status = Object.assign({}, request.payload); |
|
104 | let unset; |
||
105 | 4 | if (!status.startTime && !status.endTime) { |
|
106 | 1 | unset = { startTime: 1, endTime: 1 }; |
|
107 | } else { |
||
108 | 2 | status.startTime = (status.startTime) ? new Date(Date.parse(status.startTime)) : undefined; |
|
109 | 2 | status.endTime = (status.endTime) ? new Date(Date.parse(status.endTime)) : undefined; |
|
110 | } |
||
111 | |||
112 | 2 | Status.update(request.params.statusId, status, unset) |
|
0 ignored issues
–
show
|
|||
113 | 2 | .then(result => reply(result)) |
|
114 | .catch(err => reply(err)); |
||
115 | }, |
||
116 | config: { |
||
117 | validate: { |
||
118 | params: { |
||
119 | statusId: Joi.string().required(), |
||
120 | }, |
||
121 | payload: { |
||
122 | startTime: Joi.string().isoDate(), |
||
123 | endTime: Joi.string().isoDate(), |
||
124 | deviceTypes: Joi.array().items(Joi.string()).required(), |
||
125 | deviceSemVersion: Joi.string().regex(/^(([*>=<]{1,2}[0-9A-Za-z.-]*[\s]*)[\s|]*)+$/).required(), |
||
126 | appSemVersion: Joi.string().regex(/^(([*>=<]{1,2}[0-9A-Za-z.-]*[\s]*)[\s|]*)+$/).required(), |
||
127 | type: Joi.string().required(), |
||
128 | title: Joi.string().required(), |
||
129 | url: Joi.string().uri().allow(''), |
||
130 | contents: Joi.string(), |
||
131 | isActivated: Joi.boolean().required(), |
||
132 | }, |
||
133 | }, |
||
134 | }, |
||
135 | }, |
||
136 | { |
||
137 | method: 'PUT', |
||
138 | path: `${config.statusApiPrefix}/{statusId}/{action}`, |
||
139 | handler: (request, reply) => { |
||
140 | 2 | Status.update(request.params.statusId, { isActivated: request.params.action === 'activate' }) |
|
141 | 2 | .then(result => reply(result)) |
|
142 | .catch(err => reply(err)); |
||
143 | }, |
||
144 | config: { |
||
145 | validate: { |
||
146 | params: { |
||
147 | statusId: Joi.string().required(), |
||
148 | action: Joi.string().valid('activate', 'deactivate').required(), |
||
149 | }, |
||
150 | }, |
||
151 | }, |
||
152 | }, |
||
153 | { |
||
154 | method: 'DELETE', |
||
155 | path: `${config.statusApiPrefix}/{statusId}`, |
||
156 | handler: (request, reply) => { |
||
157 | 1 | Status.remove(request.params.statusId) |
|
158 | 1 | .then(result => reply(result)) |
|
159 | .catch(err => reply(err)); |
||
160 | }, |
||
161 | config: { |
||
162 | validate: { |
||
163 | params: { |
||
164 | statusId: Joi.string().required(), |
||
165 | }, |
||
166 | }, |
||
167 | }, |
||
168 | }, |
||
169 | ]; |
||
170 |