@terreno/admin-backend 0.7.0 → 0.7.1

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 CHANGED
@@ -167,6 +167,7 @@ var AdminApp = /** @class */ (function () {
167
167
  */
168
168
  AdminApp.prototype.register = function (app) {
169
169
  var e_2, _a;
170
+ var _this = this;
170
171
  var _b, _c, _d;
171
172
  var basePath = (_b = this.options.basePath) !== null && _b !== void 0 ? _b : "/admin";
172
173
  var modelConfigs = this.options.models;
@@ -233,11 +234,115 @@ var AdminApp = /** @class */ (function () {
233
234
  description: script.description,
234
235
  name: script.name,
235
236
  }); });
236
- var configResponse = { models: configModels, scripts: configScripts };
237
+ var configResponse = {
238
+ customScreens: [
239
+ {
240
+ displayName: "Version Config",
241
+ name: "version-config",
242
+ },
243
+ ],
244
+ models: configModels,
245
+ scripts: configScripts,
246
+ };
237
247
  // GET /admin/config
238
- app.get("".concat(basePath, "/config"), function (_req, res) {
239
- return res.json(configResponse);
240
- });
248
+ app.get("".concat(basePath, "/config"), (0, api_1.authenticateMiddleware)(), (0, api_1.asyncHandler)(function (req, res) { return __awaiter(_this, void 0, void 0, function () {
249
+ return __generator(this, function (_a) {
250
+ switch (_a.label) {
251
+ case 0: return [4 /*yield*/, (0, api_1.checkPermissions)("read", [api_1.Permissions.IsAdmin], req.user)];
252
+ case 1:
253
+ if (!(_a.sent())) {
254
+ throw new api_1.APIError({ status: 403, title: "Admin access required" });
255
+ }
256
+ return [2 /*return*/, res.json(configResponse)];
257
+ }
258
+ });
259
+ }); }));
260
+ // Version config singleton routes (GET/PUT /admin/version-config)
261
+ var versionConfigPath = "".concat(basePath, "/version-config");
262
+ app.get(versionConfigPath, (0, api_1.authenticateMiddleware)(), (0, api_1.asyncHandler)(function (req, res) { return __awaiter(_this, void 0, void 0, function () {
263
+ var config, defaults;
264
+ return __generator(this, function (_a) {
265
+ switch (_a.label) {
266
+ case 0: return [4 /*yield*/, (0, api_1.checkPermissions)("read", [api_1.Permissions.IsAdmin], req.user)];
267
+ case 1:
268
+ if (!(_a.sent())) {
269
+ throw new api_1.APIError({ status: 403, title: "Admin access required" });
270
+ }
271
+ return [4 /*yield*/, api_1.VersionConfig.findOneOrNone({ _singleton: "config" })];
272
+ case 2:
273
+ config = _a.sent();
274
+ defaults = {
275
+ mobileRequiredVersion: 0,
276
+ mobileWarningVersion: 0,
277
+ requiredMessage: "This version is no longer supported. Please update to continue.",
278
+ updateUrl: undefined,
279
+ warningMessage: "A new version is available. Please update for the best experience.",
280
+ webRequiredVersion: 0,
281
+ webWarningVersion: 0,
282
+ };
283
+ return [2 /*return*/, res.json(config !== null && config !== void 0 ? config : defaults)];
284
+ }
285
+ });
286
+ }); }));
287
+ app.put(versionConfigPath, (0, api_1.authenticateMiddleware)(), (0, api_1.asyncHandler)(function (req, res) { return __awaiter(_this, void 0, void 0, function () {
288
+ var raw, allowedFields, setFields, unsetFields, allowedFields_1, allowedFields_1_1, field, updateOp, doc;
289
+ var e_5, _a;
290
+ return __generator(this, function (_b) {
291
+ switch (_b.label) {
292
+ case 0: return [4 /*yield*/, (0, api_1.checkPermissions)("update", [api_1.Permissions.IsAdmin], req.user)];
293
+ case 1:
294
+ if (!(_b.sent())) {
295
+ throw new api_1.APIError({ status: 403, title: "Admin access required" });
296
+ }
297
+ raw = req.body;
298
+ allowedFields = [
299
+ "mobileRequiredVersion",
300
+ "mobileWarningVersion",
301
+ "requiredMessage",
302
+ "updateUrl",
303
+ "webRequiredVersion",
304
+ "webWarningVersion",
305
+ "warningMessage",
306
+ ];
307
+ setFields = {};
308
+ unsetFields = {};
309
+ try {
310
+ for (allowedFields_1 = __values(allowedFields), allowedFields_1_1 = allowedFields_1.next(); !allowedFields_1_1.done; allowedFields_1_1 = allowedFields_1.next()) {
311
+ field = allowedFields_1_1.value;
312
+ if (raw[field] === null) {
313
+ unsetFields[field] = 1;
314
+ }
315
+ else if (raw[field] !== undefined) {
316
+ setFields[field] = raw[field];
317
+ }
318
+ }
319
+ }
320
+ catch (e_5_1) { e_5 = { error: e_5_1 }; }
321
+ finally {
322
+ try {
323
+ if (allowedFields_1_1 && !allowedFields_1_1.done && (_a = allowedFields_1.return)) _a.call(allowedFields_1);
324
+ }
325
+ finally { if (e_5) throw e_5.error; }
326
+ }
327
+ updateOp = {};
328
+ if (Object.keys(setFields).length > 0) {
329
+ updateOp.$set = setFields;
330
+ }
331
+ if (Object.keys(unsetFields).length > 0) {
332
+ updateOp.$unset = unsetFields;
333
+ }
334
+ return [4 /*yield*/, api_1.VersionConfig.findOneAndUpdate({ _singleton: "config" }, updateOp, {
335
+ new: true,
336
+ runValidators: true,
337
+ setDefaultsOnInsert: true,
338
+ upsert: true,
339
+ }).lean()];
340
+ case 2:
341
+ doc = _b.sent();
342
+ return [2 /*return*/, res.json(doc)];
343
+ }
344
+ });
345
+ }); }));
241
346
  try {
242
347
  // Mount modelRouter for each model with IsAdmin permissions
243
348
  for (var modelConfigs_1 = __values(modelConfigs), modelConfigs_1_1 = modelConfigs_1.next(); !modelConfigs_1_1.done; modelConfigs_1_1 = modelConfigs_1.next()) {
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.7.0",
8
+ "@terreno/api": "0.7.1",
9
9
  "express": "^5.2.1",
10
10
  "luxon": "^3.7.2",
11
11
  "multer": "^1.4.5-lts.1"
@@ -49,5 +49,5 @@
49
49
  "test:ci": "bun test"
50
50
  },
51
51
  "types": "dist/index.d.ts",
52
- "version": "0.7.0"
52
+ "version": "0.7.1"
53
53
  }
package/src/adminApp.ts CHANGED
@@ -4,6 +4,7 @@ import {
4
4
  authenticateMiddleware,
5
5
  BackgroundTask,
6
6
  type BackgroundTaskDocument,
7
+ checkPermissions,
7
8
  getOpenApiSpecForModel,
8
9
  logger,
9
10
  type ModelRouterOptions,
@@ -13,6 +14,7 @@ import {
13
14
  type ScriptResult,
14
15
  type ScriptRunner,
15
16
  TaskCancelledError,
17
+ VersionConfig,
16
18
  } from "@terreno/api";
17
19
  import express from "express";
18
20
  import {DateTime} from "luxon";
@@ -91,6 +93,7 @@ interface AdminScriptMeta {
91
93
  }
92
94
 
93
95
  interface AdminConfigResponse {
96
+ customScreens?: {displayName: string; name: string}[];
94
97
  models: AdminModelMeta[];
95
98
  scripts: AdminScriptMeta[];
96
99
  }
@@ -236,12 +239,93 @@ export class AdminApp {
236
239
  name: script.name,
237
240
  }));
