@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/src/adminApp.ts
CHANGED
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
type BackgroundTaskDocument,
|
|
7
7
|
checkPermissions,
|
|
8
8
|
getOpenApiSpecForModel,
|
|
9
|
+
type JSONValue,
|
|
9
10
|
logger,
|
|
10
11
|
type ModelRouterOptions,
|
|
11
12
|
modelRouter,
|
|
@@ -15,12 +16,13 @@ import {
|
|
|
15
16
|
type ScriptResult,
|
|
16
17
|
type ScriptRunner,
|
|
17
18
|
TaskCancelledError,
|
|
19
|
+
type User,
|
|
18
20
|
VersionConfig,
|
|
19
21
|
} from "@terreno/api";
|
|
20
22
|
import express from "express";
|
|
21
23
|
import {DateTime} from "luxon";
|
|
22
|
-
import type mongoose from "mongoose";
|
|
23
24
|
import type {Model} from "mongoose";
|
|
25
|
+
import mongoose from "mongoose";
|
|
24
26
|
|
|
25
27
|
/**
|
|
26
28
|
* Configuration for a single model in the admin panel.
|
|
@@ -31,6 +33,8 @@ export interface AdminFieldOverride {
|
|
|
31
33
|
}
|
|
32
34
|
|
|
33
35
|
export interface AdminModelConfig {
|
|
36
|
+
// noExplicitAny: Model must accept any document type for the generic admin panel;
|
|
37
|
+
// Model<unknown> breaks callers since Model<T> is invariant in T.
|
|
34
38
|
/** The Mongoose model to expose in the admin panel */
|
|
35
39
|
model: Model<any>;
|
|
36
40
|
/** Route path for this model's endpoints, relative to basePath (e.g., "/users") */
|
|
@@ -78,11 +82,18 @@ interface AdminFieldMeta {
|
|
|
78
82
|
required: boolean;
|
|
79
83
|
description?: string;
|
|
80
84
|
enum?: string[];
|
|
81
|
-
default?:
|
|
85
|
+
default?: unknown;
|
|
82
86
|
ref?: string;
|
|
87
|
+
searchable?: boolean;
|
|
83
88
|
widget?: string;
|
|
84
|
-
/** For array fields: metadata about each item's sub-fields */
|
|
89
|
+
/** For array fields of sub-documents: metadata about each item's sub-fields */
|
|
85
90
|
items?: Record<string, AdminFieldMeta>;
|
|
91
|
+
/** For array fields of primitives: the item type (string/number/boolean/objectid) */
|
|
92
|
+
itemType?: string;
|
|
93
|
+
/** For array fields of primitives: enum values for each item */
|
|
94
|
+
itemEnum?: string[];
|
|
95
|
+
/** For array fields of ObjectId refs: the referenced model name */
|
|
96
|
+
itemRef?: string;
|
|
86
97
|
}
|
|
87
98
|
|
|
88
99
|
interface AdminModelMeta {
|
|
@@ -106,11 +117,16 @@ interface AdminConfigResponse {
|
|
|
106
117
|
scripts: AdminScriptMeta[];
|
|
107
118
|
}
|
|
108
119
|
|
|
109
|
-
const toPlainObject = (value:
|
|
110
|
-
if (
|
|
111
|
-
|
|
120
|
+
const toPlainObject = (value: unknown): Record<string, unknown> => {
|
|
121
|
+
if (
|
|
122
|
+
value &&
|
|
123
|
+
typeof value === "object" &&
|
|
124
|
+
"toObject" in value &&
|
|
125
|
+
typeof value.toObject === "function"
|
|
126
|
+
) {
|
|
127
|
+
return (value as {toObject: () => Record<string, unknown>}).toObject();
|
|
112
128
|
}
|
|
113
|
-
return value
|
|
129
|
+
return value as Record<string, unknown>;
|
|
114
130
|
};
|
|
115
131
|
|
|
116
132
|
const removeHiddenFields = (value: unknown, hiddenFieldSet: Set<string>): unknown => {
|
|
@@ -120,7 +136,7 @@ const removeHiddenFields = (value: unknown, hiddenFieldSet: Set<string>): unknow
|
|
|
120
136
|
if (!value || typeof value !== "object") {
|
|
121
137
|
return value;
|
|
122
138
|
}
|
|
123
|
-
const plainValue = toPlainObject(value
|
|
139
|
+
const plainValue = toPlainObject(value);
|
|
124
140
|
const nextValue: Record<string, unknown> = {};
|
|
125
141
|
for (const [key, fieldValue] of Object.entries(plainValue)) {
|
|
126
142
|
if (!hiddenFieldSet.has(key)) {
|
|
@@ -130,25 +146,58 @@ const removeHiddenFields = (value: unknown, hiddenFieldSet: Set<string>): unknow
|
|
|
130
146
|
return nextValue;
|
|
131
147
|
};
|
|
132
148
|
|
|
149
|
+
interface OpenApiProperty {
|
|
150
|
+
default?: unknown;
|
|
151
|
+
description?: string;
|
|
152
|
+
enum?: string[];
|
|
153
|
+
$ref?: string;
|
|
154
|
+
type?: string;
|
|
155
|
+
format?: string;
|
|
156
|
+
items?: {
|
|
157
|
+
properties?: Record<string, OpenApiProperty>;
|
|
158
|
+
required?: string[];
|
|
159
|
+
type?: string;
|
|
160
|
+
enum?: string[];
|
|
161
|
+
format?: string;
|
|
162
|
+
$ref?: string;
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
|
|
133
166
|
const extractFieldMeta = (
|
|
134
|
-
properties: Record<string,
|
|
167
|
+
properties: Record<string, OpenApiProperty>,
|
|
135
168
|
required: string[]
|
|
136
169
|
): Record<string, AdminFieldMeta> => {
|
|
137
170
|
const fields: Record<string, AdminFieldMeta> = {};
|
|
138
171
|
for (const [key, prop] of Object.entries(properties)) {
|
|
172
|
+
const fieldType = prop.type ?? "string";
|
|
139
173
|
fields[key] = {
|
|
140
174
|
default: prop.default,
|
|
141
175
|
description: prop.description,
|
|
142
176
|
enum: prop.enum,
|
|
143
177
|
ref: prop.$ref ? prop.$ref.replace("#/components/schemas/", "") : undefined,
|
|
144
178
|
required: required.includes(key),
|
|
145
|
-
|
|
179
|
+
searchable: fieldType === "string" && !prop.enum,
|
|
180
|
+
type: fieldType,
|
|
146
181
|
};
|
|
147
182
|
|
|
148
183
|
// For array fields, extract item sub-field metadata
|
|
149
184
|
if (prop.type === "array" && prop.items?.properties) {
|
|
150
185
|
const itemRequired: string[] = prop.items.required ?? [];
|
|
151
|
-
fields[key].items = extractFieldMeta(
|
|
186
|
+
fields[key].items = extractFieldMeta(
|
|
187
|
+
prop.items.properties as Record<string, OpenApiProperty>,
|
|
188
|
+
itemRequired
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// For array fields of primitives, capture the item type and enum
|
|
193
|
+
if (prop.type === "array" && prop.items && !prop.items.properties) {
|
|
194
|
+
const itemProp = prop.items as OpenApiProperty;
|
|
195
|
+
if (itemProp.type) {
|
|
196
|
+
fields[key].itemType = itemProp.type;
|
|
197
|
+
}
|
|
198
|
+
if (itemProp.enum) {
|
|
199
|
+
fields[key].itemEnum = itemProp.enum;
|
|
200
|
+
}
|
|
152
201
|
}
|
|
153
202
|
|
|
154
203
|
// Check for ObjectId references in the raw property
|
|
@@ -233,7 +282,10 @@ export class AdminApp {
|
|
|
233
282
|
|
|
234
283
|
// Build config response with field metadata from Mongoose schemas
|
|
235
284
|
const configModels: AdminModelMeta[] = modelConfigs.map((config) => {
|
|
236
|
-
const {properties, required} = getOpenApiSpecForModel(config.model)
|
|
285
|
+
const {properties, required} = getOpenApiSpecForModel(config.model) as {
|
|
286
|
+
properties: Record<string, OpenApiProperty>;
|
|
287
|
+
required: string[];
|
|
288
|
+
};
|
|
237
289
|
const hiddenFieldSet = new Set(config.hiddenFields ?? []);
|
|
238
290
|
const filteredProperties = Object.fromEntries(
|
|
239
291
|
Object.entries(properties).filter(([key]) => !hiddenFieldSet.has(key))
|
|
@@ -249,10 +301,39 @@ export class AdminApp {
|
|
|
249
301
|
if (pathOptions?.ref) {
|
|
250
302
|
field.ref = pathOptions.ref;
|
|
251
303
|
}
|
|
252
|
-
// Handle array of refs
|
|
304
|
+
// Handle array of refs (legacy: also set ref for back-compat)
|
|
253
305
|
if (Array.isArray(pathOptions?.type) && pathOptions.type[0]?.ref) {
|
|
254
306
|
field.ref = pathOptions.type[0].ref;
|
|
255
307
|
}
|
|
308
|
+
// For arrays, use the caster to infer the primitive item type/ref.
|
|
309
|
+
// Mongoose caster.instance is "String" | "Number" | "Boolean" | "ObjectID".
|
|
310
|
+
if (schemaPath.instance === "Array") {
|
|
311
|
+
const caster = (
|
|
312
|
+
schemaPath as unknown as {
|
|
313
|
+
caster?: {instance?: string; options?: {ref?: string; enum?: string[]}};
|
|
314
|
+
}
|
|
315
|
+
).caster;
|
|
316
|
+
if (caster?.instance && !field.items) {
|
|
317
|
+
const instanceToType: Record<string, string> = {
|
|
318
|
+
Boolean: "boolean",
|
|
319
|
+
Number: "number",
|
|
320
|
+
ObjectID: "objectid",
|
|
321
|
+
ObjectId: "objectid",
|
|
322
|
+
SchemaObjectId: "objectid",
|
|
323
|
+
String: "string",
|
|
324
|
+
};
|
|
325
|
+
const mapped = instanceToType[caster.instance];
|
|
326
|
+
if (mapped) {
|
|
327
|
+
field.itemType = mapped;
|
|
328
|
+
}
|
|
329
|
+
if (caster.options?.ref) {
|
|
330
|
+
field.itemRef = caster.options.ref;
|
|
331
|
+
}
|
|
332
|
+
if (caster.options?.enum) {
|
|
333
|
+
field.itemEnum = caster.options.enum;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
}
|
|
256
337
|
}
|
|
257
338
|
}
|
|
258
339
|
|
|
@@ -299,7 +380,9 @@ export class AdminApp {
|
|
|
299
380
|
`${basePath}/config`,
|
|
300
381
|
authenticateMiddleware(),
|
|
301
382
|
asyncHandler(async (req, res) => {
|
|
302
|
-
if (
|
|
383
|
+
if (
|
|
384
|
+
!(await checkPermissions("read", [Permissions.IsAdmin], req.user as User | undefined))
|
|
385
|
+
) {
|
|
303
386
|
throw new APIError({status: 403, title: "Admin access required"});
|
|
304
387
|
}
|
|
305
388
|
return res.json(configResponse);
|
|
@@ -312,7 +395,9 @@ export class AdminApp {
|
|
|
312
395
|
versionConfigPath,
|
|
313
396
|
authenticateMiddleware(),
|
|
314
397
|
asyncHandler(async (req, res) => {
|
|
315
|
-
if (
|
|
398
|
+
if (
|
|
399
|
+
!(await checkPermissions("read", [Permissions.IsAdmin], req.user as User | undefined))
|
|
400
|
+
) {
|
|
316
401
|
throw new APIError({status: 403, title: "Admin access required"});
|
|
317
402
|
}
|
|
318
403
|
const config = await VersionConfig.findOneOrNone({_singleton: "config"});
|
|
@@ -332,7 +417,9 @@ export class AdminApp {
|
|
|
332
417
|
versionConfigPath,
|
|
333
418
|
authenticateMiddleware(),
|
|
334
419
|
asyncHandler(async (req, res) => {
|
|
335
|
-
if (
|
|
420
|
+
if (
|
|
421
|
+
!(await checkPermissions("update", [Permissions.IsAdmin], req.user as User | undefined))
|
|
422
|
+
) {
|
|
336
423
|
throw new APIError({status: 403, title: "Admin access required"});
|
|
337
424
|
}
|
|
338
425
|
const raw = req.body as Record<string, unknown>;
|
|
@@ -371,9 +458,92 @@ export class AdminApp {
|
|
|
371
458
|
})
|
|
372
459
|
);
|
|
373
460
|
|
|
461
|
+
// Mount search endpoint for each model
|
|
462
|
+
for (const config of modelConfigs) {
|
|
463
|
+
// Determine searchable fields from the actual Mongoose schema type,
|
|
464
|
+
// not the OpenAPI type (which reports ObjectId as "string")
|
|
465
|
+
const searchableFields: string[] = [];
|
|
466
|
+
const objectIdFields: string[] = [];
|
|
467
|
+
const modelMeta = configModels.find((m) => m.name === config.model.modelName);
|
|
468
|
+
if (modelMeta) {
|
|
469
|
+
for (const key of Object.keys(modelMeta.fields)) {
|
|
470
|
+
const schemaPath = config.model.schema.path(key);
|
|
471
|
+
if (schemaPath && schemaPath.instance === "String" && !modelMeta.fields[key].enum) {
|
|
472
|
+
searchableFields.push(key);
|
|
473
|
+
} else if (schemaPath && schemaPath.instance === "ObjectID") {
|
|
474
|
+
objectIdFields.push(key);
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
logger.info(`Admin search fields for ${config.model.modelName}`, {
|
|
479
|
+
objectIdFields,
|
|
480
|
+
searchableFields,
|
|
481
|
+
});
|
|
482
|
+
|
|
483
|
+
app.get(
|
|
484
|
+
`${basePath}${config.routePath}/search`,
|
|
485
|
+
authenticateMiddleware(),
|
|
486
|
+
asyncHandler(async (req, res) => {
|
|
487
|
+
if (!(req as any).user?.admin) {
|
|
488
|
+
throw new APIError({
|
|
489
|
+
disableExternalErrorTracking: true,
|
|
490
|
+
status: 403,
|
|
491
|
+
title: "Forbidden",
|
|
492
|
+
});
|
|
493
|
+
}
|
|
494
|
+
const q = String(req.query.q ?? "");
|
|
495
|
+
if (!q) {
|
|
496
|
+
return res.json({data: []});
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
const escapedQ = q.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
500
|
+
const regex = new RegExp(escapedQ, "i");
|
|
501
|
+
|
|
502
|
+
const fields =
|
|
503
|
+
typeof req.query.fields === "string"
|
|
504
|
+
? req.query.fields.split(",").filter((f: string) => searchableFields.includes(f))
|
|
505
|
+
: searchableFields;
|
|
506
|
+
|
|
507
|
+
const orConditions = fields.map((field: string) => ({[field]: {$regex: regex}}));
|
|
508
|
+
|
|
509
|
+
// If the query is a valid ObjectId, also match against ObjectId fields
|
|
510
|
+
if (mongoose.isValidObjectId(q)) {
|
|
511
|
+
for (const field of objectIdFields) {
|
|
512
|
+
orConditions.push({[field]: new mongoose.Types.ObjectId(q)});
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
if (orConditions.length === 0) {
|
|
517
|
+
return res.json({data: []});
|
|
518
|
+
}
|
|
519
|
+
logger.debug("Admin search query", {
|
|
520
|
+
fields,
|
|
521
|
+
model: config.model.modelName,
|
|
522
|
+
q,
|
|
523
|
+
});
|
|
524
|
+
try {
|
|
525
|
+
const results = await config.model.find({$or: orConditions}).limit(20).lean();
|
|
526
|
+
logger.debug("Admin search results", {
|
|
527
|
+
count: results.length,
|
|
528
|
+
model: config.model.modelName,
|
|
529
|
+
});
|
|
530
|
+
return res.json({data: results});
|
|
531
|
+
} catch (err) {
|
|
532
|
+
logger.error("Admin search failed", {
|
|
533
|
+
error: err,
|
|
534
|
+
fields,
|
|
535
|
+
model: config.model.modelName,
|
|
536
|
+
});
|
|
537
|
+
throw err;
|
|
538
|
+
}
|
|
539
|
+
})
|
|
540
|
+
);
|
|
541
|
+
}
|
|
542
|
+
|
|
374
543
|
// Mount modelRouter for each model with IsAdmin permissions
|
|
375
544
|
for (const config of modelConfigs) {
|
|
376
545
|
const hiddenFieldSet = new Set(config.hiddenFields ?? []);
|
|
546
|
+
// noExplicitAny: ModelRouterOptions<any> matches the Model<any> from AdminModelConfig.
|
|
377
547
|
const routerOptions: ModelRouterOptions<any> = {
|
|
378
548
|
...(openApi ? {openApi: openApi as OpenApiMiddleware} : {}),
|
|
379
549
|
permissions: {
|
|
@@ -385,8 +555,8 @@ export class AdminApp {
|
|
|
385
555
|
},
|
|
386
556
|
responseHandler:
|
|
387
557
|
hiddenFieldSet.size > 0
|
|
388
|
-
? async (value, _method, _request, _options): Promise<
|
|
389
|
-
removeHiddenFields(value, hiddenFieldSet) as
|
|
558
|
+
? async (value, _method, _request, _options): Promise<JSONValue> =>
|
|
559
|
+
removeHiddenFields(value, hiddenFieldSet) as JSONValue
|
|
390
560
|
: undefined,
|
|
391
561
|
sort: config.defaultSort ?? "-created",
|
|
392
562
|
};
|