@knovator/pagecreator-node 0.0.6 → 0.2.0

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 (116) hide show
  1. package/README.md +230 -161
  2. package/package.json +3 -6
  3. package/src/constants/index.ts +26 -0
  4. package/src/controllers/ItemController.ts +139 -0
  5. package/src/controllers/PageController.ts +65 -0
  6. package/src/controllers/UserController.ts +265 -0
  7. package/src/controllers/WidgetController.ts +165 -0
  8. package/src/index.ts +29 -0
  9. package/src/models/Item.ts +29 -0
  10. package/src/models/Page.ts +17 -0
  11. package/src/models/SrcSet.ts +16 -0
  12. package/src/models/Widget.ts +44 -0
  13. package/src/models/index.ts +6 -0
  14. package/src/plugins/softDelete.ts +84 -0
  15. package/src/routes/ItemRoute.ts +32 -0
  16. package/src/routes/PageRoute.ts +31 -0
  17. package/src/routes/UserRoute.ts +32 -0
  18. package/src/routes/WidgetRoute.ts +56 -0
  19. package/src/services/dbService.ts +82 -0
  20. package/src/types/{IRequest.d.ts → IRequest.ts} +4 -3
  21. package/src/types/{IResponse.d.ts → IResponse.ts} +4 -3
  22. package/src/types/{Router.d.ts → Router.ts} +2 -1
  23. package/src/types/common.ts +130 -0
  24. package/src/types/enums.ts +13 -0
  25. package/src/types/{index.d.ts → index.ts} +0 -0
  26. package/src/utils/defaults.ts +30 -0
  27. package/src/utils/helper.ts +133 -0
  28. package/src/utils/responseHandlers.ts +54 -0
  29. package/src/utils/validate.ts +22 -0
  30. package/src/utils/validations/item.ts +51 -0
  31. package/src/utils/validations/page.ts +59 -0
  32. package/src/utils/validations/user.ts +9 -0
  33. package/src/utils/validations/widget.ts +118 -0
  34. package/src/constants/index.d.ts +0 -16
  35. package/src/constants/index.js +0 -30
  36. package/src/constants/index.js.map +0 -1
  37. package/src/controllers/PageController.d.ts +0 -4
  38. package/src/controllers/PageController.js +0 -61
  39. package/src/controllers/PageController.js.map +0 -1
  40. package/src/controllers/TileController.d.ts +0 -4
  41. package/src/controllers/TileController.js +0 -45
  42. package/src/controllers/TileController.js.map +0 -1
  43. package/src/controllers/UserController.d.ts +0 -2
  44. package/src/controllers/UserController.js +0 -174
  45. package/src/controllers/UserController.js.map +0 -1
  46. package/src/controllers/WidgetController.d.ts +0 -8
  47. package/src/controllers/WidgetController.js +0 -129
  48. package/src/controllers/WidgetController.js.map +0 -1
  49. package/src/index.d.ts +0 -8
  50. package/src/index.js +0 -29
  51. package/src/index.js.map +0 -1
  52. package/src/models/Page.d.ts +0 -3
  53. package/src/models/Page.js +0 -16
  54. package/src/models/Page.js.map +0 -1
  55. package/src/models/Tile.d.ts +0 -3
  56. package/src/models/Tile.js +0 -29
  57. package/src/models/Tile.js.map +0 -1
  58. package/src/models/Widget.d.ts +0 -3
  59. package/src/models/Widget.js +0 -41
  60. package/src/models/Widget.js.map +0 -1
  61. package/src/models/index.d.ts +0 -4
  62. package/src/models/index.js +0 -11
  63. package/src/models/index.js.map +0 -1
  64. package/src/plugins/softDelete.d.ts +0 -7
  65. package/src/plugins/softDelete.js +0 -64
  66. package/src/plugins/softDelete.js.map +0 -1
  67. package/src/routes/PageRoute.d.ts +0 -3
  68. package/src/routes/PageRoute.js +0 -30
  69. package/src/routes/PageRoute.js.map +0 -1
  70. package/src/routes/TileRoute.d.ts +0 -3
  71. package/src/routes/TileRoute.js +0 -31
  72. package/src/routes/TileRoute.js.map +0 -1
  73. package/src/routes/UserRoute.d.ts +0 -3
  74. package/src/routes/UserRoute.js +0 -22
  75. package/src/routes/UserRoute.js.map +0 -1
  76. package/src/routes/WidgetRoute.d.ts +0 -3
  77. package/src/routes/WidgetRoute.js +0 -47
  78. package/src/routes/WidgetRoute.js.map +0 -1
  79. package/src/services/dbService.d.ts +0 -8
  80. package/src/services/dbService.js +0 -60
  81. package/src/services/dbService.js.map +0 -1
  82. package/src/types/IRequest.js +0 -3
  83. package/src/types/IRequest.js.map +0 -1
  84. package/src/types/IResponse.js +0 -3
  85. package/src/types/IResponse.js.map +0 -1
  86. package/src/types/Router.js +0 -3
  87. package/src/types/Router.js.map +0 -1
  88. package/src/types/common.d.ts +0 -66
  89. package/src/types/common.js +0 -3
  90. package/src/types/common.js.map +0 -1
  91. package/src/types/enums.d.ts +0 -11
  92. package/src/types/enums.js +0 -18
  93. package/src/types/enums.js.map +0 -1
  94. package/src/types/index.js +0 -9
  95. package/src/types/index.js.map +0 -1
  96. package/src/utils/defaults.d.ts +0 -2
  97. package/src/utils/defaults.js +0 -23
  98. package/src/utils/defaults.js.map +0 -1
  99. package/src/utils/responseHandlers.d.ts +0 -6
  100. package/src/utils/responseHandlers.js +0 -57
  101. package/src/utils/responseHandlers.js.map +0 -1
  102. package/src/utils/validate.d.ts +0 -4
  103. package/src/utils/validate.js +0 -24
  104. package/src/utils/validate.js.map +0 -1
  105. package/src/utils/validations/page.d.ts +0 -7
  106. package/src/utils/validations/page.js +0 -58
  107. package/src/utils/validations/page.js.map +0 -1
  108. package/src/utils/validations/tile.d.ts +0 -7
  109. package/src/utils/validations/tile.js +0 -43
  110. package/src/utils/validations/tile.js.map +0 -1
  111. package/src/utils/validations/user.d.ts +0 -3
  112. package/src/utils/validations/user.js +0 -12
  113. package/src/utils/validations/user.js.map +0 -1
  114. package/src/utils/validations/widget.d.ts +0 -9
  115. package/src/utils/validations/widget.js +0 -108
  116. package/src/utils/validations/widget.js.map +0 -1
