@knovator/pagecreator-node 1.2.0 → 1.2.2

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.
Files changed (128) hide show
  1. package/package.json +4 -2
  2. package/src/constants/index.d.ts +20 -0
  3. package/src/constants/index.js +34 -0
  4. package/src/constants/index.js.map +1 -0
  5. package/src/controllers/PageController.d.ts +4 -0
  6. package/src/controllers/PageController.js +84 -0
  7. package/src/controllers/PageController.js.map +1 -0
  8. package/src/controllers/TabController.d.ts +4 -0
  9. package/src/controllers/TabController.js +46 -0
  10. package/src/controllers/TabController.js.map +1 -0
  11. package/src/controllers/UserController.d.ts +2 -0
  12. package/src/controllers/UserController.js +65 -0
  13. package/src/controllers/UserController.js.map +1 -0
  14. package/src/controllers/WidgetController.d.ts +10 -0
  15. package/src/controllers/WidgetController.js +329 -0
  16. package/src/controllers/WidgetController.js.map +1 -0
  17. package/src/index.d.ts +8 -0
  18. package/src/index.js +48 -0
  19. package/src/index.js.map +1 -0
  20. package/src/models/Item.d.ts +30 -0
  21. package/src/models/Item.js +50 -0
  22. package/src/models/Item.js.map +1 -0
  23. package/src/models/Page.d.ts +30 -0
  24. package/src/models/Page.js +23 -0
  25. package/src/models/Page.js.map +1 -0
  26. package/src/models/SrcSet.d.ts +30 -0
  27. package/src/models/SrcSet.js +17 -0
  28. package/src/models/SrcSet.js.map +1 -0
  29. package/src/models/Tab.d.ts +30 -0
  30. package/src/models/Tab.js +28 -0
  31. package/src/models/Tab.js.map +1 -0
  32. package/src/models/Widget.d.ts +30 -0
  33. package/src/models/Widget.js +63 -0
  34. package/src/models/Widget.js.map +1 -0
  35. package/src/models/{index.ts → index.d.ts} +1 -13
  36. package/src/models/index.js +19 -0
  37. package/src/models/index.js.map +1 -0
  38. package/src/plugins/softDelete.d.ts +7 -0
  39. package/src/plugins/softDelete.js +64 -0
  40. package/src/plugins/softDelete.js.map +1 -0
  41. package/src/routes/PageRoute.d.ts +3 -0
  42. package/src/routes/PageRoute.js +30 -0
  43. package/src/routes/PageRoute.js.map +1 -0
  44. package/src/routes/UserRoute.d.ts +3 -0
  45. package/src/routes/UserRoute.js +23 -0
  46. package/src/routes/UserRoute.js.map +1 -0
  47. package/src/routes/WidgetRoute.d.ts +3 -0
  48. package/src/routes/WidgetRoute.js +68 -0
  49. package/src/routes/WidgetRoute.js.map +1 -0
  50. package/src/services/dataService.d.ts +9 -0
  51. package/src/services/dataService.js +403 -0
  52. package/src/services/dataService.js.map +1 -0
  53. package/src/services/dbService.d.ts +16 -0
  54. package/src/services/dbService.js +88 -0
  55. package/src/services/dbService.js.map +1 -0
  56. package/src/types/{IRequest.ts → IRequest.d.ts} +3 -4
  57. package/src/types/IRequest.js +3 -0
  58. package/src/types/IRequest.js.map +1 -0
  59. package/src/types/{IResponse.ts → IResponse.d.ts} +3 -4
  60. package/src/types/IResponse.js +3 -0
  61. package/src/types/IResponse.js.map +1 -0
  62. package/src/types/{Router.ts → Router.d.ts} +1 -2
  63. package/src/types/Router.js +3 -0
  64. package/src/types/Router.js.map +1 -0
  65. package/src/types/common.d.ts +142 -0
  66. package/src/types/common.js +3 -0
  67. package/src/types/common.js.map +1 -0
  68. package/src/types/enums.d.ts +14 -0
  69. package/src/types/enums.js +21 -0
  70. package/src/types/enums.js.map +1 -0
  71. package/src/types/index.js +9 -0
  72. package/src/types/index.js.map +1 -0
  73. package/src/utils/defaults.d.ts +7 -0
  74. package/src/utils/defaults.js +41 -0
  75. package/src/utils/defaults.js.map +1 -0
  76. package/src/utils/helper.d.ts +7 -0
  77. package/src/utils/helper.js +225 -0
  78. package/src/utils/helper.js.map +1 -0
  79. package/src/utils/redis.d.ts +3 -0
  80. package/src/utils/redis.js +59 -0
  81. package/src/utils/redis.js.map +1 -0
  82. package/src/utils/responseHandlers.d.ts +6 -0
  83. package/src/utils/responseHandlers.js +57 -0
  84. package/src/utils/responseHandlers.js.map +1 -0
  85. package/src/utils/validate.d.ts +4 -0
  86. package/src/utils/validate.js +24 -0
  87. package/src/utils/validate.js.map +1 -0
  88. package/src/utils/validations/page.d.ts +7 -0
  89. package/src/utils/validations/page.js +51 -0
  90. package/src/utils/validations/page.js.map +1 -0
  91. package/src/utils/validations/tab.d.ts +6 -0
  92. package/src/utils/validations/tab.js +26 -0
  93. package/src/utils/validations/tab.js.map +1 -0
  94. package/src/utils/validations/user.d.ts +3 -0
  95. package/src/utils/validations/user.js +13 -0
  96. package/src/utils/validations/user.js.map +1 -0
  97. package/src/utils/validations/widget.d.ts +9 -0
  98. package/src/utils/validations/widget.js +153 -0
  99. package/src/utils/validations/widget.js.map +1 -0
  100. package/src/constants/index.ts +0 -30
  101. package/src/controllers/PageController.ts +0 -88
  102. package/src/controllers/TabController.ts +0 -47
  103. package/src/controllers/UserController.ts +0 -69
  104. package/src/controllers/WidgetController.ts +0 -420
  105. package/src/index.ts +0 -59
  106. package/src/models/Item.ts +0 -53
  107. package/src/models/Page.ts +0 -22
  108. package/src/models/SrcSet.ts +0 -16
  109. package/src/models/Tab.ts +0 -28
  110. package/src/models/Widget.ts +0 -65
  111. package/src/plugins/softDelete.ts +0 -84
  112. package/src/routes/PageRoute.ts +0 -30
  113. package/src/routes/UserRoute.ts +0 -32
  114. package/src/routes/WidgetRoute.ts +0 -77
  115. package/src/services/dataService.ts +0 -475
  116. package/src/services/dbService.ts +0 -102
  117. package/src/types/common.ts +0 -172
  118. package/src/types/enums.ts +0 -16
  119. package/src/utils/defaults.ts +0 -41
  120. package/src/utils/helper.ts +0 -285
  121. package/src/utils/redis.ts +0 -57
  122. package/src/utils/responseHandlers.ts +0 -60
  123. package/src/utils/validate.ts +0 -22
  124. package/src/utils/validations/page.ts +0 -52
  125. package/src/utils/validations/tab.ts +0 -26
  126. package/src/utils/validations/user.ts +0 -10
  127. package/src/utils/validations/widget.ts +0 -169
  128. /package/src/types/{index.ts → index.d.ts} +0 -0
