-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
107 lines (99 loc) · 3.05 KB
/
index.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
const debug = require("debug")("evolvus-docket:index");
const _ = require("lodash");
const model = require("./model/docketSchema");
const dbSchema = require("./db/docketSchema");
// const docket = require("./db/docket");
const Dao = require("@evolvus/evolvus-mongo-dao").Dao;
const collection = new Dao("audit", dbSchema);
const validate = require("jsonschema")
.validate;
var filterAttributes = model.filterAttributes;
var sortAttributes = model.sortableAttributes;
module.exports = {
model,
dbSchema,
filterAttributes,
sortAttributes
};
module.exports.validate = (docket) => {
return new Promise((resolve, reject) => {
try {
debug(`Input object is ${JSON.stringify(docket)}`);
var res = validate(docket, model.schema);
if (res.valid) {
debug(`Validation against JSON schema: Successfull`);
resolve(res.valid);
} else {
debug(`Validation against JSON schema: Validation failed due to: ${JSON.stringify(res.errors)}`);
reject(res.errors);
}
} catch (err) {
debug(`Try-catch failed due to: ${e}`);
reject(err);
}
});
};
module.exports.save = (docketObject) => {
return new Promise((resolve, reject) => {
try {
if (docketObject == null) {
throw new Error(`IllegalArgumentException: docketObject is ${docketObject}`);
}
debug(`Input docket object: ${JSON.stringify(docketObject.name)}`);
let res = validate(docketObject, model.schema);
debug(`Validation against JSON schema:result:${JSON.stringify(res.valid)}`);
if (res.errors.length != 0) {
reject(res.errors);
} else {
docketObject.status = docketObject.status.toUpperCase();
collection.save(docketObject).then((result) => {
debug(`Audit saved successfully: ${result.name}`);
resolve(result);
}).catch((e) => {
debug(`collection.save promise failed: ${e}`);
reject(e);
});
}
} catch (e) {
debug(`Try-catch failed: ${e}`);
reject(e);
}
});
};
module.exports.find = (filter, orderby, skipCount, limit) => {
return new Promise((resolve, reject) => {
try {
let filterObject = _.pick(filter, filterAttributes);
if (filter.fromDate != null && filter.toDate != null) {
filterObject = {
$and: [{
$and: [filterObject]
},
{
$and: [{
eventDateTime: {
$gte: filter.fromDate
}
}, {
eventDateTime: {
$lte: filter.toDate
}
}]
}
]
};
}
debug(`FilterObject is: ${JSON.stringify(filterObject)}`);
collection.find(filterObject, orderby, skipCount, limit).then((audits) => {
debug(`Audits stored in database are: ${audits.length}`);
resolve(audits);
}).catch((e) => {
debug(`Collection.find promise failed: ${e}`);
reject(e);
});
} catch (e) {
debug(`Try-catch failed: ${e}`);
reject(e);
}
});
};