@knovator/pagecreator-node 0.2.0 → 0.3.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.
- package/package.json +6 -3
- package/src/constants/index.d.ts +16 -0
- package/src/constants/index.js +30 -0
- package/src/constants/index.js.map +1 -0
- package/src/controllers/ItemController.d.ts +4 -0
- package/src/controllers/ItemController.js +108 -0
- package/src/controllers/ItemController.js.map +1 -0
- package/src/controllers/PageController.d.ts +4 -0
- package/src/controllers/PageController.js +61 -0
- package/src/controllers/PageController.js.map +1 -0
- package/src/controllers/UserController.d.ts +2 -0
- package/src/controllers/UserController.js +238 -0
- package/src/controllers/UserController.js.map +1 -0
- package/src/controllers/WidgetController.d.ts +8 -0
- package/src/controllers/WidgetController.js +131 -0
- package/src/controllers/WidgetController.js.map +1 -0
- package/src/index.d.ts +8 -0
- package/src/index.js +29 -0
- package/src/index.js.map +1 -0
- package/src/models/Item.d.ts +3 -0
- package/src/models/Item.js +29 -0
- package/src/models/Item.js.map +1 -0
- package/src/models/Page.d.ts +3 -0
- package/src/models/Page.js +16 -0
- package/src/models/Page.js.map +1 -0
- package/src/models/SrcSet.d.ts +4 -0
- package/src/models/SrcSet.js +15 -0
- package/src/models/SrcSet.js.map +1 -0
- package/src/models/Widget.d.ts +3 -0
- package/src/models/Widget.js +41 -0
- package/src/models/Widget.js.map +1 -0
- package/src/models/{index.ts → index.d.ts} +0 -1
- package/src/models/index.js +13 -0
- package/src/models/index.js.map +1 -0
- package/src/plugins/softDelete.d.ts +7 -0
- package/src/plugins/softDelete.js +64 -0
- package/src/plugins/softDelete.js.map +1 -0
- package/src/routes/ItemRoute.d.ts +3 -0
- package/src/routes/ItemRoute.js +31 -0
- package/src/routes/ItemRoute.js.map +1 -0
- package/src/routes/PageRoute.d.ts +3 -0
- package/src/routes/PageRoute.js +30 -0
- package/src/routes/PageRoute.js.map +1 -0
- package/src/routes/UserRoute.d.ts +3 -0
- package/src/routes/UserRoute.js +23 -0
- package/src/routes/UserRoute.js.map +1 -0
- package/src/routes/WidgetRoute.d.ts +3 -0
- package/src/routes/WidgetRoute.js +47 -0
- package/src/routes/WidgetRoute.js.map +1 -0
- package/src/services/dbService.d.ts +10 -0
- package/src/services/dbService.js +74 -0
- package/src/services/dbService.js.map +1 -0
- package/src/types/{IRequest.ts → IRequest.d.ts} +3 -4
- package/src/types/IRequest.js +3 -0
- package/src/types/IRequest.js.map +1 -0
- package/src/types/{IResponse.ts → IResponse.d.ts} +3 -4
- package/src/types/IResponse.js +3 -0
- package/src/types/IResponse.js.map +1 -0
- package/src/types/{Router.ts → Router.d.ts} +1 -2
- package/src/types/Router.js +3 -0
- package/src/types/Router.js.map +1 -0
- package/src/types/common.d.ts +91 -0
- package/src/types/common.js +3 -0
- package/src/types/common.js.map +1 -0
- package/src/types/enums.d.ts +11 -0
- package/src/types/enums.js +18 -0
- package/src/types/enums.js.map +1 -0
- package/src/types/{index.ts → index.d.ts} +0 -0
- package/src/types/index.js +9 -0
- package/src/types/index.js.map +1 -0
- package/src/utils/defaults.d.ts +7 -0
- package/src/utils/defaults.js +28 -0
- package/src/utils/defaults.js.map +1 -0
- package/src/utils/helper.d.ts +33 -0
- package/src/utils/helper.js +111 -0
- package/src/utils/helper.js.map +1 -0
- package/src/utils/responseHandlers.d.ts +6 -0
- package/src/utils/responseHandlers.js +57 -0
- package/src/utils/responseHandlers.js.map +1 -0
- package/src/utils/validate.d.ts +4 -0
- package/src/utils/validate.js +24 -0
- package/src/utils/validate.js.map +1 -0
- package/src/utils/validations/item.d.ts +7 -0
- package/src/utils/validations/item.js +50 -0
- package/src/utils/validations/item.js.map +1 -0
- package/src/utils/validations/page.d.ts +7 -0
- package/src/utils/validations/page.js +58 -0
- package/src/utils/validations/page.js.map +1 -0
- package/src/utils/validations/user.d.ts +3 -0
- package/src/utils/validations/user.js +12 -0
- package/src/utils/validations/user.js.map +1 -0
- package/src/utils/validations/widget.d.ts +9 -0
- package/src/utils/validations/widget.js +108 -0
- package/src/utils/validations/widget.js.map +1 -0
- package/src/constants/index.ts +0 -26
- package/src/controllers/ItemController.ts +0 -139
- package/src/controllers/PageController.ts +0 -65
- package/src/controllers/UserController.ts +0 -265
- package/src/controllers/WidgetController.ts +0 -165
- package/src/index.ts +0 -29
- package/src/models/Item.ts +0 -29
- package/src/models/Page.ts +0 -17
- package/src/models/SrcSet.ts +0 -16
- package/src/models/Widget.ts +0 -44
- package/src/plugins/softDelete.ts +0 -84
- package/src/routes/ItemRoute.ts +0 -32
- package/src/routes/PageRoute.ts +0 -31
- package/src/routes/UserRoute.ts +0 -32
- package/src/routes/WidgetRoute.ts +0 -56
- package/src/services/dbService.ts +0 -82
- package/src/types/common.ts +0 -130
- package/src/types/enums.ts +0 -13
- package/src/utils/defaults.ts +0 -30
- package/src/utils/helper.ts +0 -133
- package/src/utils/responseHandlers.ts +0 -54
- package/src/utils/validate.ts +0 -22
- package/src/utils/validations/item.ts +0 -51
- package/src/utils/validations/page.ts +0 -59
- package/src/utils/validations/user.ts +0 -9
- package/src/utils/validations/widget.ts +0 -118
|
@@ -1,265 +0,0 @@
|
|
|
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
|
-
});
|
|
@@ -1,165 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
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
|
-
};
|
package/src/models/Item.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
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;
|
package/src/models/Page.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
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;
|
package/src/models/SrcSet.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { Schema, model, Types, Model } from 'mongoose';
|
|
2
|
-
import { ISrcSetSchema } from '../types';
|
|
3
|
-
|
|
4
|
-
const SrcSetSchema = new Schema<ISrcSetSchema>({
|
|
5
|
-
width: Number,
|
|
6
|
-
height: Number,
|
|
7
|
-
screenSize: Number,
|
|
8
|
-
itemId: {
|
|
9
|
-
type: Types.ObjectId,
|
|
10
|
-
ref: 'Item',
|
|
11
|
-
},
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
const SrcSet: Model<ISrcSetSchema> = model('SrcSet', SrcSetSchema);
|
|
15
|
-
|
|
16
|
-
export default SrcSet;
|
package/src/models/Widget.ts
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { Schema, model, Types } from 'mongoose';
|
|
2
|
-
import mongoosePaginate from 'mongoose-paginate-v2';
|
|
3
|
-
import { softDeletePlugin } from '../plugins/softDelete';
|
|
4
|
-
import { IModel, IWidgetSchema, WidgetTypes, ItemsType } from '../types';
|
|
5
|
-
|
|
6
|
-
const WidgetSchema = new Schema<IWidgetSchema>({
|
|
7
|
-
name: String,
|
|
8
|
-
code: String,
|
|
9
|
-
autoPlay: {
|
|
10
|
-
type: Boolean,
|
|
11
|
-
default: false,
|
|
12
|
-
},
|
|
13
|
-
isActive: {
|
|
14
|
-
type: Boolean,
|
|
15
|
-
default: true,
|
|
16
|
-
},
|
|
17
|
-
widgetTitle: String,
|
|
18
|
-
webPerRow: Number,
|
|
19
|
-
mobilePerRow: Number,
|
|
20
|
-
tabletPerRow: Number,
|
|
21
|
-
collectionName: String,
|
|
22
|
-
collectionItems: [{ type: Types.ObjectId, refPath: 'collectionName' }],
|
|
23
|
-
itemsType: {
|
|
24
|
-
type: String,
|
|
25
|
-
default: ItemsType.Image,
|
|
26
|
-
required: true,
|
|
27
|
-
},
|
|
28
|
-
widgetType: {
|
|
29
|
-
type: String,
|
|
30
|
-
enum: Object.values(WidgetTypes),
|
|
31
|
-
default: WidgetTypes.FixedCard,
|
|
32
|
-
required: true,
|
|
33
|
-
},
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
WidgetSchema.plugin(softDeletePlugin);
|
|
37
|
-
WidgetSchema.plugin(mongoosePaginate);
|
|
38
|
-
|
|
39
|
-
const Widget = model(
|
|
40
|
-
'Widget',
|
|
41
|
-
WidgetSchema
|
|
42
|
-
) as unknown as IModel<IWidgetSchema>;
|
|
43
|
-
|
|
44
|
-
export default Widget;
|
|
@@ -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 };
|
package/src/routes/ItemRoute.ts
DELETED
|
@@ -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 itemValidation from '../utils/validations/item';
|
|
8
|
-
import * as itemController from '../controllers/ItemController';
|
|
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 all items
|
|
16
|
-
routes
|
|
17
|
-
.get(`/:widgetId`, validate(itemValidation.list), itemController.getItems)
|
|
18
|
-
.descriptor(`${descriptorPrefix}item.getAll`);
|
|
19
|
-
// Create a item
|
|
20
|
-
routes
|
|
21
|
-
.post(`/`, validate(itemValidation.create), itemController.createItem)
|
|
22
|
-
.descriptor(`${descriptorPrefix}item.create`);
|
|
23
|
-
// Update a item
|
|
24
|
-
routes
|
|
25
|
-
.put(`/:id`, validate(itemValidation.update), itemController.updateItem)
|
|
26
|
-
.descriptor(`${descriptorPrefix}item.update`);
|
|
27
|
-
// Delete a item
|
|
28
|
-
routes
|
|
29
|
-
.delete(`/:id`, itemController.deleteItem)
|
|
30
|
-
.descriptor(`${descriptorPrefix}item.delete`);
|
|
31
|
-
|
|
32
|
-
export default routes;
|