-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathContentManager.js
269 lines (223 loc) · 7.69 KB
/
ContentManager.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
'use strict';
const _ = require('lodash');
const parseMultipartBody = require('../utils/parse-multipart');
const {
validateGenerateUIDInput,
validateCheckUIDAvailabilityInput,
validateUIDField,
} = require('./validation');
module.exports = {
async generateUID(ctx) {
const { contentTypeUID, field, data } = await validateGenerateUIDInput(ctx.request.body);
await validateUIDField(contentTypeUID, field);
const uidService = strapi.plugins['content-manager'].services.uid;
ctx.body = {
data: await uidService.generateUIDField({ contentTypeUID, field, data }),
};
},
async checkUIDAvailability(ctx) {
const { contentTypeUID, field, value } = await validateCheckUIDAvailabilityInput(
ctx.request.body
);
await validateUIDField(contentTypeUID, field);
const uidService = strapi.plugins['content-manager'].services.uid;
const isAvailable = await uidService.checkUIDAvailability({ contentTypeUID, field, value });
ctx.body = {
isAvailable,
suggestion: !isAvailable
? await uidService.findUniqueUID({ contentTypeUID, field, value })
: null,
};
},
/**
* Returns a list of entities of a content-type matching the query parameters
*/
async find(ctx) {
const { model } = ctx.params;
const contentManagerService = strapi.plugins['content-manager'].services.contentmanager;
let entities = [];
if (_.has(ctx.request.query, '_q')) {
entities = await contentManagerService.search({ model }, ctx.request.query);
} else {
entities = await contentManagerService.fetchAll({ model }, ctx.request.query);
}
ctx.body = entities;
},
/**
* Returns an entity of a content type by id
*/
async findOne(ctx) {
const { model, id } = ctx.params;
const contentManagerService = strapi.plugins['content-manager'].services.contentmanager;
const entry = await contentManagerService.fetch({ model, id });
// Entry not found
if (!entry) {
return ctx.notFound('Entry not found');
}
ctx.body = entry;
},
/**
* Returns a count of entities of a content type matching query parameters
*/
async count(ctx) {
const { model } = ctx.params;
const contentManagerService = strapi.plugins['content-manager'].services.contentmanager;
let count;
if (_.has(ctx.request.query, '_q')) {
count = await contentManagerService.countSearch({ model }, ctx.request.query);
} else {
count = await contentManagerService.count({ model }, ctx.request.query);
}
ctx.body = {
count: _.isNumber(count) ? count : _.toNumber(count),
};
},
/**
* Creates an entity of a content type
*/
async create(ctx) {
const contentManagerService = strapi.plugins['content-manager'].services.contentmanager;
const { model } = ctx.params;
try {
if (ctx.is('multipart')) {
const { data, files } = parseMultipartBody(ctx);
ctx.body = await contentManagerService.create(data, { files, model });
} else {
// Create an entry using `queries` system
ctx.body = await contentManagerService.create(ctx.request.body, { model });
}
// decrement item quantity by one
if(model == 'application::lend-items.lend-items'){
const id = ctx.body.item.id
await contentManagerService.edit({ id }, {
availableUnits: ctx.body.item.availableUnits-1
}, {
model:"application::items.items",
});
}
await strapi.telemetry.send('didCreateFirstContentTypeEntry', { model });
} catch (error) {
strapi.log.error(error);
ctx.badRequest(null, [
{
messages: [{ id: error.message, message: error.message, field: error.field }],
errors: _.get(error, 'data.errors'),
},
]);
}
},
/**
* Updates an entity of a content type
*/
async update(ctx) {
const { id, model } = ctx.params;
const contentManagerService = strapi.plugins['content-manager'].services.contentmanager;
try {
if (ctx.is('multipart')) {
const { data, files } = parseMultipartBody(ctx);
if(model == 'application::lend-items.lend-items'){
const entry = await contentManagerService.fetch({ model, id });
const prev = await contentManagerService.edit({ id:entry.item.id }, {
availableUnits: entry.item.availableUnits+1
}, {
model:"application::items.items",
});
ctx.body = await contentManagerService.edit({ id }, data, {
files,
model,
});
ctx.body = await contentManagerService.edit({ id:ctx.body.item.id }, {
availableUnits: ctx.body.item.availableUnits-1
}, {
model:"application::items.items",
});
}else{
ctx.body = await contentManagerService.edit({ id }, data, {
files,
model,
});
}
} else {
// Return the last one which is the current model.
ctx.body = await contentManagerService.edit({ id }, ctx.request.body, {
model,
});
}
} catch (error) {
//rollback the action
// await contentManagerService.edit({ id:prev.item.id }, {
// availableUnits: prev.item.availableUnits-1
// }, {
// model:"application::items.items",
// });
strapi.log.error(error);
ctx.badRequest(null, [
{
messages: [{ id: error.message, message: error.message, field: error.field }],
errors: _.get(error, 'data.errors'),
},
]);
}
},
/**
* Deletes one entity of a content type matching a query
*/
async delete(ctx) {
const { id, model } = ctx.params;
const contentManagerService = strapi.plugins['content-manager'].services.contentmanager;
ctx.body = await contentManagerService.delete({ id, model });
try {
// increment item quantity by one
if(model == 'application::lend-items.lend-items'){
const id = ctx.body.item.id
await contentManagerService.edit({ id }, {
availableUnits: ctx.body.item.availableUnits+1
}, {
model:"application::items.items",
});
}
}catch (error) {
strapi.log.error(error);
ctx.badRequest(null, [
{
messages: [{ id: error.message, message: error.message, field: error.field }],
errors: _.get(error, 'data.errors'),
},
]);
}
},
/**
* Deletes multiple entities of a content type matching a query
*/
async deleteMany(ctx) {
const { model } = ctx.params;
const contentManagerService = strapi.plugins['content-manager'].services.contentmanager;
ctx.body = await contentManagerService.deleteMany({ model }, ctx.request.query);
console.log(ctx.body);
if(model == 'application::lend-items.lend-items'){
try {
const itemCount = _.map(ctx.body,'item')
const itemUnits = _.uniqBy(itemCount,'id')
const uniqItems = _.countBy(itemCount,'id')
// increment each item quantity by one
for (const idx in uniqItems){
const id = parseInt(idx)
const units = _.find(itemUnits,['id',id]).availableUnits
await contentManagerService.edit({ id }, {
availableUnits: units+uniqItems[idx]
}, {
model:"application::items.items",
});
}
}catch (error) {
strapi.log.error(error);
ctx.badRequest(null, [
{
messages: [{ id: error.message, message: error.message, field: error.field }],
errors: _.get(error, 'data.errors'),
},
]);
}
};
},
};