@@ -0,0 +1,139 @@
1
+ import { Types } from 'mongoose';
2
+ import { SrcSet, Item } from '../models';
3
+ import {
4
+ create,
5
+ remove,
6
+ update,
7
+ deleteAll,
8
+ bulkInsert,
9
+ } from '../services/dbService';
10
+ import {
11
+ successResponse,
12
+ createdDocumentResponse,
13
+ } from '../utils/responseHandlers';
14
+
15
+ import { commonExcludedFields, defaults } from '../utils/defaults';
16
+ import { IRequest, IResponse, IItemSchema, SrcSetItem } from '../types';
17
+
18
+ const catchAsync = (fn: any) => {
19
+ return defaults.catchAsync(fn, 'Notification');
20
+ };
21
+
22
+ export const createItem = catchAsync(async (req: IRequest, res: IResponse) => {
23
+ const data = req.body;
24
+ const createdItem = (await create(Item, data)) as IItemSchema;
25
+ let response = createdItem.toJSON();
26
+ if (data.srcset) {
27
+ response = {
28
+ ...response,
29
+ srcset: (await updateItemSrcSet(
30
+ createdItem._id,
31
+ data.srcset
32
+ )) as SrcSetItem[],
33
+ };
34
+ }
35
+ res.message = req?.i18n?.t('item.create');
36
+ return createdDocumentResponse(response, res);
37
+ });
38
+
39
+ export const updateItem = catchAsync(async (req: IRequest, res: IResponse) => {
40
+ const data = req.body;
41
+ const _id = req.params['id'];
42
+ const updatedItem = (await update(Item, { _id }, data)) as IItemSchema;
43
+ let response = updatedItem.toJSON();
44
+ if (data.srcset) {
45
+ response = {
46
+ ...response,
47
+ srcset: (await updateItemSrcSet(_id, data.srcset)) as SrcSetItem[],
48
+ };
49
+ }
50
+ res.message = req?.i18n?.t('item.update');
51
+ return successResponse(response, res);
52
+ });
53
+
54
+ export const deleteItem = catchAsync(async (req: IRequest, res: IResponse) => {
55
+ const _id = new Types.ObjectId(req.params['id']);
56
+ const deletedItem = await remove(Item, { _id });
57
+ res.message = req?.i18n?.t('item.delete');
58
+ return successResponse(deletedItem, res);
59
+ });
60
+
61
+ export const getItems = catchAsync(async (req: IRequest, res: IResponse) => {
62
+ const widgetId = req.params['widgetId'];
63
+ const items = await Item.aggregate([
64
+ {
65
+ $match: {
66
+ widgetId: new Types.ObjectId(widgetId),
67
+ isDeleted: false,
68
+ },
69
+ },
70
+ {
71
+ $project: {
72
+ ...commonExcludedFields,
73
+ },
74
+ },
75
+ {
76
+ $lookup: {
77
+ from: 'file',
78
+ let: { imgId: '$img' },
79
+ as: 'img',
80
+ pipeline: [
81
+ {
82
+ $match: {
83
+ $expr: {
84
+ $eq: ['$_id', '$$imgId'],
85
+ },
86
+ },
87
+ },
88
+ {
89
+ $project: {
90
+ ...commonExcludedFields,
91
+ width: 0,
92
+ module: 0,
93
+ height: 0,
94
+ },
95
+ },
96
+ ],
97
+ },
98
+ },
99
+ {
100
+ $lookup: {
101
+ from: 'srcsets',
102
+ let: { item: '$_id' },
103
+ as: 'srcset',
104
+ pipeline: [
105
+ {
106
+ $match: {
107
+ $expr: {
108
+ $eq: ['$itemId', '$$item'],
109
+ },
110
+ },
111
+ },
112
+ {
113
+ $project: {
114
+ ...commonExcludedFields,
115
+ _id: 0,
116
+ itemId: 0,
117
+ },
118
+ },
119
+ ],
120
+ },
121
+ },
122
+ {
123
+ $unwind: '$img',
124
+ },
125
+ ]);
126
+ res.message = req?.i18n?.t('item.getAll');
127
+ return successResponse(items, res);
128
+ });
129
+
130
+ const updateItemSrcSet = async (itemId: string, srcSets: SrcSetItem[]) => {
131
+ await deleteAll(SrcSet, { itemId });
132
+ const modifiedSrcSets = srcSets.map((set) => ({
133
+ ...set,
134
+ itemId,
135
+ }));
136
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
137
+ // @ts-ignore
138
+ return await bulkInsert(SrcSet, modifiedSrcSets);
139
+ };
@@ -0,0 +1,65 @@
1
+ import { Types } from 'mongoose';
2
+ import { Page } from './../models';
3
+ import { create, remove, update, list } from '../services/dbService';
4
+ import {
5
+ successResponse,
6
+ createdDocumentResponse,
7
+ } from './../utils/responseHandlers';
8
+ import { IRequest, IResponse } from '../types';
9
+
10
+ import { defaults } from '../utils/defaults';
11
+
12
+ const catchAsync = (fn: any) => {
13
+ return defaults.catchAsync(fn, 'Page');
14
+ };
15
+
16
+ export const createPage = catchAsync(async (req: IRequest, res: IResponse) => {
17
+ const data = req.body;
18
+ const page = await create(Page, data);
19
+ res.message = req?.i18n?.t('page.create');
20
+ return createdDocumentResponse(page, res);
21
+ });
22
+
23
+ export const updatePage = catchAsync(async (req: IRequest, res: IResponse) => {
24
+ const data = req.body;
25
+ const _id = req.params['id'];
26
+ const updatedPage = await update(Page, { _id }, data);
27
+ res.message = req?.i18n?.t('page.update');
28
+ return successResponse(updatedPage, res);
29
+ });
30
+
31
+ export const deletePage = catchAsync(async (req: IRequest, res: IResponse) => {
32
+ const _id = new Types.ObjectId(req.params['id']);
33
+ const createdPage = await remove(Page, { _id });
34
+ res.message = req?.i18n?.t('page.delete');
35
+ return successResponse(createdPage, res);
36
+ });
37
+
38
+ export const getPages = catchAsync(async (req: IRequest, res: IResponse) => {
39
+ const search = req.body.search || '';
40
+ const { page, limit, populate } = req.body.options;
41
+ const customOptions = {
42
+ populate,
43
+ ...(page && limit ? { page, limit } : {}),
44
+ };
45
+ const query = {
46
+ isDeleted: false,
47
+ $or: [
48
+ {
49
+ name: {
50
+ $regex: search,
51
+ $options: 'i',
52
+ },
53
+ },
54
+ {
55
+ code: {
56
+ $regex: search,
57
+ $options: 'i',
58
+ },
59
+ },
60
+ ],
61
+ };
62
+ const pages = await list(Page, query, customOptions);
63
+ res.message = req?.i18n?.t('page.getAll');
64
+ return successResponse(pages, res);
65
+ });
@@ -0,0 +1,265 @@
1
+ import { AggregateOptions, models } from 'mongoose';
2
+ import { Widget, Page } from './../models';
3
+ import { AddSrcSetsToItems, appendCollectionData } from '../utils/helper';
4
+ import { successResponse } from './../utils/responseHandlers';
5
+ import { defaults, commonExcludedFields } from '../utils/defaults';
6
+ import { IPageSchema, IRequest, IResponse, IWidgetSchema } from '../types';
7
+
8
+ const catchAsync = (fn: any) => {
9
+ return defaults.catchAsync(fn, 'User');
10
+ };
11
+
12
+ // TO Do: Optimize the following
13
+ export const getWidgetData = catchAsync(
14
+ async (req: IRequest, res: IResponse) => {
15
+ const { code } = req.body;
16
+ const widgetDataArr = (await Widget.aggregate([
17
+ {
18
+ $match: {
19
+ isDeleted: false,
20
+ isActive: true,
21
+ code,
22
+ },
23
+ },
24
+ {
25
+ // Get only the fields that are not excluded
26
+ $project: {
27
+ ...commonExcludedFields,
28
+ },
29
+ },
30
+ {
31
+ // Get Items data
32
+ $lookup: {
33
+ from: 'items',
34
+ let: { widget: '$_id' },
35
+ pipeline: [
36
+ {
37
+ $match: {
38
+ $expr: {
39
+ $eq: ['$widgetId', '$$widget'],
40
+ },
41
+ isDeleted: false,
42
+ },
43
+ },
44
+ {
45
+ $project: {
46
+ ...commonExcludedFields,
47
+ },
48
+ },
49
+ {
50
+ $lookup: {
51
+ from: 'file',
52
+ let: { img: '$img' },
53
+ as: 'image',
54
+ pipeline: [
55
+ {
56
+ $match: {
57
+ $expr: {
58
+ $eq: ['$_id', '$$img'],
59
+ },
60
+ },
61
+ },
62
+ {
63
+ $project: {
64
+ _id: 1,
65
+ uri: 1,
66
+ },
67
+ },
68
+ ],
69
+ },
70
+ },
71
+ {
72
+ $lookup: {
73
+ from: 'srcsets',
74
+ let: { item: '$_id' },
75
+ as: 'srcset',
76
+ pipeline: [
77
+ {
78
+ $match: {
79
+ $expr: {
80
+ $eq: ['$itemId', '$$item'],
81
+ },
82
+ },
83
+ },
84
+ {
85
+ $project: {
86
+ ...commonExcludedFields,
87
+ _id: 0,
88
+ itemId: 0,
89
+ },
90
+ },
91
+ ],
92
+ },
93
+ },
94
+ {
95
+ $unwind: '$image',
96
+ },
97
+ ],
98
+ as: 'items',
99
+ },
100
+ },
101
+ ])) as Array<IWidgetSchema>;
102
+
103
+ if (!widgetDataArr.length) throw new Error(`Widget not found`);
104
+ const widgetData = widgetDataArr[0];
105
+
106
+ if (widgetData.collectionName && widgetData.collectionItems.length > 0) {
107
+ const collectionConfig = defaults.collections.find(
108
+ (c) => c.collectionName === widgetData.collectionName
109
+ );
110
+ const aggregateQuery: AggregateOptions = {
111
+ $match: {
112
+ _id: {
113
+ $in: widgetData.collectionItems,
114
+ },
115
+ ...(collectionConfig?.match || {}),
116
+ },
117
+ };
118
+ if (collectionConfig?.project)
119
+ aggregateQuery['$project'] = collectionConfig?.project;
120
+ if (collectionConfig?.lookup)
121
+ aggregateQuery['$lookup'] = collectionConfig?.lookup;
122
+
123
+ const aggregateQueryItem: AggregateOptions[] = [];
124
+ if (aggregateQuery['$match'])
125
+ aggregateQueryItem.push({ $match: aggregateQuery['$match'] });
126
+ if (aggregateQuery['$lookup'])
127
+ aggregateQueryItem.push({ $lookup: aggregateQuery['$lookup'] });
128
+ if (aggregateQuery['$project'])
129
+ aggregateQueryItem.push({ $project: aggregateQuery['$project'] });
130
+
131
+ const collectionItems = await models[widgetData.collectionName].aggregate(
132
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
133
+ // @ts-ignore
134
+ aggregateQueryItem
135
+ );
136
+ widgetData.collectionItems = collectionItems;
137
+ }
138
+ AddSrcSetsToItems(widgetData);
139
+ return successResponse(widgetData, res);
140
+ }
141
+ );
142
+
143
+ // TO Do: Optimize the following
144
+ export const getPageData = catchAsync(async (req: IRequest, res: IResponse) => {
145
+ const { code } = req.body;
146
+ const pageData: any = (await Page.aggregate([
147
+ {
148
+ $match: {
149
+ isDeleted: false,
150
+ code: code,
151
+ },
152
+ },
153
+ {
154
+ $project: {
155
+ isDeleted: 0,
156
+ deletedAt: 0,
157
+ __v: 0,
158
+ },
159
+ },
160
+ {
161
+ $lookup: {
162
+ from: 'widgets',
163
+ let: { widgets: '$widgets' },
164
+ pipeline: [
165
+ {
166
+ $match: {
167
+ $expr: {
168
+ $in: ['$_id', '$$widgets'],
169
+ },
170
+ isDeleted: false,
171
+ },
172
+ },
173
+ {
174
+ $project: {
175
+ widgetId: 0,
176
+ sequence: 0,
177
+ ...commonExcludedFields,
178
+ },
179
+ },
180
+ {
181
+ $lookup: {
182
+ from: 'items',
183
+ let: { widget: '$_id' },
184
+ pipeline: [
185
+ {
186
+ $match: {
187
+ $expr: {
188
+ $eq: ['$widgetId', '$$widget'],
189
+ },
190
+ isDeleted: false,
191
+ },
192
+ },
193
+ {
194
+ $project: {
195
+ ...commonExcludedFields,
196
+ },
197
+ },
198
+ {
199
+ $lookup: {
200
+ from: 'file',
201
+ let: { img: '$img' },
202
+ as: 'image',
203
+ pipeline: [
204
+ {
205
+ $match: {
206
+ $expr: {
207
+ $eq: ['$_id', '$$img'],
208
+ },
209
+ },
210
+ },
211
+ {
212
+ $project: {
213
+ _id: 1,
214
+ uri: 1,
215
+ },
216
+ },
217
+ ],
218
+ },
219
+ },
220
+ {
221
+ $lookup: {
222
+ from: 'srcsets',
223
+ let: { item: '$_id' },
224
+ as: 'srcset',
225
+ pipeline: [
226
+ {
227
+ $match: {
228
+ $expr: {
229
+ $eq: ['$itemId', '$$item'],
230
+ },
231
+ },
232
+ },
233
+ {
234
+ $project: {
235
+ ...commonExcludedFields,
236
+ _id: 0,
237
+ itemId: 0,
238
+ },
239
+ },
240
+ ],
241
+ },
242
+ },
243
+ {
244
+ $unwind: '$image',
245
+ },
246
+ ],
247
+ as: 'items',
248
+ },
249
+ },
250
+ ],
251
+ as: 'widgets',
252
+ },
253
+ },
254
+ ])) as Array<IPageSchema>;
255
+
256
+ if (!pageData.length) throw new Error('Page not found');
257
+ pageData[0].widgets = await appendCollectionData(pageData[0].widgets);
258
+ if (Array.isArray(pageData[0].widgets) && pageData[0].widgets.length > 0) {
259
+ pageData[0].widgets.forEach((widget: IWidgetSchema) => {
260
+ AddSrcSetsToItems(widget);
261
+ });
262
+ }
263
+ res.message = req?.i18n?.t('user.pageData');
264
+ return successResponse(pageData[0], res);
265
+ });
@@ -0,0 +1,165 @@
1
+ import { Types, model, Schema, models } from 'mongoose';
2
+ import mongoosePaginate from 'mongoose-paginate-v2';
3
+ import { Widget } from './../models';
4
+ import { create, remove, update, list, getAll } from '../services/dbService';
5
+ import {
6
+ successResponse,
7
+ createdDocumentResponse,
8
+ } from './../utils/responseHandlers';
9
+
10
+ import { defaults } from '../utils/defaults';
11
+ import {
12
+ CollectionItem,
13
+ IModel,
14
+ IRequest,
15
+ IResponse,
16
+ WidgetTypes,
17
+ TypesType,
18
+ ItemsType,
19
+ } from '../types';
20
+
21
+ const catchAsync = (fn: any) => {
22
+ return defaults.catchAsync(fn, 'Notification');
23
+ };
24
+
25
+ export const createWidget = catchAsync(
26
+ async (req: IRequest, res: IResponse) => {
27
+ const data = req.body;
28
+ const notification = await create(Widget, data);
29
+ res.message = req?.i18n?.t('widget.create');
30
+ return createdDocumentResponse(notification, res);
31
+ }
32
+ );
33
+
34
+ export const updateWidget = catchAsync(
35
+ async (req: IRequest, res: IResponse) => {
36
+ const data = req.body;
37
+ const _id = req.params['id'];
38
+ const updatedNotification = await update(Widget, { _id }, data);
39
+ res.message = req?.i18n?.t('widget.update');
40
+ return successResponse(updatedNotification, res);
41
+ }
42
+ );
43
+
44
+ export const deleteWidget = catchAsync(
45
+ async (req: IRequest, res: IResponse) => {
46
+ const _id = new Types.ObjectId(req.params['id']);
47
+ const deletedNotification = await remove(Widget, { _id });
48
+ res.message = req?.i18n?.t('widget.delete');
49
+ return successResponse(deletedNotification, res);
50
+ }
51
+ );
52
+
53
+ export const getWidgets = catchAsync(async (req: IRequest, res: IResponse) => {
54
+ const search = req.body.search || '';
55
+ const { page, limit } = req.body.options;
56
+ const all =
57
+ (typeof req.body.all !== 'undefined' && req.body.all === true) || false;
58
+ const isActive =
59
+ typeof req.body.isActive !== 'undefined'
60
+ ? req.body.isActive || false
61
+ : null;
62
+ const customOptions = {
63
+ pagination: !all,
64
+ ...(page && limit ? { page, limit } : {}),
65
+ };
66
+ const query = {
67
+ isDeleted: false,
68
+ isActive: { $in: isActive === null ? [true, false] : [isActive] },
69
+ $or: [
70
+ {
71
+ name: {
72
+ $regex: search,
73
+ $options: 'i',
74
+ },
75
+ },
76
+ {
77
+ code: {
78
+ $regex: search,
79
+ $options: 'i',
80
+ },
81
+ },
82
+ ],
83
+ };
84
+ const notifications = await list(Widget, query, customOptions);
85
+ res.message = req?.i18n?.t('widget.getAll');
86
+ return successResponse(notifications, res);
87
+ });
88
+
89
+ export const partialUpdateWidget = catchAsync(
90
+ async (req: IRequest, res: IResponse) => {
91
+ const data = req.body;
92
+ const _id = req.params['id'];
93
+ const updatedNotification = await update(Widget, { _id }, data);
94
+ res.message = req?.i18n?.t('widget.partialUpdate');
95
+ return successResponse(updatedNotification, res);
96
+ }
97
+ );
98
+
99
+ export const getItemsTypes = catchAsync(
100
+ async (req: IRequest, res: IResponse) => {
101
+ const itemsTypes: TypesType[] = [
102
+ {
103
+ value: Object.keys(ItemsType)[0],
104
+ label: Object.values(ItemsType)[0],
105
+ },
106
+ ];
107
+ defaults.collections.forEach((item: CollectionItem) => {
108
+ itemsTypes.push({
109
+ value: item.collectionName,
110
+ label: item.title,
111
+ });
112
+ });
113
+ res.message = req?.i18n?.t('widget.getItemsTypes');
114
+ return successResponse(itemsTypes, res);
115
+ }
116
+ );
117
+
118
+ export const getWidgetTypes = catchAsync(
119
+ async (req: IRequest, res: IResponse) => {
120
+ const widgetTypes = Object.entries(WidgetTypes).map((e) => ({
121
+ label: e[1],
122
+ value: e[0],
123
+ }));
124
+ res.message = req?.i18n?.t('widget.getWidgetTypes');
125
+ return successResponse(widgetTypes, res);
126
+ }
127
+ );
128
+
129
+ export const getCollectionData = catchAsync(
130
+ async (req: IRequest, res: IResponse) => {
131
+ const limit = 10;
132
+ const { search, collectionName } = req.body;
133
+ const collectionItem: CollectionItem | undefined =
134
+ defaults.collections.find(
135
+ (collection) => collection.collectionName === collectionName
136
+ );
137
+ if (!collectionItem) {
138
+ throw new Error(`No collection is specified with ${collectionName}`);
139
+ }
140
+ // setting up mongoose model
141
+ let TempModel = models[collectionName] as unknown as IModel<any>;
142
+ if (!TempModel) {
143
+ const tempSchema = new Schema({}, { strict: false });
144
+ TempModel = model(collectionName, tempSchema) as unknown as IModel<any>;
145
+ }
146
+ // fetching data
147
+ let query: any = collectionItem.filters || {};
148
+ if (search) {
149
+ query = {
150
+ ...query,
151
+ $or: collectionItem.searchColumns
152
+ ? collectionItem.searchColumns.map((column) => ({
153
+ [column]: {
154
+ $regex: search,
155
+ $options: 'i',
156
+ },
157
+ }))
158
+ : [],
159
+ };
160
+ }
161
+ const collectionData = await getAll(TempModel, query, { limit });
162
+ res.message = req?.i18n?.t('widget.getCollectionData');
163
+ return successResponse({ docs: collectionData }, res);
164
+ }
165
+ );
package/src/index.ts ADDED
@@ -0,0 +1,29 @@
1
+ import { defaults } from './utils/defaults';
2
+ import WidgetRoutes from './routes/WidgetRoute';
3
+ import ItemRoutes from './routes/ItemRoute';
4
+ import PageRoutes from './routes/PageRoute';
5
+ import UserRoutes from './routes/UserRoute';
6
+ import { Widget, Item, Page } from './models';
7
+ import { IConfig } from './types';
8
+
9
+ function setConfig(config: Partial<IConfig>) {
10
+ if (config.logger) {
11
+ defaults.logger = config.logger;
12
+ }
13
+ if (typeof config.catchAsync === 'function')
14
+ defaults.catchAsync = config.catchAsync;
15
+ if (Array.isArray(config.collections)) {
16
+ defaults.collections = config.collections;
17
+ }
18
+ }
19
+
20
+ export {
21
+ WidgetRoutes,
22
+ ItemRoutes,
23
+ PageRoutes,
24
+ UserRoutes,
25
+ Widget,
26
+ Item,
27
+ Page,
28
+ setConfig,
29
+ };
@@ -0,0 +1,29 @@
1
+ import { Schema, Types, model } from 'mongoose';
2
+ import mongoosePaginate from 'mongoose-paginate-v2';
3
+ import { softDeletePlugin } from '../plugins/softDelete';
4
+ import { IModel, IItemSchema, ItemTypes } from '../types';
5
+
6
+ const ItemSchema = new Schema<IItemSchema>({
7
+ widgetId: {
8
+ type: Types.ObjectId,
9
+ ref: 'Widget',
10
+ },
11
+ title: String,
12
+ altText: String,
13
+ link: String,
14
+ sequence: Number,
15
+ img: { type: Schema.Types.ObjectId, ref: 'file' },
16
+ itemType: {
17
+ type: String,
18
+ enum: Object.values(ItemTypes),
19
+ default: ItemTypes.Web,
20
+ required: true,
21
+ },
22
+ });
23
+
24
+ ItemSchema.plugin(softDeletePlugin);
25
+ ItemSchema.plugin(mongoosePaginate);
26
+
27
+ const Item = model('Item', ItemSchema) as unknown as IModel<IItemSchema>;
28
+
29
+ export default Item;
@@ -0,0 +1,17 @@
1
+ import { Schema, model, Types } from 'mongoose';
2
+ import mongoosePaginate from 'mongoose-paginate-v2';
3
+ import { softDeletePlugin } from '../plugins/softDelete';
4
+ import { IModel, IPageSchema } from '../types';
5
+
6
+ const PageSchema = new Schema<IPageSchema>({
7
+ name: String,
8
+ code: String,
9
+ widgets: [{ type: Types.ObjectId, ref: 'Widget' }],
10
+ });
11
+
12
+ PageSchema.plugin(softDeletePlugin);
13
+ PageSchema.plugin(mongoosePaginate);
14
+
15
+ const Page = model('Page', PageSchema) as unknown as IModel<IPageSchema>;
16
+
17
+ export default Page;