238
241
 
239
- const configResponse: AdminConfigResponse = {models: configModels, scripts: configScripts};
242
+ const configResponse: AdminConfigResponse = {
243
+ customScreens: [
244
+ {
245
+ displayName: "Version Config",
246
+ name: "version-config",
247
+ },
248
+ ],
249
+ models: configModels,
250
+ scripts: configScripts,
251
+ };
240
252
 
241
253
  // GET /admin/config
242
- app.get(`${basePath}/config`, (_req, res) => {
243
- return res.json(configResponse);
244
- });
254
+ app.get(
255
+ `${basePath}/config`,
256
+ authenticateMiddleware(),
257
+ asyncHandler(async (req, res) => {
258
+ if (!(await checkPermissions("read", [Permissions.IsAdmin], req.user as any))) {
259
+ throw new APIError({status: 403, title: "Admin access required"});
260
+ }
261
+ return res.json(configResponse);
262
+ })
263
+ );
264
+
265
+ // Version config singleton routes (GET/PUT /admin/version-config)
266
+ const versionConfigPath = `${basePath}/version-config`;
267
+ app.get(
268
+ versionConfigPath,
269
+ authenticateMiddleware(),
270
+ asyncHandler(async (req, res) => {
271
+ if (!(await checkPermissions("read", [Permissions.IsAdmin], req.user as any))) {
272
+ throw new APIError({status: 403, title: "Admin access required"});
273
+ }
274
+ const config = await VersionConfig.findOneOrNone({_singleton: "config"});
275
+ const defaults = {
276
+ mobileRequiredVersion: 0,
277
+ mobileWarningVersion: 0,
278
+ requiredMessage: "This version is no longer supported. Please update to continue.",
279
+ updateUrl: undefined as string | undefined,
280
+ warningMessage: "A new version is available. Please update for the best experience.",
281
+ webRequiredVersion: 0,
282
+ webWarningVersion: 0,
283
+ };
284
+ return res.json(config ?? defaults);
285
+ })
286
+ );
287
+ app.put(
288
+ versionConfigPath,
289
+ authenticateMiddleware(),
290
+ asyncHandler(async (req, res) => {
291
+ if (!(await checkPermissions("update", [Permissions.IsAdmin], req.user as any))) {
292
+ throw new APIError({status: 403, title: "Admin access required"});
293
+ }
294
+ const raw = req.body as Record<string, unknown>;
295
+ const allowedFields = [
296
+ "mobileRequiredVersion",
297
+ "mobileWarningVersion",
298
+ "requiredMessage",
299
+ "updateUrl",
300
+ "webRequiredVersion",
301
+ "webWarningVersion",
302
+ "warningMessage",
303
+ ] as const;
304
+ const setFields: Record<string, unknown> = {};
305
+ const unsetFields: Record<string, 1> = {};
306
+ for (const field of allowedFields) {
307
+ if (raw[field] === null) {
308
+ unsetFields[field] = 1;
309
+ } else if (raw[field] !== undefined) {
310
+ setFields[field] = raw[field];
311
+ }
312
+ }
313
+ const updateOp: Record<string, unknown> = {};
314
+ if (Object.keys(setFields).length > 0) {
315
+ updateOp.$set = setFields;
316
+ }
317
+ if (Object.keys(unsetFields).length > 0) {
318
+ updateOp.$unset = unsetFields;
319
+ }
320
+ const doc = await VersionConfig.findOneAndUpdate({_singleton: "config"}, updateOp, {
321
+ new: true,
322
+ runValidators: true,
323
+ setDefaultsOnInsert: true,
324
+ upsert: true,
325
+ }).lean();
326
+ return res.json(doc);
327
+ })
328
+ );
245
329
 
246
330
  // Mount modelRouter for each model with IsAdmin permissions
247
331
  for (const config of modelConfigs) {