@@ -1,84 +0,0 @@
1
- import mongoose from 'mongoose';
2
-
3
- export type TWithSoftDeleted = {
4
- isDeleted?: boolean;
5
- deletedAt?: Date | null;
6
- };
7
-
8
- type TDocument = TWithSoftDeleted & mongoose.Document;
9
-
10
- const softDeletePlugin = (schema: mongoose.Schema) => {
11
- schema.add({
12
- isDeleted: {
13
- type: Boolean,
14
- required: true,
15
- default: false,
16
- },
17
- deletedAt: {
18
- type: Date,
19
- default: null,
20
- },
21
- });
22
-
23
- const typesFindQueryMiddleware = [
24
- 'count',
25
- 'find',
26
- 'findOne',
27
- 'findOneAndDelete',
28
- 'findOneAndRemove',
29
- 'findOneAndUpdate',
30
- 'update',
31
- 'updateOne',
32
- 'updateMany',
33
- ];
34
-
35
- const setDocumentIsDeleted = async (doc: TDocument) => {
36
- doc.isDeleted = true;
37
- doc.deletedAt = new Date();
38
- doc.$isDeleted(true);
39
- await doc.save();
40
- };
41
-
42
- const excludeInFindQueriesIsDeleted = async function (
43
- this: mongoose.FilterQuery<TDocument>,
44
- next: mongoose.CallbackWithoutResultAndOptionalError
45
- ) {
46
- this['where']({ isDeleted: false });
47
- next();
48
- };
49
-
50
- const excludeInDeletedInAggregateMiddleware = async function (
51
- this: mongoose.Aggregate<any>,
52
- next: mongoose.CallbackWithoutResultAndOptionalError
53
- ) {
54
- this.pipeline().unshift({ $match: { isDeleted: false } });
55
- next();
56
- };
57
-
58
- schema.pre(
59
- 'remove',
60
- async function (
61
- this: TDocument,
62
- next: mongoose.CallbackWithoutResultAndOptionalError
63
- ) {
64
- await setDocumentIsDeleted(this);
65
- next();
66
- }
67
- );
68
-
69
- typesFindQueryMiddleware.forEach((type: any) => {
70
- schema.pre(type, excludeInFindQueriesIsDeleted);
71
- });
72
-
73
- schema.pre('aggregate', excludeInDeletedInAggregateMiddleware);
74
-
75
- schema.method('toJSON', function () {
76
- const obj: TDocument = this.toObject();
77
- delete obj.isDeleted;
78
- delete obj.deletedAt;
79
-
80
- return obj;
81
- });
82
- };
83
-
84
- export { softDeletePlugin };
@@ -1,30 +0,0 @@
1
- import express from 'express';
2
- // eslint-disable-next-line @typescript-eslint/no-var-requires
3
- require('express-list-endpoints-descriptor')(express);
4
- import { IRouter } from '../types';
5
-
6
- import validate from '../utils/validate';
7
- import * as pageValidation from '../utils/validations/page';
8
- import * as pageController from '../controllers/PageController';
9
-
10
- const descriptorPrefix = process.env['PAGECREATOR_DESCRIPTOR_PREFIX'] || '';
11
- const routes = express.Router() as IRouter;
12
- routes.use(express.json());
13
-
14
- // Get all pages
15
- routes
16
- .post(`/list`, validate(pageValidation.list), pageController.getPages)
17
- .descriptor(`${descriptorPrefix}page.getAll`);
18
- // Create a page
19
- routes
20
- .post(`/`, validate(pageValidation.create), pageController.createPage)
21
- .descriptor(`${descriptorPrefix}page.create`);
22
- // Update a page
23
- routes
24
- .put(`/:id`, validate(pageValidation.update), pageController.updatePage)
25
- .descriptor(`${descriptorPrefix}page.update`);
26
- // Delete a page
27
- routes
28
- .delete(`/:id`, pageController.deletePage)
29
- .descriptor(`${descriptorPrefix}page.delete`);
30
- export default routes;
@@ -1,32 +0,0 @@
1
- import express from 'express';
2
- // eslint-disable-next-line @typescript-eslint/no-var-requires
3
- require('express-list-endpoints-descriptor')(express);
4
- import { IRouter } from '../types';
5
-
6
- import validate from '../utils/validate';
7
- import * as userValidation from '../utils/validations/user';
8
- import * as userController from '../controllers/UserController';
9
-
10
- const descriptorPrefix = process.env['PAGECREATOR_DESCRIPTOR_PREFIX'] || '';
11
- const routes = express.Router() as IRouter;
12
- routes.use(express.json());
13
-
14
- // Item Routes
15
- // Get Widget Data
16
- routes
17
- .post(
18
- `/widget-data`,
19
- validate(userValidation.getWidgetData),
20
- userController.getWidgetData
21
- )
22
- .descriptor(`${descriptorPrefix}user.getWidgetData`);
23
- // Get Page Data
24
- routes
25
- .post(
26
- '/page-data',
27
- validate(userValidation.getPageData),
28
- userController.getPageData
29
- )
30
- .descriptor(`${descriptorPrefix}user.getPageData`);
31
-
32
- export default routes;
@@ -1,77 +0,0 @@
1
- import express from 'express';
2
- // eslint-disable-next-line @typescript-eslint/no-var-requires
3
- require('express-list-endpoints-descriptor')(express);
4
- import { IRouter } from '../types';
5
-
6
- import validate from '../utils/validate';
7
- import * as tabValidation from '../utils/validations/tab';
8
- import * as widgetValidation from '../utils/validations/widget';
9
- import * as widgetController from '../controllers/WidgetController';
10
- import * as tabController from '../controllers/TabController';
11
-
12
- const descriptorPrefix = process.env['PAGECREATOR_DESCRIPTOR_PREFIX'] || '';
13
- const routes = express.Router() as IRouter;
14
- routes.use(express.json());
15
-
16
- // Widget Routes
17
- // Get widget types
18
- routes
19
- .get('/widget-types', widgetController.getItemsTypes)
20
- .descriptor(`${descriptorPrefix}widget.getItemsTypes`);
21
- // Get widget selection types
22
- routes
23
- .get('/selection-types', widgetController.getWidgetTypes)
24
- .descriptor(`${descriptorPrefix}widget.getWidgetTypes`);
25
- // Get all widgets
26
- routes
27
- .post(`/list`, validate(widgetValidation.list), widgetController.getWidgets)
28
- .descriptor(`${descriptorPrefix}widget.getAll`);
29
- // Create a widget
30
- routes
31
- .post(`/`, validate(widgetValidation.create), widgetController.createWidget)
32
- .descriptor(`${descriptorPrefix}widget.create`);
33
- // Update a widget
34
- routes
35
- .put(`/:id`, validate(widgetValidation.update), widgetController.updateWidget)
36
- .descriptor(`${descriptorPrefix}widget.update`);
37
- // Partial Update a widget
38
- routes
39
- .patch(
40
- `/:id`,
41
- validate(widgetValidation.partialUpdate),
42
- widgetController.partialUpdateWidget
43
- )
44
- .descriptor(`${descriptorPrefix}widget.partialUpdate`);
45
- // Delete a widget
46
- routes
47
- .delete(`/:id`, widgetController.deleteWidget)
48
- .descriptor(`${descriptorPrefix}widget.delete`);
49
- // Get dynamic collection data
50
- routes
51
- .post(
52
- '/collection-data',
53
- validate(widgetValidation.getCollectionData),
54
- widgetController.getCollectionData
55
- )
56
- .descriptor(`${descriptorPrefix}widget.getCollectionData`);
57
- // Tabs
58
- routes
59
- .post('/tabs', validate(tabValidation.create), tabController.createTab)
60
- .descriptor(`${descriptorPrefix}tab.create`);
61
- routes
62
- .put('/tabs/:tabId', validate(tabValidation.update), tabController.updateTab)
63
- .descriptor(`${descriptorPrefix}tab.update`);
64
- routes
65
- .delete('/tabs/:tabId', tabController.deleteTab)
66
- .descriptor(`${descriptorPrefix}tab.delete`);
67
- routes
68
- .get('/tabs/:widgetId', tabController.getTabs)
69
- .descriptor(`${descriptorPrefix}tab.getAll`);
70
- // Get all languages
71
- routes.get(`/languages`, widgetController.getLanguages);
72
- // Get single widget
73
- routes
74
- .get(`/:id`, widgetController.getSingleWidget)
75
- .descriptor(`${descriptorPrefix}widget.getOne`);
76
-
77
- export default routes;
@@ -1,475 +0,0 @@
1
- import { AggregateOptions, Models } from 'mongoose';
2
- import {
3
- AddSrcSetsToItems,
4
- appendCollectionData,
5
- getCollectionModal,
6
- } from '../utils/helper';
7
- import { setRedisValue, deleteRedisValue } from '../utils/redis';
8
- import { defaults, commonExcludedFields } from '../utils/defaults';
9
- import { IPageSchema, IWidgetSchema } from '../types';
10
-
11
- const getAggregationQuery = ({
12
- collectionName,
13
- ids,
14
- }: {
15
- collectionName: string;
16
- ids: string[];
17
- }) => {
18
- const collectionConfig = defaults.collections.find(
19
- (c) => c.collectionName === collectionName
20
- );
21
- const aggregateQueryItem: AggregateOptions[] = [];
22
- if (
23
- Array.isArray(collectionConfig?.aggregations) &&
24
- collectionConfig?.aggregations.length
25
- ) {
26
- aggregateQueryItem.push(...collectionConfig.aggregations);
27
- }
28
- aggregateQueryItem.push(
29
- {
30
- $match: {
31
- _id: {
32
- $in: ids,
33
- },
34
- ...(collectionConfig?.match || {}),
35
- },
36
- },
37
- { $addFields: { __order: { $indexOfArray: [ids, '$_id'] } } },
38
- { $sort: { __order: 1 } }
39
- );
40
- return aggregateQueryItem;
41
- };
42
-
43
- export const getWidgetDataDB = async (code: string, models: Models) => {
44
- const { Widget } = models;
45
- const widgetDataArr = (await Widget.aggregate([
46
- {
47
- $match: {
48
- isDeleted: false,
49
- isActive: true,
50
- code,
51
- },
52
- },
53
- {
54
- // Get only the fields that are not excluded
55
- $project: {
56
- ...commonExcludedFields,
57
- },
58
- },
59
- {
60
- // Get Items data
61
- $lookup: {
62
- from: 'items',
63
- let: { widget: '$_id' },
64
- pipeline: [
65
- {
66
- $match: {
67
- $expr: {
68
- $eq: ['$widgetId', '$$widget'],
69
- },
70
- isDeleted: false,
71
- },
72
- },
73
- ...(defaults.languages && defaults.languages?.length > 0
74
- ? defaults.languages.reduce((arr: any[], lng) => {
75
- arr.push(
76
- {
77
- $lookup: {
78
- from: 'file',
79
- let: { img: { $toObjectId: `$imgs.${lng.code}` } },
80
- as: `images.${lng.code}`,
81
- pipeline: [
82
- {
83
- $match: {
84
- $expr: {
85
- $eq: ['$_id', '$$img'],
86
- },
87
- },
88
- },
89
- {
90
- $project: {
91
- _id: 1,
92
- uri: 1,
93
- },
94
- },
95
- ],
96
- },
97
- },
98
- {
99
- $unwind: {
100
- path: `$images.${lng.code}`,
101
- preserveNullAndEmptyArrays: true,
102
- },
103
- }
104
- );
105
- return arr;
106
- }, [])
107
- : [
108
- {
109
- $lookup: {
110
- from: 'file',
111
- let: { img: '$img' },
112
- as: 'image',
113
- pipeline: [
114
- {
115
- $match: {
116
- $expr: {
117
- $eq: ['$_id', '$$img'],
118
- },
119
- },
120
- },
121
- {
122
- $project: {
123
- _id: 1,
124
- uri: 1,
125
- },
126
- },
127
- ],
128
- },
129
- },
130
- {
131
- $unwind: {
132
- path: '$image',
133
- preserveNullAndEmptyArrays: true,
134
- },
135
- },
136
- ]),
137
- {
138
- $project: {
139
- sequence: 0,
140
- imgs: 0,
141
- ...commonExcludedFields,
142
- },
143
- },
144
- // {
145
- // $lookup: {
146
- // from: 'srcsets',
147
- // let: { item: '$_id' },
148
- // as: 'srcset',
149
- // pipeline: [
150
- // {
151
- // $match: {
152
- // $expr: {
153
- // $eq: ['$itemId', '$$item'],
154
- // },
155
- // },
156
- // },
157
- // {
158
- // $project: {
159
- // ...commonExcludedFields,
160
- // _id: 0,
161
- // itemId: 0,
162
- // },
163
- // },
164
- // ],
165
- // },
166
- // },
167
- ],
168
- as: 'items',
169
- },
170
- },
171
- ])) as Array<IWidgetSchema>;
172
-
173
- if (!widgetDataArr.length) {
174
- return null;
175
- }
176
- const widgetData = widgetDataArr[0];
177
-
178
- if (
179
- widgetData.collectionName &&
180
- widgetData.collectionItems &&
181
- widgetData.collectionItems.length > 0
182
- ) {
183
- const aggregateQueryItem = getAggregationQuery({
184
- collectionName: widgetData.collectionName,
185
- ids: widgetData.collectionItems,
186
- });
187
- const collectionModal: any = getCollectionModal(widgetData.collectionName, models);
188
- const collectionItems = await collectionModal.aggregate(aggregateQueryItem);
189
- widgetData.collectionItems = collectionItems;
190
- }
191
- if (
192
- widgetData.collectionName &&
193
- widgetData.tabs &&
194
- widgetData.tabs.length > 0
195
- ) {
196
- const tabCollectionItemIds = widgetData.tabs.reduce(
197
- (acc: string[], tabItem: any) => {
198
- acc.push(...tabItem.collectionItems);
199
- return acc;
200
- },
201
- []
202
- );
203
- const aggregateQueryItem = getAggregationQuery({
204
- collectionName: widgetData.collectionName,
205
- ids: tabCollectionItemIds,
206
- });
207
-
208
- const collectionModal: any = getCollectionModal(widgetData.collectionName, models);
209
- const collectionItems: any = await collectionModal.aggregate(
210
- aggregateQueryItem
211
- );
212
- // converting colleciton items to obj to better access them
213
- const collectionItemsObj = collectionItems.reduce((acc: any, item: any) => {
214
- acc[item._id] = item;
215
- return acc;
216
- }, {});
217
- widgetData.tabs = widgetData.tabs.map((tabItem) => {
218
- return {
219
- name: tabItem.name,
220
- names: tabItem.names,
221
- collectionItems: tabItem.collectionItems.map(
222
- (collectionId) => collectionItemsObj[collectionId]
223
- ),
224
- };
225
- });
226
- }
227
- AddSrcSetsToItems(widgetData);
228
- return widgetData;
229
- };
230
-
231
- export const updateRedisWidget = async (code: string, models: Models) => {
232
- const widgetData = await getWidgetDataDB(code, models);
233
- if (widgetData) {
234
- await setRedisValue(`widgetData_${code}`, widgetData as unknown as JSON);
235
- }
236
- };
237
-
238
- export const updateWidgetPagesData = async (
239
- widgetIds: string[],
240
- models: Models
241
- ) => {
242
- const { Page } = models;
243
- const pageCodes = await Page.find(
244
- {
245
- widgets: {
246
- $in: widgetIds,
247
- },
248
- },
249
- 'code'
250
- ).distinct('code');
251
- if (pageCodes.length) {
252
- pageCodes.forEach((code) => {
253
- deleteRedisValue(`pageData_${code}`);
254
- });
255
- }
256
- };
257
-
258
- export const getPageDataDB = async (code: string, models: Models) => {
259
- const { Page } = models;
260
- const pageData: any = (await Page.aggregate([
261
- {
262
- $match: {
263
- isDeleted: false,
264
- code: code,
265
- },
266
- },
267
- {
268
- $project: {
269
- isDeleted: 0,
270
- deletedAt: 0,
271
- __v: 0,
272
- },
273
- },
274
- {
275
- $lookup: {
276
- from: 'widgets',
277
- let: { widgets: '$widgets' },
278
- pipeline: [
279
- {
280
- $match: {
281
- $expr: {
282
- $in: ['$_id', '$$widgets'],
283
- },
284
- isDeleted: false,
285
- isActive: true,
286
- },
287
- },
288
- {
289
- $project: {
290
- widgetId: 0,
291
- sequence: 0,
292
- ...commonExcludedFields,
293
- },
294
- },
295
- {
296
- $lookup: {
297
- from: 'items',
298
- let: { widget: '$_id' },
299
- pipeline: [
300
- {
301
- $match: {
302
- $expr: {
303
- $eq: ['$widgetId', '$$widget'],
304
- },
305
- isDeleted: false,
306
- },
307
- },
308
- ...(defaults.languages && defaults.languages?.length > 0
309
- ? defaults.languages.reduce((arr: any[], lng) => {
310
- arr.push(
311
- {
312
- $lookup: {
313
- from: 'file',
314
- let: { img: { $toObjectId: `$imgs.${lng.code}` } },
315
- as: `images.${lng.code}`,
316
- pipeline: [
317
- {
318
- $match: {
319
- $expr: {
320
- $eq: ['$_id', '$$img'],
321
- },
322
- },
323
- },
324
- {
325
- $project: {
326
- _id: 1,
327
- uri: 1,
328
- },
329
- },
330
- ],
331
- },
332
- },
333
- {
334
- $unwind: {
335
- path: `$images.${lng.code}`,
336
- preserveNullAndEmptyArrays: true,
337
- },
338
- }
339
- );
340
- return arr;
341
- }, [])
342
- : [
343
- {
344
- $lookup: {
345
- from: 'file',
346
- let: { img: '$img' },
347
- as: 'image',
348
- pipeline: [
349
- {
350
- $match: {
351
- $expr: {
352
- $eq: ['$_id', '$$img'],
353
- },
354
- },
355
- },
356
- {
357
- $project: {
358
- _id: 1,
359
- uri: 1,
360
- },
361
- },
362
- ],
363
- },
364
- },
365
- {
366
- $unwind: {
367
- path: '$image',
368
- preserveNullAndEmptyArrays: true,
369
- },
370
- },
371
- ]),
372
- {
373
- $project: {
374
- sequence: 0,
375
- imgs: 0,
376
- ...commonExcludedFields,
377
- },
378
- },
379
- // {
380
- // $lookup: {
381
- // from: 'srcsets',
382
- // let: { item: '$_id' },
383
- // as: 'srcset',
384
- // pipeline: [
385
- // {
386
- // $match: {
387
- // $expr: {
388
- // $eq: ['$itemId', '$$item'],
389
- // },
390
- // },
391
- // },
392
- // {
393
- // $project: {
394
- // ...commonExcludedFields,
395
- // _id: 0,
396
- // itemId: 0,
397
- // },
398
- // },
399
- // ],
400
- // },
401
- // },
402
- ],
403
- as: 'items',
404
- },
405
- },
406
- ],
407
- as: 'widgetsData',
408
- },
409
- },
410
- ])) as Array<IPageSchema>;
411
-
412
- if (!pageData.length) {
413
- return null;
414
- }
415
- pageData[0].widgetsData = await appendCollectionData(pageData[0].widgetsData, models);
416
- if (
417
- Array.isArray(pageData[0].widgetsData) &&
418
- pageData[0].widgetsData.length > 0
419
- ) {
420
- pageData[0].widgetsData.forEach((widget: IWidgetSchema) => {
421
- AddSrcSetsToItems(widget);
422
- });
423
- }
424
- pageData[0].widgetsData = pageData[0].widgetsData.reduce(
425
- (acc: any, widgetItem: any) => {
426
- acc[widgetItem._id] = widgetItem;
427
- return acc;
428
- },
429
- []
430
- );
431
- pageData[0].widgets = pageData[0].widgets
432
- .map((widgetId: string) => pageData[0].widgetsData[widgetId])
433
- .filter(Boolean);
434
- delete pageData[0].widgetsData;
435
- return pageData[0];
436
- };
437
-
438
- export const updateRedisPage = async (code: string, models: Models) => {
439
- const pageData = await getPageDataDB(code, models);
440
- if (pageData) {
441
- await setRedisValue(`pageData_${code}`, pageData);
442
- }
443
- };
444
-
445
- export const handleUpdateData = async (
446
- collectionName: string,
447
- itemId: string | string[],
448
- models: Models
449
- ) => {
450
- if (!models) throw new Error('models is required');
451
- const { Widget } = models;
452
- const widgets = await Widget.find(
453
- {
454
- collectionName: collectionName,
455
- collectionItems: {
456
- $in: Array.isArray(itemId) ? itemId : [itemId],
457
- },
458
- },
459
- 'code _id'
460
- ).lean();
461
- if (widgets.length) {
462
- updateWidgetPagesData(
463
- widgets.map((widget: any) => widget._id),
464
- models
465
- );
466
- widgets.forEach((widget) => {
467
- deleteRedisValue(`widgetData_${widget['code']}`);
468
- });
469
- }
470
- };
471
-
472
- export const handleResetData = (type: 'widget' | 'page', code: string) => {
473
- if ((type === 'widget' || type === 'page') && code)
474
- deleteRedisValue(`${type}_${code}`);
475
- };