@knovator/pagecreator-node 1.2.2 → 1.2.3
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 +458 -458
- package/package.json +3 -2
- package/src/constants/index.d.ts +20 -20
- package/src/constants/index.js +33 -33
- package/src/controllers/PageController.d.ts +4 -4
- package/src/controllers/PageController.js +83 -83
- package/src/controllers/TabController.d.ts +4 -4
- package/src/controllers/TabController.js +45 -45
- package/src/controllers/UserController.d.ts +2 -2
- package/src/controllers/UserController.js +64 -64
- package/src/controllers/WidgetController.d.ts +10 -10
- package/src/controllers/WidgetController.js +328 -328
- package/src/index.d.ts +8 -8
- package/src/index.js +47 -47
- package/src/models/Item.d.ts +30 -30
- package/src/models/Item.js +49 -49
- package/src/models/Page.d.ts +30 -30
- package/src/models/Page.js +22 -22
- package/src/models/SrcSet.d.ts +30 -30
- package/src/models/SrcSet.js +16 -16
- package/src/models/Tab.d.ts +30 -30
- package/src/models/Tab.js +27 -27
- package/src/models/Widget.d.ts +30 -30
- package/src/models/Widget.js +62 -62
- package/src/models/index.d.ts +6 -6
- package/src/models/index.js +18 -18
- package/src/plugins/softDelete.d.ts +7 -7
- package/src/plugins/softDelete.js +63 -63
- package/src/routes/PageRoute.d.ts +3 -3
- package/src/routes/PageRoute.js +29 -29
- package/src/routes/UserRoute.d.ts +3 -3
- package/src/routes/UserRoute.js +22 -22
- package/src/routes/WidgetRoute.d.ts +3 -3
- package/src/routes/WidgetRoute.js +67 -67
- package/src/services/dataService.d.ts +9 -9
- package/src/services/dataService.js +402 -402
- package/src/services/dbService.d.ts +16 -16
- package/src/services/dbService.js +88 -87
- package/src/services/dbService.js.map +1 -1
- package/src/types/IRequest.d.ts +6 -6
- package/src/types/IRequest.js +2 -2
- package/src/types/IResponse.d.ts +6 -6
- package/src/types/IResponse.js +2 -2
- package/src/types/Router.d.ts +4 -4
- package/src/types/Router.js +2 -2
- package/src/types/common.d.ts +143 -142
- package/src/types/common.js +2 -2
- package/src/types/enums.d.ts +14 -14
- package/src/types/enums.js +20 -20
- package/src/types/index.d.ts +5 -5
- package/src/types/index.js +8 -8
- package/src/utils/defaults.d.ts +7 -7
- package/src/utils/defaults.js +40 -40
- package/src/utils/helper.d.ts +7 -7
- package/src/utils/helper.js +224 -224
- package/src/utils/redis.d.ts +3 -3
- package/src/utils/redis.js +58 -58
- package/src/utils/responseHandlers.d.ts +6 -6
- package/src/utils/responseHandlers.js +56 -56
- package/src/utils/validate.d.ts +4 -4
- package/src/utils/validate.js +23 -23
- package/src/utils/validations/page.d.ts +7 -7
- package/src/utils/validations/page.js +52 -50
- package/src/utils/validations/page.js.map +1 -1
- package/src/utils/validations/tab.d.ts +6 -6
- package/src/utils/validations/tab.js +25 -25
- package/src/utils/validations/user.d.ts +3 -3
- package/src/utils/validations/user.js +12 -12
- package/src/utils/validations/widget.d.ts +9 -9
- package/src/utils/validations/widget.js +152 -152
package/src/types/enums.js
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ItemTypes = exports.WidgetTypes = exports.ItemsType = void 0;
|
|
4
|
-
var ItemsType;
|
|
5
|
-
(function (ItemsType) {
|
|
6
|
-
ItemsType["Image"] = "Image";
|
|
7
|
-
})(ItemsType = exports.ItemsType || (exports.ItemsType = {}));
|
|
8
|
-
var WidgetTypes;
|
|
9
|
-
(function (WidgetTypes) {
|
|
10
|
-
WidgetTypes["FixedCard"] = "FixedCard";
|
|
11
|
-
WidgetTypes["Carousel"] = "Carousel";
|
|
12
|
-
WidgetTypes["Tabs"] = "Tabs";
|
|
13
|
-
WidgetTypes["Text"] = "Text";
|
|
14
|
-
WidgetTypes["HTML"] = "HTML";
|
|
15
|
-
})(WidgetTypes = exports.WidgetTypes || (exports.WidgetTypes = {}));
|
|
16
|
-
var ItemTypes;
|
|
17
|
-
(function (ItemTypes) {
|
|
18
|
-
ItemTypes["Web"] = "Web";
|
|
19
|
-
ItemTypes["Mobile"] = "Mobile";
|
|
20
|
-
})(ItemTypes = exports.ItemTypes || (exports.ItemTypes = {}));
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ItemTypes = exports.WidgetTypes = exports.ItemsType = void 0;
|
|
4
|
+
var ItemsType;
|
|
5
|
+
(function (ItemsType) {
|
|
6
|
+
ItemsType["Image"] = "Image";
|
|
7
|
+
})(ItemsType = exports.ItemsType || (exports.ItemsType = {}));
|
|
8
|
+
var WidgetTypes;
|
|
9
|
+
(function (WidgetTypes) {
|
|
10
|
+
WidgetTypes["FixedCard"] = "FixedCard";
|
|
11
|
+
WidgetTypes["Carousel"] = "Carousel";
|
|
12
|
+
WidgetTypes["Tabs"] = "Tabs";
|
|
13
|
+
WidgetTypes["Text"] = "Text";
|
|
14
|
+
WidgetTypes["HTML"] = "HTML";
|
|
15
|
+
})(WidgetTypes = exports.WidgetTypes || (exports.WidgetTypes = {}));
|
|
16
|
+
var ItemTypes;
|
|
17
|
+
(function (ItemTypes) {
|
|
18
|
+
ItemTypes["Web"] = "Web";
|
|
19
|
+
ItemTypes["Mobile"] = "Mobile";
|
|
20
|
+
})(ItemTypes = exports.ItemTypes || (exports.ItemTypes = {}));
|
|
21
21
|
//# sourceMappingURL=enums.js.map
|
package/src/types/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export * from './IRequest';
|
|
2
|
-
export * from './IResponse';
|
|
3
|
-
export * from './enums';
|
|
4
|
-
export * from './Router';
|
|
5
|
-
export * from './common';
|
|
1
|
+
export * from './IRequest';
|
|
2
|
+
export * from './IResponse';
|
|
3
|
+
export * from './enums';
|
|
4
|
+
export * from './Router';
|
|
5
|
+
export * from './common';
|
package/src/types/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const tslib_1 = require("tslib");
|
|
4
|
-
tslib_1.__exportStar(require("./IRequest"), exports);
|
|
5
|
-
tslib_1.__exportStar(require("./IResponse"), exports);
|
|
6
|
-
tslib_1.__exportStar(require("./enums"), exports);
|
|
7
|
-
tslib_1.__exportStar(require("./Router"), exports);
|
|
8
|
-
tslib_1.__exportStar(require("./common"), exports);
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
tslib_1.__exportStar(require("./IRequest"), exports);
|
|
5
|
+
tslib_1.__exportStar(require("./IResponse"), exports);
|
|
6
|
+
tslib_1.__exportStar(require("./enums"), exports);
|
|
7
|
+
tslib_1.__exportStar(require("./Router"), exports);
|
|
8
|
+
tslib_1.__exportStar(require("./common"), exports);
|
|
9
9
|
//# sourceMappingURL=index.js.map
|
package/src/utils/defaults.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { IConfig } from '../types';
|
|
2
|
-
export declare const defaults: IConfig;
|
|
3
|
-
export declare const commonExcludedFields: {
|
|
4
|
-
__v: number;
|
|
5
|
-
isDeleted: number;
|
|
6
|
-
deletedAt: number;
|
|
7
|
-
};
|
|
1
|
+
import { IConfig } from '../types';
|
|
2
|
+
export declare const defaults: IConfig;
|
|
3
|
+
export declare const commonExcludedFields: {
|
|
4
|
+
__v: number;
|
|
5
|
+
isDeleted: number;
|
|
6
|
+
deletedAt: number;
|
|
7
|
+
};
|
package/src/utils/defaults.js
CHANGED
|
@@ -1,41 +1,41 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.commonExcludedFields = exports.defaults = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
const mongoose_1 = tslib_1.__importDefault(require("mongoose"));
|
|
6
|
-
const constants_1 = require("../constants");
|
|
7
|
-
exports.defaults = {
|
|
8
|
-
logger: console,
|
|
9
|
-
getModals: () => mongoose_1.default.models,
|
|
10
|
-
catchAsync: (fn, modal = '') => (req, res, next) => {
|
|
11
|
-
Promise.resolve(fn(req, res, next)).catch((err) => {
|
|
12
|
-
let message = err.message;
|
|
13
|
-
if (message.match(constants_1.REGEXS.OBJECTID_CAST_FAILED)) {
|
|
14
|
-
message = `${modal} not found with given id!`;
|
|
15
|
-
}
|
|
16
|
-
else if (message.match(constants_1.REGEXS.IS_AVAILABLE)) {
|
|
17
|
-
return res.status(constants_1.validationError).json({
|
|
18
|
-
code: constants_1.RESPONSE_CODES.VALIDATION_FAILED,
|
|
19
|
-
message,
|
|
20
|
-
data: undefined,
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
// this.logger.error(err.message);
|
|
24
|
-
return res.status(constants_1.internalServerError).json({
|
|
25
|
-
code: constants_1.RESPONSE_CODES.ERROR,
|
|
26
|
-
message,
|
|
27
|
-
data: undefined,
|
|
28
|
-
});
|
|
29
|
-
});
|
|
30
|
-
},
|
|
31
|
-
collections: [],
|
|
32
|
-
customWidgetTypes: [],
|
|
33
|
-
redis: undefined,
|
|
34
|
-
languages: [],
|
|
35
|
-
};
|
|
36
|
-
exports.commonExcludedFields = {
|
|
37
|
-
__v: 0,
|
|
38
|
-
isDeleted: 0,
|
|
39
|
-
deletedAt: 0,
|
|
40
|
-
};
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.commonExcludedFields = exports.defaults = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const mongoose_1 = tslib_1.__importDefault(require("mongoose"));
|
|
6
|
+
const constants_1 = require("../constants");
|
|
7
|
+
exports.defaults = {
|
|
8
|
+
logger: console,
|
|
9
|
+
getModals: () => mongoose_1.default.models,
|
|
10
|
+
catchAsync: (fn, modal = '') => (req, res, next) => {
|
|
11
|
+
Promise.resolve(fn(req, res, next)).catch((err) => {
|
|
12
|
+
let message = err.message;
|
|
13
|
+
if (message.match(constants_1.REGEXS.OBJECTID_CAST_FAILED)) {
|
|
14
|
+
message = `${modal} not found with given id!`;
|
|
15
|
+
}
|
|
16
|
+
else if (message.match(constants_1.REGEXS.IS_AVAILABLE)) {
|
|
17
|
+
return res.status(constants_1.validationError).json({
|
|
18
|
+
code: constants_1.RESPONSE_CODES.VALIDATION_FAILED,
|
|
19
|
+
message,
|
|
20
|
+
data: undefined,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
// this.logger.error(err.message);
|
|
24
|
+
return res.status(constants_1.internalServerError).json({
|
|
25
|
+
code: constants_1.RESPONSE_CODES.ERROR,
|
|
26
|
+
message,
|
|
27
|
+
data: undefined,
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
},
|
|
31
|
+
collections: [],
|
|
32
|
+
customWidgetTypes: [],
|
|
33
|
+
redis: undefined,
|
|
34
|
+
languages: [],
|
|
35
|
+
};
|
|
36
|
+
exports.commonExcludedFields = {
|
|
37
|
+
__v: 0,
|
|
38
|
+
isDeleted: 0,
|
|
39
|
+
deletedAt: 0,
|
|
40
|
+
};
|
|
41
41
|
//# sourceMappingURL=defaults.js.map
|
package/src/utils/helper.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { Models, Types } from 'mongoose';
|
|
2
|
-
import { IWidgetSchema, SrcSetItem } from '../types';
|
|
3
|
-
export declare function appendCollectionData(widgetData: IWidgetSchema[], models: Models): Promise<IWidgetSchema[]>;
|
|
4
|
-
export declare function buildSrcSetItem(uri: string, setItem: SrcSetItem): string;
|
|
5
|
-
export declare function AddSrcSetsToItems(widgetData: IWidgetSchema): void;
|
|
6
|
-
export declare const getCollectionModal: (collectionName: string, models: Models) => any;
|
|
7
|
-
export declare const formatCollectionItems: (collectionItems: any[]) => Types.ObjectId[];
|
|
1
|
+
import { Models, Types } from 'mongoose';
|
|
2
|
+
import { IWidgetSchema, SrcSetItem } from '../types';
|
|
3
|
+
export declare function appendCollectionData(widgetData: IWidgetSchema[], models: Models): Promise<IWidgetSchema[]>;
|
|
4
|
+
export declare function buildSrcSetItem(uri: string, setItem: SrcSetItem): string;
|
|
5
|
+
export declare function AddSrcSetsToItems(widgetData: IWidgetSchema): void;
|
|
6
|
+
export declare const getCollectionModal: (collectionName: string, models: Models) => any;
|
|
7
|
+
export declare const formatCollectionItems: (collectionItems: any[]) => Types.ObjectId[];
|
package/src/utils/helper.js
CHANGED
|
@@ -1,225 +1,225 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.formatCollectionItems = exports.getCollectionModal = exports.AddSrcSetsToItems = exports.buildSrcSetItem = exports.appendCollectionData = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
const mongoose_1 = require("mongoose");
|
|
6
|
-
const defaults_1 = require("./defaults");
|
|
7
|
-
function appendCollectionData(widgetData, models) {
|
|
8
|
-
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
9
|
-
const { Widget } = models;
|
|
10
|
-
// reduce widget data to optimize query
|
|
11
|
-
const newData = widgetData.reduce((acc, widget) => {
|
|
12
|
-
if (widget.collectionName) {
|
|
13
|
-
acc[widget.code] = {
|
|
14
|
-
_id: widget._id,
|
|
15
|
-
code: widget.code,
|
|
16
|
-
collectionName: widget.collectionName,
|
|
17
|
-
collectionItems: widget.collectionItems,
|
|
18
|
-
tabs: widget.tabs,
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
return acc;
|
|
22
|
-
}, {});
|
|
23
|
-
if (Object.keys(newData).length > 0) {
|
|
24
|
-
const aggregationQueryCollectionItems = buildCollectionItemsQuery(newData);
|
|
25
|
-
if (aggregationQueryCollectionItems.length > 0) {
|
|
26
|
-
// getting collection data by populating widget
|
|
27
|
-
let aggregationData = yield Widget.aggregate(aggregationQueryCollectionItems);
|
|
28
|
-
aggregationData = aggregationData.reduce((acc, aggregation) => {
|
|
29
|
-
acc[aggregation.code] = aggregation[aggregation.code];
|
|
30
|
-
return acc;
|
|
31
|
-
}, {});
|
|
32
|
-
// adding collection data to widgets
|
|
33
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
34
|
-
// @ts-ignore
|
|
35
|
-
widgetData = widgetData.map((widget) => {
|
|
36
|
-
if (aggregationData[widget.code]) {
|
|
37
|
-
return Object.assign(Object.assign({}, widget), { collectionItems: aggregationData[widget.code] });
|
|
38
|
-
}
|
|
39
|
-
return widget;
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
const aggregationQueryTabs = buildTabCollectionItemsQuery(newData);
|
|
43
|
-
if (aggregationQueryTabs.length > 0) {
|
|
44
|
-
let aggregationDataTabs = yield Widget.aggregate(aggregationQueryTabs);
|
|
45
|
-
aggregationDataTabs = aggregationDataTabs.reduce((acc, aggregation) => {
|
|
46
|
-
if (aggregation[aggregation.code])
|
|
47
|
-
acc[aggregation.code] = aggregation[aggregation.code];
|
|
48
|
-
return acc;
|
|
49
|
-
}, {});
|
|
50
|
-
// adding collection data to widgets
|
|
51
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
52
|
-
// @ts-ignore
|
|
53
|
-
widgetData = widgetData.map((widget) => {
|
|
54
|
-
if (!aggregationDataTabs[widget.code])
|
|
55
|
-
return widget;
|
|
56
|
-
const collectionItemsObj = aggregationDataTabs[widget.code].reduce((acc, item) => {
|
|
57
|
-
acc[item._id] = item;
|
|
58
|
-
return acc;
|
|
59
|
-
}, {});
|
|
60
|
-
return Object.assign(Object.assign({}, widget), { tabs: widget.tabs.map((tabItem) => {
|
|
61
|
-
return {
|
|
62
|
-
name: tabItem.name,
|
|
63
|
-
names: tabItem.names,
|
|
64
|
-
collectionItems: tabItem.collectionItems
|
|
65
|
-
.map((collectionId) => collectionItemsObj[collectionId])
|
|
66
|
-
.filter(Boolean),
|
|
67
|
-
};
|
|
68
|
-
}) });
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
// returning widget data as it is if they do not have dynamic collection
|
|
73
|
-
return widgetData;
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
exports.appendCollectionData = appendCollectionData;
|
|
77
|
-
function buildCollectionItemsQuery(formattedWidgetData) {
|
|
78
|
-
const aggregationQuery = [
|
|
79
|
-
{
|
|
80
|
-
$match: {
|
|
81
|
-
_id: {
|
|
82
|
-
$in: Object.values(formattedWidgetData).map((item) => item._id),
|
|
83
|
-
},
|
|
84
|
-
},
|
|
85
|
-
},
|
|
86
|
-
{
|
|
87
|
-
// Get only the fields that are not excluded
|
|
88
|
-
$project: {
|
|
89
|
-
_id: 1,
|
|
90
|
-
code: 1,
|
|
91
|
-
},
|
|
92
|
-
},
|
|
93
|
-
];
|
|
94
|
-
let collectionConfig;
|
|
95
|
-
Object.keys(formattedWidgetData).forEach((key) => {
|
|
96
|
-
if (formattedWidgetData[key].collectionItems &&
|
|
97
|
-
formattedWidgetData[key].collectionItems.length > 0) {
|
|
98
|
-
const aggregationQueryPiplelines = [];
|
|
99
|
-
collectionConfig = defaults_1.defaults.collections.find((c) => c.collectionName === formattedWidgetData[key].collectionName);
|
|
100
|
-
if (Array.isArray(collectionConfig === null || collectionConfig === void 0 ? void 0 : collectionConfig.aggregations) &&
|
|
101
|
-
(collectionConfig === null || collectionConfig === void 0 ? void 0 : collectionConfig.aggregations.length)) {
|
|
102
|
-
aggregationQueryPiplelines.push(...collectionConfig.aggregations);
|
|
103
|
-
}
|
|
104
|
-
const ids = (0, exports.formatCollectionItems)(formattedWidgetData[key].collectionItems);
|
|
105
|
-
// Build piplelines with config
|
|
106
|
-
aggregationQueryPiplelines.push(...[
|
|
107
|
-
{
|
|
108
|
-
$match: Object.assign({ _id: {
|
|
109
|
-
$in: ids,
|
|
110
|
-
} }, ((collectionConfig === null || collectionConfig === void 0 ? void 0 : collectionConfig.match) || {})),
|
|
111
|
-
},
|
|
112
|
-
{
|
|
113
|
-
$project: Object.assign({}, defaults_1.commonExcludedFields),
|
|
114
|
-
},
|
|
115
|
-
{ $addFields: { __order: { $indexOfArray: [ids, '$_id'] } } },
|
|
116
|
-
{ $sort: { __order: 1 } },
|
|
117
|
-
]);
|
|
118
|
-
// Build Aggregation Query
|
|
119
|
-
aggregationQuery.push({
|
|
120
|
-
$lookup: {
|
|
121
|
-
from: formattedWidgetData[key].collectionName,
|
|
122
|
-
pipeline: aggregationQueryPiplelines,
|
|
123
|
-
as: formattedWidgetData[key].code,
|
|
124
|
-
},
|
|
125
|
-
});
|
|
126
|
-
}
|
|
127
|
-
});
|
|
128
|
-
return aggregationQuery;
|
|
129
|
-
}
|
|
130
|
-
function buildTabCollectionItemsQuery(formattedWidgetData) {
|
|
131
|
-
const aggregationQuery = [
|
|
132
|
-
{
|
|
133
|
-
$match: {
|
|
134
|
-
_id: {
|
|
135
|
-
$in: Object.values(formattedWidgetData).map((item) => item._id),
|
|
136
|
-
},
|
|
137
|
-
},
|
|
138
|
-
},
|
|
139
|
-
{
|
|
140
|
-
// Get only the fields that are not excluded
|
|
141
|
-
$project: {
|
|
142
|
-
_id: 1,
|
|
143
|
-
code: 1,
|
|
144
|
-
},
|
|
145
|
-
},
|
|
146
|
-
];
|
|
147
|
-
let collectionConfig;
|
|
148
|
-
Object.keys(formattedWidgetData).forEach((key) => {
|
|
149
|
-
if (formattedWidgetData[key].tabs &&
|
|
150
|
-
formattedWidgetData[key].tabs.length > 0) {
|
|
151
|
-
const aggregationQueryPiplelines = [];
|
|
152
|
-
collectionConfig = defaults_1.defaults.collections.find((c) => c.collectionName === formattedWidgetData[key].collectionName);
|
|
153
|
-
if (Array.isArray(collectionConfig === null || collectionConfig === void 0 ? void 0 : collectionConfig.aggregations) &&
|
|
154
|
-
(collectionConfig === null || collectionConfig === void 0 ? void 0 : collectionConfig.aggregations.length)) {
|
|
155
|
-
aggregationQueryPiplelines.push(...collectionConfig.aggregations);
|
|
156
|
-
}
|
|
157
|
-
// Build piplelines with config
|
|
158
|
-
aggregationQueryPiplelines.push(...[
|
|
159
|
-
{
|
|
160
|
-
$match: Object.assign({ _id: {
|
|
161
|
-
$in: formattedWidgetData[key].tabs.reduce((arr, tabItem) => {
|
|
162
|
-
arr.push(...(0, exports.formatCollectionItems)(tabItem.collectionItems));
|
|
163
|
-
return arr;
|
|
164
|
-
}, []),
|
|
165
|
-
} }, ((collectionConfig === null || collectionConfig === void 0 ? void 0 : collectionConfig.match) || {})),
|
|
166
|
-
},
|
|
167
|
-
{
|
|
168
|
-
$project: Object.assign({}, defaults_1.commonExcludedFields),
|
|
169
|
-
},
|
|
170
|
-
]);
|
|
171
|
-
// Build Aggregation Query
|
|
172
|
-
aggregationQuery.push({
|
|
173
|
-
$lookup: {
|
|
174
|
-
from: formattedWidgetData[key].collectionName,
|
|
175
|
-
pipeline: aggregationQueryPiplelines,
|
|
176
|
-
as: formattedWidgetData[key].code,
|
|
177
|
-
},
|
|
178
|
-
});
|
|
179
|
-
}
|
|
180
|
-
});
|
|
181
|
-
return aggregationQuery;
|
|
182
|
-
}
|
|
183
|
-
function buildSrcSetItem(uri, setItem) {
|
|
184
|
-
const imageItemArr = (uri === null || uri === void 0 ? void 0 : uri.split('/')) || [];
|
|
185
|
-
imageItemArr.splice(imageItemArr.length - 1, 0, `${setItem.width}x${setItem.height}`);
|
|
186
|
-
return imageItemArr.join('/');
|
|
187
|
-
}
|
|
188
|
-
exports.buildSrcSetItem = buildSrcSetItem;
|
|
189
|
-
function AddSrcSetsToItems(widgetData) {
|
|
190
|
-
if (Array.isArray(widgetData.items) && widgetData.items.length > 0) {
|
|
191
|
-
widgetData.items.forEach((item) => {
|
|
192
|
-
if (Array.isArray(item.srcset) && item.srcset.length > 0 && item.image) {
|
|
193
|
-
item.srcSets = item.srcset.reduce((strArr, setItem) => {
|
|
194
|
-
const imageUri = buildSrcSetItem(item.image.uri, setItem);
|
|
195
|
-
strArr.push(`${imageUri} ${setItem.screenSize}w`);
|
|
196
|
-
return strArr;
|
|
197
|
-
}, []);
|
|
198
|
-
item.srcSets = item.srcSets.join(', ');
|
|
199
|
-
}
|
|
200
|
-
});
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
exports.AddSrcSetsToItems = AddSrcSetsToItems;
|
|
204
|
-
const getCollectionModal = (collectionName, models) => {
|
|
205
|
-
let collectionModal = models[collectionName];
|
|
206
|
-
if (!collectionModal)
|
|
207
|
-
collectionModal = models[collectionName.charAt(0).toUpperCase() + collectionName.slice(1)];
|
|
208
|
-
if (!collectionModal) {
|
|
209
|
-
const schema = new mongoose_1.Schema({}, { strict: false });
|
|
210
|
-
collectionModal = (0, mongoose_1.model)(collectionName, schema, collectionName);
|
|
211
|
-
}
|
|
212
|
-
return collectionModal;
|
|
213
|
-
};
|
|
214
|
-
exports.getCollectionModal = getCollectionModal;
|
|
215
|
-
const formatCollectionItems = (collectionItems) => {
|
|
216
|
-
if (Array.isArray(collectionItems) && collectionItems.length === 0)
|
|
217
|
-
return [];
|
|
218
|
-
return collectionItems.map((item) => {
|
|
219
|
-
if (item instanceof mongoose_1.Types.ObjectId)
|
|
220
|
-
return item;
|
|
221
|
-
return new mongoose_1.Types.ObjectId(item);
|
|
222
|
-
});
|
|
223
|
-
};
|
|
224
|
-
exports.formatCollectionItems = formatCollectionItems;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.formatCollectionItems = exports.getCollectionModal = exports.AddSrcSetsToItems = exports.buildSrcSetItem = exports.appendCollectionData = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const mongoose_1 = require("mongoose");
|
|
6
|
+
const defaults_1 = require("./defaults");
|
|
7
|
+
function appendCollectionData(widgetData, models) {
|
|
8
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
9
|
+
const { Widget } = models;
|
|
10
|
+
// reduce widget data to optimize query
|
|
11
|
+
const newData = widgetData.reduce((acc, widget) => {
|
|
12
|
+
if (widget.collectionName) {
|
|
13
|
+
acc[widget.code] = {
|
|
14
|
+
_id: widget._id,
|
|
15
|
+
code: widget.code,
|
|
16
|
+
collectionName: widget.collectionName,
|
|
17
|
+
collectionItems: widget.collectionItems,
|
|
18
|
+
tabs: widget.tabs,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
return acc;
|
|
22
|
+
}, {});
|
|
23
|
+
if (Object.keys(newData).length > 0) {
|
|
24
|
+
const aggregationQueryCollectionItems = buildCollectionItemsQuery(newData);
|
|
25
|
+
if (aggregationQueryCollectionItems.length > 0) {
|
|
26
|
+
// getting collection data by populating widget
|
|
27
|
+
let aggregationData = yield Widget.aggregate(aggregationQueryCollectionItems);
|
|
28
|
+
aggregationData = aggregationData.reduce((acc, aggregation) => {
|
|
29
|
+
acc[aggregation.code] = aggregation[aggregation.code];
|
|
30
|
+
return acc;
|
|
31
|
+
}, {});
|
|
32
|
+
// adding collection data to widgets
|
|
33
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
34
|
+
// @ts-ignore
|
|
35
|
+
widgetData = widgetData.map((widget) => {
|
|
36
|
+
if (aggregationData[widget.code]) {
|
|
37
|
+
return Object.assign(Object.assign({}, widget), { collectionItems: aggregationData[widget.code] });
|
|
38
|
+
}
|
|
39
|
+
return widget;
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
const aggregationQueryTabs = buildTabCollectionItemsQuery(newData);
|
|
43
|
+
if (aggregationQueryTabs.length > 0) {
|
|
44
|
+
let aggregationDataTabs = yield Widget.aggregate(aggregationQueryTabs);
|
|
45
|
+
aggregationDataTabs = aggregationDataTabs.reduce((acc, aggregation) => {
|
|
46
|
+
if (aggregation[aggregation.code])
|
|
47
|
+
acc[aggregation.code] = aggregation[aggregation.code];
|
|
48
|
+
return acc;
|
|
49
|
+
}, {});
|
|
50
|
+
// adding collection data to widgets
|
|
51
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
52
|
+
// @ts-ignore
|
|
53
|
+
widgetData = widgetData.map((widget) => {
|
|
54
|
+
if (!aggregationDataTabs[widget.code])
|
|
55
|
+
return widget;
|
|
56
|
+
const collectionItemsObj = aggregationDataTabs[widget.code].reduce((acc, item) => {
|
|
57
|
+
acc[item._id] = item;
|
|
58
|
+
return acc;
|
|
59
|
+
}, {});
|
|
60
|
+
return Object.assign(Object.assign({}, widget), { tabs: widget.tabs.map((tabItem) => {
|
|
61
|
+
return {
|
|
62
|
+
name: tabItem.name,
|
|
63
|
+
names: tabItem.names,
|
|
64
|
+
collectionItems: tabItem.collectionItems
|
|
65
|
+
.map((collectionId) => collectionItemsObj[collectionId])
|
|
66
|
+
.filter(Boolean),
|
|
67
|
+
};
|
|
68
|
+
}) });
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
// returning widget data as it is if they do not have dynamic collection
|
|
73
|
+
return widgetData;
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
exports.appendCollectionData = appendCollectionData;
|
|
77
|
+
function buildCollectionItemsQuery(formattedWidgetData) {
|
|
78
|
+
const aggregationQuery = [
|
|
79
|
+
{
|
|
80
|
+
$match: {
|
|
81
|
+
_id: {
|
|
82
|
+
$in: Object.values(formattedWidgetData).map((item) => item._id),
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
// Get only the fields that are not excluded
|
|
88
|
+
$project: {
|
|
89
|
+
_id: 1,
|
|
90
|
+
code: 1,
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
];
|
|
94
|
+
let collectionConfig;
|
|
95
|
+
Object.keys(formattedWidgetData).forEach((key) => {
|
|
96
|
+
if (formattedWidgetData[key].collectionItems &&
|
|
97
|
+
formattedWidgetData[key].collectionItems.length > 0) {
|
|
98
|
+
const aggregationQueryPiplelines = [];
|
|
99
|
+
collectionConfig = defaults_1.defaults.collections.find((c) => c.collectionName === formattedWidgetData[key].collectionName);
|
|
100
|
+
if (Array.isArray(collectionConfig === null || collectionConfig === void 0 ? void 0 : collectionConfig.aggregations) &&
|
|
101
|
+
(collectionConfig === null || collectionConfig === void 0 ? void 0 : collectionConfig.aggregations.length)) {
|
|
102
|
+
aggregationQueryPiplelines.push(...collectionConfig.aggregations);
|
|
103
|
+
}
|
|
104
|
+
const ids = (0, exports.formatCollectionItems)(formattedWidgetData[key].collectionItems);
|
|
105
|
+
// Build piplelines with config
|
|
106
|
+
aggregationQueryPiplelines.push(...[
|
|
107
|
+
{
|
|
108
|
+
$match: Object.assign({ _id: {
|
|
109
|
+
$in: ids,
|
|
110
|
+
} }, ((collectionConfig === null || collectionConfig === void 0 ? void 0 : collectionConfig.match) || {})),
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
$project: Object.assign({}, defaults_1.commonExcludedFields),
|
|
114
|
+
},
|
|
115
|
+
{ $addFields: { __order: { $indexOfArray: [ids, '$_id'] } } },
|
|
116
|
+
{ $sort: { __order: 1 } },
|
|
117
|
+
]);
|
|
118
|
+
// Build Aggregation Query
|
|
119
|
+
aggregationQuery.push({
|
|
120
|
+
$lookup: {
|
|
121
|
+
from: formattedWidgetData[key].collectionName,
|
|
122
|
+
pipeline: aggregationQueryPiplelines,
|
|
123
|
+
as: formattedWidgetData[key].code,
|
|
124
|
+
},
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
return aggregationQuery;
|
|
129
|
+
}
|
|
130
|
+
function buildTabCollectionItemsQuery(formattedWidgetData) {
|
|
131
|
+
const aggregationQuery = [
|
|
132
|
+
{
|
|
133
|
+
$match: {
|
|
134
|
+
_id: {
|
|
135
|
+
$in: Object.values(formattedWidgetData).map((item) => item._id),
|
|
136
|
+
},
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
// Get only the fields that are not excluded
|
|
141
|
+
$project: {
|
|
142
|
+
_id: 1,
|
|
143
|
+
code: 1,
|
|
144
|
+
},
|
|
145
|
+
},
|
|
146
|
+
];
|
|
147
|
+
let collectionConfig;
|
|
148
|
+
Object.keys(formattedWidgetData).forEach((key) => {
|
|
149
|
+
if (formattedWidgetData[key].tabs &&
|
|
150
|
+
formattedWidgetData[key].tabs.length > 0) {
|
|
151
|
+
const aggregationQueryPiplelines = [];
|
|
152
|
+
collectionConfig = defaults_1.defaults.collections.find((c) => c.collectionName === formattedWidgetData[key].collectionName);
|
|
153
|
+
if (Array.isArray(collectionConfig === null || collectionConfig === void 0 ? void 0 : collectionConfig.aggregations) &&
|
|
154
|
+
(collectionConfig === null || collectionConfig === void 0 ? void 0 : collectionConfig.aggregations.length)) {
|
|
155
|
+
aggregationQueryPiplelines.push(...collectionConfig.aggregations);
|
|
156
|
+
}
|
|
157
|
+
// Build piplelines with config
|
|
158
|
+
aggregationQueryPiplelines.push(...[
|
|
159
|
+
{
|
|
160
|
+
$match: Object.assign({ _id: {
|
|
161
|
+
$in: formattedWidgetData[key].tabs.reduce((arr, tabItem) => {
|
|
162
|
+
arr.push(...(0, exports.formatCollectionItems)(tabItem.collectionItems));
|
|
163
|
+
return arr;
|
|
164
|
+
}, []),
|
|
165
|
+
} }, ((collectionConfig === null || collectionConfig === void 0 ? void 0 : collectionConfig.match) || {})),
|
|
166
|
+
},
|
|
167
|
+
{
|
|
168
|
+
$project: Object.assign({}, defaults_1.commonExcludedFields),
|
|
169
|
+
},
|
|
170
|
+
]);
|
|
171
|
+
// Build Aggregation Query
|
|
172
|
+
aggregationQuery.push({
|
|
173
|
+
$lookup: {
|
|
174
|
+
from: formattedWidgetData[key].collectionName,
|
|
175
|
+
pipeline: aggregationQueryPiplelines,
|
|
176
|
+
as: formattedWidgetData[key].code,
|
|
177
|
+
},
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
return aggregationQuery;
|
|
182
|
+
}
|
|
183
|
+
function buildSrcSetItem(uri, setItem) {
|
|
184
|
+
const imageItemArr = (uri === null || uri === void 0 ? void 0 : uri.split('/')) || [];
|
|
185
|
+
imageItemArr.splice(imageItemArr.length - 1, 0, `${setItem.width}x${setItem.height}`);
|
|
186
|
+
return imageItemArr.join('/');
|
|
187
|
+
}
|
|
188
|
+
exports.buildSrcSetItem = buildSrcSetItem;
|
|
189
|
+
function AddSrcSetsToItems(widgetData) {
|
|
190
|
+
if (Array.isArray(widgetData.items) && widgetData.items.length > 0) {
|
|
191
|
+
widgetData.items.forEach((item) => {
|
|
192
|
+
if (Array.isArray(item.srcset) && item.srcset.length > 0 && item.image) {
|
|
193
|
+
item.srcSets = item.srcset.reduce((strArr, setItem) => {
|
|
194
|
+
const imageUri = buildSrcSetItem(item.image.uri, setItem);
|
|
195
|
+
strArr.push(`${imageUri} ${setItem.screenSize}w`);
|
|
196
|
+
return strArr;
|
|
197
|
+
}, []);
|
|
198
|
+
item.srcSets = item.srcSets.join(', ');
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
exports.AddSrcSetsToItems = AddSrcSetsToItems;
|
|
204
|
+
const getCollectionModal = (collectionName, models) => {
|
|
205
|
+
let collectionModal = models[collectionName];
|
|
206
|
+
if (!collectionModal)
|
|
207
|
+
collectionModal = models[collectionName.charAt(0).toUpperCase() + collectionName.slice(1)];
|
|
208
|
+
if (!collectionModal) {
|
|
209
|
+
const schema = new mongoose_1.Schema({}, { strict: false });
|
|
210
|
+
collectionModal = (0, mongoose_1.model)(collectionName, schema, collectionName);
|
|
211
|
+
}
|
|
212
|
+
return collectionModal;
|
|
213
|
+
};
|
|
214
|
+
exports.getCollectionModal = getCollectionModal;
|
|
215
|
+
const formatCollectionItems = (collectionItems) => {
|
|
216
|
+
if (Array.isArray(collectionItems) && collectionItems.length === 0)
|
|
217
|
+
return [];
|
|
218
|
+
return collectionItems.map((item) => {
|
|
219
|
+
if (item instanceof mongoose_1.Types.ObjectId)
|
|
220
|
+
return item;
|
|
221
|
+
return new mongoose_1.Types.ObjectId(item);
|
|
222
|
+
});
|
|
223
|
+
};
|
|
224
|
+
exports.formatCollectionItems = formatCollectionItems;
|
|
225
225
|
//# sourceMappingURL=helper.js.map
|
package/src/utils/redis.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export declare const getRedisValue: (key: string) => Promise<any>;
|
|
2
|
-
export declare const setRedisValue: (key: string, value: JSON) => Promise<"OK" | null>;
|
|
3
|
-
export declare const deleteRedisValue: (key: string) => Promise<number | null>;
|
|
1
|
+
export declare const getRedisValue: (key: string) => Promise<any>;
|
|
2
|
+
export declare const setRedisValue: (key: string, value: JSON) => Promise<"OK" | null>;
|
|
3
|
+
export declare const deleteRedisValue: (key: string) => Promise<number | null>;
|