@knovator/pagecreator-node 1.2.2 → 1.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +458 -458
- package/package.json +3 -2
- package/src/constants/index.d.ts +20 -20
- package/src/constants/index.js +33 -33
- package/src/controllers/PageController.d.ts +4 -4
- package/src/controllers/PageController.js +83 -83
- package/src/controllers/TabController.d.ts +4 -4
- package/src/controllers/TabController.js +45 -45
- package/src/controllers/UserController.d.ts +2 -2
- package/src/controllers/UserController.js +64 -64
- package/src/controllers/WidgetController.d.ts +10 -10
- package/src/controllers/WidgetController.js +328 -328
- package/src/index.d.ts +8 -8
- package/src/index.js +47 -47
- package/src/models/Item.d.ts +30 -30
- package/src/models/Item.js +49 -49
- package/src/models/Page.d.ts +30 -30
- package/src/models/Page.js +23 -22
- package/src/models/Page.js.map +1 -1
- package/src/models/SrcSet.d.ts +30 -30
- package/src/models/SrcSet.js +16 -16
- package/src/models/Tab.d.ts +30 -30
- package/src/models/Tab.js +27 -27
- package/src/models/Widget.d.ts +30 -30
- package/src/models/Widget.js +62 -62
- package/src/models/index.d.ts +6 -6
- package/src/models/index.js +18 -18
- package/src/plugins/softDelete.d.ts +7 -7
- package/src/plugins/softDelete.js +63 -63
- package/src/routes/PageRoute.d.ts +3 -3
- package/src/routes/PageRoute.js +29 -29
- package/src/routes/UserRoute.d.ts +3 -3
- package/src/routes/UserRoute.js +22 -22
- package/src/routes/WidgetRoute.d.ts +3 -3
- package/src/routes/WidgetRoute.js +67 -67
- package/src/services/dataService.d.ts +9 -9
- package/src/services/dataService.js +402 -402
- package/src/services/dbService.d.ts +16 -16
- package/src/services/dbService.js +88 -87
- package/src/services/dbService.js.map +1 -1
- package/src/types/IRequest.d.ts +6 -6
- package/src/types/IRequest.js +2 -2
- package/src/types/IResponse.d.ts +6 -6
- package/src/types/IResponse.js +2 -2
- package/src/types/Router.d.ts +4 -4
- package/src/types/Router.js +2 -2
- package/src/types/common.d.ts +143 -142
- package/src/types/common.js +2 -2
- package/src/types/enums.d.ts +14 -14
- package/src/types/enums.js +20 -20
- package/src/types/index.d.ts +5 -5
- package/src/types/index.js +8 -8
- package/src/utils/defaults.d.ts +7 -7
- package/src/utils/defaults.js +40 -40
- package/src/utils/helper.d.ts +7 -7
- package/src/utils/helper.js +224 -224
- package/src/utils/redis.d.ts +3 -3
- package/src/utils/redis.js +58 -58
- package/src/utils/responseHandlers.d.ts +6 -6
- package/src/utils/responseHandlers.js +56 -56
- package/src/utils/validate.d.ts +4 -4
- package/src/utils/validate.js +23 -23
- package/src/utils/validations/page.d.ts +7 -7
- package/src/utils/validations/page.js +52 -50
- package/src/utils/validations/page.js.map +1 -1
- package/src/utils/validations/tab.d.ts +6 -6
- package/src/utils/validations/tab.js +25 -25
- package/src/utils/validations/user.d.ts +3 -3
- package/src/utils/validations/user.js +12 -12
- package/src/utils/validations/widget.d.ts +9 -9
- package/src/utils/validations/widget.js +152 -152
|
@@ -1,403 +1,403 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.handleResetData = exports.handleUpdateData = exports.updateRedisPage = exports.getPageDataDB = exports.updateWidgetPagesData = exports.updateRedisWidget = exports.getWidgetDataDB = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
const helper_1 = require("../utils/helper");
|
|
6
|
-
const redis_1 = require("../utils/redis");
|
|
7
|
-
const defaults_1 = require("../utils/defaults");
|
|
8
|
-
const getAggregationQuery = ({ collectionName, ids, }) => {
|
|
9
|
-
const collectionConfig = defaults_1.defaults.collections.find((c) => c.collectionName === collectionName);
|
|
10
|
-
const aggregateQueryItem = [];
|
|
11
|
-
if (Array.isArray(collectionConfig === null || collectionConfig === void 0 ? void 0 : collectionConfig.aggregations) &&
|
|
12
|
-
(collectionConfig === null || collectionConfig === void 0 ? void 0 : collectionConfig.aggregations.length)) {
|
|
13
|
-
aggregateQueryItem.push(...collectionConfig.aggregations);
|
|
14
|
-
}
|
|
15
|
-
aggregateQueryItem.push({
|
|
16
|
-
$match: Object.assign({ _id: {
|
|
17
|
-
$in: ids,
|
|
18
|
-
} }, ((collectionConfig === null || collectionConfig === void 0 ? void 0 : collectionConfig.match) || {})),
|
|
19
|
-
}, { $addFields: { __order: { $indexOfArray: [ids, '$_id'] } } }, { $sort: { __order: 1 } });
|
|
20
|
-
return aggregateQueryItem;
|
|
21
|
-
};
|
|
22
|
-
const getWidgetDataDB = (code, models) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
23
|
-
var _a;
|
|
24
|
-
const { Widget } = models;
|
|
25
|
-
const widgetDataArr = (yield Widget.aggregate([
|
|
26
|
-
{
|
|
27
|
-
$match: {
|
|
28
|
-
isDeleted: false,
|
|
29
|
-
isActive: true,
|
|
30
|
-
code,
|
|
31
|
-
},
|
|
32
|
-
},
|
|
33
|
-
{
|
|
34
|
-
// Get only the fields that are not excluded
|
|
35
|
-
$project: Object.assign({}, defaults_1.commonExcludedFields),
|
|
36
|
-
},
|
|
37
|
-
{
|
|
38
|
-
// Get Items data
|
|
39
|
-
$lookup: {
|
|
40
|
-
from: 'items',
|
|
41
|
-
let: { widget: '$_id' },
|
|
42
|
-
pipeline: [
|
|
43
|
-
{
|
|
44
|
-
$match: {
|
|
45
|
-
$expr: {
|
|
46
|
-
$eq: ['$widgetId', '$$widget'],
|
|
47
|
-
},
|
|
48
|
-
isDeleted: false,
|
|
49
|
-
},
|
|
50
|
-
},
|
|
51
|
-
...(defaults_1.defaults.languages && ((_a = defaults_1.defaults.languages) === null || _a === void 0 ? void 0 : _a.length) > 0
|
|
52
|
-
? defaults_1.defaults.languages.reduce((arr, lng) => {
|
|
53
|
-
arr.push({
|
|
54
|
-
$lookup: {
|
|
55
|
-
from: 'file',
|
|
56
|
-
let: { img: { $toObjectId: `$imgs.${lng.code}` } },
|
|
57
|
-
as: `images.${lng.code}`,
|
|
58
|
-
pipeline: [
|
|
59
|
-
{
|
|
60
|
-
$match: {
|
|
61
|
-
$expr: {
|
|
62
|
-
$eq: ['$_id', '$$img'],
|
|
63
|
-
},
|
|
64
|
-
},
|
|
65
|
-
},
|
|
66
|
-
{
|
|
67
|
-
$project: {
|
|
68
|
-
_id: 1,
|
|
69
|
-
uri: 1,
|
|
70
|
-
},
|
|
71
|
-
},
|
|
72
|
-
],
|
|
73
|
-
},
|
|
74
|
-
}, {
|
|
75
|
-
$unwind: {
|
|
76
|
-
path: `$images.${lng.code}`,
|
|
77
|
-
preserveNullAndEmptyArrays: true,
|
|
78
|
-
},
|
|
79
|
-
});
|
|
80
|
-
return arr;
|
|
81
|
-
}, [])
|
|
82
|
-
: [
|
|
83
|
-
{
|
|
84
|
-
$lookup: {
|
|
85
|
-
from: 'file',
|
|
86
|
-
let: { img: '$img' },
|
|
87
|
-
as: 'image',
|
|
88
|
-
pipeline: [
|
|
89
|
-
{
|
|
90
|
-
$match: {
|
|
91
|
-
$expr: {
|
|
92
|
-
$eq: ['$_id', '$$img'],
|
|
93
|
-
},
|
|
94
|
-
},
|
|
95
|
-
},
|
|
96
|
-
{
|
|
97
|
-
$project: {
|
|
98
|
-
_id: 1,
|
|
99
|
-
uri: 1,
|
|
100
|
-
},
|
|
101
|
-
},
|
|
102
|
-
],
|
|
103
|
-
},
|
|
104
|
-
},
|
|
105
|
-
{
|
|
106
|
-
$unwind: {
|
|
107
|
-
path: '$image',
|
|
108
|
-
preserveNullAndEmptyArrays: true,
|
|
109
|
-
},
|
|
110
|
-
},
|
|
111
|
-
]),
|
|
112
|
-
{
|
|
113
|
-
$project: Object.assign({ sequence: 0, imgs: 0 }, defaults_1.commonExcludedFields),
|
|
114
|
-
},
|
|
115
|
-
// {
|
|
116
|
-
// $lookup: {
|
|
117
|
-
// from: 'srcsets',
|
|
118
|
-
// let: { item: '$_id' },
|
|
119
|
-
// as: 'srcset',
|
|
120
|
-
// pipeline: [
|
|
121
|
-
// {
|
|
122
|
-
// $match: {
|
|
123
|
-
// $expr: {
|
|
124
|
-
// $eq: ['$itemId', '$$item'],
|
|
125
|
-
// },
|
|
126
|
-
// },
|
|
127
|
-
// },
|
|
128
|
-
// {
|
|
129
|
-
// $project: {
|
|
130
|
-
// ...commonExcludedFields,
|
|
131
|
-
// _id: 0,
|
|
132
|
-
// itemId: 0,
|
|
133
|
-
// },
|
|
134
|
-
// },
|
|
135
|
-
// ],
|
|
136
|
-
// },
|
|
137
|
-
// },
|
|
138
|
-
],
|
|
139
|
-
as: 'items',
|
|
140
|
-
},
|
|
141
|
-
},
|
|
142
|
-
]));
|
|
143
|
-
if (!widgetDataArr.length) {
|
|
144
|
-
return null;
|
|
145
|
-
}
|
|
146
|
-
const widgetData = widgetDataArr[0];
|
|
147
|
-
if (widgetData.collectionName &&
|
|
148
|
-
widgetData.collectionItems &&
|
|
149
|
-
widgetData.collectionItems.length > 0) {
|
|
150
|
-
const aggregateQueryItem = getAggregationQuery({
|
|
151
|
-
collectionName: widgetData.collectionName,
|
|
152
|
-
ids: widgetData.collectionItems,
|
|
153
|
-
});
|
|
154
|
-
const collectionModal = (0, helper_1.getCollectionModal)(widgetData.collectionName, models);
|
|
155
|
-
const collectionItems = yield collectionModal.aggregate(aggregateQueryItem);
|
|
156
|
-
widgetData.collectionItems = collectionItems;
|
|
157
|
-
}
|
|
158
|
-
if (widgetData.collectionName &&
|
|
159
|
-
widgetData.tabs &&
|
|
160
|
-
widgetData.tabs.length > 0) {
|
|
161
|
-
const tabCollectionItemIds = widgetData.tabs.reduce((acc, tabItem) => {
|
|
162
|
-
acc.push(...tabItem.collectionItems);
|
|
163
|
-
return acc;
|
|
164
|
-
}, []);
|
|
165
|
-
const aggregateQueryItem = getAggregationQuery({
|
|
166
|
-
collectionName: widgetData.collectionName,
|
|
167
|
-
ids: tabCollectionItemIds,
|
|
168
|
-
});
|
|
169
|
-
const collectionModal = (0, helper_1.getCollectionModal)(widgetData.collectionName, models);
|
|
170
|
-
const collectionItems = yield collectionModal.aggregate(aggregateQueryItem);
|
|
171
|
-
// converting colleciton items to obj to better access them
|
|
172
|
-
const collectionItemsObj = collectionItems.reduce((acc, item) => {
|
|
173
|
-
acc[item._id] = item;
|
|
174
|
-
return acc;
|
|
175
|
-
}, {});
|
|
176
|
-
widgetData.tabs = widgetData.tabs.map((tabItem) => {
|
|
177
|
-
return {
|
|
178
|
-
name: tabItem.name,
|
|
179
|
-
names: tabItem.names,
|
|
180
|
-
collectionItems: tabItem.collectionItems.map((collectionId) => collectionItemsObj[collectionId]),
|
|
181
|
-
};
|
|
182
|
-
});
|
|
183
|
-
}
|
|
184
|
-
(0, helper_1.AddSrcSetsToItems)(widgetData);
|
|
185
|
-
return widgetData;
|
|
186
|
-
});
|
|
187
|
-
exports.getWidgetDataDB = getWidgetDataDB;
|
|
188
|
-
const updateRedisWidget = (code, models) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
189
|
-
const widgetData = yield (0, exports.getWidgetDataDB)(code, models);
|
|
190
|
-
if (widgetData) {
|
|
191
|
-
yield (0, redis_1.setRedisValue)(`widgetData_${code}`, widgetData);
|
|
192
|
-
}
|
|
193
|
-
});
|
|
194
|
-
exports.updateRedisWidget = updateRedisWidget;
|
|
195
|
-
const updateWidgetPagesData = (widgetIds, models) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
196
|
-
const { Page } = models;
|
|
197
|
-
const pageCodes = yield Page.find({
|
|
198
|
-
widgets: {
|
|
199
|
-
$in: widgetIds,
|
|
200
|
-
},
|
|
201
|
-
}, 'code').distinct('code');
|
|
202
|
-
if (pageCodes.length) {
|
|
203
|
-
pageCodes.forEach((code) => {
|
|
204
|
-
(0, redis_1.deleteRedisValue)(`pageData_${code}`);
|
|
205
|
-
});
|
|
206
|
-
}
|
|
207
|
-
});
|
|
208
|
-
exports.updateWidgetPagesData = updateWidgetPagesData;
|
|
209
|
-
const getPageDataDB = (code, models) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
210
|
-
var _b;
|
|
211
|
-
const { Page } = models;
|
|
212
|
-
const pageData = (yield Page.aggregate([
|
|
213
|
-
{
|
|
214
|
-
$match: {
|
|
215
|
-
isDeleted: false,
|
|
216
|
-
code: code,
|
|
217
|
-
},
|
|
218
|
-
},
|
|
219
|
-
{
|
|
220
|
-
$project: {
|
|
221
|
-
isDeleted: 0,
|
|
222
|
-
deletedAt: 0,
|
|
223
|
-
__v: 0,
|
|
224
|
-
},
|
|
225
|
-
},
|
|
226
|
-
{
|
|
227
|
-
$lookup: {
|
|
228
|
-
from: 'widgets',
|
|
229
|
-
let: { widgets: '$widgets' },
|
|
230
|
-
pipeline: [
|
|
231
|
-
{
|
|
232
|
-
$match: {
|
|
233
|
-
$expr: {
|
|
234
|
-
$in: ['$_id', '$$widgets'],
|
|
235
|
-
},
|
|
236
|
-
isDeleted: false,
|
|
237
|
-
isActive: true,
|
|
238
|
-
},
|
|
239
|
-
},
|
|
240
|
-
{
|
|
241
|
-
$project: Object.assign({ widgetId: 0, sequence: 0 }, defaults_1.commonExcludedFields),
|
|
242
|
-
},
|
|
243
|
-
{
|
|
244
|
-
$lookup: {
|
|
245
|
-
from: 'items',
|
|
246
|
-
let: { widget: '$_id' },
|
|
247
|
-
pipeline: [
|
|
248
|
-
{
|
|
249
|
-
$match: {
|
|
250
|
-
$expr: {
|
|
251
|
-
$eq: ['$widgetId', '$$widget'],
|
|
252
|
-
},
|
|
253
|
-
isDeleted: false,
|
|
254
|
-
},
|
|
255
|
-
},
|
|
256
|
-
...(defaults_1.defaults.languages && ((_b = defaults_1.defaults.languages) === null || _b === void 0 ? void 0 : _b.length) > 0
|
|
257
|
-
? defaults_1.defaults.languages.reduce((arr, lng) => {
|
|
258
|
-
arr.push({
|
|
259
|
-
$lookup: {
|
|
260
|
-
from: 'file',
|
|
261
|
-
let: { img: { $toObjectId: `$imgs.${lng.code}` } },
|
|
262
|
-
as: `images.${lng.code}`,
|
|
263
|
-
pipeline: [
|
|
264
|
-
{
|
|
265
|
-
$match: {
|
|
266
|
-
$expr: {
|
|
267
|
-
$eq: ['$_id', '$$img'],
|
|
268
|
-
},
|
|
269
|
-
},
|
|
270
|
-
},
|
|
271
|
-
{
|
|
272
|
-
$project: {
|
|
273
|
-
_id: 1,
|
|
274
|
-
uri: 1,
|
|
275
|
-
},
|
|
276
|
-
},
|
|
277
|
-
],
|
|
278
|
-
},
|
|
279
|
-
}, {
|
|
280
|
-
$unwind: {
|
|
281
|
-
path: `$images.${lng.code}`,
|
|
282
|
-
preserveNullAndEmptyArrays: true,
|
|
283
|
-
},
|
|
284
|
-
});
|
|
285
|
-
return arr;
|
|
286
|
-
}, [])
|
|
287
|
-
: [
|
|
288
|
-
{
|
|
289
|
-
$lookup: {
|
|
290
|
-
from: 'file',
|
|
291
|
-
let: { img: '$img' },
|
|
292
|
-
as: 'image',
|
|
293
|
-
pipeline: [
|
|
294
|
-
{
|
|
295
|
-
$match: {
|
|
296
|
-
$expr: {
|
|
297
|
-
$eq: ['$_id', '$$img'],
|
|
298
|
-
},
|
|
299
|
-
},
|
|
300
|
-
},
|
|
301
|
-
{
|
|
302
|
-
$project: {
|
|
303
|
-
_id: 1,
|
|
304
|
-
uri: 1,
|
|
305
|
-
},
|
|
306
|
-
},
|
|
307
|
-
],
|
|
308
|
-
},
|
|
309
|
-
},
|
|
310
|
-
{
|
|
311
|
-
$unwind: {
|
|
312
|
-
path: '$image',
|
|
313
|
-
preserveNullAndEmptyArrays: true,
|
|
314
|
-
},
|
|
315
|
-
},
|
|
316
|
-
]),
|
|
317
|
-
{
|
|
318
|
-
$project: Object.assign({ sequence: 0, imgs: 0 }, defaults_1.commonExcludedFields),
|
|
319
|
-
},
|
|
320
|
-
// {
|
|
321
|
-
// $lookup: {
|
|
322
|
-
// from: 'srcsets',
|
|
323
|
-
// let: { item: '$_id' },
|
|
324
|
-
// as: 'srcset',
|
|
325
|
-
// pipeline: [
|
|
326
|
-
// {
|
|
327
|
-
// $match: {
|
|
328
|
-
// $expr: {
|
|
329
|
-
// $eq: ['$itemId', '$$item'],
|
|
330
|
-
// },
|
|
331
|
-
// },
|
|
332
|
-
// },
|
|
333
|
-
// {
|
|
334
|
-
// $project: {
|
|
335
|
-
// ...commonExcludedFields,
|
|
336
|
-
// _id: 0,
|
|
337
|
-
// itemId: 0,
|
|
338
|
-
// },
|
|
339
|
-
// },
|
|
340
|
-
// ],
|
|
341
|
-
// },
|
|
342
|
-
// },
|
|
343
|
-
],
|
|
344
|
-
as: 'items',
|
|
345
|
-
},
|
|
346
|
-
},
|
|
347
|
-
],
|
|
348
|
-
as: 'widgetsData',
|
|
349
|
-
},
|
|
350
|
-
},
|
|
351
|
-
]));
|
|
352
|
-
if (!pageData.length) {
|
|
353
|
-
return null;
|
|
354
|
-
}
|
|
355
|
-
pageData[0].widgetsData = yield (0, helper_1.appendCollectionData)(pageData[0].widgetsData, models);
|
|
356
|
-
if (Array.isArray(pageData[0].widgetsData) &&
|
|
357
|
-
pageData[0].widgetsData.length > 0) {
|
|
358
|
-
pageData[0].widgetsData.forEach((widget) => {
|
|
359
|
-
(0, helper_1.AddSrcSetsToItems)(widget);
|
|
360
|
-
});
|
|
361
|
-
}
|
|
362
|
-
pageData[0].widgetsData = pageData[0].widgetsData.reduce((acc, widgetItem) => {
|
|
363
|
-
acc[widgetItem._id] = widgetItem;
|
|
364
|
-
return acc;
|
|
365
|
-
}, []);
|
|
366
|
-
pageData[0].widgets = pageData[0].widgets
|
|
367
|
-
.map((widgetId) => pageData[0].widgetsData[widgetId])
|
|
368
|
-
.filter(Boolean);
|
|
369
|
-
delete pageData[0].widgetsData;
|
|
370
|
-
return pageData[0];
|
|
371
|
-
});
|
|
372
|
-
exports.getPageDataDB = getPageDataDB;
|
|
373
|
-
const updateRedisPage = (code, models) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
374
|
-
const pageData = yield (0, exports.getPageDataDB)(code, models);
|
|
375
|
-
if (pageData) {
|
|
376
|
-
yield (0, redis_1.setRedisValue)(`pageData_${code}`, pageData);
|
|
377
|
-
}
|
|
378
|
-
});
|
|
379
|
-
exports.updateRedisPage = updateRedisPage;
|
|
380
|
-
const handleUpdateData = (collectionName, itemId, models) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
381
|
-
if (!models)
|
|
382
|
-
throw new Error('models is required');
|
|
383
|
-
const { Widget } = models;
|
|
384
|
-
const widgets = yield Widget.find({
|
|
385
|
-
collectionName: collectionName,
|
|
386
|
-
collectionItems: {
|
|
387
|
-
$in: Array.isArray(itemId) ? itemId : [itemId],
|
|
388
|
-
},
|
|
389
|
-
}, 'code _id').lean();
|
|
390
|
-
if (widgets.length) {
|
|
391
|
-
(0, exports.updateWidgetPagesData)(widgets.map((widget) => widget._id), models);
|
|
392
|
-
widgets.forEach((widget) => {
|
|
393
|
-
(0, redis_1.deleteRedisValue)(`widgetData_${widget['code']}`);
|
|
394
|
-
});
|
|
395
|
-
}
|
|
396
|
-
});
|
|
397
|
-
exports.handleUpdateData = handleUpdateData;
|
|
398
|
-
const handleResetData = (type, code) => {
|
|
399
|
-
if ((type === 'widget' || type === 'page') && code)
|
|
400
|
-
(0, redis_1.deleteRedisValue)(`${type}_${code}`);
|
|
401
|
-
};
|
|
402
|
-
exports.handleResetData = handleResetData;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.handleResetData = exports.handleUpdateData = exports.updateRedisPage = exports.getPageDataDB = exports.updateWidgetPagesData = exports.updateRedisWidget = exports.getWidgetDataDB = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const helper_1 = require("../utils/helper");
|
|
6
|
+
const redis_1 = require("../utils/redis");
|
|
7
|
+
const defaults_1 = require("../utils/defaults");
|
|
8
|
+
const getAggregationQuery = ({ collectionName, ids, }) => {
|
|
9
|
+
const collectionConfig = defaults_1.defaults.collections.find((c) => c.collectionName === collectionName);
|
|
10
|
+
const aggregateQueryItem = [];
|
|
11
|
+
if (Array.isArray(collectionConfig === null || collectionConfig === void 0 ? void 0 : collectionConfig.aggregations) &&
|
|
12
|
+
(collectionConfig === null || collectionConfig === void 0 ? void 0 : collectionConfig.aggregations.length)) {
|
|
13
|
+
aggregateQueryItem.push(...collectionConfig.aggregations);
|
|
14
|
+
}
|
|
15
|
+
aggregateQueryItem.push({
|
|
16
|
+
$match: Object.assign({ _id: {
|
|
17
|
+
$in: ids,
|
|
18
|
+
} }, ((collectionConfig === null || collectionConfig === void 0 ? void 0 : collectionConfig.match) || {})),
|
|
19
|
+
}, { $addFields: { __order: { $indexOfArray: [ids, '$_id'] } } }, { $sort: { __order: 1 } });
|
|
20
|
+
return aggregateQueryItem;
|
|
21
|
+
};
|
|
22
|
+
const getWidgetDataDB = (code, models) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
23
|
+
var _a;
|
|
24
|
+
const { Widget } = models;
|
|
25
|
+
const widgetDataArr = (yield Widget.aggregate([
|
|
26
|
+
{
|
|
27
|
+
$match: {
|
|
28
|
+
isDeleted: false,
|
|
29
|
+
isActive: true,
|
|
30
|
+
code,
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
// Get only the fields that are not excluded
|
|
35
|
+
$project: Object.assign({}, defaults_1.commonExcludedFields),
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
// Get Items data
|
|
39
|
+
$lookup: {
|
|
40
|
+
from: 'items',
|
|
41
|
+
let: { widget: '$_id' },
|
|
42
|
+
pipeline: [
|
|
43
|
+
{
|
|
44
|
+
$match: {
|
|
45
|
+
$expr: {
|
|
46
|
+
$eq: ['$widgetId', '$$widget'],
|
|
47
|
+
},
|
|
48
|
+
isDeleted: false,
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
...(defaults_1.defaults.languages && ((_a = defaults_1.defaults.languages) === null || _a === void 0 ? void 0 : _a.length) > 0
|
|
52
|
+
? defaults_1.defaults.languages.reduce((arr, lng) => {
|
|
53
|
+
arr.push({
|
|
54
|
+
$lookup: {
|
|
55
|
+
from: 'file',
|
|
56
|
+
let: { img: { $toObjectId: `$imgs.${lng.code}` } },
|
|
57
|
+
as: `images.${lng.code}`,
|
|
58
|
+
pipeline: [
|
|
59
|
+
{
|
|
60
|
+
$match: {
|
|
61
|
+
$expr: {
|
|
62
|
+
$eq: ['$_id', '$$img'],
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
$project: {
|
|
68
|
+
_id: 1,
|
|
69
|
+
uri: 1,
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
],
|
|
73
|
+
},
|
|
74
|
+
}, {
|
|
75
|
+
$unwind: {
|
|
76
|
+
path: `$images.${lng.code}`,
|
|
77
|
+
preserveNullAndEmptyArrays: true,
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
return arr;
|
|
81
|
+
}, [])
|
|
82
|
+
: [
|
|
83
|
+
{
|
|
84
|
+
$lookup: {
|
|
85
|
+
from: 'file',
|
|
86
|
+
let: { img: '$img' },
|
|
87
|
+
as: 'image',
|
|
88
|
+
pipeline: [
|
|
89
|
+
{
|
|
90
|
+
$match: {
|
|
91
|
+
$expr: {
|
|
92
|
+
$eq: ['$_id', '$$img'],
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
$project: {
|
|
98
|
+
_id: 1,
|
|
99
|
+
uri: 1,
|
|
100
|
+
},
|
|
101
|
+
},
|
|
102
|
+
],
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
$unwind: {
|
|
107
|
+
path: '$image',
|
|
108
|
+
preserveNullAndEmptyArrays: true,
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
]),
|
|
112
|
+
{
|
|
113
|
+
$project: Object.assign({ sequence: 0, imgs: 0 }, defaults_1.commonExcludedFields),
|
|
114
|
+
},
|
|
115
|
+
// {
|
|
116
|
+
// $lookup: {
|
|
117
|
+
// from: 'srcsets',
|
|
118
|
+
// let: { item: '$_id' },
|
|
119
|
+
// as: 'srcset',
|
|
120
|
+
// pipeline: [
|
|
121
|
+
// {
|
|
122
|
+
// $match: {
|
|
123
|
+
// $expr: {
|
|
124
|
+
// $eq: ['$itemId', '$$item'],
|
|
125
|
+
// },
|
|
126
|
+
// },
|
|
127
|
+
// },
|
|
128
|
+
// {
|
|
129
|
+
// $project: {
|
|
130
|
+
// ...commonExcludedFields,
|
|
131
|
+
// _id: 0,
|
|
132
|
+
// itemId: 0,
|
|
133
|
+
// },
|
|
134
|
+
// },
|
|
135
|
+
// ],
|
|
136
|
+
// },
|
|
137
|
+
// },
|
|
138
|
+
],
|
|
139
|
+
as: 'items',
|
|
140
|
+
},
|
|
141
|
+
},
|
|
142
|
+
]));
|
|
143
|
+
if (!widgetDataArr.length) {
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
const widgetData = widgetDataArr[0];
|
|
147
|
+
if (widgetData.collectionName &&
|
|
148
|
+
widgetData.collectionItems &&
|
|
149
|
+
widgetData.collectionItems.length > 0) {
|
|
150
|
+
const aggregateQueryItem = getAggregationQuery({
|
|
151
|
+
collectionName: widgetData.collectionName,
|
|
152
|
+
ids: widgetData.collectionItems,
|
|
153
|
+
});
|
|
154
|
+
const collectionModal = (0, helper_1.getCollectionModal)(widgetData.collectionName, models);
|
|
155
|
+
const collectionItems = yield collectionModal.aggregate(aggregateQueryItem);
|
|
156
|
+
widgetData.collectionItems = collectionItems;
|
|
157
|
+
}
|
|
158
|
+
if (widgetData.collectionName &&
|
|
159
|
+
widgetData.tabs &&
|
|
160
|
+
widgetData.tabs.length > 0) {
|
|
161
|
+
const tabCollectionItemIds = widgetData.tabs.reduce((acc, tabItem) => {
|
|
162
|
+
acc.push(...tabItem.collectionItems);
|
|
163
|
+
return acc;
|
|
164
|
+
}, []);
|
|
165
|
+
const aggregateQueryItem = getAggregationQuery({
|
|
166
|
+
collectionName: widgetData.collectionName,
|
|
167
|
+
ids: tabCollectionItemIds,
|
|
168
|
+
});
|
|
169
|
+
const collectionModal = (0, helper_1.getCollectionModal)(widgetData.collectionName, models);
|
|
170
|
+
const collectionItems = yield collectionModal.aggregate(aggregateQueryItem);
|
|
171
|
+
// converting colleciton items to obj to better access them
|
|
172
|
+
const collectionItemsObj = collectionItems.reduce((acc, item) => {
|
|
173
|
+
acc[item._id] = item;
|
|
174
|
+
return acc;
|
|
175
|
+
}, {});
|
|
176
|
+
widgetData.tabs = widgetData.tabs.map((tabItem) => {
|
|
177
|
+
return {
|
|
178
|
+
name: tabItem.name,
|
|
179
|
+
names: tabItem.names,
|
|
180
|
+
collectionItems: tabItem.collectionItems.map((collectionId) => collectionItemsObj[collectionId]),
|
|
181
|
+
};
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
(0, helper_1.AddSrcSetsToItems)(widgetData);
|
|
185
|
+
return widgetData;
|
|
186
|
+
});
|
|
187
|
+
exports.getWidgetDataDB = getWidgetDataDB;
|
|
188
|
+
const updateRedisWidget = (code, models) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
189
|
+
const widgetData = yield (0, exports.getWidgetDataDB)(code, models);
|
|
190
|
+
if (widgetData) {
|
|
191
|
+
yield (0, redis_1.setRedisValue)(`widgetData_${code}`, widgetData);
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
exports.updateRedisWidget = updateRedisWidget;
|
|
195
|
+
const updateWidgetPagesData = (widgetIds, models) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
196
|
+
const { Page } = models;
|
|
197
|
+
const pageCodes = yield Page.find({
|
|
198
|
+
widgets: {
|
|
199
|
+
$in: widgetIds,
|
|
200
|
+
},
|
|
201
|
+
}, 'code').distinct('code');
|
|
202
|
+
if (pageCodes.length) {
|
|
203
|
+
pageCodes.forEach((code) => {
|
|
204
|
+
(0, redis_1.deleteRedisValue)(`pageData_${code}`);
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
exports.updateWidgetPagesData = updateWidgetPagesData;
|
|
209
|
+
const getPageDataDB = (code, models) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
210
|
+
var _b;
|
|
211
|
+
const { Page } = models;
|
|
212
|
+
const pageData = (yield Page.aggregate([
|
|
213
|
+
{
|
|
214
|
+
$match: {
|
|
215
|
+
isDeleted: false,
|
|
216
|
+
code: code,
|
|
217
|
+
},
|
|
218
|
+
},
|
|
219
|
+
{
|
|
220
|
+
$project: {
|
|
221
|
+
isDeleted: 0,
|
|
222
|
+
deletedAt: 0,
|
|
223
|
+
__v: 0,
|
|
224
|
+
},
|
|
225
|
+
},
|
|
226
|
+
{
|
|
227
|
+
$lookup: {
|
|
228
|
+
from: 'widgets',
|
|
229
|
+
let: { widgets: '$widgets' },
|
|
230
|
+
pipeline: [
|
|
231
|
+
{
|
|
232
|
+
$match: {
|
|
233
|
+
$expr: {
|
|
234
|
+
$in: ['$_id', '$$widgets'],
|
|
235
|
+
},
|
|
236
|
+
isDeleted: false,
|
|
237
|
+
isActive: true,
|
|
238
|
+
},
|
|
239
|
+
},
|
|
240
|
+
{
|
|
241
|
+
$project: Object.assign({ widgetId: 0, sequence: 0 }, defaults_1.commonExcludedFields),
|
|
242
|
+
},
|
|
243
|
+
{
|
|
244
|
+
$lookup: {
|
|
245
|
+
from: 'items',
|
|
246
|
+
let: { widget: '$_id' },
|
|
247
|
+
pipeline: [
|
|
248
|
+
{
|
|
249
|
+
$match: {
|
|
250
|
+
$expr: {
|
|
251
|
+
$eq: ['$widgetId', '$$widget'],
|
|
252
|
+
},
|
|
253
|
+
isDeleted: false,
|
|
254
|
+
},
|
|
255
|
+
},
|
|
256
|
+
...(defaults_1.defaults.languages && ((_b = defaults_1.defaults.languages) === null || _b === void 0 ? void 0 : _b.length) > 0
|
|
257
|
+
? defaults_1.defaults.languages.reduce((arr, lng) => {
|
|
258
|
+
arr.push({
|
|
259
|
+
$lookup: {
|
|
260
|
+
from: 'file',
|
|
261
|
+
let: { img: { $toObjectId: `$imgs.${lng.code}` } },
|
|
262
|
+
as: `images.${lng.code}`,
|
|
263
|
+
pipeline: [
|
|
264
|
+
{
|
|
265
|
+
$match: {
|
|
266
|
+
$expr: {
|
|
267
|
+
$eq: ['$_id', '$$img'],
|
|
268
|
+
},
|
|
269
|
+
},
|
|
270
|
+
},
|
|
271
|
+
{
|
|
272
|
+
$project: {
|
|
273
|
+
_id: 1,
|
|
274
|
+
uri: 1,
|
|
275
|
+
},
|
|
276
|
+
},
|
|
277
|
+
],
|
|
278
|
+
},
|
|
279
|
+
}, {
|
|
280
|
+
$unwind: {
|
|
281
|
+
path: `$images.${lng.code}`,
|
|
282
|
+
preserveNullAndEmptyArrays: true,
|
|
283
|
+
},
|
|
284
|
+
});
|
|
285
|
+
return arr;
|
|
286
|
+
}, [])
|
|
287
|
+
: [
|
|
288
|
+
{
|
|
289
|
+
$lookup: {
|
|
290
|
+
from: 'file',
|
|
291
|
+
let: { img: '$img' },
|
|
292
|
+
as: 'image',
|
|
293
|
+
pipeline: [
|
|
294
|
+
{
|
|
295
|
+
$match: {
|
|
296
|
+
$expr: {
|
|
297
|
+
$eq: ['$_id', '$$img'],
|
|
298
|
+
},
|
|
299
|
+
},
|
|
300
|
+
},
|
|
301
|
+
{
|
|
302
|
+
$project: {
|
|
303
|
+
_id: 1,
|
|
304
|
+
uri: 1,
|
|
305
|
+
},
|
|
306
|
+
},
|
|
307
|
+
],
|
|
308
|
+
},
|
|
309
|
+
},
|
|
310
|
+
{
|
|
311
|
+
$unwind: {
|
|
312
|
+
path: '$image',
|
|
313
|
+
preserveNullAndEmptyArrays: true,
|
|
314
|
+
},
|
|
315
|
+
},
|
|
316
|
+
]),
|
|
317
|
+
{
|
|
318
|
+
$project: Object.assign({ sequence: 0, imgs: 0 }, defaults_1.commonExcludedFields),
|
|
319
|
+
},
|
|
320
|
+
// {
|
|
321
|
+
// $lookup: {
|
|
322
|
+
// from: 'srcsets',
|
|
323
|
+
// let: { item: '$_id' },
|
|
324
|
+
// as: 'srcset',
|
|
325
|
+
// pipeline: [
|
|
326
|
+
// {
|
|
327
|
+
// $match: {
|
|
328
|
+
// $expr: {
|
|
329
|
+
// $eq: ['$itemId', '$$item'],
|
|
330
|
+
// },
|
|
331
|
+
// },
|
|
332
|
+
// },
|
|
333
|
+
// {
|
|
334
|
+
// $project: {
|
|
335
|
+
// ...commonExcludedFields,
|
|
336
|
+
// _id: 0,
|
|
337
|
+
// itemId: 0,
|
|
338
|
+
// },
|
|
339
|
+
// },
|
|
340
|
+
// ],
|
|
341
|
+
// },
|
|
342
|
+
// },
|
|
343
|
+
],
|
|
344
|
+
as: 'items',
|
|
345
|
+
},
|
|
346
|
+
},
|
|
347
|
+
],
|
|
348
|
+
as: 'widgetsData',
|
|
349
|
+
},
|
|
350
|
+
},
|
|
351
|
+
]));
|
|
352
|
+
if (!pageData.length) {
|
|
353
|
+
return null;
|
|
354
|
+
}
|
|
355
|
+
pageData[0].widgetsData = yield (0, helper_1.appendCollectionData)(pageData[0].widgetsData, models);
|
|
356
|
+
if (Array.isArray(pageData[0].widgetsData) &&
|
|
357
|
+
pageData[0].widgetsData.length > 0) {
|
|
358
|
+
pageData[0].widgetsData.forEach((widget) => {
|
|
359
|
+
(0, helper_1.AddSrcSetsToItems)(widget);
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
pageData[0].widgetsData = pageData[0].widgetsData.reduce((acc, widgetItem) => {
|
|
363
|
+
acc[widgetItem._id] = widgetItem;
|
|
364
|
+
return acc;
|
|
365
|
+
}, []);
|
|
366
|
+
pageData[0].widgets = pageData[0].widgets
|
|
367
|
+
.map((widgetId) => pageData[0].widgetsData[widgetId])
|
|
368
|
+
.filter(Boolean);
|
|
369
|
+
delete pageData[0].widgetsData;
|
|
370
|
+
return pageData[0];
|
|
371
|
+
});
|
|
372
|
+
exports.getPageDataDB = getPageDataDB;
|
|
373
|
+
const updateRedisPage = (code, models) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
374
|
+
const pageData = yield (0, exports.getPageDataDB)(code, models);
|
|
375
|
+
if (pageData) {
|
|
376
|
+
yield (0, redis_1.setRedisValue)(`pageData_${code}`, pageData);
|
|
377
|
+
}
|
|
378
|
+
});
|
|
379
|
+
exports.updateRedisPage = updateRedisPage;
|
|
380
|
+
const handleUpdateData = (collectionName, itemId, models) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
381
|
+
if (!models)
|
|
382
|
+
throw new Error('models is required');
|
|
383
|
+
const { Widget } = models;
|
|
384
|
+
const widgets = yield Widget.find({
|
|
385
|
+
collectionName: collectionName,
|
|
386
|
+
collectionItems: {
|
|
387
|
+
$in: Array.isArray(itemId) ? itemId : [itemId],
|
|
388
|
+
},
|
|
389
|
+
}, 'code _id').lean();
|
|
390
|
+
if (widgets.length) {
|
|
391
|
+
(0, exports.updateWidgetPagesData)(widgets.map((widget) => widget._id), models);
|
|
392
|
+
widgets.forEach((widget) => {
|
|
393
|
+
(0, redis_1.deleteRedisValue)(`widgetData_${widget['code']}`);
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
});
|
|
397
|
+
exports.handleUpdateData = handleUpdateData;
|
|
398
|
+
const handleResetData = (type, code) => {
|
|
399
|
+
if ((type === 'widget' || type === 'page') && code)
|
|
400
|
+
(0, redis_1.deleteRedisValue)(`${type}_${code}`);
|
|
401
|
+
};
|
|
402
|
+
exports.handleResetData = handleResetData;
|
|
403
403
|
//# sourceMappingURL=dataService.js.map
|