-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathclassifications.js
242 lines (231 loc) · 8.51 KB
/
classifications.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
"use strict";
var express = require("express");
var router = express.Router();
var CategorySerializer = require("../serializers/category");
var AnnotationSerializer = require("../serializers/annotation");
var ErrorSerializer = require("../serializers/error");
var categories;
/**
* @api {get} /classifications/categories/:category_id Get Category of classification
* @apiName Get Category of classification
* @apiGroup 11. Classification
* @apiVersion 2.0.1
*
* @apiUse Auth
* @apiParam {uuid-v4} [category_id] Category Id
* @apiParam {String} [name] Category name
*
* @apiUse 200
* @apiUse 401
* @apiUse 405
* @apiUse 429
*/
router.get("/categories/(:category_id([0-9a-z\-]+))?", expressJwt({secret: jwtsettings.secret, algorithms: jwtsettings.algorithms}), function (req, res) {
var category_id = req.params.category_id;
var name = req.query.name;
var size = typeof req.query.size!=="undefined"?req.query.size:20;
var page = typeof req.query.page!=="undefined"?req.query.page:1;
page = page>0?page:1;
var offset = Math.ceil(size*(page-1));
categories = db_classifications.getCollection("categories");
var query;
if ( typeof category_id !== "undefined" ) {
query = {
"$and": [
{ "user_id" : req.user.id },
{ "id" : category_id },
]
};
} else {
if ( typeof name !== "undefined" ) {
query = {
"$and": [
{ "user_id" : req.user.id },
{ "name": { "$regex": [name, "i"] } }
]
};
} else {
query = {
"$and": [
{ "user_id" : req.user.id },
]
};
}
}
var json = categories.chain().find(query).offset(offset).limit(size).data();
var total = categories.find(query).length;
json.size = size;
json.pageSelf = page;
json.pageFirst = 1;
json.pagePrev = json.pageSelf>json.pageFirst?Math.ceil(json.pageSelf)-1:json.pageFirst;
json.pageLast = Math.ceil(total/size);
json.pageNext = json.pageSelf<json.pageLast?Math.ceil(json.pageSelf)+1:undefined;
json = json.length>0?json:[];
res.status(200).send(new CategorySerializer(json).serialize());
});
/**
* @api {post} /classifications/categories/ Create new Category for classification
* @apiName Create new Category for classification
* @apiGroup 11. Classification
* @apiVersion 2.0.1
*
* @apiUse Auth
* @apiBody {String} [name=unamed] Category Name
* @apiBody {String} [color] Color
* @apiBody {String{1024}} [description] Object Description
*
* @apiUse 201
* @apiUse 400
* @apiUse 429
*/
router.post("/categories/?", expressJwt({secret: jwtsettings.secret, algorithms: jwtsettings.algorithms}), function (req, res) {
categories = db_classifications.getCollection("categories");
/* Check for quota limitation */
var queryR = { "user_id" : req.user.id };
var i = (categories.find(queryR)).length;
if( i >= (quota[req.user.role]).categories ) {
res.status(429).send(new ErrorSerializer({"id": 18329, "code": 429, "message": "Too Many Requests"}).serialize());
} else {
if ( typeof req.user.id !== "undefined" ) {
var category_id = uuid.v4();
var newCategory = {
id: category_id,
user_id: req.user.id,
name: typeof req.body.name!=="undefined"?req.body.name:"unamed",
color: typeof req.body.color!=="undefined"?req.body.color:"",
description:typeof req.body.description!=="undefined"?(req.body.description).substring(0, 1024):"",
};
t6events.addStat("t6Api", "category add", newCategory.id, req.user.id);
t6events.addAudit("t6Api", "category add", req.user.id, newCategory.id, {error_id: null, status: 201});
categories.insert(newCategory);
res.header("Location", "/v"+version+"/categories/"+newCategory.id);
res.status(201).send({ "code": 201, message: "Created", category: new CategorySerializer(newCategory).serialize() });
}
}
});
/**
* @api {put} /classifications/categories/:category_id/ Edit a Category
* @apiName Edit a Category
* @apiGroup 11. Classification
* @apiVersion 2.0.1
*
* @apiUse Auth
* @apiParam {uuid-v4} [category_id] Category Id
* @apiBody {String} [name=unamed] Category Name
* @apiBody {String} [color] Color
* @apiBody {String{1024}} [description] Object Description
*
* @apiUse 200
* @apiUse 400
* @apiUse 401
* @apiUse 403
* @apiUse 404
* @apiUse 405
* @apiUse 409
* @apiUse 429
*/
router.put("/categories/:category_id([0-9a-z\-]+)", expressJwt({secret: jwtsettings.secret, algorithms: jwtsettings.algorithms}), function (req, res) {
var category_id = req.params.category_id;
if ( category_id ) {
categories = db_classifications.getCollection("categories");
var query = {
"$and": [
{ "id": category_id },
{ "user_id": req.user.id },
]
};
var category = categories.findOne( query );
if ( category ) {
if ( req.body.meta && req.body.meta.revision && (req.body.meta.revision - category.meta.revision) !== 0 ) {
res.status(409).send(new ErrorSerializer({"id": 18001, "code": 409, "message": "Bad Request"}).serialize());
} else {
var result;
categories.chain().find({ "id": category_id }).update(function(item) {
item.name = typeof req.body.name!=="undefined"?req.body.name:item.name;
item.color = typeof req.body.color!=="undefined"?req.body.color:item.color;
item.description= typeof req.body.description!=="undefined"?(req.body.description).substring(0, 1024):item.description;
result = item;
});
if ( typeof result !== "undefined" ) {
db_classifications.save();
res.header("Location", "/v"+version+"/categories/"+category_id);
res.status(200).send({ "code": 200, message: "Successfully updated", category: new CategorySerializer(result).serialize() });
} else {
res.status(404).send(new ErrorSerializer({"id": 18273, "code": 404, "message": "Not Found"}).serialize());
}
}
} else {
res.status(401).send(new ErrorSerializer({"id": 18272, "code": 401, "message": "Forbidden"}).serialize());
}
} else {
res.status(404).send(new ErrorSerializer({"id": 18271, "code": 404, "message": "Not Found"}).serialize());
}
});
/**
* @api {delete} /classifications/categories/:category_id Delete a Category
* @apiName Delete a Category
* @apiGroup 11. Classification
* @apiVersion 2.0.1
*
* @apiUse Auth
* @apiParam {uuid-v4} [category_id] Category Id to be deleted
*
* @apiUse 200
* @apiUse 403
* @apiUse 404
*/
router.delete("/categories/:category_id([0-9a-z\-]+)", expressJwt({secret: jwtsettings.secret, algorithms: jwtsettings.algorithms}), function (req, res) {
var category_id = req.params.category_id;
categories = db_classifications.getCollection("categories");
var query = {
"$and": [
{ "user_id" : req.user.id, }, // delete only category from current user
{ "id" : category_id, },
],
};
var c = categories.find(query);
if ( c.length > 0 ) {
categories.remove(c);
db_classifications.saveDatabase();
res.status(200).send({ "code": 200, message: "Successfully deleted", removed_id: category_id }); // TODO: missing serializer
} else {
res.status(404).send(new ErrorSerializer({"id": 532, "code": 404, "message": "Not Found"}).serialize());
}
});
/**
* @api {post} /classifications/annotations/ Add an annotation to a Flow
* @apiName Add an annotation to a Flow
* @apiGroup 11. Classification
* @apiVersion 2.0.1
*
* @apiUse Auth
* @apiParam {uuid-v4} [category_id] Category Id to be deleted
* @apiParam {String} [from_ts] Timestamp or formatted date YYYY-MM-DD HH:MM:SS
* @apiParam {String} [to_ts] Timestamp or formatted date YYYY-MM-DD HH:MM:SS
*
* @apiUse 200
* @apiUse 403
* @apiUse 404
*/
router.post("/annotations/?", expressJwt({secret: jwtsettings.secret, algorithms: jwtsettings.algorithms}), function (req, res) {
let from_ts;
let to_ts;
if(req.body.from_ts && req.body.to_ts) {
if (moment(req.body.from_ts).isBefore(req.body.to_ts)) {
from_ts = moment(req.body.from_ts).format("x")*1000;
to_ts = moment(req.body.to_ts).format("x")*1000;
} else {
from_ts = moment(req.body.to_ts).format("x")*1000;
to_ts = moment(req.body.from_ts).format("x")*1000;
}
}
if ( typeof req.user.id!=="undefined" && typeof req.body.flow_id!=="undefined" && typeof req.body.category_id!=="undefined" && from_ts!==null && to_ts!==null ) {
let newAnnotation = annotate(req.user.id, from_ts, to_ts, req.body.category_id, req.body.flow_id);
res.header("Location", "/v"+version+"/annotations/"+newAnnotation.id);
res.status(201).send({ "code": 201, message: "Created", annotation: new AnnotationSerializer(newAnnotation).serialize() });
} else {
res.status(409).send(new ErrorSerializer({"id": 18001, "code": 409, "message": "Bad Request"}).serialize());
}
});
t6console.log(`Route ${path.basename(__filename)} loaded`.padEnd(59));
module.exports = router;