@knovator/pagecreator-node 0.0.7 → 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.
- package/README.md +13 -13
- package/package.json +3 -6
- package/src/constants/index.ts +26 -0
- package/src/controllers/ItemController.ts +139 -0
- package/src/controllers/PageController.ts +65 -0
- package/src/controllers/UserController.ts +265 -0
- package/src/controllers/WidgetController.ts +165 -0
- package/src/index.ts +29 -0
- package/src/models/Item.ts +29 -0
- package/src/models/Page.ts +17 -0
- package/src/models/SrcSet.ts +16 -0
- package/src/models/Widget.ts +44 -0
- package/src/models/index.ts +6 -0
- package/src/plugins/softDelete.ts +84 -0
- package/src/routes/ItemRoute.ts +32 -0
- package/src/routes/PageRoute.ts +31 -0
- package/src/routes/UserRoute.ts +32 -0
- package/src/routes/WidgetRoute.ts +56 -0
- package/src/services/dbService.ts +82 -0
- package/src/types/{IRequest.d.ts → IRequest.ts} +4 -3
- package/src/types/{IResponse.d.ts → IResponse.ts} +4 -3
- package/src/types/{Router.d.ts → Router.ts} +2 -1
- package/src/types/common.ts +130 -0
- package/src/types/enums.ts +13 -0
- package/src/types/{index.d.ts → index.ts} +0 -0
- package/src/utils/defaults.ts +30 -0
- package/src/utils/helper.ts +133 -0
- package/src/utils/responseHandlers.ts +54 -0
- package/src/utils/validate.ts +22 -0
- package/src/utils/validations/item.ts +51 -0
- package/src/utils/validations/page.ts +59 -0
- package/src/utils/validations/user.ts +9 -0
- package/src/utils/validations/widget.ts +118 -0
- package/src/constants/index.d.ts +0 -16
- package/src/constants/index.js +0 -30
- package/src/constants/index.js.map +0 -1
- package/src/controllers/PageController.d.ts +0 -4
- package/src/controllers/PageController.js +0 -61
- package/src/controllers/PageController.js.map +0 -1
- package/src/controllers/TileController.d.ts +0 -4
- package/src/controllers/TileController.js +0 -45
- package/src/controllers/TileController.js.map +0 -1
- package/src/controllers/UserController.d.ts +0 -2
- package/src/controllers/UserController.js +0 -194
- package/src/controllers/UserController.js.map +0 -1
- package/src/controllers/WidgetController.d.ts +0 -8
- package/src/controllers/WidgetController.js +0 -129
- package/src/controllers/WidgetController.js.map +0 -1
- package/src/index.d.ts +0 -8
- package/src/index.js +0 -29
- package/src/index.js.map +0 -1
- package/src/models/Page.d.ts +0 -3
- package/src/models/Page.js +0 -16
- package/src/models/Page.js.map +0 -1
- package/src/models/Tile.d.ts +0 -3
- package/src/models/Tile.js +0 -29
- package/src/models/Tile.js.map +0 -1
- package/src/models/Widget.d.ts +0 -3
- package/src/models/Widget.js +0 -41
- package/src/models/Widget.js.map +0 -1
- package/src/models/index.d.ts +0 -4
- package/src/models/index.js +0 -11
- package/src/models/index.js.map +0 -1
- package/src/plugins/softDelete.d.ts +0 -7
- package/src/plugins/softDelete.js +0 -64
- package/src/plugins/softDelete.js.map +0 -1
- package/src/routes/PageRoute.d.ts +0 -3
- package/src/routes/PageRoute.js +0 -30
- package/src/routes/PageRoute.js.map +0 -1
- package/src/routes/TileRoute.d.ts +0 -3
- package/src/routes/TileRoute.js +0 -31
- package/src/routes/TileRoute.js.map +0 -1
- package/src/routes/UserRoute.d.ts +0 -3
- package/src/routes/UserRoute.js +0 -22
- package/src/routes/UserRoute.js.map +0 -1
- package/src/routes/WidgetRoute.d.ts +0 -3
- package/src/routes/WidgetRoute.js +0 -47
- package/src/routes/WidgetRoute.js.map +0 -1
- package/src/services/dbService.d.ts +0 -8
- package/src/services/dbService.js +0 -60
- package/src/services/dbService.js.map +0 -1
- package/src/types/IRequest.js +0 -3
- package/src/types/IRequest.js.map +0 -1
- package/src/types/IResponse.js +0 -3
- package/src/types/IResponse.js.map +0 -1
- package/src/types/Router.js +0 -3
- package/src/types/Router.js.map +0 -1
- package/src/types/common.d.ts +0 -78
- package/src/types/common.js +0 -3
- package/src/types/common.js.map +0 -1
- package/src/types/enums.d.ts +0 -11
- package/src/types/enums.js +0 -18
- package/src/types/enums.js.map +0 -1
- package/src/types/index.js +0 -9
- package/src/types/index.js.map +0 -1
- package/src/utils/defaults.d.ts +0 -7
- package/src/utils/defaults.js +0 -28
- package/src/utils/defaults.js.map +0 -1
- package/src/utils/helper.d.ts +0 -30
- package/src/utils/helper.js +0 -90
- package/src/utils/helper.js.map +0 -1
- package/src/utils/responseHandlers.d.ts +0 -6
- package/src/utils/responseHandlers.js +0 -57
- package/src/utils/responseHandlers.js.map +0 -1
- package/src/utils/validate.d.ts +0 -4
- package/src/utils/validate.js +0 -24
- package/src/utils/validate.js.map +0 -1
- package/src/utils/validations/page.d.ts +0 -7
- package/src/utils/validations/page.js +0 -58
- package/src/utils/validations/page.js.map +0 -1
- package/src/utils/validations/tile.d.ts +0 -7
- package/src/utils/validations/tile.js +0 -43
- package/src/utils/validations/tile.js.map +0 -1
- package/src/utils/validations/user.d.ts +0 -3
- package/src/utils/validations/user.js +0 -12
- package/src/utils/validations/user.js.map +0 -1
- package/src/utils/validations/widget.d.ts +0 -9
- package/src/utils/validations/widget.js +0 -108
- package/src/utils/validations/widget.js.map +0 -1
|
@@ -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;
|
|
@@ -0,0 +1,16 @@
|
|
|
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;
|
|
@@ -0,0 +1,44 @@
|
|
|
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;
|
|
@@ -0,0 +1,84 @@
|
|
|
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 };
|
|
@@ -0,0 +1,32 @@
|
|
|
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;
|
|
@@ -0,0 +1,31 @@
|
|
|
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
|
+
|
|
31
|
+
export default routes;
|
|
@@ -0,0 +1,32 @@
|
|
|
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;
|
|
@@ -0,0 +1,56 @@
|
|
|
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 widgetValidation from '../utils/validations/widget';
|
|
8
|
+
import * as widgetController from '../controllers/WidgetController';
|
|
9
|
+
|
|
10
|
+
const descriptorPrefix = process.env['PAGECREATOR_DESCRIPTOR_PREFIX'] || '';
|
|
11
|
+
const routes = express.Router() as IRouter;
|
|
12
|
+
routes.use(express.json());
|
|
13
|
+
|
|
14
|
+
// Widget Routes
|
|
15
|
+
// Get widget types
|
|
16
|
+
routes
|
|
17
|
+
.get('/widget-types', widgetController.getItemsTypes)
|
|
18
|
+
.descriptor(`${descriptorPrefix}widget.getItemsTypes`);
|
|
19
|
+
// Get widget selection types
|
|
20
|
+
routes
|
|
21
|
+
.get('/selection-types', widgetController.getWidgetTypes)
|
|
22
|
+
.descriptor(`${descriptorPrefix}widget.getWidgetTypes`);
|
|
23
|
+
// Get all widgets
|
|
24
|
+
routes
|
|
25
|
+
.post(`/list`, validate(widgetValidation.list), widgetController.getWidgets)
|
|
26
|
+
.descriptor(`${descriptorPrefix}widget.getAll`);
|
|
27
|
+
// Create a widget
|
|
28
|
+
routes
|
|
29
|
+
.post(`/`, validate(widgetValidation.create), widgetController.createWidget)
|
|
30
|
+
.descriptor(`${descriptorPrefix}widget.create`);
|
|
31
|
+
// Update a widget
|
|
32
|
+
routes
|
|
33
|
+
.put(`/:id`, validate(widgetValidation.update), widgetController.updateWidget)
|
|
34
|
+
.descriptor(`${descriptorPrefix}widget.update`);
|
|
35
|
+
// Partial Update a widget
|
|
36
|
+
routes
|
|
37
|
+
.patch(
|
|
38
|
+
`/:id`,
|
|
39
|
+
validate(widgetValidation.partialUpdate),
|
|
40
|
+
widgetController.partialUpdateWidget
|
|
41
|
+
)
|
|
42
|
+
.descriptor(`${descriptorPrefix}widget.partialUpdate`);
|
|
43
|
+
// Delete a widget
|
|
44
|
+
routes
|
|
45
|
+
.delete(`/:id`, widgetController.deleteWidget)
|
|
46
|
+
.descriptor(`${descriptorPrefix}widget.delete`);
|
|
47
|
+
// Get dynamic collection data
|
|
48
|
+
routes
|
|
49
|
+
.post(
|
|
50
|
+
'/collection-data',
|
|
51
|
+
validate(widgetValidation.getCollectionData),
|
|
52
|
+
widgetController.getCollectionData
|
|
53
|
+
)
|
|
54
|
+
.descriptor(`${descriptorPrefix}widget.getCollectionData`);
|
|
55
|
+
|
|
56
|
+
export default routes;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Model,
|
|
3
|
+
FilterQuery,
|
|
4
|
+
QueryOptions,
|
|
5
|
+
ProjectionType,
|
|
6
|
+
HydratedDocument,
|
|
7
|
+
QueryWithHelpers,
|
|
8
|
+
} from 'mongoose';
|
|
9
|
+
import { EntityType, IModel, ReturnDocument } from '../types';
|
|
10
|
+
|
|
11
|
+
// create
|
|
12
|
+
export async function create<T extends EntityType>(
|
|
13
|
+
Modal: Model<T>,
|
|
14
|
+
data: Partial<T>
|
|
15
|
+
): Promise<ReturnDocument> {
|
|
16
|
+
const modalInstance = new Modal(data);
|
|
17
|
+
return await modalInstance.save();
|
|
18
|
+
}
|
|
19
|
+
// update
|
|
20
|
+
export async function update<T extends EntityType>(
|
|
21
|
+
Modal: Model<T>,
|
|
22
|
+
query: FilterQuery<EntityType>,
|
|
23
|
+
data: Partial<T>
|
|
24
|
+
): Promise<ReturnDocument | undefined> {
|
|
25
|
+
await getOne(Modal, query);
|
|
26
|
+
const result = await Modal.findOneAndUpdate(query, data, { new: true });
|
|
27
|
+
return result || undefined;
|
|
28
|
+
}
|
|
29
|
+
// soft-delete
|
|
30
|
+
export async function remove<T extends EntityType>(
|
|
31
|
+
Modal: Model<T>,
|
|
32
|
+
query: FilterQuery<EntityType>
|
|
33
|
+
): Promise<ReturnDocument> {
|
|
34
|
+
const modalInstance = await getOne(Modal, query);
|
|
35
|
+
return await modalInstance.remove();
|
|
36
|
+
}
|
|
37
|
+
// delete-all
|
|
38
|
+
export async function deleteAll<T extends EntityType>(Modal: Model<T>, query: FilterQuery<T>) {
|
|
39
|
+
return Modal.deleteMany(query);
|
|
40
|
+
}
|
|
41
|
+
// get-all
|
|
42
|
+
export function getAll<T extends EntityType>(
|
|
43
|
+
Modal: Model<T>,
|
|
44
|
+
query: FilterQuery<EntityType> = {},
|
|
45
|
+
options?: QueryOptions<EntityType>,
|
|
46
|
+
projection?: ProjectionType<EntityType>
|
|
47
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
48
|
+
): QueryWithHelpers<Array<T>, T, {}, T> {
|
|
49
|
+
return Modal.find(query, projection, options);
|
|
50
|
+
}
|
|
51
|
+
// list
|
|
52
|
+
export async function list<T extends EntityType>(
|
|
53
|
+
Modal: IModel<T>,
|
|
54
|
+
where: FilterQuery<T>,
|
|
55
|
+
options: QueryOptions<T>
|
|
56
|
+
): Promise<ReturnDocument[]> {
|
|
57
|
+
try {
|
|
58
|
+
const documents = Modal.paginate(where, options);
|
|
59
|
+
return documents;
|
|
60
|
+
} catch (error) {
|
|
61
|
+
throw new Error((error as Error).message);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
// get-one
|
|
65
|
+
export async function getOne<T extends EntityType>(
|
|
66
|
+
Modal: Model<T>,
|
|
67
|
+
query: FilterQuery<EntityType>,
|
|
68
|
+
projection?: ProjectionType<EntityType>
|
|
69
|
+
): Promise<HydratedDocument<T>> {
|
|
70
|
+
const modalInstance: HydratedDocument<T> | null = await Modal.findOne(
|
|
71
|
+
query,
|
|
72
|
+
projection
|
|
73
|
+
);
|
|
74
|
+
if (!modalInstance)
|
|
75
|
+
throw new Error(`Record not found ${Modal.name ? `in ${Modal.name}` : ''}`);
|
|
76
|
+
|
|
77
|
+
return modalInstance;
|
|
78
|
+
}
|
|
79
|
+
// bulk-insert
|
|
80
|
+
export async function bulkInsert<T extends EntityType>(Modal: Model<T>, docs: T[]): Promise<ReturnDocument[]> {
|
|
81
|
+
return await Modal.insertMany(docs);
|
|
82
|
+
}
|