-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
172 lines (138 loc) · 4.57 KB
/
api.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
const dotenv = require('dotenv');
const _ = require('lodash');
dotenv.load();
module.exports = ArcClass => class ArcModel extends ArcClass {
getList(module, callback) {
const stagingPrefix = this.get('stgPrefix') ? this.get('stgPrefix') : 'Stg';
try {
const unprefixedList = this.utils.unprefix(module, stagingPrefix);
const prefixedList = (stagingPrefix + "-" + unprefixedList).toLowerCase();
const List = this.list(prefixedList);
return List.model.find().exec((err, data) => {
if (err) return callback(err, null);
return callback(null, data);
});
} catch (err) {
return callback(err, null);
}
}
getModule(module, id, callback) {
const stagingPrefix = this.get('stgPrefix') ? this.get('stgPrefix') : 'Stg';
try {
const unprefixedList = this.utils.unprefix(module, stagingPrefix);
const prefixedList = (stagingPrefix + "-" + unprefixedList).toLowerCase();
const List = this.list(prefixedList);
return List.model.findOne().where('_id', id).exec((err, data) => {
if (err) return callback(err, null);
return callback(null, data);
});
} catch (err) {
return callback(err, null);
}
}
getMultiModule(module, ids, callback) {
const stagingPrefix = this.get('stgPrefix') ? this.get('stgPrefix') : 'Stg';
const idsArray = ids.split(',');
try {
const unprefixedList = this.utils.unprefix(module, stagingPrefix);
const prefixedList = (stagingPrefix + "-" + unprefixedList).toLowerCase();
const List = this.list(prefixedList);
return List.model.aggregate([
{ $match: { _id: { $in: idsArray } } },
{ $project: {
weight: {
$cond: [
{ $eq: ['$_id', 4] },
1,
{
$cond: [
{ $eq: ['$_id', 2] },
2,
3
]
}
]
}
} },
{ $sort: { weight: 1 } }
]).exec((err, data) => {
if (err) return callback(err, null);
return callback(null, data);
});
// return List.model
// .find({ _id: { $in: idsArray } })
// .exec((err, data) => {
// if (err) return callback(err, null);
// return callback(null, data);
// });
} catch (err) {
return callback(err, null);
}
}
/**
*
* @param {string} module - module type to create
* @param {object} data - object of properties to create module with
* @param {*} req - express request object
* @param {*} callback - callback passed either err or data of created item
*/
createModule(module, data, req, callback) {
const stagingPrefix = this.get('stgPrefix') ? this.get('stgPrefix') : 'Stg';
try {
const unprefixedList = this.utils.unprefix(module, stagingPrefix);
const prefixedList = (stagingPrefix + "-" + unprefixedList).toLowerCase();
const List = this.list(prefixedList);
const Item = new List.model();
data = req.body;
return Item.getUpdateHandler(req).process(data, err => {
if (err) return callback({ message: 'Error creating module' }, null);
const createdItem = _.extend({}, Item, data); // faux item based on query data
return callback(null, createdItem); // return the faux item
});
} catch (err) {
return callback(err, null);
}
}
removeModule(module, id, callback) {
const stagingPrefix = this.get('stgPrefix') ? this.get('stgPrefix') : 'Stg';
try {
const unprefixedList = this.utils.unprefix(module, stagingPrefix);
const prefixedList = (stagingPrefix + "-" + unprefixedList).toLowerCase();
const List = this.list(prefixedList);
return List.model
.findOne()
.where('_id', id)
.exec((err, data) => {
if (err) return callback(err, null);
if (!data) return callback('Item already deleted or does not exist.', null);
return data.remove(rmErr => {
if (rmErr) return callback(rmErr, null);
return callback(null, { removed: data });
});
});
} catch (err) {
return callback(err, null);
}
}
updateModule(module, id, data, req, callback) {
const stagingPrefix = this.get('stgPrefix') ? this.get('stgPrefix') : 'Stg';
try {
const unprefixedList = this.utils.unprefix(module, stagingPrefix);
const prefixedList = (stagingPrefix + "-" + unprefixedList).toLowerCase();
const List = this.list(prefixedList);
// console.log('ID', id)
console.log('Data', data);
// console.log('Req', req.body);
return List.model.findOne().where('_id', id)
.exec((err, item) => {
if (err) return callback(err, null);
return item.getUpdateHandler(req).process(data, updateErr => {
if (updateErr) return callback(updateErr, null);
return callback(null, data);
});
});
} catch (err) {
return callback(err, null);
}
}
};