@knovator/pagecreator-node 1.2.0 → 1.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (128) hide show
  1. package/package.json +4 -2
  2. package/src/constants/index.d.ts +20 -0
  3. package/src/constants/index.js +34 -0
  4. package/src/constants/index.js.map +1 -0
  5. package/src/controllers/PageController.d.ts +4 -0
  6. package/src/controllers/PageController.js +84 -0
  7. package/src/controllers/PageController.js.map +1 -0
  8. package/src/controllers/TabController.d.ts +4 -0
  9. package/src/controllers/TabController.js +46 -0
  10. package/src/controllers/TabController.js.map +1 -0
  11. package/src/controllers/UserController.d.ts +2 -0
  12. package/src/controllers/UserController.js +65 -0
  13. package/src/controllers/UserController.js.map +1 -0
  14. package/src/controllers/WidgetController.d.ts +10 -0
  15. package/src/controllers/WidgetController.js +329 -0
  16. package/src/controllers/WidgetController.js.map +1 -0
  17. package/src/index.d.ts +8 -0
  18. package/src/index.js +48 -0
  19. package/src/index.js.map +1 -0
  20. package/src/models/Item.d.ts +30 -0
  21. package/src/models/Item.js +50 -0
  22. package/src/models/Item.js.map +1 -0
  23. package/src/models/Page.d.ts +30 -0
  24. package/src/models/Page.js +23 -0
  25. package/src/models/Page.js.map +1 -0
  26. package/src/models/SrcSet.d.ts +30 -0
  27. package/src/models/SrcSet.js +17 -0
  28. package/src/models/SrcSet.js.map +1 -0
  29. package/src/models/Tab.d.ts +30 -0
  30. package/src/models/Tab.js +28 -0
  31. package/src/models/Tab.js.map +1 -0
  32. package/src/models/Widget.d.ts +30 -0
  33. package/src/models/Widget.js +63 -0
  34. package/src/models/Widget.js.map +1 -0
  35. package/src/models/{index.ts → index.d.ts} +1 -13
  36. package/src/models/index.js +19 -0
  37. package/src/models/index.js.map +1 -0
  38. package/src/plugins/softDelete.d.ts +7 -0
  39. package/src/plugins/softDelete.js +64 -0
  40. package/src/plugins/softDelete.js.map +1 -0
  41. package/src/routes/PageRoute.d.ts +3 -0
  42. package/src/routes/PageRoute.js +30 -0
  43. package/src/routes/PageRoute.js.map +1 -0
  44. package/src/routes/UserRoute.d.ts +3 -0
  45. package/src/routes/UserRoute.js +23 -0
  46. package/src/routes/UserRoute.js.map +1 -0
  47. package/src/routes/WidgetRoute.d.ts +3 -0
  48. package/src/routes/WidgetRoute.js +68 -0
  49. package/src/routes/WidgetRoute.js.map +1 -0
  50. package/src/services/dataService.d.ts +9 -0
  51. package/src/services/dataService.js +403 -0
  52. package/src/services/dataService.js.map +1 -0
  53. package/src/services/dbService.d.ts +16 -0
  54. package/src/services/dbService.js +88 -0
  55. package/src/services/dbService.js.map +1 -0
  56. package/src/types/{IRequest.ts → IRequest.d.ts} +3 -4
  57. package/src/types/IRequest.js +3 -0
  58. package/src/types/IRequest.js.map +1 -0
  59. package/src/types/{IResponse.ts → IResponse.d.ts} +3 -4
  60. package/src/types/IResponse.js +3 -0
  61. package/src/types/IResponse.js.map +1 -0
  62. package/src/types/{Router.ts → Router.d.ts} +1 -2
  63. package/src/types/Router.js +3 -0
  64. package/src/types/Router.js.map +1 -0
  65. package/src/types/common.d.ts +142 -0
  66. package/src/types/common.js +3 -0
  67. package/src/types/common.js.map +1 -0
  68. package/src/types/enums.d.ts +14 -0
  69. package/src/types/enums.js +21 -0
  70. package/src/types/enums.js.map +1 -0
  71. package/src/types/index.js +9 -0
  72. package/src/types/index.js.map +1 -0
  73. package/src/utils/defaults.d.ts +7 -0
  74. package/src/utils/defaults.js +41 -0
  75. package/src/utils/defaults.js.map +1 -0
  76. package/src/utils/helper.d.ts +7 -0
  77. package/src/utils/helper.js +225 -0
  78. package/src/utils/helper.js.map +1 -0
  79. package/src/utils/redis.d.ts +3 -0
  80. package/src/utils/redis.js +59 -0
  81. package/src/utils/redis.js.map +1 -0
  82. package/src/utils/responseHandlers.d.ts +6 -0
  83. package/src/utils/responseHandlers.js +57 -0
  84. package/src/utils/responseHandlers.js.map +1 -0
  85. package/src/utils/validate.d.ts +4 -0
  86. package/src/utils/validate.js +24 -0
  87. package/src/utils/validate.js.map +1 -0
  88. package/src/utils/validations/page.d.ts +7 -0
  89. package/src/utils/validations/page.js +51 -0
  90. package/src/utils/validations/page.js.map +1 -0
  91. package/src/utils/validations/tab.d.ts +6 -0
  92. package/src/utils/validations/tab.js +26 -0
  93. package/src/utils/validations/tab.js.map +1 -0
  94. package/src/utils/validations/user.d.ts +3 -0
  95. package/src/utils/validations/user.js +13 -0
  96. package/src/utils/validations/user.js.map +1 -0
  97. package/src/utils/validations/widget.d.ts +9 -0
  98. package/src/utils/validations/widget.js +153 -0
  99. package/src/utils/validations/widget.js.map +1 -0
  100. package/src/constants/index.ts +0 -30
  101. package/src/controllers/PageController.ts +0 -88
  102. package/src/controllers/TabController.ts +0 -47
  103. package/src/controllers/UserController.ts +0 -69
  104. package/src/controllers/WidgetController.ts +0 -420
  105. package/src/index.ts +0 -59
  106. package/src/models/Item.ts +0 -53
  107. package/src/models/Page.ts +0 -22
  108. package/src/models/SrcSet.ts +0 -16
  109. package/src/models/Tab.ts +0 -28
  110. package/src/models/Widget.ts +0 -65
  111. package/src/plugins/softDelete.ts +0 -84
  112. package/src/routes/PageRoute.ts +0 -30
  113. package/src/routes/UserRoute.ts +0 -32
  114. package/src/routes/WidgetRoute.ts +0 -77
  115. package/src/services/dataService.ts +0 -475
  116. package/src/services/dbService.ts +0 -102
  117. package/src/types/common.ts +0 -172
  118. package/src/types/enums.ts +0 -16
  119. package/src/utils/defaults.ts +0 -41
  120. package/src/utils/helper.ts +0 -285
  121. package/src/utils/redis.ts +0 -57
  122. package/src/utils/responseHandlers.ts +0 -60
  123. package/src/utils/validate.ts +0 -22
  124. package/src/utils/validations/page.ts +0 -52
  125. package/src/utils/validations/tab.ts +0 -26
  126. package/src/utils/validations/user.ts +0 -10
  127. package/src/utils/validations/widget.ts +0 -169
  128. /package/src/types/{index.ts → index.d.ts} +0 -0
