@webtypen/webframez-core 0.3.63 → 0.3.65

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.
@@ -1,4 +1,14 @@
1
1
  import { Request } from "../Router/Request";
2
+ export interface DatatableModelSelectorConfig {
3
+ value?: string;
4
+ label?: string;
5
+ subtitle?: string;
6
+ }
7
+ export interface NormalizedDatatableModelSelectorConfig {
8
+ value: string;
9
+ label: string;
10
+ subtitle?: string;
11
+ }
2
12
  export declare class Datatable {
3
13
  collection: string | Function;
4
14
  aggregation: {
@@ -26,6 +36,7 @@ export declare class Datatable {
26
36
  defaultUnwind?: string | null;
27
37
  disablePerPageConfig: boolean;
28
38
  selectable: boolean;
39
+ modelSelector: boolean | DatatableModelSelectorConfig;
29
40
  selectableFunctions: {
30
41
  [key: string]: {
31
42
  label: string;
@@ -38,6 +49,8 @@ export declare class Datatable {
38
49
  };
39
50
  } | null;
40
51
  getInit(req: Request): Promise<any>;
52
+ getModelSelectorConfig(): NormalizedDatatableModelSelectorConfig | null;
53
+ getModelSelectorPreview(req: Request): Promise<any>;
41
54
  getSelectableFunctionsDef(req: Request): Promise<{
42
55
  label: string;
43
56
  icon: string | undefined;
@@ -8,12 +8,24 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
+ var __rest = (this && this.__rest) || function (s, e) {
12
+ var t = {};
13
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
14
+ t[p] = s[p];
15
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
16
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
17
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
18
+ t[p[i]] = s[p[i]];
19
+ }
20
+ return t;
21
+ };
11
22
  var __importDefault = (this && this.__importDefault) || function (mod) {
12
23
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
24
  };
14
25
  Object.defineProperty(exports, "__esModule", { value: true });
15
26
  exports.Datatable = void 0;
16
27
  const moment_1 = __importDefault(require("moment"));
28
+ const mongodb_1 = require("mongodb");
17
29
  const DBConnection_1 = require("../Database/DBConnection");
18
30
  const NumericFunctions_1 = require("../Functions/NumericFunctions");
19
31
  class Datatable {
@@ -32,6 +44,7 @@ class Datatable {
32
44
  this.defaultUnwind = null;
33
45
  this.disablePerPageConfig = false;
34
46
  this.selectable = false;
47
+ this.modelSelector = false;
35
48
  this.selectableFunctions = null;
36
49
  }
37
50
  getInit(req) {
@@ -41,6 +54,10 @@ class Datatable {
41
54
  selectable: this.selectable ? true : false,
42
55
  selectableFunctions: yield this.getSelectableFunctionsDef(req),
43
56
  };
57
+ const modelSelector = this.getModelSelectorConfig();
58
+ if (modelSelector) {
59
+ out.modelSelector = modelSelector;
60
+ }
44
61
  const promises = [];
45
62
  promises.push(new Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
46
63
  out.filter = yield this.getFilter(req);
@@ -78,6 +95,73 @@ class Datatable {
78
95
  return out;
79
96
  });
80
97
  }
98
+ getModelSelectorConfig() {
99
+ if (!this.modelSelector) {
100
+ return null;
101
+ }
102
+ const config = this.modelSelector === true ? {} : this.modelSelector;
103
+ const subtitle = typeof config.subtitle === "string" && config.subtitle.trim() !== "" ? config.subtitle : undefined;
104
+ return Object.assign({ value: typeof config.value === "string" && config.value.trim() !== "" ? config.value.trim() : "_id", label: typeof config.label === "string" && config.label.trim() !== "" ? config.label.trim() : "_id" }, (subtitle ? { subtitle: subtitle.trim() } : {}));
105
+ }
106
+ getModelSelectorPreview(req) {
107
+ return __awaiter(this, void 0, void 0, function* () {
108
+ const config = this.getModelSelectorConfig();
109
+ if (!config) {
110
+ throw new Error("Datatable is not enabled for model selection.");
111
+ }
112
+ const rawValue = req.body.value;
113
+ if (rawValue === undefined || rawValue === null || rawValue === "") {
114
+ return null;
115
+ }
116
+ let value = rawValue;
117
+ if (config.value === "_id") {
118
+ if (rawValue instanceof mongodb_1.ObjectId) {
119
+ value = rawValue;
120
+ }
121
+ else if (typeof rawValue === "string" && mongodb_1.ObjectId.isValid(rawValue)) {
122
+ value = new mongodb_1.ObjectId(rawValue);
123
+ }
124
+ else {
125
+ return null;
126
+ }
127
+ }
128
+ const normalizeStages = (stages) => (stages !== null && stages !== void 0 ? stages : []).map((stage) => {
129
+ if (!stage || typeof stage !== "object" || !Object.prototype.hasOwnProperty.call(stage, "skipStats")) {
130
+ return stage;
131
+ }
132
+ const { skipStats: _skipStats } = stage, normalized = __rest(stage, ["skipStats"]);
133
+ return normalized;
134
+ });
135
+ const aggregation = [
136
+ ...normalizeStages(yield this.getAggregation(req)),
137
+ ...normalizeStages(yield this.getSubAggregation(req)),
138
+ { $match: { [config.value]: value } },
139
+ { $limit: 1 },
140
+ ];
141
+ const connection = yield DBConnection_1.DBConnection.getConnection();
142
+ const results = yield connection.client
143
+ .db(null)
144
+ .collection(yield this.getCollection(req))
145
+ .aggregate(aggregation, {
146
+ collation: {
147
+ locale: "de",
148
+ strength: 2,
149
+ },
150
+ })
151
+ .toArray();
152
+ if (!results[0]) {
153
+ return null;
154
+ }
155
+ let result = results[0];
156
+ if (this.onRow) {
157
+ result = yield this.onRow(result);
158
+ }
159
+ if (this.onAttributes) {
160
+ result.__cellattr = yield this.onAttributes(result);
161
+ }
162
+ return result;
163
+ });
164
+ }
81
165
  getSelectableFunctionsDef(req) {
82
166
  return __awaiter(this, void 0, void 0, function* () {
83
167
  if (!this.selectableFunctions) {
@@ -43,6 +43,18 @@ class DatatableController extends Controller_1.Controller {
43
43
  });
44
44
  }
45
45
  const table = new tableClass();
46
+ if (req.body.mode === "model-selector-preview") {
47
+ if (!table.getModelSelectorConfig()) {
48
+ return res.status(403).send({
49
+ status: "error",
50
+ message: "Datatable is not enabled for model selection.",
51
+ });
52
+ }
53
+ return res.send({
54
+ status: "success",
55
+ data: yield table.getModelSelectorPreview(req),
56
+ });
57
+ }
46
58
  if (req.body.type === "stats" || req.body.mode === "stats") {
47
59
  return res.send({
48
60
  status: "success",
@@ -123,6 +123,8 @@ declare class NotificationServiceFacade {
123
123
  set registy(registry: {
124
124
  [key: string]: NotificationDefinition;
125
125
  });
126
+ private getObjectIdString;
127
+ private normalizeObjectId;
126
128
  private getNotificationReferenceId;
127
129
  init(): void;
128
130
  registerType(type: NotificationDefinition): this;
@@ -56,16 +56,23 @@ class NotificationServiceFacade {
56
56
  set registy(registry) {
57
57
  this.registry = registry;
58
58
  }
59
- getNotificationReferenceId(notificationRef) {
60
- const notificationId = typeof notificationRef === "string"
61
- ? notificationRef
62
- : notificationRef && typeof notificationRef.toString === "function"
63
- ? notificationRef.toString()
59
+ getObjectIdString(value) {
60
+ const objectIdString = typeof value === "string"
61
+ ? value
62
+ : value && typeof value.toString === "function"
63
+ ? value.toString()
64
64
  : null;
65
- return notificationId && notificationId.length === 24 && mongodb_1.ObjectId.isValid(notificationId)
66
- ? notificationId
65
+ return objectIdString && objectIdString.length === 24 && mongodb_1.ObjectId.isValid(objectIdString)
66
+ ? objectIdString
67
67
  : null;
68
68
  }
69
+ normalizeObjectId(value) {
70
+ const objectIdString = this.getObjectIdString(value);
71
+ return objectIdString ? new mongodb_1.ObjectId(objectIdString) : null;
72
+ }
73
+ getNotificationReferenceId(notificationRef) {
74
+ return this.getObjectIdString(notificationRef);
75
+ }
69
76
  init() {
70
77
  this.registry = {};
71
78
  this.notificationsConfig = Config_1.Config.get("notifications") || {};
@@ -168,14 +175,11 @@ class NotificationServiceFacade {
168
175
  return this.targetRegistry[target] || null;
169
176
  }
170
177
  normalizeTargetId(targetId) {
171
- var _a;
172
- if (targetId instanceof mongodb_1.ObjectId)
173
- return targetId;
174
- const value = typeof targetId === "string" ? targetId : (_a = targetId === null || targetId === void 0 ? void 0 : targetId.toString) === null || _a === void 0 ? void 0 : _a.call(targetId);
175
- if (typeof value !== "string" || value.length !== 24 || !mongodb_1.ObjectId.isValid(value)) {
178
+ const normalized = this.normalizeObjectId(targetId);
179
+ if (!normalized) {
176
180
  throw new NotificationTargetValidationError("Invalid notification target_id.");
177
181
  }
178
- return new mongodb_1.ObjectId(value);
182
+ return normalized;
179
183
  }
180
184
  normalizeTargetContext(context) {
181
185
  if (!context || typeof context.target !== "string" || context.target.trim() === "") {
@@ -243,15 +247,8 @@ class NotificationServiceFacade {
243
247
  return notification;
244
248
  }
245
249
  getModelId(model) {
246
- var _a, _b;
247
- const value = (_a = model === null || model === void 0 ? void 0 : model._id) !== null && _a !== void 0 ? _a : model === null || model === void 0 ? void 0 : model.id;
248
- if (value instanceof mongodb_1.ObjectId)
249
- return value;
250
- const stringValue = typeof value === "string" ? value : (_b = value === null || value === void 0 ? void 0 : value.toString) === null || _b === void 0 ? void 0 : _b.call(value);
251
- if (typeof stringValue === "string" && stringValue.length === 24 && mongodb_1.ObjectId.isValid(stringValue)) {
252
- return new mongodb_1.ObjectId(stringValue);
253
- }
254
- return null;
250
+ var _a;
251
+ return this.normalizeObjectId((_a = model === null || model === void 0 ? void 0 : model._id) !== null && _a !== void 0 ? _a : model === null || model === void 0 ? void 0 : model.id);
255
252
  }
256
253
  getPreferencesField(target) {
257
254
  var _a;
@@ -746,18 +743,9 @@ class NotificationServiceFacade {
746
743
  getNotificationForTarget(notificationRef, context) {
747
744
  return __awaiter(this, void 0, void 0, function* () {
748
745
  const notificationId = notificationRef instanceof Notification_1.Notification ? notificationRef._id : notificationRef;
749
- if (!notificationId)
746
+ const objectId = this.normalizeObjectId(notificationId);
747
+ if (!objectId)
750
748
  return null;
751
- let objectId;
752
- if (notificationId instanceof mongodb_1.ObjectId) {
753
- objectId = notificationId;
754
- }
755
- else if (typeof notificationId === "string" && mongodb_1.ObjectId.isValid(notificationId)) {
756
- objectId = new mongodb_1.ObjectId(notificationId);
757
- }
758
- else {
759
- return null;
760
- }
761
749
  const notifications = yield Notification_1.Notification.aggregate([
762
750
  {
763
751
  $match: Object.assign(Object.assign({}, (yield this.getLoadMatch(context))), { _id: objectId }),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webtypen/webframez-core",
3
- "version": "0.3.63",
3
+ "version": "0.3.65",
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",