@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,130 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Document,
|
|
3
|
+
Model,
|
|
4
|
+
Types,
|
|
5
|
+
FilterQuery,
|
|
6
|
+
QueryOptions,
|
|
7
|
+
HydratedDocument,
|
|
8
|
+
} from 'mongoose';
|
|
9
|
+
import { ItemsType, WidgetTypes, ItemTypes } from '.';
|
|
10
|
+
|
|
11
|
+
export type TypesType = { value: string; label: string };
|
|
12
|
+
// export type CollectionItem = {
|
|
13
|
+
// title: string;
|
|
14
|
+
// collectionName: string;
|
|
15
|
+
// filters?: { [key: string]: string };
|
|
16
|
+
// searchColumns: string[];
|
|
17
|
+
// };
|
|
18
|
+
export interface iConfig {
|
|
19
|
+
logger: any;
|
|
20
|
+
catchAsync: (
|
|
21
|
+
fn: any,
|
|
22
|
+
modal?: string
|
|
23
|
+
) => (req: any, res: any, next: any) => void;
|
|
24
|
+
collections: CollectionItem[];
|
|
25
|
+
}
|
|
26
|
+
// Collection
|
|
27
|
+
export interface IPageSchema extends Document {
|
|
28
|
+
name: string;
|
|
29
|
+
code: string;
|
|
30
|
+
widgets: string[];
|
|
31
|
+
}
|
|
32
|
+
export interface ISrcSetSchema extends Document {
|
|
33
|
+
width: number;
|
|
34
|
+
height: number;
|
|
35
|
+
screenSize: number;
|
|
36
|
+
itemId: typeof Types.ObjectId;
|
|
37
|
+
}
|
|
38
|
+
export interface IWidgetSchema extends Document {
|
|
39
|
+
items: any;
|
|
40
|
+
name: string;
|
|
41
|
+
code: string;
|
|
42
|
+
autoPlay: boolean;
|
|
43
|
+
isActive: boolean;
|
|
44
|
+
widgetTitle: string;
|
|
45
|
+
webPerRow: number;
|
|
46
|
+
mobilePerRow: number;
|
|
47
|
+
tabletPerRow: number;
|
|
48
|
+
itemsType: ItemsType;
|
|
49
|
+
widgetType: WidgetTypes;
|
|
50
|
+
collectionName: string;
|
|
51
|
+
collectionItems: string[];
|
|
52
|
+
}
|
|
53
|
+
export interface IItemSchema extends Document {
|
|
54
|
+
widgetId: typeof Types.ObjectId;
|
|
55
|
+
title: string;
|
|
56
|
+
altText: string;
|
|
57
|
+
link: string;
|
|
58
|
+
sequence: number;
|
|
59
|
+
itemType: ItemTypes;
|
|
60
|
+
img: any;
|
|
61
|
+
srcset?: SrcSetItem[];
|
|
62
|
+
}
|
|
63
|
+
export interface SrcSetItem {
|
|
64
|
+
screenSize: number;
|
|
65
|
+
width: number;
|
|
66
|
+
height: number;
|
|
67
|
+
}
|
|
68
|
+
// \ End of Collection
|
|
69
|
+
|
|
70
|
+
export type CollectionItem = {
|
|
71
|
+
title: string;
|
|
72
|
+
collectionName: string;
|
|
73
|
+
filters?: { [key: string]: string | number | boolean };
|
|
74
|
+
searchColumns?: string[];
|
|
75
|
+
match?: ObjectType;
|
|
76
|
+
project?: ObjectType;
|
|
77
|
+
lookup?: ObjectType;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
export interface IConfig {
|
|
81
|
+
logger: any;
|
|
82
|
+
catchAsync: (
|
|
83
|
+
fn: any,
|
|
84
|
+
modal?: string
|
|
85
|
+
) => (req: any, res: any, next: any) => void;
|
|
86
|
+
collections: CollectionItem[];
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export type EntityType =
|
|
90
|
+
| IWidgetSchema
|
|
91
|
+
| IItemSchema
|
|
92
|
+
| IPageSchema
|
|
93
|
+
| ISrcSetSchema;
|
|
94
|
+
export type ReturnDocument = EntityType;
|
|
95
|
+
export interface IModel<T> extends Model<T> {
|
|
96
|
+
paginate: (
|
|
97
|
+
query: FilterQuery<T>,
|
|
98
|
+
options?: QueryOptions
|
|
99
|
+
) => Promise<HydratedDocument<ReturnDocument>[]>;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export type ObjectType = {
|
|
103
|
+
[key: string]:
|
|
104
|
+
| string
|
|
105
|
+
| number
|
|
106
|
+
| boolean
|
|
107
|
+
| null
|
|
108
|
+
| undefined
|
|
109
|
+
| string[]
|
|
110
|
+
| number[]
|
|
111
|
+
| ObjectType
|
|
112
|
+
| ObjectType[]
|
|
113
|
+
| any;
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
export interface IDefaultValidations {
|
|
117
|
+
createdBy: string;
|
|
118
|
+
updatedBy: string;
|
|
119
|
+
deletedBy: string;
|
|
120
|
+
deletedAt: string;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// helper.ts
|
|
124
|
+
export interface IWidgetDataSchema {
|
|
125
|
+
_id: string;
|
|
126
|
+
code: string;
|
|
127
|
+
collectionName: string;
|
|
128
|
+
collectionItems: string[];
|
|
129
|
+
}
|
|
130
|
+
export type IWidgetData = { [key: string]: IWidgetDataSchema };
|
|
File without changes
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { NextFunction } from 'express';
|
|
2
|
+
import { CollectionItem, IConfig, IRequest, IResponse } from '../types';
|
|
3
|
+
import { RESPONSE_CODES, internalServerError, REGEXS } from '../constants';
|
|
4
|
+
|
|
5
|
+
export const defaults: IConfig = {
|
|
6
|
+
logger: console as any,
|
|
7
|
+
catchAsync:
|
|
8
|
+
(fn: any, modal = '') =>
|
|
9
|
+
(req: IRequest, res: IResponse, next: NextFunction) => {
|
|
10
|
+
Promise.resolve(fn(req, res, next)).catch((err) => {
|
|
11
|
+
let message = (err as Error).message;
|
|
12
|
+
if (message.match(REGEXS.OBJECTID_CAST_FAILED)) {
|
|
13
|
+
message = `${modal} not found with given id!`;
|
|
14
|
+
}
|
|
15
|
+
// this.logger.error(err.message);
|
|
16
|
+
res.status(internalServerError).json({
|
|
17
|
+
code: RESPONSE_CODES.ERROR,
|
|
18
|
+
message,
|
|
19
|
+
data: undefined,
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
},
|
|
23
|
+
collections: [] as CollectionItem[],
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export const commonExcludedFields = {
|
|
27
|
+
__v: 0,
|
|
28
|
+
isDeleted: 0,
|
|
29
|
+
deletedAt: 0,
|
|
30
|
+
};
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { Widget } from '../models';
|
|
2
|
+
import { commonExcludedFields, defaults } from './defaults';
|
|
3
|
+
import {
|
|
4
|
+
IWidgetData,
|
|
5
|
+
IWidgetDataSchema,
|
|
6
|
+
IWidgetSchema,
|
|
7
|
+
SrcSetItem,
|
|
8
|
+
} from '../types';
|
|
9
|
+
|
|
10
|
+
export async function appendCollectionData(widgetData: IWidgetSchema[]) {
|
|
11
|
+
// reduce widget data to optimize query
|
|
12
|
+
const newData: IWidgetData = widgetData.reduce(
|
|
13
|
+
(acc: IWidgetData, widget: IWidgetSchema) => {
|
|
14
|
+
if (widget.collectionName) {
|
|
15
|
+
acc[widget.code] = {
|
|
16
|
+
_id: widget._id,
|
|
17
|
+
code: widget.code,
|
|
18
|
+
collectionName: widget.collectionName,
|
|
19
|
+
collectionItems: widget.collectionItems,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
return acc;
|
|
23
|
+
},
|
|
24
|
+
{}
|
|
25
|
+
);
|
|
26
|
+
if (Object.keys(newData).length > 0) {
|
|
27
|
+
const aggregationQuery: any = [
|
|
28
|
+
{
|
|
29
|
+
$match: {
|
|
30
|
+
_id: {
|
|
31
|
+
$in: Object.values(newData).map(
|
|
32
|
+
(item: IWidgetDataSchema) => item._id
|
|
33
|
+
),
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
// Get only the fields that are not excluded
|
|
39
|
+
$project: {
|
|
40
|
+
_id: 1,
|
|
41
|
+
code: 1,
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
];
|
|
45
|
+
let collectionConfig, aggregationQueryPiplelines: any[];
|
|
46
|
+
Object.keys(newData).forEach((key: string) => {
|
|
47
|
+
collectionConfig = defaults.collections.find(
|
|
48
|
+
(c) => c.collectionName === newData[key].collectionName
|
|
49
|
+
);
|
|
50
|
+
// Build piplelines with config
|
|
51
|
+
aggregationQueryPiplelines = [
|
|
52
|
+
{
|
|
53
|
+
$match: {
|
|
54
|
+
_id: {
|
|
55
|
+
$in: newData[key].collectionItems,
|
|
56
|
+
},
|
|
57
|
+
...(collectionConfig?.match || {}),
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
$project: {
|
|
62
|
+
...commonExcludedFields,
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
];
|
|
66
|
+
// add project config if it exists
|
|
67
|
+
if (collectionConfig?.project)
|
|
68
|
+
aggregationQueryPiplelines.push({
|
|
69
|
+
$project: collectionConfig?.project,
|
|
70
|
+
});
|
|
71
|
+
// add lookup config if it exists
|
|
72
|
+
if (collectionConfig?.lookup)
|
|
73
|
+
aggregationQueryPiplelines.push({
|
|
74
|
+
$lookup: collectionConfig?.lookup,
|
|
75
|
+
});
|
|
76
|
+
// Build Aggregation Query
|
|
77
|
+
aggregationQuery.push({
|
|
78
|
+
$lookup: {
|
|
79
|
+
from: newData[key].collectionName,
|
|
80
|
+
pipeline: aggregationQueryPiplelines,
|
|
81
|
+
as: newData[key].code,
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
// getting collection data by populating widget
|
|
86
|
+
let aggregationData: any = await Widget.aggregate(aggregationQuery);
|
|
87
|
+
aggregationData = aggregationData.reduce((acc: any, aggregation: any) => {
|
|
88
|
+
acc[aggregation.code] = aggregation[aggregation.code];
|
|
89
|
+
return acc;
|
|
90
|
+
}, {});
|
|
91
|
+
// adding collection data to widgets
|
|
92
|
+
return widgetData.map((widget: IWidgetSchema) => {
|
|
93
|
+
if (aggregationData[widget.code]) {
|
|
94
|
+
return {
|
|
95
|
+
...widget,
|
|
96
|
+
collectionItems: aggregationData[widget.code],
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
return widget;
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
// returning widget data as it is if they do not have dynamic collection
|
|
103
|
+
return widgetData;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function buildSrcSetItem(uri: string, setItem: SrcSetItem) {
|
|
107
|
+
const imageItemArr = uri?.split('/') || [];
|
|
108
|
+
imageItemArr.splice(
|
|
109
|
+
imageItemArr.length - 1,
|
|
110
|
+
0,
|
|
111
|
+
`${setItem.width}x${setItem.height}`
|
|
112
|
+
);
|
|
113
|
+
return imageItemArr.join('/');
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function AddSrcSetsToItems(widgetData: IWidgetSchema) {
|
|
117
|
+
if (Array.isArray(widgetData.items) && widgetData.items.length > 0) {
|
|
118
|
+
widgetData.items.forEach((item) => {
|
|
119
|
+
if (Array.isArray(item.srcset) && item.srcset.length > 0 && item.image) {
|
|
120
|
+
item.srcSets = item.srcset.reduce(
|
|
121
|
+
(strArr: string[], setItem: SrcSetItem) => {
|
|
122
|
+
const imageUri = buildSrcSetItem(item.image.uri, setItem);
|
|
123
|
+
strArr.push(`${imageUri} ${setItem.screenSize}w`);
|
|
124
|
+
return strArr;
|
|
125
|
+
},
|
|
126
|
+
[]
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
item.srcSets = item.srcSets.join(', ');
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { RESPONSE_CODES, validationError, success, create } from '../constants';
|
|
2
|
+
import { IResponse } from '../types';
|
|
3
|
+
|
|
4
|
+
export const failureResponse = (data: any, res: IResponse) => {
|
|
5
|
+
let i = 0;
|
|
6
|
+
if (data.name === 'ValidationError') {
|
|
7
|
+
Object.keys(data.errors).forEach((key) => {
|
|
8
|
+
if (i !== 1) {
|
|
9
|
+
data.message = data.errors[key].message;
|
|
10
|
+
}
|
|
11
|
+
i++;
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
res.message = data.message;
|
|
15
|
+
return res.status(validationError).json({
|
|
16
|
+
code: RESPONSE_CODES.ERROR,
|
|
17
|
+
message: data.message ? data.message : data,
|
|
18
|
+
});
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export const successResponse = (data: any, res: IResponse) => {
|
|
22
|
+
return res.status(success).json({
|
|
23
|
+
code: RESPONSE_CODES.DEFAULT,
|
|
24
|
+
message: res.message,
|
|
25
|
+
data: data,
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export const createdDocumentResponse = (data: any, res: IResponse) => {
|
|
30
|
+
return res.status(create).json({
|
|
31
|
+
code: RESPONSE_CODES.DEFAULT,
|
|
32
|
+
message: res.message,
|
|
33
|
+
data: data,
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export const recordNotFound = (res: IResponse) => {
|
|
38
|
+
return res.status(success).json({
|
|
39
|
+
code: RESPONSE_CODES.DEFAULT,
|
|
40
|
+
message: res.message,
|
|
41
|
+
data: {},
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export const inValidParam = (message: any, res: IResponse) => {
|
|
46
|
+
/* eslint-disable no-useless-escape */
|
|
47
|
+
message = message.replace(/\"/g, '');
|
|
48
|
+
res.message = message;
|
|
49
|
+
return res.status(validationError).json({
|
|
50
|
+
code: RESPONSE_CODES.ERROR,
|
|
51
|
+
message: message,
|
|
52
|
+
data: undefined,
|
|
53
|
+
});
|
|
54
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { ValidationError } from 'joi';
|
|
2
|
+
import { NextFunction } from 'express';
|
|
3
|
+
import { IRequest, IResponse } from './../types';
|
|
4
|
+
|
|
5
|
+
import { defaults } from './defaults';
|
|
6
|
+
import { inValidParam, failureResponse } from './responseHandlers';
|
|
7
|
+
|
|
8
|
+
const validate = (validator: any) => {
|
|
9
|
+
return async function (req: IRequest, res: IResponse, next: NextFunction) {
|
|
10
|
+
try {
|
|
11
|
+
req.body = await validator.validateAsync(req.body);
|
|
12
|
+
next();
|
|
13
|
+
} catch (err) {
|
|
14
|
+
defaults.logger.error('ValidationError', err);
|
|
15
|
+
if ((err as ValidationError).isJoi)
|
|
16
|
+
inValidParam((err as Error).message, res);
|
|
17
|
+
else failureResponse((err as Error).message, res);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export default validate;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import joi from 'joi';
|
|
2
|
+
import { IItemSchema, IDefaultValidations, ItemTypes } from '../../types';
|
|
3
|
+
|
|
4
|
+
type ItemValidation = IItemSchema & IDefaultValidations;
|
|
5
|
+
|
|
6
|
+
const srcset = joi.object().keys({
|
|
7
|
+
screenSize: joi.number().required(),
|
|
8
|
+
width: joi.number().required(),
|
|
9
|
+
height: joi.number().required(),
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
export const create = joi.object<ItemValidation>({
|
|
13
|
+
widgetId: joi.string().required(),
|
|
14
|
+
title: joi.string().required(),
|
|
15
|
+
altText: joi.string().optional(),
|
|
16
|
+
link: joi.string().required(),
|
|
17
|
+
sequence: joi.number().optional(),
|
|
18
|
+
srcset: joi.array().items(srcset),
|
|
19
|
+
img: joi.string().allow(null).optional(),
|
|
20
|
+
itemType: joi
|
|
21
|
+
.string()
|
|
22
|
+
.valid(...Object.values(ItemTypes))
|
|
23
|
+
.optional()
|
|
24
|
+
.default(ItemTypes.Web),
|
|
25
|
+
createdBy: joi.any().optional(),
|
|
26
|
+
updatedBy: joi.any().optional(),
|
|
27
|
+
deletedBy: joi.any().optional(),
|
|
28
|
+
deletedAt: joi.any().optional(),
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
export const update = joi.object<ItemValidation>({
|
|
32
|
+
widgetId: joi.string().required(),
|
|
33
|
+
title: joi.string().required(),
|
|
34
|
+
altText: joi.string().optional(),
|
|
35
|
+
link: joi.string().required(),
|
|
36
|
+
sequence: joi.number().optional(),
|
|
37
|
+
img: joi.string().allow(null).optional(),
|
|
38
|
+
srcset: joi.array().items(srcset),
|
|
39
|
+
itemType: joi
|
|
40
|
+
.string()
|
|
41
|
+
.valid(...Object.values(ItemTypes))
|
|
42
|
+
.optional(),
|
|
43
|
+
createdBy: joi.any().optional(),
|
|
44
|
+
updatedBy: joi.any().optional(),
|
|
45
|
+
deletedBy: joi.any().optional(),
|
|
46
|
+
deletedAt: joi.any().optional(),
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
export const list = joi.object({
|
|
50
|
+
search: joi.string().allow('').replace(/\s+/g, '_').default(''),
|
|
51
|
+
});
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import joi from 'joi';
|
|
2
|
+
import { Page } from '../../models';
|
|
3
|
+
import { getOne } from '../../services/dbService';
|
|
4
|
+
import { VALIDATION } from '../../constants';
|
|
5
|
+
import { IPageSchema, IDefaultValidations } from '../../types';
|
|
6
|
+
|
|
7
|
+
type PageValidation = IPageSchema & IDefaultValidations;
|
|
8
|
+
|
|
9
|
+
const checkUnique = async (value: string): Promise<void> => {
|
|
10
|
+
let result;
|
|
11
|
+
try {
|
|
12
|
+
// throws error if document not found
|
|
13
|
+
result = await getOne(Page, {
|
|
14
|
+
code: value,
|
|
15
|
+
});
|
|
16
|
+
// eslint-disable-next-line no-empty
|
|
17
|
+
} catch (e) {}
|
|
18
|
+
if (result) {
|
|
19
|
+
throw new Error(VALIDATION.WIDGET_EXISTS);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export const create = joi.object<PageValidation>({
|
|
24
|
+
name: joi.string().required(),
|
|
25
|
+
code: joi
|
|
26
|
+
.string()
|
|
27
|
+
.uppercase()
|
|
28
|
+
.replace(/\s+/g, '_')
|
|
29
|
+
.external(checkUnique)
|
|
30
|
+
.required(),
|
|
31
|
+
widgets: joi.array().items(joi.string()).optional(),
|
|
32
|
+
createdBy: joi.any().optional(),
|
|
33
|
+
updatedBy: joi.any().optional(),
|
|
34
|
+
deletedBy: joi.any().optional(),
|
|
35
|
+
deletedAt: joi.any().optional(),
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
export const update = joi.object<PageValidation>({
|
|
39
|
+
name: joi.string().optional(),
|
|
40
|
+
widgets: joi.array().items(joi.string()).optional(),
|
|
41
|
+
createdBy: joi.any().optional(),
|
|
42
|
+
updatedBy: joi.any().optional(),
|
|
43
|
+
deletedBy: joi.any().optional(),
|
|
44
|
+
deletedAt: joi.any().optional(),
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
export const list = joi.object({
|
|
48
|
+
search: joi.string().allow('').replace(/\s+/g, '_').optional().default(''),
|
|
49
|
+
options: joi
|
|
50
|
+
.object({
|
|
51
|
+
// sort: joi.alternatives().try(joi.object(), joi.string()).optional(),
|
|
52
|
+
populate: joi.array().items().optional().default([]),
|
|
53
|
+
offset: joi.number().optional(),
|
|
54
|
+
page: joi.number().optional(),
|
|
55
|
+
limit: joi.number().optional(),
|
|
56
|
+
pagination: joi.boolean().default(false),
|
|
57
|
+
})
|
|
58
|
+
.default({}),
|
|
59
|
+
});
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import joi from 'joi';
|
|
2
|
+
import { Widget } from '../../models';
|
|
3
|
+
import { defaults } from '../defaults';
|
|
4
|
+
import { VALIDATION } from '../../constants';
|
|
5
|
+
import { getOne } from '../../services/dbService';
|
|
6
|
+
import {
|
|
7
|
+
ItemsType,
|
|
8
|
+
WidgetTypes,
|
|
9
|
+
IWidgetSchema,
|
|
10
|
+
CollectionItem,
|
|
11
|
+
IDefaultValidations,
|
|
12
|
+
} from '../../types';
|
|
13
|
+
|
|
14
|
+
type ItemValidation = IWidgetSchema & IDefaultValidations;
|
|
15
|
+
|
|
16
|
+
const checkUnique = async (value: string) => {
|
|
17
|
+
let result;
|
|
18
|
+
try {
|
|
19
|
+
// throws error if document found
|
|
20
|
+
result = await getOne(Widget, {
|
|
21
|
+
code: value,
|
|
22
|
+
});
|
|
23
|
+
// eslint-disable-next-line no-empty
|
|
24
|
+
} catch (e) {}
|
|
25
|
+
if (result) {
|
|
26
|
+
throw new Error(VALIDATION.WIDGET_EXISTS);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export const create = joi.object<ItemValidation>({
|
|
31
|
+
name: joi.string().required(),
|
|
32
|
+
widgetTitle: joi.string().required(),
|
|
33
|
+
code: joi
|
|
34
|
+
.string()
|
|
35
|
+
.uppercase()
|
|
36
|
+
.replace(/\s+/g, '_')
|
|
37
|
+
.external(checkUnique)
|
|
38
|
+
.required(),
|
|
39
|
+
isActive: joi.boolean().default(true).optional(),
|
|
40
|
+
autoPlay: joi.boolean().default(false).optional(),
|
|
41
|
+
webPerRow: joi.number().allow(null).optional(),
|
|
42
|
+
mobilePerRow: joi.number().allow(null).optional(),
|
|
43
|
+
tabletPerRow: joi.number().allow(null).optional(),
|
|
44
|
+
collectionName: joi.string().optional(),
|
|
45
|
+
collectionItems: joi.array().items(joi.string()).optional(),
|
|
46
|
+
itemsType: joi
|
|
47
|
+
.string()
|
|
48
|
+
.custom((value) => {
|
|
49
|
+
if (Object.keys(ItemsType).includes(value)) {
|
|
50
|
+
return value;
|
|
51
|
+
}
|
|
52
|
+
const collectionIndex = defaults.collections.findIndex(
|
|
53
|
+
(collection: CollectionItem) => collection.collectionName === value
|
|
54
|
+
);
|
|
55
|
+
if (collectionIndex > -1) {
|
|
56
|
+
return value;
|
|
57
|
+
}
|
|
58
|
+
throw new Error(`${value} is not a valid widget type`);
|
|
59
|
+
})
|
|
60
|
+
.optional()
|
|
61
|
+
.default(ItemsType.Image),
|
|
62
|
+
widgetType: joi
|
|
63
|
+
.string()
|
|
64
|
+
.valid(...Object.values(WidgetTypes))
|
|
65
|
+
.optional()
|
|
66
|
+
.default(WidgetTypes.FixedCard),
|
|
67
|
+
createdBy: joi.any().optional(),
|
|
68
|
+
updatedBy: joi.any().optional(),
|
|
69
|
+
deletedBy: joi.any().optional(),
|
|
70
|
+
deletedAt: joi.any().optional(),
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
export const update = joi.object<ItemValidation>({
|
|
74
|
+
name: joi.string().required(),
|
|
75
|
+
widgetTitle: joi.string().required(),
|
|
76
|
+
isActive: joi.boolean().optional(),
|
|
77
|
+
webPerRow: joi.number().allow(null).optional(),
|
|
78
|
+
mobilePerRow: joi.number().allow(null).optional(),
|
|
79
|
+
tabletPerRow: joi.number().allow(null).optional(),
|
|
80
|
+
autoPlay: joi.boolean().default(false).optional(),
|
|
81
|
+
collectionItems: joi.array().items(joi.string()).optional(),
|
|
82
|
+
widgetType: joi
|
|
83
|
+
.string()
|
|
84
|
+
.valid(...Object.values(WidgetTypes))
|
|
85
|
+
.optional(),
|
|
86
|
+
createdBy: joi.any().optional(),
|
|
87
|
+
updatedBy: joi.any().optional(),
|
|
88
|
+
deletedBy: joi.any().optional(),
|
|
89
|
+
deletedAt: joi.any().optional(),
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
export const list = joi.object({
|
|
93
|
+
search: joi.string().allow('').optional().default(''),
|
|
94
|
+
options: joi
|
|
95
|
+
.object({
|
|
96
|
+
// sort: joi.alternatives().try(joi.object(), joi.string()).optional(),
|
|
97
|
+
// populate: joi.array().items().optional(),
|
|
98
|
+
offset: joi.number().optional(),
|
|
99
|
+
page: joi.number().optional(),
|
|
100
|
+
limit: joi.number().optional(),
|
|
101
|
+
})
|
|
102
|
+
.default({}),
|
|
103
|
+
isActive: joi.boolean().optional(),
|
|
104
|
+
all: joi.boolean().default(false),
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
export const partialUpdate = joi.object({
|
|
108
|
+
isActive: joi.boolean().optional(),
|
|
109
|
+
createdBy: joi.any().optional(),
|
|
110
|
+
updatedBy: joi.any().optional(),
|
|
111
|
+
deletedBy: joi.any().optional(),
|
|
112
|
+
deletedAt: joi.any().optional(),
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
export const getCollectionData = joi.object({
|
|
116
|
+
collectionName: joi.string().required(),
|
|
117
|
+
search: joi.string().allow('').optional().default(''),
|
|
118
|
+
});
|
package/src/constants/index.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
export declare const VALIDATION: {
|
|
2
|
-
WIDGET_EXISTS: string;
|
|
3
|
-
};
|
|
4
|
-
export declare const RESPONSE_CODES: {
|
|
5
|
-
DEFAULT: string;
|
|
6
|
-
ERROR: string;
|
|
7
|
-
};
|
|
8
|
-
export declare const REGEXS: {
|
|
9
|
-
OBJECTID_CAST_FAILED: RegExp;
|
|
10
|
-
};
|
|
11
|
-
export declare const MONGOOSE_FIND_QUERIES: string[];
|
|
12
|
-
export declare const success = 200;
|
|
13
|
-
export declare const create = 201;
|
|
14
|
-
export declare const internalServerError = 500;
|
|
15
|
-
export declare const validationError = 422;
|
|
16
|
-
export declare const messageRoutePrefix = "msg";
|
package/src/constants/index.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.messageRoutePrefix = exports.validationError = exports.internalServerError = exports.create = exports.success = exports.MONGOOSE_FIND_QUERIES = exports.REGEXS = exports.RESPONSE_CODES = exports.VALIDATION = void 0;
|
|
4
|
-
exports.VALIDATION = {
|
|
5
|
-
WIDGET_EXISTS: 'Widget with same code is available',
|
|
6
|
-
};
|
|
7
|
-
exports.RESPONSE_CODES = {
|
|
8
|
-
DEFAULT: 'SUCCESS',
|
|
9
|
-
ERROR: 'ERROR',
|
|
10
|
-
};
|
|
11
|
-
exports.REGEXS = {
|
|
12
|
-
OBJECTID_CAST_FAILED: /Cast to ObjectId failed/gm,
|
|
13
|
-
};
|
|
14
|
-
exports.MONGOOSE_FIND_QUERIES = [
|
|
15
|
-
'count',
|
|
16
|
-
'find',
|
|
17
|
-
'findOne',
|
|
18
|
-
'findOneAndDelete',
|
|
19
|
-
'findOneAndRemove',
|
|
20
|
-
'findOneAndUpdate',
|
|
21
|
-
'update',
|
|
22
|
-
'updateOne',
|
|
23
|
-
'updateMany',
|
|
24
|
-
];
|
|
25
|
-
exports.success = 200;
|
|
26
|
-
exports.create = 201;
|
|
27
|
-
exports.internalServerError = 500;
|
|
28
|
-
exports.validationError = 422;
|
|
29
|
-
exports.messageRoutePrefix = 'msg';
|
|
30
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../libs/node/src/constants/index.ts"],"names":[],"mappings":";;;AAAa,QAAA,UAAU,GAAG;IACzB,aAAa,EAAE,oCAAoC;CACnD,CAAC;AACW,QAAA,cAAc,GAAG;IAC7B,OAAO,EAAE,SAAS;IAClB,KAAK,EAAE,OAAO;CACd,CAAC;AACW,QAAA,MAAM,GAAG;IACrB,oBAAoB,EAAE,2BAA2B;CACjD,CAAC;AACW,QAAA,qBAAqB,GAAG;IACpC,OAAO;IACP,MAAM;IACN,SAAS;IACT,kBAAkB;IAClB,kBAAkB;IAClB,kBAAkB;IAClB,QAAQ;IACR,WAAW;IACX,YAAY;CACZ,CAAC;AACW,QAAA,OAAO,GAAG,GAAG,CAAC;AACd,QAAA,MAAM,GAAG,GAAG,CAAC;AACb,QAAA,mBAAmB,GAAG,GAAG,CAAC;AAC1B,QAAA,eAAe,GAAG,GAAG,CAAC;AACtB,QAAA,kBAAkB,GAAG,KAAK,CAAC"}
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
export declare const createPage: (req: any, res: any, next: any) => void;
|
|
2
|
-
export declare const updatePage: (req: any, res: any, next: any) => void;
|
|
3
|
-
export declare const deletePage: (req: any, res: any, next: any) => void;
|
|
4
|
-
export declare const getPages: (req: any, res: any, next: any) => void;
|