@terreno/admin-backend 0.9.3 → 0.10.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/dist/adminApp.js +158 -34
- package/package.json +2 -2
- package/src/adminApp.ts +87 -2
package/dist/adminApp.js
CHANGED
|
@@ -81,6 +81,7 @@ exports.AdminApp = void 0;
|
|
|
81
81
|
var api_1 = require("@terreno/api");
|
|
82
82
|
var express_1 = __importDefault(require("express"));
|
|
83
83
|
var luxon_1 = require("luxon");
|
|
84
|
+
var mongoose_1 = __importDefault(require("mongoose"));
|
|
84
85
|
var toPlainObject = function (value) {
|
|
85
86
|
if (value && typeof value === "object" && typeof value.toObject === "function") {
|
|
86
87
|
return value.toObject();
|
|
@@ -121,13 +122,15 @@ var extractFieldMeta = function (properties, required) {
|
|
|
121
122
|
try {
|
|
122
123
|
for (var _e = __values(Object.entries(properties)), _f = _e.next(); !_f.done; _f = _e.next()) {
|
|
123
124
|
var _g = __read(_f.value, 2), key = _g[0], prop = _g[1];
|
|
125
|
+
var fieldType = (_b = prop.type) !== null && _b !== void 0 ? _b : "string";
|
|
124
126
|
fields[key] = {
|
|
125
127
|
default: prop.default,
|
|
126
128
|
description: prop.description,
|
|
127
129
|
enum: prop.enum,
|
|
128
130
|
ref: prop.$ref ? prop.$ref.replace("#/components/schemas/", "") : undefined,
|
|
129
131
|
required: required.includes(key),
|
|
130
|
-
|
|
132
|
+
searchable: fieldType === "string" && !prop.enum,
|
|
133
|
+
type: fieldType,
|
|
131
134
|
};
|
|
132
135
|
// For array fields, extract item sub-field metadata
|
|
133
136
|
if (prop.type === "array" && ((_c = prop.items) === null || _c === void 0 ? void 0 : _c.properties)) {
|
|
@@ -215,14 +218,14 @@ var AdminApp = /** @class */ (function () {
|
|
|
215
218
|
* @param app - The Express application instance to register with
|
|
216
219
|
*/
|
|
217
220
|
AdminApp.prototype.register = function (app, openApi) {
|
|
218
|
-
var e_3, _a;
|
|
221
|
+
var e_3, _a, e_4, _b;
|
|
219
222
|
var _this = this;
|
|
220
|
-
var
|
|
221
|
-
var basePath = (
|
|
223
|
+
var _c, _d, _e, _f;
|
|
224
|
+
var basePath = (_c = this.options.basePath) !== null && _c !== void 0 ? _c : "/admin";
|
|
222
225
|
var modelConfigs = this.options.models;
|
|
223
226
|
// Build config response with field metadata from Mongoose schemas
|
|
224
227
|
var configModels = modelConfigs.map(function (config) {
|
|
225
|
-
var
|
|
228
|
+
var e_5, _a, e_6, _b;
|
|
226
229
|
var _c, _d, _e;
|
|
227
230
|
var _f = (0, api_1.getOpenApiSpecForModel)(config.model), properties = _f.properties, required = _f.required;
|
|
228
231
|
var hiddenFieldSet = new Set((_c = config.hiddenFields) !== null && _c !== void 0 ? _c : []);
|
|
@@ -249,12 +252,12 @@ var AdminApp = /** @class */ (function () {
|
|
|
249
252
|
}
|
|
250
253
|
}
|
|
251
254
|
}
|
|
252
|
-
catch (
|
|
255
|
+
catch (e_5_1) { e_5 = { error: e_5_1 }; }
|
|
253
256
|
finally {
|
|
254
257
|
try {
|
|
255
258
|
if (_h && !_h.done && (_a = _g.return)) _a.call(_g);
|
|
256
259
|
}
|
|
257
|
-
finally { if (
|
|
260
|
+
finally { if (e_5) throw e_5.error; }
|
|
258
261
|
}
|
|
259
262
|
// Apply field overrides (e.g., widget: "markdown")
|
|
260
263
|
if (config.fieldOverrides) {
|
|
@@ -266,12 +269,12 @@ var AdminApp = /** @class */ (function () {
|
|
|
266
269
|
}
|
|
267
270
|
}
|
|
268
271
|
}
|
|
269
|
-
catch (
|
|
272
|
+
catch (e_6_1) { e_6 = { error: e_6_1 }; }
|
|
270
273
|
finally {
|
|
271
274
|
try {
|
|
272
275
|
if (_l && !_l.done && (_b = _k.return)) _b.call(_k);
|
|
273
276
|
}
|
|
274
|
-
finally { if (
|
|
277
|
+
finally { if (e_6) throw e_6.error; }
|
|
275
278
|
}
|
|
276
279
|
}
|
|
277
280
|
return {
|
|
@@ -285,7 +288,7 @@ var AdminApp = /** @class */ (function () {
|
|
|
285
288
|
};
|
|
286
289
|
});
|
|
287
290
|
// Build script metadata for config response
|
|
288
|
-
var scriptConfigs = (
|
|
291
|
+
var scriptConfigs = (_d = this.options.scripts) !== null && _d !== void 0 ? _d : [];
|
|
289
292
|
var configScripts = scriptConfigs.map(function (script) { return ({
|
|
290
293
|
description: script.description,
|
|
291
294
|
name: script.name,
|
|
@@ -342,7 +345,7 @@ var AdminApp = /** @class */ (function () {
|
|
|
342
345
|
}); }));
|
|
343
346
|
app.put(versionConfigPath, (0, api_1.authenticateMiddleware)(), (0, api_1.asyncHandler)(function (req, res) { return __awaiter(_this, void 0, void 0, function () {
|
|
344
347
|
var raw, allowedFields, setFields, unsetFields, allowedFields_1, allowedFields_1_1, field, updateOp, doc;
|
|
345
|
-
var
|
|
348
|
+
var e_7, _a;
|
|
346
349
|
return __generator(this, function (_b) {
|
|
347
350
|
switch (_b.label) {
|
|
348
351
|
case 0: return [4 /*yield*/, (0, api_1.checkPermissions)("update", [api_1.Permissions.IsAdmin], req.user)];
|
|
@@ -373,12 +376,12 @@ var AdminApp = /** @class */ (function () {
|
|
|
373
376
|
}
|
|
374
377
|
}
|
|
375
378
|
}
|
|
376
|
-
catch (
|
|
379
|
+
catch (e_7_1) { e_7 = { error: e_7_1 }; }
|
|
377
380
|
finally {
|
|
378
381
|
try {
|
|
379
382
|
if (allowedFields_1_1 && !allowedFields_1_1.done && (_a = allowedFields_1.return)) _a.call(allowedFields_1);
|
|
380
383
|
}
|
|
381
|
-
finally { if (
|
|
384
|
+
finally { if (e_7) throw e_7.error; }
|
|
382
385
|
}
|
|
383
386
|
updateOp = {};
|
|
384
387
|
if (Object.keys(setFields).length > 0) {
|
|
@@ -400,7 +403,128 @@ var AdminApp = /** @class */ (function () {
|
|
|
400
403
|
});
|
|
401
404
|
}); }));
|
|
402
405
|
var _loop_1 = function (config) {
|
|
403
|
-
var
|
|
406
|
+
var e_8, _g;
|
|
407
|
+
// Determine searchable fields from the actual Mongoose schema type,
|
|
408
|
+
// not the OpenAPI type (which reports ObjectId as "string")
|
|
409
|
+
var searchableFields = [];
|
|
410
|
+
var objectIdFields = [];
|
|
411
|
+
var modelMeta = configModels.find(function (m) { return m.name === config.model.modelName; });
|
|
412
|
+
if (modelMeta) {
|
|
413
|
+
try {
|
|
414
|
+
for (var _h = (e_8 = void 0, __values(Object.keys(modelMeta.fields))), _j = _h.next(); !_j.done; _j = _h.next()) {
|
|
415
|
+
var key = _j.value;
|
|
416
|
+
var schemaPath = config.model.schema.path(key);
|
|
417
|
+
if (schemaPath && schemaPath.instance === "String" && !modelMeta.fields[key].enum) {
|
|
418
|
+
searchableFields.push(key);
|
|
419
|
+
}
|
|
420
|
+
else if (schemaPath && schemaPath.instance === "ObjectID") {
|
|
421
|
+
objectIdFields.push(key);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
catch (e_8_1) { e_8 = { error: e_8_1 }; }
|
|
426
|
+
finally {
|
|
427
|
+
try {
|
|
428
|
+
if (_j && !_j.done && (_g = _h.return)) _g.call(_h);
|
|
429
|
+
}
|
|
430
|
+
finally { if (e_8) throw e_8.error; }
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
api_1.logger.info("Admin search fields for ".concat(config.model.modelName), {
|
|
434
|
+
objectIdFields: objectIdFields,
|
|
435
|
+
searchableFields: searchableFields,
|
|
436
|
+
});
|
|
437
|
+
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 () {
|
|
438
|
+
var q, escapedQ, regex, fields, orConditions, objectIdFields_1, objectIdFields_1_1, field, results, err_1;
|
|
439
|
+
var e_9, _a, _b;
|
|
440
|
+
var _c, _d;
|
|
441
|
+
return __generator(this, function (_e) {
|
|
442
|
+
switch (_e.label) {
|
|
443
|
+
case 0:
|
|
444
|
+
if (!((_c = req.user) === null || _c === void 0 ? void 0 : _c.admin)) {
|
|
445
|
+
throw new api_1.APIError({
|
|
446
|
+
disableExternalErrorTracking: true,
|
|
447
|
+
status: 403,
|
|
448
|
+
title: "Forbidden",
|
|
449
|
+
});
|
|
450
|
+
}
|
|
451
|
+
q = String((_d = req.query.q) !== null && _d !== void 0 ? _d : "");
|
|
452
|
+
if (!q) {
|
|
453
|
+
return [2 /*return*/, res.json({ data: [] })];
|
|
454
|
+
}
|
|
455
|
+
escapedQ = q.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
456
|
+
regex = new RegExp(escapedQ, "i");
|
|
457
|
+
fields = typeof req.query.fields === "string"
|
|
458
|
+
? req.query.fields.split(",").filter(function (f) { return searchableFields.includes(f); })
|
|
459
|
+
: searchableFields;
|
|
460
|
+
orConditions = fields.map(function (field) {
|
|
461
|
+
var _a;
|
|
462
|
+
return (_a = {}, _a[field] = { $regex: regex }, _a);
|
|
463
|
+
});
|
|
464
|
+
// If the query is a valid ObjectId, also match against ObjectId fields
|
|
465
|
+
if (mongoose_1.default.isValidObjectId(q)) {
|
|
466
|
+
try {
|
|
467
|
+
for (objectIdFields_1 = __values(objectIdFields), objectIdFields_1_1 = objectIdFields_1.next(); !objectIdFields_1_1.done; objectIdFields_1_1 = objectIdFields_1.next()) {
|
|
468
|
+
field = objectIdFields_1_1.value;
|
|
469
|
+
orConditions.push((_b = {}, _b[field] = new mongoose_1.default.Types.ObjectId(q), _b));
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
catch (e_9_1) { e_9 = { error: e_9_1 }; }
|
|
473
|
+
finally {
|
|
474
|
+
try {
|
|
475
|
+
if (objectIdFields_1_1 && !objectIdFields_1_1.done && (_a = objectIdFields_1.return)) _a.call(objectIdFields_1);
|
|
476
|
+
}
|
|
477
|
+
finally { if (e_9) throw e_9.error; }
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
if (orConditions.length === 0) {
|
|
481
|
+
return [2 /*return*/, res.json({ data: [] })];
|
|
482
|
+
}
|
|
483
|
+
api_1.logger.debug("Admin search query", {
|
|
484
|
+
fields: fields,
|
|
485
|
+
model: config.model.modelName,
|
|
486
|
+
q: q,
|
|
487
|
+
});
|
|
488
|
+
_e.label = 1;
|
|
489
|
+
case 1:
|
|
490
|
+
_e.trys.push([1, 3, , 4]);
|
|
491
|
+
return [4 /*yield*/, config.model.find({ $or: orConditions }).limit(20).lean()];
|
|
492
|
+
case 2:
|
|
493
|
+
results = _e.sent();
|
|
494
|
+
api_1.logger.debug("Admin search results", {
|
|
495
|
+
count: results.length,
|
|
496
|
+
model: config.model.modelName,
|
|
497
|
+
});
|
|
498
|
+
return [2 /*return*/, res.json({ data: results })];
|
|
499
|
+
case 3:
|
|
500
|
+
err_1 = _e.sent();
|
|
501
|
+
api_1.logger.error("Admin search failed", {
|
|
502
|
+
error: err_1,
|
|
503
|
+
fields: fields,
|
|
504
|
+
model: config.model.modelName,
|
|
505
|
+
});
|
|
506
|
+
throw err_1;
|
|
507
|
+
case 4: return [2 /*return*/];
|
|
508
|
+
}
|
|
509
|
+
});
|
|
510
|
+
}); }));
|
|
511
|
+
};
|
|
512
|
+
try {
|
|
513
|
+
// Mount search endpoint for each model
|
|
514
|
+
for (var modelConfigs_1 = __values(modelConfigs), modelConfigs_1_1 = modelConfigs_1.next(); !modelConfigs_1_1.done; modelConfigs_1_1 = modelConfigs_1.next()) {
|
|
515
|
+
var config = modelConfigs_1_1.value;
|
|
516
|
+
_loop_1(config);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
520
|
+
finally {
|
|
521
|
+
try {
|
|
522
|
+
if (modelConfigs_1_1 && !modelConfigs_1_1.done && (_a = modelConfigs_1.return)) _a.call(modelConfigs_1);
|
|
523
|
+
}
|
|
524
|
+
finally { if (e_3) throw e_3.error; }
|
|
525
|
+
}
|
|
526
|
+
var _loop_2 = function (config) {
|
|
527
|
+
var hiddenFieldSet = new Set((_e = config.hiddenFields) !== null && _e !== void 0 ? _e : []);
|
|
404
528
|
var routerOptions = __assign(__assign({}, (openApi ? { openApi: openApi } : {})), { permissions: {
|
|
405
529
|
create: [api_1.Permissions.IsAdmin],
|
|
406
530
|
delete: [api_1.Permissions.IsAdmin],
|
|
@@ -411,22 +535,22 @@ var AdminApp = /** @class */ (function () {
|
|
|
411
535
|
? function (value, _method, _request, _options) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
|
|
412
536
|
return [2 /*return*/, removeHiddenFields(value, hiddenFieldSet)];
|
|
413
537
|
}); }); }
|
|
414
|
-
: undefined, sort: (
|
|
538
|
+
: undefined, sort: (_f = config.defaultSort) !== null && _f !== void 0 ? _f : "-created" });
|
|
415
539
|
app.use("".concat(basePath).concat(config.routePath), (0, api_1.modelRouter)(config.model, routerOptions));
|
|
416
540
|
};
|
|
417
541
|
try {
|
|
418
542
|
// Mount modelRouter for each model with IsAdmin permissions
|
|
419
|
-
for (var
|
|
420
|
-
var config =
|
|
421
|
-
|
|
543
|
+
for (var modelConfigs_2 = __values(modelConfigs), modelConfigs_2_1 = modelConfigs_2.next(); !modelConfigs_2_1.done; modelConfigs_2_1 = modelConfigs_2.next()) {
|
|
544
|
+
var config = modelConfigs_2_1.value;
|
|
545
|
+
_loop_2(config);
|
|
422
546
|
}
|
|
423
547
|
}
|
|
424
|
-
catch (
|
|
548
|
+
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
425
549
|
finally {
|
|
426
550
|
try {
|
|
427
|
-
if (
|
|
551
|
+
if (modelConfigs_2_1 && !modelConfigs_2_1.done && (_b = modelConfigs_2.return)) _b.call(modelConfigs_2);
|
|
428
552
|
}
|
|
429
|
-
finally { if (
|
|
553
|
+
finally { if (e_4) throw e_4.error; }
|
|
430
554
|
}
|
|
431
555
|
// Mount script routes
|
|
432
556
|
if (scriptConfigs.length > 0) {
|
|
@@ -441,7 +565,7 @@ var AdminApp = /** @class */ (function () {
|
|
|
441
565
|
var scriptsByName = new Map(scripts.map(function (s) { return [s.name, s]; }));
|
|
442
566
|
// POST /admin/scripts/:name/run — Execute a script
|
|
443
567
|
router.post("/:name/run", (0, api_1.asyncHandler)(function (req, res) { return __awaiter(_this, void 0, void 0, function () {
|
|
444
|
-
var user, script, isWetRun, now, task,
|
|
568
|
+
var user, script, isWetRun, now, task, err_2, detail, ctx;
|
|
445
569
|
var _this = this;
|
|
446
570
|
var _a;
|
|
447
571
|
return __generator(this, function (_b) {
|
|
@@ -475,8 +599,8 @@ var AdminApp = /** @class */ (function () {
|
|
|
475
599
|
task = (_b.sent());
|
|
476
600
|
return [3 /*break*/, 4];
|
|
477
601
|
case 3:
|
|
478
|
-
|
|
479
|
-
detail =
|
|
602
|
+
err_2 = _b.sent();
|
|
603
|
+
detail = err_2 instanceof Error ? err_2.message : String(err_2);
|
|
480
604
|
api_1.logger.error("Failed to create background task for ".concat(script.name, ": ").concat(detail));
|
|
481
605
|
throw new api_1.APIError({
|
|
482
606
|
detail: detail,
|
|
@@ -531,7 +655,7 @@ var AdminApp = /** @class */ (function () {
|
|
|
531
655
|
// Run the script asynchronously — use atomic updates to avoid overwriting
|
|
532
656
|
// cancellation or other intermediate state changes.
|
|
533
657
|
void (function () { return __awaiter(_this, void 0, void 0, function () {
|
|
534
|
-
var result,
|
|
658
|
+
var result, err_3, message;
|
|
535
659
|
return __generator(this, function (_a) {
|
|
536
660
|
switch (_a.label) {
|
|
537
661
|
case 0:
|
|
@@ -553,11 +677,11 @@ var AdminApp = /** @class */ (function () {
|
|
|
553
677
|
_a.sent();
|
|
554
678
|
return [3 /*break*/, 5];
|
|
555
679
|
case 3:
|
|
556
|
-
|
|
557
|
-
if (
|
|
680
|
+
err_3 = _a.sent();
|
|
681
|
+
if (err_3 instanceof api_1.TaskCancelledError) {
|
|
558
682
|
return [2 /*return*/];
|
|
559
683
|
}
|
|
560
|
-
message =
|
|
684
|
+
message = err_3 instanceof Error ? err_3.message : String(err_3);
|
|
561
685
|
api_1.logger.error("Script ".concat(script.name, " failed: ").concat(message));
|
|
562
686
|
// Atomically update only if still running
|
|
563
687
|
return [4 /*yield*/, api_1.BackgroundTask.findOneAndUpdate({ _id: task._id, status: "running" }, {
|
|
@@ -582,7 +706,7 @@ var AdminApp = /** @class */ (function () {
|
|
|
582
706
|
}); }));
|
|
583
707
|
// GET /admin/scripts/tasks/:id — Poll task status
|
|
584
708
|
router.get("/tasks/:id", (0, api_1.asyncHandler)(function (req, res) { return __awaiter(_this, void 0, void 0, function () {
|
|
585
|
-
var user, task,
|
|
709
|
+
var user, task, err_4, detail;
|
|
586
710
|
return __generator(this, function (_a) {
|
|
587
711
|
switch (_a.label) {
|
|
588
712
|
case 0:
|
|
@@ -598,8 +722,8 @@ var AdminApp = /** @class */ (function () {
|
|
|
598
722
|
task = _a.sent();
|
|
599
723
|
return [3 /*break*/, 4];
|
|
600
724
|
case 3:
|
|
601
|
-
|
|
602
|
-
detail =
|
|
725
|
+
err_4 = _a.sent();
|
|
726
|
+
detail = err_4 instanceof Error ? err_4.message : String(err_4);
|
|
603
727
|
throw new api_1.APIError({ detail: detail, status: 400, title: "Invalid task ID" });
|
|
604
728
|
case 4:
|
|
605
729
|
if (!task) {
|
|
@@ -611,7 +735,7 @@ var AdminApp = /** @class */ (function () {
|
|
|
611
735
|
}); }));
|
|
612
736
|
// DELETE /admin/scripts/tasks/:id — Cancel a running task
|
|
613
737
|
router.delete("/tasks/:id", (0, api_1.asyncHandler)(function (req, res) { return __awaiter(_this, void 0, void 0, function () {
|
|
614
|
-
var user, task,
|
|
738
|
+
var user, task, err_5, detail, cancelled;
|
|
615
739
|
var _a;
|
|
616
740
|
return __generator(this, function (_b) {
|
|
617
741
|
switch (_b.label) {
|
|
@@ -628,8 +752,8 @@ var AdminApp = /** @class */ (function () {
|
|
|
628
752
|
task = _b.sent();
|
|
629
753
|
return [3 /*break*/, 4];
|
|
630
754
|
case 3:
|
|
631
|
-
|
|
632
|
-
detail =
|
|
755
|
+
err_5 = _b.sent();
|
|
756
|
+
detail = err_5 instanceof Error ? err_5.message : String(err_5);
|
|
633
757
|
throw new api_1.APIError({ detail: detail, status: 400, title: "Invalid task ID" });
|
|
634
758
|
case 4:
|
|
635
759
|
if (!task) {
|
package/package.json
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
},
|
|
6
6
|
"dependencies": {
|
|
7
7
|
"@google-cloud/storage": "^7.15.0",
|
|
8
|
-
"@terreno/api": "0.
|
|
8
|
+
"@terreno/api": "0.10.0",
|
|
9
9
|
"express": "^5.2.1",
|
|
10
10
|
"luxon": "^3.7.2",
|
|
11
11
|
"multer": "^1.4.5-lts.1"
|
|
@@ -52,5 +52,5 @@
|
|
|
52
52
|
"test:ci": "bun test"
|
|
53
53
|
},
|
|
54
54
|
"types": "dist/index.d.ts",
|
|
55
|
-
"version": "0.
|
|
55
|
+
"version": "0.10.0"
|
|
56
56
|
}
|
package/src/adminApp.ts
CHANGED
|
@@ -19,8 +19,8 @@ import {
|
|
|
19
19
|
} from "@terreno/api";
|
|
20
20
|
import express from "express";
|
|
21
21
|
import {DateTime} from "luxon";
|
|
22
|
-
import type mongoose from "mongoose";
|
|
23
22
|
import type {Model} from "mongoose";
|
|
23
|
+
import mongoose from "mongoose";
|
|
24
24
|
|
|
25
25
|
/**
|
|
26
26
|
* Configuration for a single model in the admin panel.
|
|
@@ -80,6 +80,7 @@ interface AdminFieldMeta {
|
|
|
80
80
|
enum?: string[];
|
|
81
81
|
default?: any;
|
|
82
82
|
ref?: string;
|
|
83
|
+
searchable?: boolean;
|
|
83
84
|
widget?: string;
|
|
84
85
|
/** For array fields: metadata about each item's sub-fields */
|
|
85
86
|
items?: Record<string, AdminFieldMeta>;
|
|
@@ -136,13 +137,15 @@ const extractFieldMeta = (
|
|
|
136
137
|
): Record<string, AdminFieldMeta> => {
|
|
137
138
|
const fields: Record<string, AdminFieldMeta> = {};
|
|
138
139
|
for (const [key, prop] of Object.entries(properties)) {
|
|
140
|
+
const fieldType = prop.type ?? "string";
|
|
139
141
|
fields[key] = {
|
|
140
142
|
default: prop.default,
|
|
141
143
|
description: prop.description,
|
|
142
144
|
enum: prop.enum,
|
|
143
145
|
ref: prop.$ref ? prop.$ref.replace("#/components/schemas/", "") : undefined,
|
|
144
146
|
required: required.includes(key),
|
|
145
|
-
|
|
147
|
+
searchable: fieldType === "string" && !prop.enum,
|
|
148
|
+
type: fieldType,
|
|
146
149
|
};
|
|
147
150
|
|
|
148
151
|
// For array fields, extract item sub-field metadata
|
|
@@ -371,6 +374,88 @@ export class AdminApp {
|
|
|
371
374
|
})
|
|
372
375
|
);
|
|
373
376
|
|
|
377
|
+
// Mount search endpoint for each model
|
|
378
|
+
for (const config of modelConfigs) {
|
|
379
|
+
// Determine searchable fields from the actual Mongoose schema type,
|
|
380
|
+
// not the OpenAPI type (which reports ObjectId as "string")
|
|
381
|
+
const searchableFields: string[] = [];
|
|
382
|
+
const objectIdFields: string[] = [];
|
|
383
|
+
const modelMeta = configModels.find((m) => m.name === config.model.modelName);
|
|
384
|
+
if (modelMeta) {
|
|
385
|
+
for (const key of Object.keys(modelMeta.fields)) {
|
|
386
|
+
const schemaPath = config.model.schema.path(key);
|
|
387
|
+
if (schemaPath && schemaPath.instance === "String" && !modelMeta.fields[key].enum) {
|
|
388
|
+
searchableFields.push(key);
|
|
389
|
+
} else if (schemaPath && schemaPath.instance === "ObjectID") {
|
|
390
|
+
objectIdFields.push(key);
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
logger.info(`Admin search fields for ${config.model.modelName}`, {
|
|
395
|
+
objectIdFields,
|
|
396
|
+
searchableFields,
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
app.get(
|
|
400
|
+
`${basePath}${config.routePath}/search`,
|
|
401
|
+
authenticateMiddleware(),
|
|
402
|
+
asyncHandler(async (req, res) => {
|
|
403
|
+
if (!(req as any).user?.admin) {
|
|
404
|
+
throw new APIError({
|
|
405
|
+
disableExternalErrorTracking: true,
|
|
406
|
+
status: 403,
|
|
407
|
+
title: "Forbidden",
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
const q = String(req.query.q ?? "");
|
|
411
|
+
if (!q) {
|
|
412
|
+
return res.json({data: []});
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
const escapedQ = q.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
416
|
+
const regex = new RegExp(escapedQ, "i");
|
|
417
|
+
|
|
418
|
+
const fields =
|
|
419
|
+
typeof req.query.fields === "string"
|
|
420
|
+
? req.query.fields.split(",").filter((f: string) => searchableFields.includes(f))
|
|
421
|
+
: searchableFields;
|
|
422
|
+
|
|
423
|
+
const orConditions = fields.map((field: string) => ({[field]: {$regex: regex}}));
|
|
424
|
+
|
|
425
|
+
// If the query is a valid ObjectId, also match against ObjectId fields
|
|
426
|
+
if (mongoose.isValidObjectId(q)) {
|
|
427
|
+
for (const field of objectIdFields) {
|
|
428
|
+
orConditions.push({[field]: new mongoose.Types.ObjectId(q)});
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
if (orConditions.length === 0) {
|
|
433
|
+
return res.json({data: []});
|
|
434
|
+
}
|
|
435
|
+
logger.debug("Admin search query", {
|
|
436
|
+
fields,
|
|
437
|
+
model: config.model.modelName,
|
|
438
|
+
q,
|
|
439
|
+
});
|
|
440
|
+
try {
|
|
441
|
+
const results = await config.model.find({$or: orConditions}).limit(20).lean();
|
|
442
|
+
logger.debug("Admin search results", {
|
|
443
|
+
count: results.length,
|
|
444
|
+
model: config.model.modelName,
|
|
445
|
+
});
|
|
446
|
+
return res.json({data: results});
|
|
447
|
+
} catch (err) {
|
|
448
|
+
logger.error("Admin search failed", {
|
|
449
|
+
error: err,
|
|
450
|
+
fields,
|
|
451
|
+
model: config.model.modelName,
|
|
452
|
+
});
|
|
453
|
+
throw err;
|
|
454
|
+
}
|
|
455
|
+
})
|
|
456
|
+
);
|
|
457
|
+
}
|
|
458
|
+
|
|
374
459
|
// Mount modelRouter for each model with IsAdmin permissions
|
|
375
460
|
for (const config of modelConfigs) {
|
|
376
461
|
const hiddenFieldSet = new Set(config.hiddenFields ?? []);
|