@terreno/admin-backend 0.20.1 → 0.21.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/.ai/guidelines/core.md +5 -0
- package/.github/copilot-instructions.md +1 -1
- package/AGENTS.md +5 -1
- package/bunfig.toml +1 -1
- package/dist/adminApp.d.ts +74 -1
- package/dist/adminApp.js +620 -83
- package/dist/adminApp.models.test.js +537 -8
- package/dist/adminApp.test.js +182 -0
- package/dist/adminUiV2.d.ts +96 -0
- package/dist/adminUiV2.js +189 -0
- package/dist/adminUiV2.test.d.ts +1 -0
- package/dist/adminUiV2.test.js +28 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +15 -0
- package/dist/tests/testEnv.d.ts +0 -0
- package/dist/tests/testEnv.js +6 -0
- package/package.json +5 -4
- package/src/adminApp.models.test.ts +284 -2
- package/src/adminApp.test.ts +95 -0
- package/src/adminApp.ts +598 -22
- package/src/adminUiV2.test.ts +28 -0
- package/src/adminUiV2.ts +188 -0
- package/src/index.ts +9 -1
- package/src/tests/testEnv.ts +5 -0
package/dist/adminApp.js
CHANGED
|
@@ -73,6 +73,15 @@ var __read = (this && this.__read) || function (o, n) {
|
|
|
73
73
|
}
|
|
74
74
|
return ar;
|
|
75
75
|
};
|
|
76
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
77
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
78
|
+
if (ar || !(i in from)) {
|
|
79
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
80
|
+
ar[i] = from[i];
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
84
|
+
};
|
|
76
85
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
77
86
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
78
87
|
};
|
|
@@ -82,6 +91,7 @@ var api_1 = require("@terreno/api");
|
|
|
82
91
|
var express_1 = __importDefault(require("express"));
|
|
83
92
|
var luxon_1 = require("luxon");
|
|
84
93
|
var mongoose_1 = __importDefault(require("mongoose"));
|
|
94
|
+
var adminUiV2_1 = require("./adminUiV2");
|
|
85
95
|
var toPlainObject = function (value) {
|
|
86
96
|
if (value &&
|
|
87
97
|
typeof value === "object" &&
|
|
@@ -118,8 +128,46 @@ var removeHiddenFields = function (value, hiddenFieldSet) {
|
|
|
118
128
|
}
|
|
119
129
|
return nextValue;
|
|
120
130
|
};
|
|
121
|
-
var
|
|
131
|
+
var auditDocumentToPlain = function (value) {
|
|
132
|
+
if (value &&
|
|
133
|
+
typeof value === "object" &&
|
|
134
|
+
"toObject" in value &&
|
|
135
|
+
typeof value.toObject === "function") {
|
|
136
|
+
return value.toObject();
|
|
137
|
+
}
|
|
138
|
+
return value;
|
|
139
|
+
};
|
|
140
|
+
var auditLabelFromListFields = function (doc, listFields) {
|
|
122
141
|
var e_2, _a;
|
|
142
|
+
try {
|
|
143
|
+
for (var listFields_1 = __values(listFields), listFields_1_1 = listFields_1.next(); !listFields_1_1.done; listFields_1_1 = listFields_1.next()) {
|
|
144
|
+
var key = listFields_1_1.value;
|
|
145
|
+
var fieldValue = doc[key];
|
|
146
|
+
if (fieldValue == null || typeof fieldValue === "object") {
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
return String(fieldValue);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
153
|
+
finally {
|
|
154
|
+
try {
|
|
155
|
+
if (listFields_1_1 && !listFields_1_1.done && (_a = listFields_1.return)) _a.call(listFields_1);
|
|
156
|
+
}
|
|
157
|
+
finally { if (e_2) throw e_2.error; }
|
|
158
|
+
}
|
|
159
|
+
var id = doc._id;
|
|
160
|
+
return id != null ? String(id) : undefined;
|
|
161
|
+
};
|
|
162
|
+
var auditActorId = function (request) {
|
|
163
|
+
var user = request.user;
|
|
164
|
+
if (!user || user._id == null) {
|
|
165
|
+
return undefined;
|
|
166
|
+
}
|
|
167
|
+
return String(user._id);
|
|
168
|
+
};
|
|
169
|
+
var extractFieldMeta = function (properties, required) {
|
|
170
|
+
var e_3, _a;
|
|
123
171
|
var _b, _c, _d;
|
|
124
172
|
var fields = {};
|
|
125
173
|
try {
|
|
@@ -156,15 +204,24 @@ var extractFieldMeta = function (properties, required) {
|
|
|
156
204
|
}
|
|
157
205
|
}
|
|
158
206
|
}
|
|
159
|
-
catch (
|
|
207
|
+
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
160
208
|
finally {
|
|
161
209
|
try {
|
|
162
210
|
if (_f && !_f.done && (_a = _e.return)) _a.call(_e);
|
|
163
211
|
}
|
|
164
|
-
finally { if (
|
|
212
|
+
finally { if (e_3) throw e_3.error; }
|
|
165
213
|
}
|
|
166
214
|
return fields;
|
|
167
215
|
};
|
|
216
|
+
var asMiddlewareList = function (middleware) {
|
|
217
|
+
if (middleware === undefined) {
|
|
218
|
+
return [];
|
|
219
|
+
}
|
|
220
|
+
if (Array.isArray(middleware)) {
|
|
221
|
+
return middleware;
|
|
222
|
+
}
|
|
223
|
+
return [middleware];
|
|
224
|
+
};
|
|
168
225
|
/**
|
|
169
226
|
* TerrenoPlugin that auto-generates admin CRUD endpoints for Mongoose models.
|
|
170
227
|
*
|
|
@@ -191,6 +248,8 @@ var extractFieldMeta = function (properties, required) {
|
|
|
191
248
|
* routePath: "/todos",
|
|
192
249
|
* displayName: "Todos",
|
|
193
250
|
* listFields: ["title", "completed", "ownerId"],
|
|
251
|
+
* // Optional: constrain admin list queries (e.g. tenant) — same as modelRouter queryFilter.
|
|
252
|
+
* queryFilter: (_user, query) => ({...query, tenantId: "acme"}),
|
|
194
253
|
* },
|
|
195
254
|
* ],
|
|
196
255
|
* });
|
|
@@ -231,16 +290,43 @@ var AdminApp = /** @class */ (function () {
|
|
|
231
290
|
* @param app - The Express application instance to register with
|
|
232
291
|
*/
|
|
233
292
|
AdminApp.prototype.register = function (app, openApi) {
|
|
234
|
-
var
|
|
293
|
+
var e_4, _a, e_5, _b;
|
|
235
294
|
var _this = this;
|
|
236
|
-
var _c, _d, _e, _f;
|
|
295
|
+
var _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
|
|
237
296
|
var basePath = (_c = this.options.basePath) !== null && _c !== void 0 ? _c : "/admin";
|
|
297
|
+
var openApiMw = openApi;
|
|
238
298
|
var modelConfigs = this.options.models;
|
|
299
|
+
var onAdminAudit = this.options.onAdminAudit;
|
|
300
|
+
/** Audit is best-effort: failures must not change HTTP outcomes after mutations succeed. */
|
|
301
|
+
var safeOnAdminAudit = function (request, event) { return __awaiter(_this, void 0, void 0, function () {
|
|
302
|
+
var err_1, detail;
|
|
303
|
+
return __generator(this, function (_a) {
|
|
304
|
+
switch (_a.label) {
|
|
305
|
+
case 0:
|
|
306
|
+
if (!onAdminAudit) {
|
|
307
|
+
return [2 /*return*/];
|
|
308
|
+
}
|
|
309
|
+
_a.label = 1;
|
|
310
|
+
case 1:
|
|
311
|
+
_a.trys.push([1, 3, , 4]);
|
|
312
|
+
return [4 /*yield*/, onAdminAudit(event, request)];
|
|
313
|
+
case 2:
|
|
314
|
+
_a.sent();
|
|
315
|
+
return [3 /*break*/, 4];
|
|
316
|
+
case 3:
|
|
317
|
+
err_1 = _a.sent();
|
|
318
|
+
detail = err_1 instanceof Error ? err_1.message : String(err_1);
|
|
319
|
+
api_1.logger.error("onAdminAudit failed after ".concat(event.verb, " on ").concat(event.modelName, ": ").concat(detail));
|
|
320
|
+
return [3 /*break*/, 4];
|
|
321
|
+
case 4: return [2 /*return*/];
|
|
322
|
+
}
|
|
323
|
+
});
|
|
324
|
+
}); };
|
|
239
325
|
// Build config response with field metadata from Mongoose schemas
|
|
240
326
|
var configModels = modelConfigs.map(function (config) {
|
|
241
|
-
var
|
|
242
|
-
var _c, _d, _e, _f, _g;
|
|
243
|
-
var
|
|
327
|
+
var e_6, _a, e_7, _b;
|
|
328
|
+
var _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u;
|
|
329
|
+
var _v = (0, api_1.getOpenApiSpecForModel)(config.model), properties = _v.properties, required = _v.required;
|
|
244
330
|
var hiddenFieldSet = new Set((_c = config.hiddenFields) !== null && _c !== void 0 ? _c : []);
|
|
245
331
|
var filteredProperties = Object.fromEntries(Object.entries(properties).filter(function (_a) {
|
|
246
332
|
var _b = __read(_a, 1), key = _b[0];
|
|
@@ -250,8 +336,8 @@ var AdminApp = /** @class */ (function () {
|
|
|
250
336
|
// Extract ref information directly from the Mongoose schema
|
|
251
337
|
var fields = extractFieldMeta(filteredProperties, filteredRequired);
|
|
252
338
|
try {
|
|
253
|
-
for (var
|
|
254
|
-
var
|
|
339
|
+
for (var _w = __values(Object.entries(fields)), _x = _w.next(); !_x.done; _x = _w.next()) {
|
|
340
|
+
var _y = __read(_x.value, 2), key = _y[0], field = _y[1];
|
|
255
341
|
var schemaPath = config.model.schema.path(key);
|
|
256
342
|
if (schemaPath) {
|
|
257
343
|
var pathOptions = schemaPath.options;
|
|
@@ -290,39 +376,71 @@ var AdminApp = /** @class */ (function () {
|
|
|
290
376
|
}
|
|
291
377
|
}
|
|
292
378
|
}
|
|
293
|
-
catch (
|
|
379
|
+
catch (e_6_1) { e_6 = { error: e_6_1 }; }
|
|
294
380
|
finally {
|
|
295
381
|
try {
|
|
296
|
-
if (
|
|
382
|
+
if (_x && !_x.done && (_a = _w.return)) _a.call(_w);
|
|
297
383
|
}
|
|
298
|
-
finally { if (
|
|
384
|
+
finally { if (e_6) throw e_6.error; }
|
|
299
385
|
}
|
|
300
386
|
// Apply field overrides (e.g., widget: "markdown")
|
|
301
387
|
if (config.fieldOverrides) {
|
|
302
388
|
try {
|
|
303
|
-
for (var
|
|
304
|
-
var
|
|
389
|
+
for (var _z = __values(Object.entries(config.fieldOverrides)), _0 = _z.next(); !_0.done; _0 = _z.next()) {
|
|
390
|
+
var _1 = __read(_0.value, 2), key = _1[0], override = _1[1];
|
|
305
391
|
if (fields[key] && override.widget) {
|
|
306
392
|
fields[key].widget = override.widget;
|
|
307
393
|
}
|
|
308
394
|
}
|
|
309
395
|
}
|
|
310
|
-
catch (
|
|
396
|
+
catch (e_7_1) { e_7 = { error: e_7_1 }; }
|
|
311
397
|
finally {
|
|
312
398
|
try {
|
|
313
|
-
if (
|
|
399
|
+
if (_0 && !_0.done && (_b = _z.return)) _b.call(_z);
|
|
314
400
|
}
|
|
315
|
-
finally { if (
|
|
401
|
+
finally { if (e_7) throw e_7.error; }
|
|
316
402
|
}
|
|
317
403
|
}
|
|
404
|
+
var readonlyFields = (_g = config.readonlyFields) !== null && _g !== void 0 ? _g : [];
|
|
405
|
+
var listFields = config.listFields.filter(function (field) { return !hiddenFieldSet.has(field); });
|
|
406
|
+
var listDisplay = (_h = config.listDisplay) !== null && _h !== void 0 ? _h : listFields;
|
|
407
|
+
var derivedSearchFields = listFields.filter(function (field) { var _a; return (_a = fields[field]) === null || _a === void 0 ? void 0 : _a.searchable; });
|
|
408
|
+
var searchFields = (_j = config.searchFields) !== null && _j !== void 0 ? _j : derivedSearchFields;
|
|
409
|
+
var sortableFields = (_k = config.sortableFields) !== null && _k !== void 0 ? _k : __spreadArray(__spreadArray([], __read(listDisplay), false), ["_id"], false);
|
|
410
|
+
var schemaPathKeys = new Set(Object.keys(config.model.schema.paths));
|
|
411
|
+
var bulkPatchAllowlist = (_l = config.bulkPatchAllowlist) !== null && _l !== void 0 ? _l : (0, adminUiV2_1.defaultBulkPatchAllowlistFrom)({
|
|
412
|
+
hiddenFieldSet: hiddenFieldSet,
|
|
413
|
+
listFields: listFields,
|
|
414
|
+
readonlyFields: readonlyFields,
|
|
415
|
+
schemaPaths: schemaPathKeys,
|
|
416
|
+
});
|
|
318
417
|
return {
|
|
319
|
-
|
|
418
|
+
actions: (_m = config.actions) !== null && _m !== void 0 ? _m : [],
|
|
419
|
+
bulkPatchAllowlist: bulkPatchAllowlist,
|
|
420
|
+
defaultSort: (_o = config.defaultSort) !== null && _o !== void 0 ? _o : "-created",
|
|
320
421
|
displayName: config.displayName,
|
|
321
422
|
fieldOrder: config.fieldOrder,
|
|
322
423
|
fields: fields,
|
|
323
|
-
|
|
424
|
+
fieldsets: config.fieldsets,
|
|
425
|
+
filters: (_p = config.filters) !== null && _p !== void 0 ? _p : [],
|
|
426
|
+
group: config.group,
|
|
427
|
+
hiddenFields: __spreadArray([], __read(hiddenFieldSet), false),
|
|
428
|
+
listDisplay: listDisplay,
|
|
429
|
+
listDisplayLinks: (_q = config.listDisplayLinks) !== null && _q !== void 0 ? _q : [],
|
|
430
|
+
listFields: listFields,
|
|
324
431
|
name: config.model.modelName,
|
|
432
|
+
pageSize: config.pageSize,
|
|
433
|
+
permissions: {
|
|
434
|
+
create: ((_r = config.permissions) === null || _r === void 0 ? void 0 : _r.create) !== false,
|
|
435
|
+
delete: ((_s = config.permissions) === null || _s === void 0 ? void 0 : _s.delete) !== false,
|
|
436
|
+
update: ((_t = config.permissions) === null || _t === void 0 ? void 0 : _t.update) !== false,
|
|
437
|
+
},
|
|
438
|
+
readonlyFields: readonlyFields,
|
|
439
|
+
realtime: (_u = config.realtime) !== null && _u !== void 0 ? _u : false,
|
|
440
|
+
recordTitleField: config.recordTitleField,
|
|
325
441
|
routePath: "".concat(basePath).concat(config.routePath),
|
|
442
|
+
searchFields: searchFields,
|
|
443
|
+
sortableFields: sortableFields,
|
|
326
444
|
};
|
|
327
445
|
});
|
|
328
446
|
// Build script metadata for config response
|
|
@@ -331,29 +449,140 @@ var AdminApp = /** @class */ (function () {
|
|
|
331
449
|
description: script.description,
|
|
332
450
|
name: script.name,
|
|
333
451
|
}); });
|
|
452
|
+
var defaultScreens = [{ displayName: "Version Config", name: "version-config" }];
|
|
453
|
+
var mergedScreens = __spreadArray(__spreadArray([], __read(defaultScreens), false), __read(((_e = this.options.customScreens) !== null && _e !== void 0 ? _e : [])), false);
|
|
334
454
|
var configResponse = {
|
|
335
|
-
customScreens:
|
|
336
|
-
|
|
337
|
-
displayName: "Version Config",
|
|
338
|
-
name: "version-config",
|
|
339
|
-
},
|
|
340
|
-
],
|
|
455
|
+
customScreens: mergedScreens,
|
|
456
|
+
home: (0, adminUiV2_1.normalizeAdminHome)(this.options.home),
|
|
341
457
|
models: configModels,
|
|
458
|
+
schemaVersion: adminUiV2_1.ADMIN_SCHEMA_VERSION,
|
|
342
459
|
scripts: configScripts,
|
|
343
460
|
};
|
|
461
|
+
var adminConfigOpenApi = openApiMw
|
|
462
|
+
? (0, api_1.createOpenApiBuilder)({ openApi: openApiMw })
|
|
463
|
+
.withTags(["admin"])
|
|
464
|
+
.withSummary("Admin panel configuration")
|
|
465
|
+
.withResponse(200, {
|
|
466
|
+
customScreens: {
|
|
467
|
+
items: {
|
|
468
|
+
properties: {
|
|
469
|
+
description: { type: "string" },
|
|
470
|
+
displayName: { type: "string" },
|
|
471
|
+
name: { type: "string" },
|
|
472
|
+
},
|
|
473
|
+
type: "object",
|
|
474
|
+
},
|
|
475
|
+
type: "array",
|
|
476
|
+
},
|
|
477
|
+
home: { type: "object" },
|
|
478
|
+
models: { type: "array" },
|
|
479
|
+
schemaVersion: { type: "number" },
|
|
480
|
+
scripts: {
|
|
481
|
+
items: {
|
|
482
|
+
properties: {
|
|
483
|
+
description: { type: "string" },
|
|
484
|
+
name: { type: "string" },
|
|
485
|
+
},
|
|
486
|
+
type: "object",
|
|
487
|
+
},
|
|
488
|
+
type: "array",
|
|
489
|
+
},
|
|
490
|
+
})
|
|
491
|
+
.build()
|
|
492
|
+
: undefined;
|
|
344
493
|
// GET /admin/config
|
|
345
|
-
app.get("".concat(basePath, "/config"), (0, api_1.authenticateMiddleware)(), (0, api_1.asyncHandler)(function (req, res) { return __awaiter(_this, void 0, void 0, function () {
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
494
|
+
app.get.apply(app, __spreadArray(__spreadArray(["".concat(basePath, "/config"), (0, api_1.authenticateMiddleware)()], __read(asMiddlewareList(adminConfigOpenApi)), false), [(0, api_1.asyncHandler)(function (req, res) { return __awaiter(_this, void 0, void 0, function () {
|
|
495
|
+
return __generator(this, function (_a) {
|
|
496
|
+
switch (_a.label) {
|
|
497
|
+
case 0: return [4 /*yield*/, (0, api_1.checkPermissions)("read", [api_1.Permissions.IsAdmin], req.user)];
|
|
498
|
+
case 1:
|
|
499
|
+
if (!(_a.sent())) {
|
|
500
|
+
throw new api_1.APIError({ status: 403, title: "Admin access required" });
|
|
501
|
+
}
|
|
502
|
+
return [2 /*return*/, res.json(configResponse)];
|
|
503
|
+
}
|
|
504
|
+
});
|
|
505
|
+
}); })], false));
|
|
506
|
+
var backgroundTasksOpenApi = openApiMw
|
|
507
|
+
? (0, api_1.createOpenApiBuilder)({ openApi: openApiMw })
|
|
508
|
+
.withTags(["admin"])
|
|
509
|
+
.withSummary("Enqueue a generic admin background task")
|
|
510
|
+
.withRequestBody({
|
|
511
|
+
ids: {
|
|
512
|
+
description: "Optional target document ids",
|
|
513
|
+
items: { type: "string" },
|
|
514
|
+
type: "array",
|
|
515
|
+
},
|
|
516
|
+
kind: {
|
|
517
|
+
description: "Task kind label persisted as taskType",
|
|
518
|
+
required: true,
|
|
519
|
+
type: "string",
|
|
520
|
+
},
|
|
521
|
+
metadata: { description: "Opaque JSON metadata for workers", type: "object" },
|
|
522
|
+
resourceRoute: {
|
|
523
|
+
description: "Optional admin model route this task relates to",
|
|
524
|
+
type: "string",
|
|
525
|
+
},
|
|
526
|
+
})
|
|
527
|
+
.withResponse(201, { taskId: { type: "string" } })
|
|
528
|
+
.build()
|
|
529
|
+
: undefined;
|
|
530
|
+
app.post.apply(app, __spreadArray(__spreadArray(["".concat(basePath, "/background-tasks"), (0, api_1.authenticateMiddleware)()], __read(asMiddlewareList(backgroundTasksOpenApi)), false), [(0, api_1.asyncHandler)(function (req, res) { return __awaiter(_this, void 0, void 0, function () {
|
|
531
|
+
var user, raw, now, summary, task, err_2, detail;
|
|
532
|
+
return __generator(this, function (_a) {
|
|
533
|
+
switch (_a.label) {
|
|
534
|
+
case 0: return [4 /*yield*/, (0, api_1.checkPermissions)("update", [api_1.Permissions.IsAdmin], req.user)];
|
|
535
|
+
case 1:
|
|
536
|
+
if (!(_a.sent())) {
|
|
537
|
+
throw new api_1.APIError({ status: 403, title: "Admin access required" });
|
|
538
|
+
}
|
|
539
|
+
user = req.user;
|
|
540
|
+
raw = req.body;
|
|
541
|
+
if (typeof raw.kind !== "string" || !raw.kind.trim()) {
|
|
542
|
+
throw new api_1.APIError({ status: 400, title: "kind is required" });
|
|
543
|
+
}
|
|
544
|
+
now = luxon_1.DateTime.now().toJSDate();
|
|
545
|
+
summary = {
|
|
546
|
+
ids: Array.isArray(raw.ids) ? raw.ids : [],
|
|
547
|
+
metadata: raw.metadata && typeof raw.metadata === "object" && !Array.isArray(raw.metadata)
|
|
548
|
+
? raw.metadata
|
|
549
|
+
: {},
|
|
550
|
+
resourceRoute: typeof raw.resourceRoute === "string" ? raw.resourceRoute : undefined,
|
|
551
|
+
};
|
|
552
|
+
_a.label = 2;
|
|
553
|
+
case 2:
|
|
554
|
+
_a.trys.push([2, 4, , 5]);
|
|
555
|
+
return [4 /*yield*/, api_1.BackgroundTask.create({
|
|
556
|
+
createdBy: user === null || user === void 0 ? void 0 : user._id,
|
|
557
|
+
isDryRun: false,
|
|
558
|
+
logs: [
|
|
559
|
+
{
|
|
560
|
+
level: "info",
|
|
561
|
+
message: "Queued background task ".concat(raw.kind, ": ").concat(JSON.stringify(summary)),
|
|
562
|
+
timestamp: now,
|
|
563
|
+
},
|
|
564
|
+
],
|
|
565
|
+
progress: { message: "Queued", percentage: 0, stage: "Queued" },
|
|
566
|
+
startedAt: now,
|
|
567
|
+
status: "pending",
|
|
568
|
+
taskType: raw.kind,
|
|
569
|
+
})];
|
|
570
|
+
case 3:
|
|
571
|
+
task = (_a.sent());
|
|
572
|
+
return [3 /*break*/, 5];
|
|
573
|
+
case 4:
|
|
574
|
+
err_2 = _a.sent();
|
|
575
|
+
detail = err_2 instanceof Error ? err_2.message : String(err_2);
|
|
576
|
+
api_1.logger.error("Failed to create admin background task: ".concat(detail));
|
|
577
|
+
throw new api_1.APIError({
|
|
578
|
+
detail: detail,
|
|
579
|
+
status: 500,
|
|
580
|
+
title: "Failed to enqueue background task",
|
|
581
|
+
});
|
|
582
|
+
case 5: return [2 /*return*/, res.status(201).json({ taskId: task._id.toString() })];
|
|
583
|
+
}
|
|
584
|
+
});
|
|
585
|
+
}); })], false));
|
|
357
586
|
// Version config singleton routes (GET/PUT /admin/version-config)
|
|
358
587
|
var versionConfigPath = "".concat(basePath, "/version-config");
|
|
359
588
|
app.get(versionConfigPath, (0, api_1.authenticateMiddleware)(), (0, api_1.asyncHandler)(function (req, res) { return __awaiter(_this, void 0, void 0, function () {
|
|
@@ -383,7 +612,7 @@ var AdminApp = /** @class */ (function () {
|
|
|
383
612
|
}); }));
|
|
384
613
|
app.put(versionConfigPath, (0, api_1.authenticateMiddleware)(), (0, api_1.asyncHandler)(function (req, res) { return __awaiter(_this, void 0, void 0, function () {
|
|
385
614
|
var raw, allowedFields, setFields, unsetFields, allowedFields_1, allowedFields_1_1, field, updateOp, doc;
|
|
386
|
-
var
|
|
615
|
+
var e_8, _a;
|
|
387
616
|
return __generator(this, function (_b) {
|
|
388
617
|
switch (_b.label) {
|
|
389
618
|
case 0: return [4 /*yield*/, (0, api_1.checkPermissions)("update", [api_1.Permissions.IsAdmin], req.user)];
|
|
@@ -414,12 +643,12 @@ var AdminApp = /** @class */ (function () {
|
|
|
414
643
|
}
|
|
415
644
|
}
|
|
416
645
|
}
|
|
417
|
-
catch (
|
|
646
|
+
catch (e_8_1) { e_8 = { error: e_8_1 }; }
|
|
418
647
|
finally {
|
|
419
648
|
try {
|
|
420
649
|
if (allowedFields_1_1 && !allowedFields_1_1.done && (_a = allowedFields_1.return)) _a.call(allowedFields_1);
|
|
421
650
|
}
|
|
422
|
-
finally { if (
|
|
651
|
+
finally { if (e_8) throw e_8.error; }
|
|
423
652
|
}
|
|
424
653
|
updateOp = {};
|
|
425
654
|
if (Object.keys(setFields).length > 0) {
|
|
@@ -441,7 +670,7 @@ var AdminApp = /** @class */ (function () {
|
|
|
441
670
|
});
|
|
442
671
|
}); }));
|
|
443
672
|
var _loop_1 = function (config) {
|
|
444
|
-
var
|
|
673
|
+
var e_9, _p;
|
|
445
674
|
// Determine searchable fields from the actual Mongoose schema type,
|
|
446
675
|
// not the OpenAPI type (which reports ObjectId as "string")
|
|
447
676
|
var searchableFields = [];
|
|
@@ -449,8 +678,8 @@ var AdminApp = /** @class */ (function () {
|
|
|
449
678
|
var modelMeta = configModels.find(function (m) { return m.name === config.model.modelName; });
|
|
450
679
|
if (modelMeta) {
|
|
451
680
|
try {
|
|
452
|
-
for (var
|
|
453
|
-
var key =
|
|
681
|
+
for (var _q = (e_9 = void 0, __values(Object.keys(modelMeta.fields))), _r = _q.next(); !_r.done; _r = _q.next()) {
|
|
682
|
+
var key = _r.value;
|
|
454
683
|
var schemaPath = config.model.schema.path(key);
|
|
455
684
|
if (schemaPath && schemaPath.instance === "String" && !modelMeta.fields[key].enum) {
|
|
456
685
|
searchableFields.push(key);
|
|
@@ -460,21 +689,17 @@ var AdminApp = /** @class */ (function () {
|
|
|
460
689
|
}
|
|
461
690
|
}
|
|
462
691
|
}
|
|
463
|
-
catch (
|
|
692
|
+
catch (e_9_1) { e_9 = { error: e_9_1 }; }
|
|
464
693
|
finally {
|
|
465
694
|
try {
|
|
466
|
-
if (
|
|
695
|
+
if (_r && !_r.done && (_p = _q.return)) _p.call(_q);
|
|
467
696
|
}
|
|
468
|
-
finally { if (
|
|
697
|
+
finally { if (e_9) throw e_9.error; }
|
|
469
698
|
}
|
|
470
699
|
}
|
|
471
|
-
api_1.logger.info("Admin search fields for ".concat(config.model.modelName), {
|
|
472
|
-
objectIdFields: objectIdFields,
|
|
473
|
-
searchableFields: searchableFields,
|
|
474
|
-
});
|
|
475
700
|
app.get("".concat(basePath).concat(config.routePath, "/search"), (0, api_1.authenticateMiddleware)(), (0, api_1.asyncHandler)(function (req, res) { return __awaiter(_this, void 0, void 0, function () {
|
|
476
|
-
var user, q, escapedQ, regex, fields, orConditions, objectIdFields_1, objectIdFields_1_1, field, results,
|
|
477
|
-
var
|
|
701
|
+
var user, q, escapedQ, regex, fields, orConditions, objectIdFields_1, objectIdFields_1_1, field, results, err_3;
|
|
702
|
+
var e_10, _a, _b;
|
|
478
703
|
var _c;
|
|
479
704
|
return __generator(this, function (_d) {
|
|
480
705
|
switch (_d.label) {
|
|
@@ -508,12 +733,12 @@ var AdminApp = /** @class */ (function () {
|
|
|
508
733
|
orConditions.push((_b = {}, _b[field] = new mongoose_1.default.Types.ObjectId(q), _b));
|
|
509
734
|
}
|
|
510
735
|
}
|
|
511
|
-
catch (
|
|
736
|
+
catch (e_10_1) { e_10 = { error: e_10_1 }; }
|
|
512
737
|
finally {
|
|
513
738
|
try {
|
|
514
739
|
if (objectIdFields_1_1 && !objectIdFields_1_1.done && (_a = objectIdFields_1.return)) _a.call(objectIdFields_1);
|
|
515
740
|
}
|
|
516
|
-
finally { if (
|
|
741
|
+
finally { if (e_10) throw e_10.error; }
|
|
517
742
|
}
|
|
518
743
|
}
|
|
519
744
|
if (orConditions.length === 0) {
|
|
@@ -536,13 +761,13 @@ var AdminApp = /** @class */ (function () {
|
|
|
536
761
|
});
|
|
537
762
|
return [2 /*return*/, res.json({ data: results })];
|
|
538
763
|
case 3:
|
|
539
|
-
|
|
764
|
+
err_3 = _d.sent();
|
|
540
765
|
api_1.logger.error("Admin search failed", {
|
|
541
|
-
error:
|
|
766
|
+
error: err_3,
|
|
542
767
|
fields: fields,
|
|
543
768
|
model: config.model.modelName,
|
|
544
769
|
});
|
|
545
|
-
throw
|
|
770
|
+
throw err_3;
|
|
546
771
|
case 4: return [2 /*return*/];
|
|
547
772
|
}
|
|
548
773
|
});
|
|
@@ -555,28 +780,291 @@ var AdminApp = /** @class */ (function () {
|
|
|
555
780
|
_loop_1(config);
|
|
556
781
|
}
|
|
557
782
|
}
|
|
558
|
-
catch (
|
|
783
|
+
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
559
784
|
finally {
|
|
560
785
|
try {
|
|
561
786
|
if (modelConfigs_1_1 && !modelConfigs_1_1.done && (_a = modelConfigs_1.return)) _a.call(modelConfigs_1);
|
|
562
787
|
}
|
|
563
|
-
finally { if (
|
|
788
|
+
finally { if (e_4) throw e_4.error; }
|
|
564
789
|
}
|
|
565
790
|
var _loop_2 = function (config) {
|
|
566
|
-
var hiddenFieldSet = new Set((
|
|
791
|
+
var hiddenFieldSet = new Set((_f = config.hiddenFields) !== null && _f !== void 0 ? _f : []);
|
|
792
|
+
var readonlySet = new Set((_g = config.readonlyFields) !== null && _g !== void 0 ? _g : []);
|
|
793
|
+
var modelMeta = configModels.find(function (m) { return m.name === config.model.modelName; });
|
|
794
|
+
var allowlist = new Set((_h = modelMeta === null || modelMeta === void 0 ? void 0 : modelMeta.bulkPatchAllowlist) !== null && _h !== void 0 ? _h : []);
|
|
795
|
+
var adminPermission = function (allowed) {
|
|
796
|
+
if (allowed === false) {
|
|
797
|
+
return [];
|
|
798
|
+
}
|
|
799
|
+
return [api_1.Permissions.IsAdmin];
|
|
800
|
+
};
|
|
801
|
+
var stripProtectedFromBody = function (body) {
|
|
802
|
+
var e_11, _a, e_12, _b, e_13, _c;
|
|
803
|
+
if (!body || typeof body !== "object" || Array.isArray(body)) {
|
|
804
|
+
return {};
|
|
805
|
+
}
|
|
806
|
+
var next = __assign({}, body);
|
|
807
|
+
try {
|
|
808
|
+
for (var readonlySet_1 = (e_11 = void 0, __values(readonlySet)), readonlySet_1_1 = readonlySet_1.next(); !readonlySet_1_1.done; readonlySet_1_1 = readonlySet_1.next()) {
|
|
809
|
+
var key = readonlySet_1_1.value;
|
|
810
|
+
delete next[key];
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
catch (e_11_1) { e_11 = { error: e_11_1 }; }
|
|
814
|
+
finally {
|
|
815
|
+
try {
|
|
816
|
+
if (readonlySet_1_1 && !readonlySet_1_1.done && (_a = readonlySet_1.return)) _a.call(readonlySet_1);
|
|
817
|
+
}
|
|
818
|
+
finally { if (e_11) throw e_11.error; }
|
|
819
|
+
}
|
|
820
|
+
try {
|
|
821
|
+
for (var hiddenFieldSet_1 = (e_12 = void 0, __values(hiddenFieldSet)), hiddenFieldSet_1_1 = hiddenFieldSet_1.next(); !hiddenFieldSet_1_1.done; hiddenFieldSet_1_1 = hiddenFieldSet_1.next()) {
|
|
822
|
+
var key = hiddenFieldSet_1_1.value;
|
|
823
|
+
delete next[key];
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
catch (e_12_1) { e_12 = { error: e_12_1 }; }
|
|
827
|
+
finally {
|
|
828
|
+
try {
|
|
829
|
+
if (hiddenFieldSet_1_1 && !hiddenFieldSet_1_1.done && (_b = hiddenFieldSet_1.return)) _b.call(hiddenFieldSet_1);
|
|
830
|
+
}
|
|
831
|
+
finally { if (e_12) throw e_12.error; }
|
|
832
|
+
}
|
|
833
|
+
try {
|
|
834
|
+
for (var SYSTEM_ADMIN_FIELDS_1 = (e_13 = void 0, __values(adminUiV2_1.SYSTEM_ADMIN_FIELDS)), SYSTEM_ADMIN_FIELDS_1_1 = SYSTEM_ADMIN_FIELDS_1.next(); !SYSTEM_ADMIN_FIELDS_1_1.done; SYSTEM_ADMIN_FIELDS_1_1 = SYSTEM_ADMIN_FIELDS_1.next()) {
|
|
835
|
+
var key = SYSTEM_ADMIN_FIELDS_1_1.value;
|
|
836
|
+
delete next[key];
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
catch (e_13_1) { e_13 = { error: e_13_1 }; }
|
|
840
|
+
finally {
|
|
841
|
+
try {
|
|
842
|
+
if (SYSTEM_ADMIN_FIELDS_1_1 && !SYSTEM_ADMIN_FIELDS_1_1.done && (_c = SYSTEM_ADMIN_FIELDS_1.return)) _c.call(SYSTEM_ADMIN_FIELDS_1);
|
|
843
|
+
}
|
|
844
|
+
finally { if (e_13) throw e_13.error; }
|
|
845
|
+
}
|
|
846
|
+
return next;
|
|
847
|
+
};
|
|
848
|
+
var bulkPatchOpenApi = openApiMw
|
|
849
|
+
? (0, api_1.createOpenApiBuilder)({ openApi: openApiMw })
|
|
850
|
+
.withTags(["admin"])
|
|
851
|
+
.withSummary("Bulk patch ".concat(config.model.modelName, " documents"))
|
|
852
|
+
.withRequestBody({
|
|
853
|
+
ids: {
|
|
854
|
+
description: "Document ids to update",
|
|
855
|
+
items: { type: "string" },
|
|
856
|
+
required: true,
|
|
857
|
+
type: "array",
|
|
858
|
+
},
|
|
859
|
+
patch: {
|
|
860
|
+
description: "Partial document; keys must be allowlisted for this model",
|
|
861
|
+
required: true,
|
|
862
|
+
type: "object",
|
|
863
|
+
},
|
|
864
|
+
})
|
|
865
|
+
.withResponse(200, {
|
|
866
|
+
failures: { type: "array" },
|
|
867
|
+
updated: { type: "number" },
|
|
868
|
+
})
|
|
869
|
+
.build()
|
|
870
|
+
: undefined;
|
|
871
|
+
var auditEligible = Boolean(onAdminAudit) && config.model.modelName !== "AdminAuditLog";
|
|
872
|
+
var auditHooks = auditEligible
|
|
873
|
+
? {
|
|
874
|
+
postCreate: function (value, request) { return __awaiter(_this, void 0, void 0, function () {
|
|
875
|
+
var doc, rid;
|
|
876
|
+
return __generator(this, function (_a) {
|
|
877
|
+
switch (_a.label) {
|
|
878
|
+
case 0:
|
|
879
|
+
doc = auditDocumentToPlain(value);
|
|
880
|
+
rid = doc._id;
|
|
881
|
+
return [4 /*yield*/, safeOnAdminAudit(request, {
|
|
882
|
+
actorId: auditActorId(request),
|
|
883
|
+
modelName: config.model.modelName,
|
|
884
|
+
recordId: rid != null ? String(rid) : undefined,
|
|
885
|
+
recordLabel: auditLabelFromListFields(doc, config.listFields),
|
|
886
|
+
verb: "created",
|
|
887
|
+
})];
|
|
888
|
+
case 1:
|
|
889
|
+
_a.sent();
|
|
890
|
+
return [2 /*return*/];
|
|
891
|
+
}
|
|
892
|
+
});
|
|
893
|
+
}); },
|
|
894
|
+
postDelete: function (request, value) { return __awaiter(_this, void 0, void 0, function () {
|
|
895
|
+
var doc, rid;
|
|
896
|
+
return __generator(this, function (_a) {
|
|
897
|
+
switch (_a.label) {
|
|
898
|
+
case 0:
|
|
899
|
+
doc = auditDocumentToPlain(value);
|
|
900
|
+
rid = doc._id;
|
|
901
|
+
return [4 /*yield*/, safeOnAdminAudit(request, {
|
|
902
|
+
actorId: auditActorId(request),
|
|
903
|
+
modelName: config.model.modelName,
|
|
904
|
+
recordId: rid != null ? String(rid) : undefined,
|
|
905
|
+
recordLabel: auditLabelFromListFields(doc, config.listFields),
|
|
906
|
+
verb: "deleted",
|
|
907
|
+
})];
|
|
908
|
+
case 1:
|
|
909
|
+
_a.sent();
|
|
910
|
+
return [2 /*return*/];
|
|
911
|
+
}
|
|
912
|
+
});
|
|
913
|
+
}); },
|
|
914
|
+
postUpdate: function (value, _cleanedBody, request, _prev) { return __awaiter(_this, void 0, void 0, function () {
|
|
915
|
+
var doc, rid;
|
|
916
|
+
return __generator(this, function (_a) {
|
|
917
|
+
switch (_a.label) {
|
|
918
|
+
case 0:
|
|
919
|
+
doc = auditDocumentToPlain(value);
|
|
920
|
+
rid = doc._id;
|
|
921
|
+
return [4 /*yield*/, safeOnAdminAudit(request, {
|
|
922
|
+
actorId: auditActorId(request),
|
|
923
|
+
modelName: config.model.modelName,
|
|
924
|
+
recordId: rid != null ? String(rid) : undefined,
|
|
925
|
+
recordLabel: auditLabelFromListFields(doc, config.listFields),
|
|
926
|
+
verb: "updated",
|
|
927
|
+
})];
|
|
928
|
+
case 1:
|
|
929
|
+
_a.sent();
|
|
930
|
+
return [2 /*return*/];
|
|
931
|
+
}
|
|
932
|
+
});
|
|
933
|
+
}); },
|
|
934
|
+
}
|
|
935
|
+
: {};
|
|
567
936
|
// biome-ignore lint/suspicious/noExplicitAny: matches the Model<any> from AdminModelConfig above.
|
|
568
|
-
var routerOptions = __assign(__assign({}, (
|
|
569
|
-
create:
|
|
570
|
-
delete:
|
|
937
|
+
var routerOptions = __assign(__assign(__assign(__assign(__assign({}, (openApiMw ? { openApi: openApiMw } : {})), { defaultLimit: (_j = config.pageSize) !== null && _j !== void 0 ? _j : 100, maxLimit: 500, permissions: {
|
|
938
|
+
create: adminPermission((_k = config.permissions) === null || _k === void 0 ? void 0 : _k.create),
|
|
939
|
+
delete: adminPermission((_l = config.permissions) === null || _l === void 0 ? void 0 : _l.delete),
|
|
571
940
|
list: [api_1.Permissions.IsAdmin],
|
|
572
941
|
read: [api_1.Permissions.IsAdmin],
|
|
573
|
-
update:
|
|
574
|
-
},
|
|
942
|
+
update: adminPermission((_m = config.permissions) === null || _m === void 0 ? void 0 : _m.update),
|
|
943
|
+
}, queryFields: (0, adminUiV2_1.buildAdminModelQueryFields)({
|
|
944
|
+
filters: config.filters,
|
|
945
|
+
listDisplay: config.listDisplay,
|
|
946
|
+
listFields: config.listFields,
|
|
947
|
+
searchFields: config.searchFields,
|
|
948
|
+
}) }), (config.queryFilter ? { queryFilter: config.queryFilter } : {})), { preCreate: function (body, _req) { return __awaiter(_this, void 0, void 0, function () {
|
|
949
|
+
return __generator(this, function (_a) {
|
|
950
|
+
if (!body || typeof body !== "object") {
|
|
951
|
+
return [2 /*return*/, body];
|
|
952
|
+
}
|
|
953
|
+
return [2 /*return*/, stripProtectedFromBody(body)];
|
|
954
|
+
});
|
|
955
|
+
}); }, preUpdate: function (body, _req) { return __awaiter(_this, void 0, void 0, function () {
|
|
956
|
+
return __generator(this, function (_a) {
|
|
957
|
+
if (!body || typeof body !== "object") {
|
|
958
|
+
return [2 /*return*/, body];
|
|
959
|
+
}
|
|
960
|
+
return [2 /*return*/, stripProtectedFromBody(body)];
|
|
961
|
+
});
|
|
962
|
+
}); }, responseHandler: hiddenFieldSet.size > 0
|
|
575
963
|
? function (value, _method, _request, _options) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
|
|
576
964
|
return [2 /*return*/, removeHiddenFields(value, hiddenFieldSet)];
|
|
577
965
|
}); }); }
|
|
578
|
-
: undefined, sort: (
|
|
579
|
-
|
|
966
|
+
: undefined, sort: (_o = config.defaultSort) !== null && _o !== void 0 ? _o : "-created" }), auditHooks);
|
|
967
|
+
var modelBase = express_1.default.Router();
|
|
968
|
+
modelBase.post.apply(modelBase, __spreadArray(__spreadArray(["/bulk-patch",
|
|
969
|
+
(0, api_1.authenticateMiddleware)()], __read(asMiddlewareList(bulkPatchOpenApi)), false), [(0, api_1.asyncHandler)(function (req, res) { return __awaiter(_this, void 0, void 0, function () {
|
|
970
|
+
var body, ids, rawPatch, unknownKeys, patch, updated, failures, ids_1, ids_1_1, id, resUpdate, err_4, message, e_14_1;
|
|
971
|
+
var e_14, _a;
|
|
972
|
+
var _b;
|
|
973
|
+
return __generator(this, function (_c) {
|
|
974
|
+
switch (_c.label) {
|
|
975
|
+
case 0: return [4 /*yield*/, (0, api_1.checkPermissions)("update", [api_1.Permissions.IsAdmin], req.user)];
|
|
976
|
+
case 1:
|
|
977
|
+
if (!(_c.sent())) {
|
|
978
|
+
throw new api_1.APIError({ status: 403, title: "Admin access required" });
|
|
979
|
+
}
|
|
980
|
+
if (((_b = config.permissions) === null || _b === void 0 ? void 0 : _b.update) === false) {
|
|
981
|
+
throw new api_1.APIError({ status: 403, title: "Updates are disabled for this model" });
|
|
982
|
+
}
|
|
983
|
+
body = req.body;
|
|
984
|
+
if (!Array.isArray(body.ids)) {
|
|
985
|
+
throw new api_1.APIError({ status: 400, title: "Request body must include an ids array" });
|
|
986
|
+
}
|
|
987
|
+
if (typeof body.patch !== "object" || body.patch === null || Array.isArray(body.patch)) {
|
|
988
|
+
throw new api_1.APIError({ status: 400, title: "Request body must include a patch object" });
|
|
989
|
+
}
|
|
990
|
+
ids = __spreadArray([], __read(new Set(body.ids.map(function (id) { return String(id); }))), false);
|
|
991
|
+
if (ids.length === 0) {
|
|
992
|
+
throw new api_1.APIError({ status: 400, title: "ids must include at least one id" });
|
|
993
|
+
}
|
|
994
|
+
if (ids.length > adminUiV2_1.MAX_BULK_PATCH_IDS) {
|
|
995
|
+
throw new api_1.APIError({
|
|
996
|
+
status: 400,
|
|
997
|
+
title: "At most ".concat(adminUiV2_1.MAX_BULK_PATCH_IDS, " ids may be patched at once"),
|
|
998
|
+
});
|
|
999
|
+
}
|
|
1000
|
+
rawPatch = body.patch;
|
|
1001
|
+
unknownKeys = Object.keys(rawPatch).filter(function (key) { return !allowlist.has(key); });
|
|
1002
|
+
if (unknownKeys.length > 0) {
|
|
1003
|
+
throw new api_1.APIError({
|
|
1004
|
+
detail: unknownKeys.join(", "),
|
|
1005
|
+
status: 400,
|
|
1006
|
+
title: "Patch contains keys that are not allowlisted for bulk patch",
|
|
1007
|
+
});
|
|
1008
|
+
}
|
|
1009
|
+
patch = stripProtectedFromBody(rawPatch);
|
|
1010
|
+
if (Object.keys(patch).length === 0) {
|
|
1011
|
+
throw new api_1.APIError({
|
|
1012
|
+
status: 400,
|
|
1013
|
+
title: "Patch must include at least one writable field",
|
|
1014
|
+
});
|
|
1015
|
+
}
|
|
1016
|
+
updated = 0;
|
|
1017
|
+
failures = [];
|
|
1018
|
+
_c.label = 2;
|
|
1019
|
+
case 2:
|
|
1020
|
+
_c.trys.push([2, 9, 10, 11]);
|
|
1021
|
+
ids_1 = __values(ids), ids_1_1 = ids_1.next();
|
|
1022
|
+
_c.label = 3;
|
|
1023
|
+
case 3:
|
|
1024
|
+
if (!!ids_1_1.done) return [3 /*break*/, 8];
|
|
1025
|
+
id = ids_1_1.value;
|
|
1026
|
+
if (!mongoose_1.default.isValidObjectId(id)) {
|
|
1027
|
+
failures.push({ id: id, title: "Invalid id" });
|
|
1028
|
+
return [3 /*break*/, 7];
|
|
1029
|
+
}
|
|
1030
|
+
_c.label = 4;
|
|
1031
|
+
case 4:
|
|
1032
|
+
_c.trys.push([4, 6, , 7]);
|
|
1033
|
+
return [4 /*yield*/, config.model.updateOne({ _id: id }, { $set: patch })];
|
|
1034
|
+
case 5:
|
|
1035
|
+
resUpdate = _c.sent();
|
|
1036
|
+
if (resUpdate.matchedCount === 0) {
|
|
1037
|
+
failures.push({ id: id, title: "Not found" });
|
|
1038
|
+
}
|
|
1039
|
+
else {
|
|
1040
|
+
updated += 1;
|
|
1041
|
+
}
|
|
1042
|
+
return [3 /*break*/, 7];
|
|
1043
|
+
case 6:
|
|
1044
|
+
err_4 = _c.sent();
|
|
1045
|
+
message = err_4 instanceof Error ? err_4.message : String(err_4);
|
|
1046
|
+
failures.push({ id: id, title: message });
|
|
1047
|
+
return [3 /*break*/, 7];
|
|
1048
|
+
case 7:
|
|
1049
|
+
ids_1_1 = ids_1.next();
|
|
1050
|
+
return [3 /*break*/, 3];
|
|
1051
|
+
case 8: return [3 /*break*/, 11];
|
|
1052
|
+
case 9:
|
|
1053
|
+
e_14_1 = _c.sent();
|
|
1054
|
+
e_14 = { error: e_14_1 };
|
|
1055
|
+
return [3 /*break*/, 11];
|
|
1056
|
+
case 10:
|
|
1057
|
+
try {
|
|
1058
|
+
if (ids_1_1 && !ids_1_1.done && (_a = ids_1.return)) _a.call(ids_1);
|
|
1059
|
+
}
|
|
1060
|
+
finally { if (e_14) throw e_14.error; }
|
|
1061
|
+
return [7 /*endfinally*/];
|
|
1062
|
+
case 11: return [2 /*return*/, res.json({ failures: failures.length > 0 ? failures : undefined, updated: updated })];
|
|
1063
|
+
}
|
|
1064
|
+
});
|
|
1065
|
+
}); })], false));
|
|
1066
|
+
modelBase.use((0, api_1.modelRouter)(config.model, routerOptions));
|
|
1067
|
+
app.use("".concat(basePath).concat(config.routePath), modelBase);
|
|
580
1068
|
};
|
|
581
1069
|
try {
|
|
582
1070
|
// Mount modelRouter for each model with IsAdmin permissions
|
|
@@ -585,12 +1073,12 @@ var AdminApp = /** @class */ (function () {
|
|
|
585
1073
|
_loop_2(config);
|
|
586
1074
|
}
|
|
587
1075
|
}
|
|
588
|
-
catch (
|
|
1076
|
+
catch (e_5_1) { e_5 = { error: e_5_1 }; }
|
|
589
1077
|
finally {
|
|
590
1078
|
try {
|
|
591
1079
|
if (modelConfigs_2_1 && !modelConfigs_2_1.done && (_b = modelConfigs_2.return)) _b.call(modelConfigs_2);
|
|
592
1080
|
}
|
|
593
|
-
finally { if (
|
|
1081
|
+
finally { if (e_5) throw e_5.error; }
|
|
594
1082
|
}
|
|
595
1083
|
// Mount script routes
|
|
596
1084
|
if (scriptConfigs.length > 0) {
|
|
@@ -603,9 +1091,58 @@ var AdminApp = /** @class */ (function () {
|
|
|
603
1091
|
AdminApp.prototype.mountScriptRoutes = function (router, scripts) {
|
|
604
1092
|
var _this = this;
|
|
605
1093
|
var scriptsByName = new Map(scripts.map(function (s) { return [s.name, s]; }));
|
|
1094
|
+
var scriptNames = scripts.map(function (s) { return s.name; });
|
|
1095
|
+
// GET /admin/scripts/runs — Paginated history of script runs (BackgroundTasks)
|
|
1096
|
+
router.get("/runs", (0, api_1.asyncHandler)(function (req, res) { return __awaiter(_this, void 0, void 0, function () {
|
|
1097
|
+
var user, limit, page, skip, requestedName, taskTypeFilter, query, _a, tasks, total, data;
|
|
1098
|
+
return __generator(this, function (_b) {
|
|
1099
|
+
switch (_b.label) {
|
|
1100
|
+
case 0:
|
|
1101
|
+
user = req.user;
|
|
1102
|
+
if (!(user === null || user === void 0 ? void 0 : user.admin)) {
|
|
1103
|
+
throw new api_1.APIError({ status: 403, title: "Only admins can view run history" });
|
|
1104
|
+
}
|
|
1105
|
+
limit = Math.min(Math.max(Number(req.query.limit) || 25, 1), 100);
|
|
1106
|
+
page = Math.max(Number(req.query.page) || 1, 1);
|
|
1107
|
+
skip = (page - 1) * limit;
|
|
1108
|
+
requestedName = typeof req.query.name === "string" ? req.query.name : undefined;
|
|
1109
|
+
if (requestedName !== undefined) {
|
|
1110
|
+
taskTypeFilter = scriptNames.includes(requestedName) ? [requestedName] : [];
|
|
1111
|
+
}
|
|
1112
|
+
else {
|
|
1113
|
+
taskTypeFilter = scriptNames;
|
|
1114
|
+
}
|
|
1115
|
+
query = taskTypeFilter.length > 0
|
|
1116
|
+
? { taskType: { $in: taskTypeFilter } }
|
|
1117
|
+
: { taskType: { $in: [] } };
|
|
1118
|
+
return [4 /*yield*/, Promise.all([
|
|
1119
|
+
api_1.BackgroundTask.find(query)
|
|
1120
|
+
.sort({ created: -1 })
|
|
1121
|
+
.skip(skip)
|
|
1122
|
+
.limit(limit)
|
|
1123
|
+
.populate({ path: "createdBy", select: "name email" })
|
|
1124
|
+
.lean(),
|
|
1125
|
+
api_1.BackgroundTask.countDocuments(query),
|
|
1126
|
+
])];
|
|
1127
|
+
case 1:
|
|
1128
|
+
_a = __read.apply(void 0, [_b.sent(), 2]), tasks = _a[0], total = _a[1];
|
|
1129
|
+
data = tasks.map(function (task) {
|
|
1130
|
+
var _a;
|
|
1131
|
+
var createdBy = task.createdBy;
|
|
1132
|
+
var createdByName = createdBy &&
|
|
1133
|
+
typeof createdBy === "object" &&
|
|
1134
|
+
!(createdBy instanceof mongoose_1.default.Types.ObjectId)
|
|
1135
|
+
? ((_a = createdBy.name) !== null && _a !== void 0 ? _a : createdBy.email)
|
|
1136
|
+
: undefined;
|
|
1137
|
+
return __assign(__assign({}, task), { createdByName: createdByName });
|
|
1138
|
+
});
|
|
1139
|
+
return [2 /*return*/, res.json({ data: data, limit: limit, more: skip + tasks.length < total, page: page, total: total })];
|
|
1140
|
+
}
|
|
1141
|
+
});
|
|
1142
|
+
}); }));
|
|
606
1143
|
// POST /admin/scripts/:name/run — Execute a script
|
|
607
1144
|
router.post("/:name/run", (0, api_1.asyncHandler)(function (req, res) { return __awaiter(_this, void 0, void 0, function () {
|
|
608
|
-
var user, script, isWetRun, now, task,
|
|
1145
|
+
var user, script, isWetRun, now, task, err_5, detail, ctx;
|
|
609
1146
|
var _this = this;
|
|
610
1147
|
var _a;
|
|
611
1148
|
return __generator(this, function (_b) {
|
|
@@ -639,8 +1176,8 @@ var AdminApp = /** @class */ (function () {
|
|
|
639
1176
|
task = (_b.sent());
|
|
640
1177
|
return [3 /*break*/, 4];
|
|
641
1178
|
case 3:
|
|
642
|
-
|
|
643
|
-
detail =
|
|
1179
|
+
err_5 = _b.sent();
|
|
1180
|
+
detail = err_5 instanceof Error ? err_5.message : String(err_5);
|
|
644
1181
|
api_1.logger.error("Failed to create background task for ".concat(script.name, ": ").concat(detail));
|
|
645
1182
|
throw new api_1.APIError({
|
|
646
1183
|
detail: detail,
|
|
@@ -695,7 +1232,7 @@ var AdminApp = /** @class */ (function () {
|
|
|
695
1232
|
// Run the script asynchronously — use atomic updates to avoid overwriting
|
|
696
1233
|
// cancellation or other intermediate state changes.
|
|
697
1234
|
void (function () { return __awaiter(_this, void 0, void 0, function () {
|
|
698
|
-
var result,
|
|
1235
|
+
var result, err_6, message;
|
|
699
1236
|
return __generator(this, function (_a) {
|
|
700
1237
|
switch (_a.label) {
|
|
701
1238
|
case 0:
|
|
@@ -717,11 +1254,11 @@ var AdminApp = /** @class */ (function () {
|
|
|
717
1254
|
_a.sent();
|
|
718
1255
|
return [3 /*break*/, 5];
|
|
719
1256
|
case 3:
|
|
720
|
-
|
|
721
|
-
if (
|
|
1257
|
+
err_6 = _a.sent();
|
|
1258
|
+
if (err_6 instanceof api_1.TaskCancelledError) {
|
|
722
1259
|
return [2 /*return*/];
|
|
723
1260
|
}
|
|
724
|
-
message =
|
|
1261
|
+
message = err_6 instanceof Error ? err_6.message : String(err_6);
|
|
725
1262
|
api_1.logger.error("Script ".concat(script.name, " failed: ").concat(message));
|
|
726
1263
|
// Atomically update only if still running
|
|
727
1264
|
return [4 /*yield*/, api_1.BackgroundTask.findOneAndUpdate({ _id: task._id, status: "running" }, {
|
|
@@ -746,7 +1283,7 @@ var AdminApp = /** @class */ (function () {
|
|
|
746
1283
|
}); }));
|
|
747
1284
|
// GET /admin/scripts/tasks/:id — Poll task status
|
|
748
1285
|
router.get("/tasks/:id", (0, api_1.asyncHandler)(function (req, res) { return __awaiter(_this, void 0, void 0, function () {
|
|
749
|
-
var user, task,
|
|
1286
|
+
var user, task, err_7, detail;
|
|
750
1287
|
return __generator(this, function (_a) {
|
|
751
1288
|
switch (_a.label) {
|
|
752
1289
|
case 0:
|
|
@@ -762,8 +1299,8 @@ var AdminApp = /** @class */ (function () {
|
|
|
762
1299
|
task = _a.sent();
|
|
763
1300
|
return [3 /*break*/, 4];
|
|
764
1301
|
case 3:
|
|
765
|
-
|
|
766
|
-
detail =
|
|
1302
|
+
err_7 = _a.sent();
|
|
1303
|
+
detail = err_7 instanceof Error ? err_7.message : String(err_7);
|
|
767
1304
|
throw new api_1.APIError({ detail: detail, status: 400, title: "Invalid task ID" });
|
|
768
1305
|
case 4:
|
|
769
1306
|
if (!task) {
|
|
@@ -775,7 +1312,7 @@ var AdminApp = /** @class */ (function () {
|
|
|
775
1312
|
}); }));
|
|
776
1313
|
// DELETE /admin/scripts/tasks/:id — Cancel a running task
|
|
777
1314
|
router.delete("/tasks/:id", (0, api_1.asyncHandler)(function (req, res) { return __awaiter(_this, void 0, void 0, function () {
|
|
778
|
-
var user, task,
|
|
1315
|
+
var user, task, err_8, detail, cancelled;
|
|
779
1316
|
var _a;
|
|
780
1317
|
return __generator(this, function (_b) {
|
|
781
1318
|
switch (_b.label) {
|
|
@@ -792,8 +1329,8 @@ var AdminApp = /** @class */ (function () {
|
|
|
792
1329
|
task = _b.sent();
|
|
793
1330
|
return [3 /*break*/, 4];
|
|
794
1331
|
case 3:
|
|
795
|
-
|
|
796
|
-
detail =
|
|
1332
|
+
err_8 = _b.sent();
|
|
1333
|
+
detail = err_8 instanceof Error ? err_8.message : String(err_8);
|
|
797
1334
|
throw new api_1.APIError({ detail: detail, status: 400, title: "Invalid task ID" });
|
|
798
1335
|
case 4:
|
|
799
1336
|
if (!task) {
|