@@ -1,102 +0,0 @@
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<T | 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<T | undefined> {
34
- const modalInstance = await getOne(Modal, query);
35
- await modalInstance.remove();
36
- return modalInstance;
37
- }
38
- // delete-all
39
- export async function deleteAll<T extends EntityType>(Modal: Model<T>, query: FilterQuery<T>) {
40
- return Modal.deleteMany(query);
41
- }
42
- // get-all
43
- export function getAll<T extends EntityType>(
44
- Modal: Model<T>,
45
- query: FilterQuery<EntityType> = {},
46
- options?: QueryOptions<EntityType>,
47
- projection?: ProjectionType<EntityType>
48
- // eslint-disable-next-line @typescript-eslint/ban-types
49
- ): QueryWithHelpers<Array<T>, T, {}, T> {
50
- return Modal.find(query, projection, options);
51
- }
52
- // list
53
- export async function list<T extends EntityType>(
54
- Modal: IModel<T>,
55
- where: FilterQuery<T>,
56
- options: QueryOptions<T>
57
- ): Promise<ReturnDocument[]> {
58
- try {
59
- const documents = Modal.paginate(where, options);
60
- return documents;
61
- } catch (error) {
62
- throw new Error((error as Error).message);
63
- }
64
- }
65
- // get-one
66
- export async function getOne<T extends EntityType>(
67
- Modal: Model<T>,
68
- query: FilterQuery<EntityType>,
69
- projection?: ProjectionType<EntityType>
70
- ): Promise<HydratedDocument<T>> {
71
- const modalInstance: HydratedDocument<T> | null = await Modal.findOne(
72
- query,
73
- projection
74
- );
75
- if (!modalInstance)
76
- throw new Error(`Record not found ${Modal.name ? `in ${Modal.name}` : ''}`);
77
-
78
- return modalInstance;
79
- }
80
- // bulk-insert
81
- export async function bulkInsert<T extends EntityType>(Modal: Model<T>, docs: T[]): Promise<ReturnDocument[]> {
82
- return await Modal.insertMany(docs);
83
- }
84
-
85
- export async function checkUnique<T extends EntityType>({
86
- Modal,
87
- uniqueField,
88
- errorMessage,
89
- value
90
- }: {
91
- Modal: Model<T>,
92
- uniqueField: keyof T,
93
- value: any,
94
- errorMessage: string
95
- }): Promise<void> {
96
- const query: FilterQuery<T> = { [uniqueField]: value } as FilterQuery<T>;
97
- let result;
98
- try {
99
- result = await getOne(Modal, query);
100
- } catch (error) {}
101
- if(result) throw new Error(errorMessage)
102
- }
@@ -1,172 +0,0 @@
1
- import {
2
- Document,
3
- Model,
4
- Models,
5
- Types,
6
- FilterQuery,
7
- QueryOptions,
8
- HydratedDocument,
9
- } from 'mongoose';
10
- import { ItemsType, WidgetTypes, ItemTypes } from '.';
11
-
12
- export type TypesType = { value: string; label: string };
13
-
14
- export type LanguageSchemaFieldType = { [key: string]: string };
15
-
16
- export interface iConfig {
17
- logger: any;
18
- catchAsync: (
19
- fn: any,
20
- modal?: string
21
- ) => (req: any, res: any, next: any) => void;
22
- collections: CollectionItem[];
23
- }
24
- // Collection
25
- export interface IPageSchema extends Document {
26
- name: string;
27
- code: string;
28
- slug: string;
29
- canDel: boolean;
30
- widgets: string[];
31
- }
32
- export interface ITabSchema extends Document {
33
- name: string;
34
- names: LanguageSchemaFieldType;
35
- widgetId: typeof Types.ObjectId;
36
- collectionItems: string[];
37
- }
38
- export interface ISrcSetSchema extends Document {
39
- width: number;
40
- height: number;
41
- screenSize: number;
42
- itemId: typeof Types.ObjectId;
43
- }
44
- export interface IWidgetSchema extends Document {
45
- items: any;
46
- name: string;
47
- code: string;
48
- autoPlay: boolean;
49
- isActive: boolean;
50
- widgetTitle: string;
51
- widgetTitles: LanguageSchemaFieldType;
52
- webPerRow: number;
53
- mobilePerRow: number;
54
- tabletPerRow: number;
55
- itemsType: ItemsType;
56
- widgetType: WidgetTypes;
57
- collectionName: string;
58
- collectionItems: string[];
59
- tabs: {
60
- name: string;
61
- names?: LanguageSchemaFieldType;
62
- collectionItems: string[];
63
- }[];
64
- backgroundColor: string;
65
- textContent: string;
66
- htmlContent: string;
67
- canDel: boolean;
68
- }
69
- export interface IItemSchema extends Document {
70
- widgetId: typeof Types.ObjectId;
71
- title: string;
72
- titles: LanguageSchemaFieldType;
73
- subtitle: string;
74
- subtitles: LanguageSchemaFieldType;
75
- altText: string;
76
- altTexts: LanguageSchemaFieldType;
77
- link: string;
78
- sequence: number;
79
- itemType: ItemTypes;
80
- img: any;
81
- imgs: any;
82
- srcset?: SrcSetItem[];
83
- }
84
- export interface SrcSetItem {
85
- screenSize: number;
86
- width: number;
87
- height: number;
88
- }
89
- // \ End of Collection
90
-
91
- export type CollectionItem = {
92
- title: string;
93
- collectionName: string;
94
- filters?: { [key: string]: string | number | boolean };
95
- searchColumns?: string[];
96
- match?: ObjectType;
97
- aggregations?: any[];
98
- searchLimit?: number;
99
- };
100
-
101
- export type RedisConfig = {
102
- HOST: string;
103
- PORT: number;
104
- PASSWORD?: string;
105
- USER?: string;
106
- DB?: number;
107
- };
108
-
109
- export interface IConfig {
110
- logger: any;
111
- catchAsync: (
112
- fn: any,
113
- modal?: string
114
- ) => (req: any, res: any, next: any) => void;
115
- getModals: (req: Express.Request) => Models;
116
- collections: CollectionItem[];
117
- customWidgetTypes: {
118
- label: string;
119
- value: string;
120
- imageOnly?: boolean;
121
- collectionsOnly?: boolean;
122
- }[];
123
- redis?: string | RedisConfig;
124
- languages?: LanguageType[];
125
- }
126
-
127
- export type EntityType =
128
- | IWidgetSchema
129
- | IItemSchema
130
- | IPageSchema
131
- | ISrcSetSchema
132
- | ITabSchema;
133
- export type ReturnDocument = EntityType;
134
- export interface IModel<T> extends Model<T> {
135
- paginate: (
136
- query: FilterQuery<T>,
137
- options?: QueryOptions
138
- ) => Promise<HydratedDocument<ReturnDocument>[]>;
139
- }
140
-
141
- export type ObjectType = {
142
- [key: string]:
143
- | string
144
- | number
145
- | boolean
146
- | null
147
- | undefined
148
- | string[]
149
- | number[]
150
- | ObjectType
151
- | ObjectType[]
152
- | any;
153
- };
154
-
155
- export interface IDefaultValidations {
156
- createdBy: string;
157
- updatedBy: string;
158
- deletedBy: string;
159
- deletedAt: string;
160
- }
161
-
162
- // helper.ts
163
- export interface IWidgetDataSchema {
164
- _id: string;
165
- code: string;
166
- collectionName: string;
167
- collectionItems: string[];
168
- tabs: { name: string; collectionItems: string[] }[];
169
- }
170
- export type IWidgetData = { [key: string]: IWidgetDataSchema };
171
-
172
- export type LanguageType = { code: string; name: string };
@@ -1,16 +0,0 @@
1
- export enum ItemsType {
2
- Image = 'Image',
3
- }
4
-
5
- export enum WidgetTypes {
6
- FixedCard = 'FixedCard',
7
- Carousel = 'Carousel',
8
- Tabs = 'Tabs',
9
- Text = 'Text',
10
- HTML = 'HTML'
11
- }
12
-
13
- export enum ItemTypes {
14
- Web = 'Web',
15
- Mobile = 'Mobile',
16
- }
@@ -1,41 +0,0 @@
1
- import mongoose from 'mongoose';
2
- import { NextFunction } from 'express';
3
- import { CollectionItem, IConfig, IRequest, IResponse } from '../types';
4
- import { RESPONSE_CODES, internalServerError, validationError , REGEXS } from '../constants';
5
-
6
- export const defaults: IConfig = {
7
- logger: console as any,
8
- getModals: () => mongoose.models,
9
- catchAsync:
10
- (fn: any, modal = '') =>
11
- (req: IRequest, res: IResponse, next: NextFunction) => {
12
- Promise.resolve(fn(req, res, next)).catch((err) => {
13
- let message = (err as Error).message;
14
- if (message.match(REGEXS.OBJECTID_CAST_FAILED)) {
15
- message = `${modal} not found with given id!`;
16
- } else if(message.match(REGEXS.IS_AVAILABLE)) {
17
- return res.status(validationError).json({
18
- code: RESPONSE_CODES.VALIDATION_FAILED,
19
- message,
20
- data: undefined,
21
- });
22
- }
23
- // this.logger.error(err.message);
24
- return res.status(internalServerError).json({
25
- code: RESPONSE_CODES.ERROR,
26
- message,
27
- data: undefined,
28
- });
29
- });
30
- },
31
- collections: [] as CollectionItem[],
32
- customWidgetTypes: [],
33
- redis: undefined,
34
- languages: [],
35
- };
36
-
37
- export const commonExcludedFields = {
38
- __v: 0,
39
- isDeleted: 0,
40
- deletedAt: 0,
41
- };
@@ -1,285 +0,0 @@
1
- import { model, Models, Schema, Types } from 'mongoose';
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[], models: Models) {
11
- const { Widget }= models;
12
- // reduce widget data to optimize query
13
- const newData: IWidgetData = widgetData.reduce(
14
- (acc: IWidgetData, widget: IWidgetSchema) => {
15
- if (widget.collectionName) {
16
- acc[widget.code] = {
17
- _id: widget._id,
18
- code: widget.code,
19
- collectionName: widget.collectionName,
20
- collectionItems: widget.collectionItems,
21
- tabs: widget.tabs,
22
- };
23
- }
24
- return acc;
25
- },
26
- {}
27
- );
28
- if (Object.keys(newData).length > 0) {
29
- const aggregationQueryCollectionItems = buildCollectionItemsQuery(newData);
30
- if (aggregationQueryCollectionItems.length > 0) {
31
- // getting collection data by populating widget
32
- let aggregationData: any = await Widget.aggregate(
33
- aggregationQueryCollectionItems
34
- );
35
- aggregationData = aggregationData.reduce((acc: any, aggregation: any) => {
36
- acc[aggregation.code] = aggregation[aggregation.code];
37
- return acc;
38
- }, {});
39
- // adding collection data to widgets
40
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
41
- // @ts-ignore
42
- widgetData = widgetData.map((widget: IWidgetSchema) => {
43
- if (aggregationData[widget.code]) {
44
- return {
45
- ...widget,
46
- collectionItems: aggregationData[widget.code],
47
- };
48
- }
49
- return widget;
50
- });
51
- }
52
- const aggregationQueryTabs = buildTabCollectionItemsQuery(newData);
53
- if (aggregationQueryTabs.length > 0) {
54
- let aggregationDataTabs: any = await Widget.aggregate(
55
- aggregationQueryTabs
56
- );
57
- aggregationDataTabs = aggregationDataTabs.reduce(
58
- (acc: any, aggregation: any) => {
59
- if (aggregation[aggregation.code])
60
- acc[aggregation.code] = aggregation[aggregation.code];
61
- return acc;
62
- },
63
- {}
64
- );
65
- // adding collection data to widgets
66
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
67
- // @ts-ignore
68
- widgetData = widgetData.map((widget) => {
69
- if (!aggregationDataTabs[widget.code]) return widget;
70
- const collectionItemsObj = aggregationDataTabs[widget.code].reduce(
71
- (acc: any, item: any) => {
72
- acc[item._id] = item;
73
- return acc;
74
- },
75
- {}
76
- );
77
- return {
78
- ...widget,
79
- tabs: widget.tabs.map((tabItem) => {
80
- return {
81
- name: tabItem.name,
82
- names: tabItem.names,
83
- collectionItems: tabItem.collectionItems
84
- .map((collectionId) => collectionItemsObj[collectionId])
85
- .filter(Boolean),
86
- };
87
- }),
88
- };
89
- });
90
- }
91
- }
92
- // returning widget data as it is if they do not have dynamic collection
93
- return widgetData;
94
- }
95
-
96
- function buildCollectionItemsQuery(formattedWidgetData: IWidgetData) {
97
- const aggregationQuery: any = [
98
- {
99
- $match: {
100
- _id: {
101
- $in: Object.values(formattedWidgetData).map(
102
- (item: IWidgetDataSchema) => item._id
103
- ),
104
- },
105
- },
106
- },
107
- {
108
- // Get only the fields that are not excluded
109
- $project: {
110
- _id: 1,
111
- code: 1,
112
- },
113
- },
114
- ];
115
- let collectionConfig;
116
- Object.keys(formattedWidgetData).forEach((key: string) => {
117
- if (
118
- formattedWidgetData[key].collectionItems &&
119
- formattedWidgetData[key].collectionItems.length > 0
120
- ) {
121
- const aggregationQueryPiplelines: any[] = [];
122
- collectionConfig = defaults.collections.find(
123
- (c) => c.collectionName === formattedWidgetData[key].collectionName
124
- );
125
- if (
126
- Array.isArray(collectionConfig?.aggregations) &&
127
- collectionConfig?.aggregations.length
128
- ) {
129
- aggregationQueryPiplelines.push(...collectionConfig.aggregations);
130
- }
131
- const ids = formatCollectionItems(
132
- formattedWidgetData[key].collectionItems
133
- );
134
- // Build piplelines with config
135
- aggregationQueryPiplelines.push(
136
- ...[
137
- {
138
- $match: {
139
- _id: {
140
- $in: ids,
141
- },
142
- ...(collectionConfig?.match || {}),
143
- },
144
- },
145
- {
146
- $project: {
147
- ...commonExcludedFields,
148
- },
149
- },
150
- { $addFields: { __order: { $indexOfArray: [ids, '$_id'] } } },
151
- { $sort: { __order: 1 } },
152
- ]
153
- );
154
- // Build Aggregation Query
155
- aggregationQuery.push({
156
- $lookup: {
157
- from: formattedWidgetData[key].collectionName,
158
- pipeline: aggregationQueryPiplelines,
159
- as: formattedWidgetData[key].code,
160
- },
161
- });
162
- }
163
- });
164
- return aggregationQuery;
165
- }
166
-
167
- function buildTabCollectionItemsQuery(formattedWidgetData: IWidgetData) {
168
- const aggregationQuery: any = [
169
- {
170
- $match: {
171
- _id: {
172
- $in: Object.values(formattedWidgetData).map(
173
- (item: IWidgetDataSchema) => item._id
174
- ),
175
- },
176
- },
177
- },
178
- {
179
- // Get only the fields that are not excluded
180
- $project: {
181
- _id: 1,
182
- code: 1,
183
- },
184
- },
185
- ];
186
- let collectionConfig;
187
- Object.keys(formattedWidgetData).forEach((key: string) => {
188
- if (
189
- formattedWidgetData[key].tabs &&
190
- formattedWidgetData[key].tabs.length > 0
191
- ) {
192
- const aggregationQueryPiplelines: any[] = [];
193
- collectionConfig = defaults.collections.find(
194
- (c) => c.collectionName === formattedWidgetData[key].collectionName
195
- );
196
- if (
197
- Array.isArray(collectionConfig?.aggregations) &&
198
- collectionConfig?.aggregations.length
199
- ) {
200
- aggregationQueryPiplelines.push(...collectionConfig.aggregations);
201
- }
202
- // Build piplelines with config
203
- aggregationQueryPiplelines.push(
204
- ...[
205
- {
206
- $match: {
207
- _id: {
208
- $in: formattedWidgetData[key].tabs.reduce(
209
- (arr: Types.ObjectId[], tabItem) => {
210
- arr.push(...formatCollectionItems(tabItem.collectionItems));
211
- return arr;
212
- },
213
- []
214
- ),
215
- },
216
- ...(collectionConfig?.match || {}),
217
- },
218
- },
219
- {
220
- $project: {
221
- ...commonExcludedFields,
222
- },
223
- },
224
- ]
225
- );
226
- // Build Aggregation Query
227
- aggregationQuery.push({
228
- $lookup: {
229
- from: formattedWidgetData[key].collectionName,
230
- pipeline: aggregationQueryPiplelines,
231
- as: formattedWidgetData[key].code,
232
- },
233
- });
234
- }
235
- });
236
- return aggregationQuery;
237
- }
238
-
239
- export function buildSrcSetItem(uri: string, setItem: SrcSetItem) {
240
- const imageItemArr = uri?.split('/') || [];
241
- imageItemArr.splice(
242
- imageItemArr.length - 1,
243
- 0,
244
- `${setItem.width}x${setItem.height}`
245
- );
246
- return imageItemArr.join('/');
247
- }
248
-
249
- export function AddSrcSetsToItems(widgetData: IWidgetSchema) {
250
- if (Array.isArray(widgetData.items) && widgetData.items.length > 0) {
251
- widgetData.items.forEach((item) => {
252
- if (Array.isArray(item.srcset) && item.srcset.length > 0 && item.image) {
253
- item.srcSets = item.srcset.reduce(
254
- (strArr: string[], setItem: SrcSetItem) => {
255
- const imageUri = buildSrcSetItem(item.image.uri, setItem);
256
- strArr.push(`${imageUri} ${setItem.screenSize}w`);
257
- return strArr;
258
- },
259
- []
260
- );
261
-
262
- item.srcSets = item.srcSets.join(', ');
263
- }
264
- });
265
- }
266
- }
267
-
268
- export const getCollectionModal = (collectionName: string, models: Models) => {
269
- let collectionModal: any = models[collectionName];
270
- if (!collectionModal) collectionModal = models[collectionName.charAt(0).toUpperCase() + collectionName.slice(1)]
271
- if (!collectionModal) {
272
- const schema = new Schema({}, { strict: false });
273
- collectionModal = model(collectionName, schema, collectionName);
274
- }
275
- return collectionModal;
276
- };
277
-
278
- export const formatCollectionItems = (collectionItems: any[]) => {
279
- if (Array.isArray(collectionItems) && collectionItems.length === 0) return [];
280
-
281
- return collectionItems.map((item) => {
282
- if (item instanceof Types.ObjectId) return item;
283
- return new Types.ObjectId(item);
284
- });
285
- };
@@ -1,57 +0,0 @@
1
- import lz from 'lzutf8';
2
- import ioredis from 'ioredis';
3
- import { defaults } from './defaults';
4
-
5
- let redis: ioredis | null | undefined = null;
6
-
7
- const initRedis = () => {
8
- if (defaults.redis && typeof defaults.redis === 'string') {
9
- redis = new ioredis(defaults.redis);
10
- } else if (defaults.redis && typeof defaults.redis === 'object') {
11
- redis = new ioredis(
12
- defaults.redis.PORT as number,
13
- defaults.redis.HOST as string,
14
- {
15
- password: defaults.redis.PASSWORD,
16
- username: defaults.redis.USER,
17
- db: defaults.redis.DB,
18
- }
19
- );
20
- } else redis = undefined;
21
- };
22
-
23
- export const getRedisValue = async (key: string) => {
24
- if (redis === null) {
25
- initRedis();
26
- }
27
- if (!redis) {
28
- return null;
29
- }
30
- const value = await redis.get(key);
31
- if (!value) {
32
- return null;
33
- }
34
- const str = lz.decompress(lz.decodeBase64(value));
35
- return JSON.parse(str);
36
- };
37
-
38
- export const setRedisValue = async (key: string, value: JSON) => {
39
- if (redis === null) {
40
- initRedis();
41
- }
42
- if (!redis) {
43
- return null;
44
- }
45
- const compressed = lz.encodeBase64(lz.compress(JSON.stringify(value)));
46
- return await redis.set(key, compressed);
47
- };
48
-
49
- export const deleteRedisValue = async (key: string) => {
50
- if (redis === null) {
51
- initRedis();
52
- }
53
- if (!redis) {
54
- return null;
55
- }
56
- return await redis.del(key);
57
- };