@webtypen/webframez-core 0.2.32 → 0.2.34

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.
@@ -16,6 +16,7 @@ export type DataBuilderSchema = {
16
16
  [key: string]: any;
17
17
  };
18
18
  newDataHandler?: Function;
19
+ canDelete: any;
19
20
  };
20
21
  export type DataBuilderType = {
21
22
  key: string;
@@ -48,7 +49,8 @@ export declare class DataBuilder {
48
49
  getFieldTypesFrontend(): any;
49
50
  getFieldsFrontend(fields: any, payload?: any): Promise<any>;
50
51
  getTypeFromRequest(req: any): DataBuilderType;
51
- validateFields(fields: any, data: any, errors?: any, path?: string): Promise<any>;
52
+ validateFields(db: any, type: any, fields: any, req: Request, errors?: any, path?: string): Promise<any>;
53
+ handleUnique(db: any, req: any, field: any, type: any): Promise<boolean>;
52
54
  typeForFrontend(type: any, req: any): Promise<any>;
53
55
  loadType(req: Request): Promise<{
54
56
  status: string;
@@ -73,16 +75,11 @@ export declare class DataBuilder {
73
75
  errors?: undefined;
74
76
  }>;
75
77
  delete(db: any, req: any): Promise<{
76
- status: string;
77
- errors: any;
78
- data?: undefined;
79
- } | {
80
78
  status: string;
81
79
  data: {
82
80
  _id: any;
83
81
  redirect: any;
84
82
  };
85
- errors?: undefined;
86
83
  }>;
87
84
  getField(req: Request, type: DataBuilderType, path: string): Promise<any>;
88
85
  removeArrayIndicators(str: string): string;
@@ -94,7 +94,7 @@ class DataBuilder {
94
94
  }
95
95
  return type;
96
96
  }
97
- validateFields(fields, data, errors, path) {
97
+ validateFields(db, type, fields, req, errors, path) {
98
98
  return __awaiter(this, void 0, void 0, function* () {
99
99
  if (!errors) {
100
100
  errors = {};
@@ -104,11 +104,12 @@ class DataBuilder {
104
104
  }
105
105
  for (let key in fields) {
106
106
  const fieldPath = (path ? path + "." : "") + key;
107
- const value = lodash_1.default.get(data, fieldPath);
107
+ const value = lodash_1.default.get(req.body.data, fieldPath);
108
+ // Check-Required
108
109
  if (fields[key].required) {
109
110
  let check = true;
110
111
  if (typeof fields[key].required === "function") {
111
- check = yield fields[key].required(data);
112
+ check = yield fields[key].required(req.body.data);
112
113
  }
113
114
  if (check) {
114
115
  if (value === null ||
@@ -121,11 +122,25 @@ class DataBuilder {
121
122
  }
122
123
  }
123
124
  }
125
+ // Check-Unique
126
+ if (fields[key].unique) {
127
+ let isUnique = false;
128
+ if (typeof fields[key].unique === "function") {
129
+ isUnique = yield fields[key].unique(req.body.data, req);
130
+ }
131
+ else if (typeof fields[key].unique === "object") {
132
+ isUnique = yield this.handleUnique(db, req, fields[key], type);
133
+ }
134
+ if (isUnique) {
135
+ errors[fieldPath] = "Es gibt bereits einen anderen Datensatz mit diesem Wert.";
136
+ continue;
137
+ }
138
+ }
124
139
  if (typeof fields[key].schema === "object") {
125
140
  if (value && value.length > 0) {
126
141
  for (let i in value) {
127
142
  const entryPath = fieldPath + "[" + i + "]";
128
- errors = yield this.validateFields(fields[key].schema, data, errors, entryPath);
143
+ errors = yield this.validateFields(db, type, fields[key].schema, req.body.data, errors, entryPath);
129
144
  }
130
145
  }
131
146
  }
@@ -136,6 +151,40 @@ class DataBuilder {
136
151
  return errors;
137
152
  });
138
153
  }
154
+ handleUnique(db, req, field, type) {
155
+ return __awaiter(this, void 0, void 0, function* () {
156
+ const match = typeof field.unique.match === "function"
157
+ ? yield field.unique.match(req)
158
+ : typeof field.unique.match === "object"
159
+ ? field.unique.match
160
+ : {};
161
+ const check = yield db
162
+ .collection(typeof field.unique.collection === "string" && field.unique.collection.trim() !== ""
163
+ ? field.unique.collection
164
+ : type.collection)
165
+ .aggregate([
166
+ { $match: match },
167
+ ...(typeof field.unique.aggregation === "function"
168
+ ? yield field.aggregation(req)
169
+ : field.aggregation && Array.isArray(field.aggregation)
170
+ ? field.aggregation
171
+ : []),
172
+ ])
173
+ .toArray();
174
+ if (!check || check.length < 1) {
175
+ return true;
176
+ }
177
+ if (!req.body.__builder_id || req.body.__builder_id === "new") {
178
+ return false;
179
+ }
180
+ for (let el of check) {
181
+ if (el && el._id && el._id.toString() !== req.body.__builder_id) {
182
+ return true;
183
+ }
184
+ }
185
+ return true;
186
+ });
187
+ }
139
188
  typeForFrontend(type, req) {
140
189
  return __awaiter(this, void 0, void 0, function* () {
141
190
  const newData = {};
@@ -277,7 +326,7 @@ class DataBuilder {
277
326
  throw new Error("Missing schema fields ...");
278
327
  }
279
328
  const schemaFields = typeof type.schema.fields === "function" ? yield type.schema.fields(req) : type.schema.fields;
280
- const errors = yield this.validateFields(schemaFields, req.body.data);
329
+ const errors = yield this.validateFields(db, type, schemaFields, req);
281
330
  if (errors && Object.keys(errors).length > 0) {
282
331
  return {
283
332
  status: "error",
@@ -387,13 +436,9 @@ class DataBuilder {
387
436
  if (!type || !type.schema || !type.schema.fields) {
388
437
  throw new Error("Missing schema fields ...");
389
438
  }
390
- const schemaFields = typeof type.schema.fields === "function" ? yield type.schema.fields(req) : type.schema.fields;
391
- const errors = yield this.validateFields(schemaFields, req.body.data);
392
- if (errors && Object.keys(errors).length > 0) {
393
- return {
394
- status: "error",
395
- errors: errors,
396
- };
439
+ const canDelete = typeof type.schema.canDelete === "function" ? yield type.schema.canDelete(req) : true;
440
+ if (!canDelete) {
441
+ throw new Error("Cannot delete entry ...");
397
442
  }
398
443
  const collection = type.schema && type.schema.collection ? type.schema.collection : undefined;
399
444
  if (!collection || collection.trim() === "") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webtypen/webframez-core",
3
- "version": "0.2.32",
3
+ "version": "0.2.34",
4
4
  "description": "The core package of the typescript based webframez (web-framework) by webtypen.de",
5
5
  "homepage": "https://webtypen.de/",
6
6
  "license": "MIT",
@@ -15,6 +15,7 @@ export type DataBuilderSchema = {
15
15
  events?: { [key: string]: any };
16
16
  fields: { [key: string]: any };
17
17
  newDataHandler?: Function;
18
+ canDelete: any;
18
19
  };
19
20
 
20
21
  export type DataBuilderType = {
@@ -132,7 +133,7 @@ export class DataBuilder {
132
133
  return type;
133
134
  }
134
135
 
135
- async validateFields(fields: any, data: any, errors?: any, path?: string) {
136
+ async validateFields(db: any, type: any, fields: any, req: Request, errors?: any, path?: string) {
136
137
  if (!errors) {
137
138
  errors = {};
138
139
  }
@@ -143,11 +144,13 @@ export class DataBuilder {
143
144
 
144
145
  for (let key in fields) {
145
146
  const fieldPath = (path ? path + "." : "") + key;
146
- const value = lodash.get(data, fieldPath);
147
+ const value = lodash.get(req.body.data, fieldPath);
148
+
149
+ // Check-Required
147
150
  if (fields[key].required) {
148
151
  let check = true;
149
152
  if (typeof fields[key].required === "function") {
150
- check = await fields[key].required(data);
153
+ check = await fields[key].required(req.body.data);
151
154
  }
152
155
 
153
156
  if (check) {
@@ -164,11 +167,26 @@ export class DataBuilder {
164
167
  }
165
168
  }
166
169
 
170
+ // Check-Unique
171
+ if (fields[key].unique) {
172
+ let isUnique = false;
173
+ if (typeof fields[key].unique === "function") {
174
+ isUnique = await fields[key].unique(req.body.data, req);
175
+ } else if (typeof fields[key].unique === "object") {
176
+ isUnique = await this.handleUnique(db, req, fields[key], type);
177
+ }
178
+
179
+ if (isUnique) {
180
+ errors[fieldPath] = "Es gibt bereits einen anderen Datensatz mit diesem Wert.";
181
+ continue;
182
+ }
183
+ }
184
+
167
185
  if (typeof fields[key].schema === "object") {
168
186
  if (value && value.length > 0) {
169
187
  for (let i in value) {
170
188
  const entryPath = fieldPath + "[" + i + "]";
171
- errors = await this.validateFields(fields[key].schema, data, errors, entryPath);
189
+ errors = await this.validateFields(db, type, fields[key].schema, req.body.data, errors, entryPath);
172
190
  }
173
191
  }
174
192
  }
@@ -181,6 +199,45 @@ export class DataBuilder {
181
199
  return errors;
182
200
  }
183
201
 
202
+ async handleUnique(db: any, req: any, field: any, type: any) {
203
+ const match: any =
204
+ typeof field.unique.match === "function"
205
+ ? await field.unique.match(req)
206
+ : typeof field.unique.match === "object"
207
+ ? field.unique.match
208
+ : {};
209
+ const check = await db
210
+ .collection(
211
+ typeof field.unique.collection === "string" && field.unique.collection.trim() !== ""
212
+ ? field.unique.collection
213
+ : type.collection
214
+ )
215
+ .aggregate([
216
+ { $match: match },
217
+ ...(typeof field.unique.aggregation === "function"
218
+ ? await field.aggregation(req)
219
+ : field.aggregation && Array.isArray(field.aggregation)
220
+ ? field.aggregation
221
+ : []),
222
+ ])
223
+ .toArray();
224
+
225
+ if (!check || check.length < 1) {
226
+ return true;
227
+ }
228
+
229
+ if (!req.body.__builder_id || req.body.__builder_id === "new") {
230
+ return false;
231
+ }
232
+
233
+ for (let el of check) {
234
+ if (el && el._id && el._id.toString() !== req.body.__builder_id) {
235
+ return true;
236
+ }
237
+ }
238
+ return true;
239
+ }
240
+
184
241
  async typeForFrontend(type: any, req: any) {
185
242
  const newData: any = {};
186
243
  for (let key in type) {
@@ -348,7 +405,7 @@ export class DataBuilder {
348
405
  }
349
406
 
350
407
  const schemaFields = typeof type.schema.fields === "function" ? await type.schema.fields(req) : type.schema.fields;
351
- const errors: any = await this.validateFields(schemaFields, req.body.data);
408
+ const errors: any = await this.validateFields(db, type, schemaFields, req);
352
409
  if (errors && Object.keys(errors).length > 0) {
353
410
  return {
354
411
  status: "error",
@@ -473,13 +530,9 @@ export class DataBuilder {
473
530
  throw new Error("Missing schema fields ...");
474
531
  }
475
532
 
476
- const schemaFields = typeof type.schema.fields === "function" ? await type.schema.fields(req) : type.schema.fields;
477
- const errors: any = await this.validateFields(schemaFields, req.body.data);
478
- if (errors && Object.keys(errors).length > 0) {
479
- return {
480
- status: "error",
481
- errors: errors,
482
- };
533
+ const canDelete = typeof type.schema.canDelete === "function" ? await type.schema.canDelete(req) : true;
534
+ if (!canDelete) {
535
+ throw new Error("Cannot delete entry ...");
483
536
  }
484
537
 
485
538
  const collection = type.schema && type.schema.collection ? type.schema.collection : undefined;