@terreno/admin-backend 0.9.3 → 0.11.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/bunfig.toml +5 -0
- package/dist/adminApp.js +208 -45
- package/dist/adminApp.models.test.d.ts +1 -0
- package/dist/adminApp.models.test.js +969 -0
- package/dist/adminApp.test.js +9 -4
- package/dist/documentStorageApp.js +44 -27
- package/dist/documentStorageApp.test.d.ts +1 -0
- package/dist/documentStorageApp.test.js +874 -0
- package/package.json +4 -3
- package/src/adminApp.models.test.ts +479 -0
- package/src/adminApp.test.ts +15 -6
- package/src/adminApp.ts +188 -18
- package/src/documentStorageApp.test.ts +540 -0
- package/src/documentStorageApp.ts +55 -30
package/bunfig.toml
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
1
|
[test]
|
|
2
2
|
preload = ["../api/src/tests/bunSetup.ts"]
|
|
3
3
|
root = "./src"
|
|
4
|
+
coveragePathIgnorePatterns = ["**/*.test.ts", "../api/**", "dist/**", "**/node_modules/**"]
|
|
5
|
+
# Enforced by scripts/check-coverage.ts; Bun parses this key but does not
|
|
6
|
+
# fail the run when coverage is below threshold. See
|
|
7
|
+
# https://github.com/oven-sh/bun/issues/7367 and https://github.com/oven-sh/bun/pull/27933.
|
|
8
|
+
coverageThreshold = { line = 95, function = 95 }
|
package/dist/adminApp.js
CHANGED
|
@@ -81,8 +81,12 @@ 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
|
-
if (value &&
|
|
86
|
+
if (value &&
|
|
87
|
+
typeof value === "object" &&
|
|
88
|
+
"toObject" in value &&
|
|
89
|
+
typeof value.toObject === "function") {
|
|
86
90
|
return value.toObject();
|
|
87
91
|
}
|
|
88
92
|
return value;
|
|
@@ -121,19 +125,31 @@ var extractFieldMeta = function (properties, required) {
|
|
|
121
125
|
try {
|
|
122
126
|
for (var _e = __values(Object.entries(properties)), _f = _e.next(); !_f.done; _f = _e.next()) {
|
|
123
127
|
var _g = __read(_f.value, 2), key = _g[0], prop = _g[1];
|
|
128
|
+
var fieldType = (_b = prop.type) !== null && _b !== void 0 ? _b : "string";
|
|
124
129
|
fields[key] = {
|
|
125
130
|
default: prop.default,
|
|
126
131
|
description: prop.description,
|
|
127
132
|
enum: prop.enum,
|
|
128
133
|
ref: prop.$ref ? prop.$ref.replace("#/components/schemas/", "") : undefined,
|
|
129
134
|
required: required.includes(key),
|
|
130
|
-
|
|
135
|
+
searchable: fieldType === "string" && !prop.enum,
|
|
136
|
+
type: fieldType,
|
|
131
137
|
};
|
|
132
138
|
// For array fields, extract item sub-field metadata
|
|
133
139
|
if (prop.type === "array" && ((_c = prop.items) === null || _c === void 0 ? void 0 : _c.properties)) {
|
|
134
140
|
var itemRequired = (_d = prop.items.required) !== null && _d !== void 0 ? _d : [];
|
|
135
141
|
fields[key].items = extractFieldMeta(prop.items.properties, itemRequired);
|
|
136
142
|
}
|
|
143
|
+
// For array fields of primitives, capture the item type and enum
|
|
144
|
+
if (prop.type === "array" && prop.items && !prop.items.properties) {
|
|
145
|
+
var itemProp = prop.items;
|
|
146
|
+
if (itemProp.type) {
|
|
147
|
+
fields[key].itemType = itemProp.type;
|
|
148
|
+
}
|
|
149
|
+
if (itemProp.enum) {
|
|
150
|
+
fields[key].itemEnum = itemProp.enum;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
137
153
|
// Check for ObjectId references in the raw property
|
|
138
154
|
if (!fields[key].ref && prop.type === "string" && prop.format === "objectid") {
|
|
139
155
|
// mongoose-to-swagger may not preserve ref directly; we'll handle this in register()
|
|
@@ -215,16 +231,16 @@ var AdminApp = /** @class */ (function () {
|
|
|
215
231
|
* @param app - The Express application instance to register with
|
|
216
232
|
*/
|
|
217
233
|
AdminApp.prototype.register = function (app, openApi) {
|
|
218
|
-
var e_3, _a;
|
|
234
|
+
var e_3, _a, e_4, _b;
|
|
219
235
|
var _this = this;
|
|
220
|
-
var
|
|
221
|
-
var basePath = (
|
|
236
|
+
var _c, _d, _e, _f;
|
|
237
|
+
var basePath = (_c = this.options.basePath) !== null && _c !== void 0 ? _c : "/admin";
|
|
222
238
|
var modelConfigs = this.options.models;
|
|
223
239
|
// Build config response with field metadata from Mongoose schemas
|
|
224
240
|
var configModels = modelConfigs.map(function (config) {
|
|
225
|
-
var
|
|
226
|
-
var _c, _d, _e;
|
|
227
|
-
var
|
|
241
|
+
var e_5, _a, e_6, _b;
|
|
242
|
+
var _c, _d, _e, _f, _g;
|
|
243
|
+
var _h = (0, api_1.getOpenApiSpecForModel)(config.model), properties = _h.properties, required = _h.required;
|
|
228
244
|
var hiddenFieldSet = new Set((_c = config.hiddenFields) !== null && _c !== void 0 ? _c : []);
|
|
229
245
|
var filteredProperties = Object.fromEntries(Object.entries(properties).filter(function (_a) {
|
|
230
246
|
var _b = __read(_a, 1), key = _b[0];
|
|
@@ -234,48 +250,73 @@ var AdminApp = /** @class */ (function () {
|
|
|
234
250
|
// Extract ref information directly from the Mongoose schema
|
|
235
251
|
var fields = extractFieldMeta(filteredProperties, filteredRequired);
|
|
236
252
|
try {
|
|
237
|
-
for (var
|
|
238
|
-
var
|
|
253
|
+
for (var _j = __values(Object.entries(fields)), _k = _j.next(); !_k.done; _k = _j.next()) {
|
|
254
|
+
var _l = __read(_k.value, 2), key = _l[0], field = _l[1];
|
|
239
255
|
var schemaPath = config.model.schema.path(key);
|
|
240
256
|
if (schemaPath) {
|
|
241
257
|
var pathOptions = schemaPath.options;
|
|
242
258
|
if (pathOptions === null || pathOptions === void 0 ? void 0 : pathOptions.ref) {
|
|
243
259
|
field.ref = pathOptions.ref;
|
|
244
260
|
}
|
|
245
|
-
// Handle array of refs
|
|
261
|
+
// Handle array of refs (legacy: also set ref for back-compat)
|
|
246
262
|
if (Array.isArray(pathOptions === null || pathOptions === void 0 ? void 0 : pathOptions.type) && ((_d = pathOptions.type[0]) === null || _d === void 0 ? void 0 : _d.ref)) {
|
|
247
263
|
field.ref = pathOptions.type[0].ref;
|
|
248
264
|
}
|
|
265
|
+
// For arrays, use the caster to infer the primitive item type/ref.
|
|
266
|
+
// Mongoose caster.instance is "String" | "Number" | "Boolean" | "ObjectID".
|
|
267
|
+
if (schemaPath.instance === "Array") {
|
|
268
|
+
var caster = schemaPath.caster;
|
|
269
|
+
if ((caster === null || caster === void 0 ? void 0 : caster.instance) && !field.items) {
|
|
270
|
+
var instanceToType = {
|
|
271
|
+
Boolean: "boolean",
|
|
272
|
+
Number: "number",
|
|
273
|
+
ObjectID: "objectid",
|
|
274
|
+
ObjectId: "objectid",
|
|
275
|
+
SchemaObjectId: "objectid",
|
|
276
|
+
String: "string",
|
|
277
|
+
};
|
|
278
|
+
var mapped = instanceToType[caster.instance];
|
|
279
|
+
if (mapped) {
|
|
280
|
+
field.itemType = mapped;
|
|
281
|
+
}
|
|
282
|
+
if ((_e = caster.options) === null || _e === void 0 ? void 0 : _e.ref) {
|
|
283
|
+
field.itemRef = caster.options.ref;
|
|
284
|
+
}
|
|
285
|
+
if ((_f = caster.options) === null || _f === void 0 ? void 0 : _f.enum) {
|
|
286
|
+
field.itemEnum = caster.options.enum;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
249
290
|
}
|
|
250
291
|
}
|
|
251
292
|
}
|
|
252
|
-
catch (
|
|
293
|
+
catch (e_5_1) { e_5 = { error: e_5_1 }; }
|
|
253
294
|
finally {
|
|
254
295
|
try {
|
|
255
|
-
if (
|
|
296
|
+
if (_k && !_k.done && (_a = _j.return)) _a.call(_j);
|
|
256
297
|
}
|
|
257
|
-
finally { if (
|
|
298
|
+
finally { if (e_5) throw e_5.error; }
|
|
258
299
|
}
|
|
259
300
|
// Apply field overrides (e.g., widget: "markdown")
|
|
260
301
|
if (config.fieldOverrides) {
|
|
261
302
|
try {
|
|
262
|
-
for (var
|
|
263
|
-
var
|
|
303
|
+
for (var _m = __values(Object.entries(config.fieldOverrides)), _o = _m.next(); !_o.done; _o = _m.next()) {
|
|
304
|
+
var _p = __read(_o.value, 2), key = _p[0], override = _p[1];
|
|
264
305
|
if (fields[key] && override.widget) {
|
|
265
306
|
fields[key].widget = override.widget;
|
|
266
307
|
}
|
|
267
308
|
}
|
|
268
309
|
}
|
|
269
|
-
catch (
|
|
310
|
+
catch (e_6_1) { e_6 = { error: e_6_1 }; }
|
|
270
311
|
finally {
|
|
271
312
|
try {
|
|
272
|
-
if (
|
|
313
|
+
if (_o && !_o.done && (_b = _m.return)) _b.call(_m);
|
|
273
314
|
}
|
|
274
|
-
finally { if (
|
|
315
|
+
finally { if (e_6) throw e_6.error; }
|
|
275
316
|
}
|
|
276
317
|
}
|
|
277
318
|
return {
|
|
278
|
-
defaultSort: (
|
|
319
|
+
defaultSort: (_g = config.defaultSort) !== null && _g !== void 0 ? _g : "-created",
|
|
279
320
|
displayName: config.displayName,
|
|
280
321
|
fieldOrder: config.fieldOrder,
|
|
281
322
|
fields: fields,
|
|
@@ -285,7 +326,7 @@ var AdminApp = /** @class */ (function () {
|
|
|
285
326
|
};
|
|
286
327
|
});
|
|
287
328
|
// Build script metadata for config response
|
|
288
|
-
var scriptConfigs = (
|
|
329
|
+
var scriptConfigs = (_d = this.options.scripts) !== null && _d !== void 0 ? _d : [];
|
|
289
330
|
var configScripts = scriptConfigs.map(function (script) { return ({
|
|
290
331
|
description: script.description,
|
|
291
332
|
name: script.name,
|
|
@@ -342,7 +383,7 @@ var AdminApp = /** @class */ (function () {
|
|
|
342
383
|
}); }));
|
|
343
384
|
app.put(versionConfigPath, (0, api_1.authenticateMiddleware)(), (0, api_1.asyncHandler)(function (req, res) { return __awaiter(_this, void 0, void 0, function () {
|
|
344
385
|
var raw, allowedFields, setFields, unsetFields, allowedFields_1, allowedFields_1_1, field, updateOp, doc;
|
|
345
|
-
var
|
|
386
|
+
var e_7, _a;
|
|
346
387
|
return __generator(this, function (_b) {
|
|
347
388
|
switch (_b.label) {
|
|
348
389
|
case 0: return [4 /*yield*/, (0, api_1.checkPermissions)("update", [api_1.Permissions.IsAdmin], req.user)];
|
|
@@ -373,12 +414,12 @@ var AdminApp = /** @class */ (function () {
|
|
|
373
414
|
}
|
|
374
415
|
}
|
|
375
416
|
}
|
|
376
|
-
catch (
|
|
417
|
+
catch (e_7_1) { e_7 = { error: e_7_1 }; }
|
|
377
418
|
finally {
|
|
378
419
|
try {
|
|
379
420
|
if (allowedFields_1_1 && !allowedFields_1_1.done && (_a = allowedFields_1.return)) _a.call(allowedFields_1);
|
|
380
421
|
}
|
|
381
|
-
finally { if (
|
|
422
|
+
finally { if (e_7) throw e_7.error; }
|
|
382
423
|
}
|
|
383
424
|
updateOp = {};
|
|
384
425
|
if (Object.keys(setFields).length > 0) {
|
|
@@ -400,7 +441,129 @@ var AdminApp = /** @class */ (function () {
|
|
|
400
441
|
});
|
|
401
442
|
}); }));
|
|
402
443
|
var _loop_1 = function (config) {
|
|
403
|
-
var
|
|
444
|
+
var e_8, _g;
|
|
445
|
+
// Determine searchable fields from the actual Mongoose schema type,
|
|
446
|
+
// not the OpenAPI type (which reports ObjectId as "string")
|
|
447
|
+
var searchableFields = [];
|
|
448
|
+
var objectIdFields = [];
|
|
449
|
+
var modelMeta = configModels.find(function (m) { return m.name === config.model.modelName; });
|
|
450
|
+
if (modelMeta) {
|
|
451
|
+
try {
|
|
452
|
+
for (var _h = (e_8 = void 0, __values(Object.keys(modelMeta.fields))), _j = _h.next(); !_j.done; _j = _h.next()) {
|
|
453
|
+
var key = _j.value;
|
|
454
|
+
var schemaPath = config.model.schema.path(key);
|
|
455
|
+
if (schemaPath && schemaPath.instance === "String" && !modelMeta.fields[key].enum) {
|
|
456
|
+
searchableFields.push(key);
|
|
457
|
+
}
|
|
458
|
+
else if (schemaPath && schemaPath.instance === "ObjectID") {
|
|
459
|
+
objectIdFields.push(key);
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
catch (e_8_1) { e_8 = { error: e_8_1 }; }
|
|
464
|
+
finally {
|
|
465
|
+
try {
|
|
466
|
+
if (_j && !_j.done && (_g = _h.return)) _g.call(_h);
|
|
467
|
+
}
|
|
468
|
+
finally { if (e_8) throw e_8.error; }
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
api_1.logger.info("Admin search fields for ".concat(config.model.modelName), {
|
|
472
|
+
objectIdFields: objectIdFields,
|
|
473
|
+
searchableFields: searchableFields,
|
|
474
|
+
});
|
|
475
|
+
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 q, escapedQ, regex, fields, orConditions, objectIdFields_1, objectIdFields_1_1, field, results, err_1;
|
|
477
|
+
var e_9, _a, _b;
|
|
478
|
+
var _c, _d;
|
|
479
|
+
return __generator(this, function (_e) {
|
|
480
|
+
switch (_e.label) {
|
|
481
|
+
case 0:
|
|
482
|
+
if (!((_c = req.user) === null || _c === void 0 ? void 0 : _c.admin)) {
|
|
483
|
+
throw new api_1.APIError({
|
|
484
|
+
disableExternalErrorTracking: true,
|
|
485
|
+
status: 403,
|
|
486
|
+
title: "Forbidden",
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
q = String((_d = req.query.q) !== null && _d !== void 0 ? _d : "");
|
|
490
|
+
if (!q) {
|
|
491
|
+
return [2 /*return*/, res.json({ data: [] })];
|
|
492
|
+
}
|
|
493
|
+
escapedQ = q.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
494
|
+
regex = new RegExp(escapedQ, "i");
|
|
495
|
+
fields = typeof req.query.fields === "string"
|
|
496
|
+
? req.query.fields.split(",").filter(function (f) { return searchableFields.includes(f); })
|
|
497
|
+
: searchableFields;
|
|
498
|
+
orConditions = fields.map(function (field) {
|
|
499
|
+
var _a;
|
|
500
|
+
return (_a = {}, _a[field] = { $regex: regex }, _a);
|
|
501
|
+
});
|
|
502
|
+
// If the query is a valid ObjectId, also match against ObjectId fields
|
|
503
|
+
if (mongoose_1.default.isValidObjectId(q)) {
|
|
504
|
+
try {
|
|
505
|
+
for (objectIdFields_1 = __values(objectIdFields), objectIdFields_1_1 = objectIdFields_1.next(); !objectIdFields_1_1.done; objectIdFields_1_1 = objectIdFields_1.next()) {
|
|
506
|
+
field = objectIdFields_1_1.value;
|
|
507
|
+
orConditions.push((_b = {}, _b[field] = new mongoose_1.default.Types.ObjectId(q), _b));
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
catch (e_9_1) { e_9 = { error: e_9_1 }; }
|
|
511
|
+
finally {
|
|
512
|
+
try {
|
|
513
|
+
if (objectIdFields_1_1 && !objectIdFields_1_1.done && (_a = objectIdFields_1.return)) _a.call(objectIdFields_1);
|
|
514
|
+
}
|
|
515
|
+
finally { if (e_9) throw e_9.error; }
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
if (orConditions.length === 0) {
|
|
519
|
+
return [2 /*return*/, res.json({ data: [] })];
|
|
520
|
+
}
|
|
521
|
+
api_1.logger.debug("Admin search query", {
|
|
522
|
+
fields: fields,
|
|
523
|
+
model: config.model.modelName,
|
|
524
|
+
q: q,
|
|
525
|
+
});
|
|
526
|
+
_e.label = 1;
|
|
527
|
+
case 1:
|
|
528
|
+
_e.trys.push([1, 3, , 4]);
|
|
529
|
+
return [4 /*yield*/, config.model.find({ $or: orConditions }).limit(20).lean()];
|
|
530
|
+
case 2:
|
|
531
|
+
results = _e.sent();
|
|
532
|
+
api_1.logger.debug("Admin search results", {
|
|
533
|
+
count: results.length,
|
|
534
|
+
model: config.model.modelName,
|
|
535
|
+
});
|
|
536
|
+
return [2 /*return*/, res.json({ data: results })];
|
|
537
|
+
case 3:
|
|
538
|
+
err_1 = _e.sent();
|
|
539
|
+
api_1.logger.error("Admin search failed", {
|
|
540
|
+
error: err_1,
|
|
541
|
+
fields: fields,
|
|
542
|
+
model: config.model.modelName,
|
|
543
|
+
});
|
|
544
|
+
throw err_1;
|
|
545
|
+
case 4: return [2 /*return*/];
|
|
546
|
+
}
|
|
547
|
+
});
|
|
548
|
+
}); }));
|
|
549
|
+
};
|
|
550
|
+
try {
|
|
551
|
+
// Mount search endpoint for each model
|
|
552
|
+
for (var modelConfigs_1 = __values(modelConfigs), modelConfigs_1_1 = modelConfigs_1.next(); !modelConfigs_1_1.done; modelConfigs_1_1 = modelConfigs_1.next()) {
|
|
553
|
+
var config = modelConfigs_1_1.value;
|
|
554
|
+
_loop_1(config);
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
558
|
+
finally {
|
|
559
|
+
try {
|
|
560
|
+
if (modelConfigs_1_1 && !modelConfigs_1_1.done && (_a = modelConfigs_1.return)) _a.call(modelConfigs_1);
|
|
561
|
+
}
|
|
562
|
+
finally { if (e_3) throw e_3.error; }
|
|
563
|
+
}
|
|
564
|
+
var _loop_2 = function (config) {
|
|
565
|
+
var hiddenFieldSet = new Set((_e = config.hiddenFields) !== null && _e !== void 0 ? _e : []);
|
|
566
|
+
// noExplicitAny: ModelRouterOptions<any> matches the Model<any> from AdminModelConfig.
|
|
404
567
|
var routerOptions = __assign(__assign({}, (openApi ? { openApi: openApi } : {})), { permissions: {
|
|
405
568
|
create: [api_1.Permissions.IsAdmin],
|
|
406
569
|
delete: [api_1.Permissions.IsAdmin],
|
|
@@ -411,22 +574,22 @@ var AdminApp = /** @class */ (function () {
|
|
|
411
574
|
? function (value, _method, _request, _options) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
|
|
412
575
|
return [2 /*return*/, removeHiddenFields(value, hiddenFieldSet)];
|
|
413
576
|
}); }); }
|
|
414
|
-
: undefined, sort: (
|
|
577
|
+
: undefined, sort: (_f = config.defaultSort) !== null && _f !== void 0 ? _f : "-created" });
|
|
415
578
|
app.use("".concat(basePath).concat(config.routePath), (0, api_1.modelRouter)(config.model, routerOptions));
|
|
416
579
|
};
|
|
417
580
|
try {
|
|
418
581
|
// Mount modelRouter for each model with IsAdmin permissions
|
|
419
|
-
for (var
|
|
420
|
-
var config =
|
|
421
|
-
|
|
582
|
+
for (var modelConfigs_2 = __values(modelConfigs), modelConfigs_2_1 = modelConfigs_2.next(); !modelConfigs_2_1.done; modelConfigs_2_1 = modelConfigs_2.next()) {
|
|
583
|
+
var config = modelConfigs_2_1.value;
|
|
584
|
+
_loop_2(config);
|
|
422
585
|
}
|
|
423
586
|
}
|
|
424
|
-
catch (
|
|
587
|
+
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
425
588
|
finally {
|
|
426
589
|
try {
|
|
427
|
-
if (
|
|
590
|
+
if (modelConfigs_2_1 && !modelConfigs_2_1.done && (_b = modelConfigs_2.return)) _b.call(modelConfigs_2);
|
|
428
591
|
}
|
|
429
|
-
finally { if (
|
|
592
|
+
finally { if (e_4) throw e_4.error; }
|
|
430
593
|
}
|
|
431
594
|
// Mount script routes
|
|
432
595
|
if (scriptConfigs.length > 0) {
|
|
@@ -441,7 +604,7 @@ var AdminApp = /** @class */ (function () {
|
|
|
441
604
|
var scriptsByName = new Map(scripts.map(function (s) { return [s.name, s]; }));
|
|
442
605
|
// POST /admin/scripts/:name/run — Execute a script
|
|
443
606
|
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,
|
|
607
|
+
var user, script, isWetRun, now, task, err_2, detail, ctx;
|
|
445
608
|
var _this = this;
|
|
446
609
|
var _a;
|
|
447
610
|
return __generator(this, function (_b) {
|
|
@@ -475,8 +638,8 @@ var AdminApp = /** @class */ (function () {
|
|
|
475
638
|
task = (_b.sent());
|
|
476
639
|
return [3 /*break*/, 4];
|
|
477
640
|
case 3:
|
|
478
|
-
|
|
479
|
-
detail =
|
|
641
|
+
err_2 = _b.sent();
|
|
642
|
+
detail = err_2 instanceof Error ? err_2.message : String(err_2);
|
|
480
643
|
api_1.logger.error("Failed to create background task for ".concat(script.name, ": ").concat(detail));
|
|
481
644
|
throw new api_1.APIError({
|
|
482
645
|
detail: detail,
|
|
@@ -531,7 +694,7 @@ var AdminApp = /** @class */ (function () {
|
|
|
531
694
|
// Run the script asynchronously — use atomic updates to avoid overwriting
|
|
532
695
|
// cancellation or other intermediate state changes.
|
|
533
696
|
void (function () { return __awaiter(_this, void 0, void 0, function () {
|
|
534
|
-
var result,
|
|
697
|
+
var result, err_3, message;
|
|
535
698
|
return __generator(this, function (_a) {
|
|
536
699
|
switch (_a.label) {
|
|
537
700
|
case 0:
|
|
@@ -553,11 +716,11 @@ var AdminApp = /** @class */ (function () {
|
|
|
553
716
|
_a.sent();
|
|
554
717
|
return [3 /*break*/, 5];
|
|
555
718
|
case 3:
|
|
556
|
-
|
|
557
|
-
if (
|
|
719
|
+
err_3 = _a.sent();
|
|
720
|
+
if (err_3 instanceof api_1.TaskCancelledError) {
|
|
558
721
|
return [2 /*return*/];
|
|
559
722
|
}
|
|
560
|
-
message =
|
|
723
|
+
message = err_3 instanceof Error ? err_3.message : String(err_3);
|
|
561
724
|
api_1.logger.error("Script ".concat(script.name, " failed: ").concat(message));
|
|
562
725
|
// Atomically update only if still running
|
|
563
726
|
return [4 /*yield*/, api_1.BackgroundTask.findOneAndUpdate({ _id: task._id, status: "running" }, {
|
|
@@ -582,7 +745,7 @@ var AdminApp = /** @class */ (function () {
|
|
|
582
745
|
}); }));
|
|
583
746
|
// GET /admin/scripts/tasks/:id — Poll task status
|
|
584
747
|
router.get("/tasks/:id", (0, api_1.asyncHandler)(function (req, res) { return __awaiter(_this, void 0, void 0, function () {
|
|
585
|
-
var user, task,
|
|
748
|
+
var user, task, err_4, detail;
|
|
586
749
|
return __generator(this, function (_a) {
|
|
587
750
|
switch (_a.label) {
|
|
588
751
|
case 0:
|
|
@@ -598,8 +761,8 @@ var AdminApp = /** @class */ (function () {
|
|
|
598
761
|
task = _a.sent();
|
|
599
762
|
return [3 /*break*/, 4];
|
|
600
763
|
case 3:
|
|
601
|
-
|
|
602
|
-
detail =
|
|
764
|
+
err_4 = _a.sent();
|
|
765
|
+
detail = err_4 instanceof Error ? err_4.message : String(err_4);
|
|
603
766
|
throw new api_1.APIError({ detail: detail, status: 400, title: "Invalid task ID" });
|
|
604
767
|
case 4:
|
|
605
768
|
if (!task) {
|
|
@@ -611,7 +774,7 @@ var AdminApp = /** @class */ (function () {
|
|
|
611
774
|
}); }));
|
|
612
775
|
// DELETE /admin/scripts/tasks/:id — Cancel a running task
|
|
613
776
|
router.delete("/tasks/:id", (0, api_1.asyncHandler)(function (req, res) { return __awaiter(_this, void 0, void 0, function () {
|
|
614
|
-
var user, task,
|
|
777
|
+
var user, task, err_5, detail, cancelled;
|
|
615
778
|
var _a;
|
|
616
779
|
return __generator(this, function (_b) {
|
|
617
780
|
switch (_b.label) {
|
|
@@ -628,8 +791,8 @@ var AdminApp = /** @class */ (function () {
|
|
|
628
791
|
task = _b.sent();
|
|
629
792
|
return [3 /*break*/, 4];
|
|
630
793
|
case 3:
|
|
631
|
-
|
|
632
|
-
detail =
|
|
794
|
+
err_5 = _b.sent();
|
|
795
|
+
detail = err_5 instanceof Error ? err_5.message : String(err_5);
|
|
633
796
|
throw new api_1.APIError({ detail: detail, status: 400, title: "Invalid task ID" });
|
|
634
797
|
case 4:
|
|
635
798
|
if (!task) {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|