From b1dfec119672553ae08b25fdb34ee30528b5763f Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Mon, 8 May 2023 17:34:40 +0300 Subject: [PATCH] applications endpoints --- .eslintrc.json | 3 + src/server/Server.ts | 5 +- .../endpoints/api/management/Applications.ts | 55 +++++++++++++++++++ src/server/endpoints/api/management/Users.ts | 4 -- src/server/interfaces/Entity.ts | 4 +- 5 files changed, 64 insertions(+), 7 deletions(-) create mode 100644 src/server/endpoints/api/management/Applications.ts diff --git a/.eslintrc.json b/.eslintrc.json index b564753..e471dc9 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -93,6 +93,9 @@ "ignoreTemplateLiterals": true, "ignoreRegExpLiterals": true }], + + "max-lines-per-function": ["warn", 100], + "max-depth": ["warn", 3], "new-parens": "warn", "no-alert": "warn", "no-array-constructor": "warn", diff --git a/src/server/Server.ts b/src/server/Server.ts index 04d77c5..8578b1a 100644 --- a/src/server/Server.ts +++ b/src/server/Server.ts @@ -58,6 +58,7 @@ class Server extends EventEmitter { #logger: LoggerClient; + // eslint-disable-next-line max-lines-per-function constructor (options: ServerOptions) { super(); @@ -195,7 +196,6 @@ class Server extends EventEmitter { // Not sure why binding is a problem this.#_app.use(this.#attachMisc.bind(this) as never); this.#_app.use(this.#logRequest.bind(this) as never); // Logs every request - this.#_app.use(this.#logError.bind(this) as never); // Logs endpoints that error and sends a 500 this.#_app.use(this.#ready.bind(this) as never); // denies requests before the server is ready process.on('message', this._handleMessage.bind(this)); @@ -222,6 +222,9 @@ class Server extends EventEmitter { this.#logger.info('Loading endpoints'); await this.#_registry.loadEndpoints(); this.#logger.debug(this.#_registry.print); + + // Must come last, after loading endpoints + this.#_app.use(this.#logError.bind(this) as never); // Logs endpoints that error and sends a 500 this.#logger.info('Creating http server'); this.#_server = http.createServer(this.#_options.http, this.app).listen(this.port); diff --git a/src/server/endpoints/api/management/Applications.ts b/src/server/endpoints/api/management/Applications.ts new file mode 100644 index 0000000..48e03a1 --- /dev/null +++ b/src/server/endpoints/api/management/Applications.ts @@ -0,0 +1,55 @@ +import { Request, Response } from "../../../../../@types/Server.js"; +import PermissionValidationError from "../../../../util/errors/PermissionValidationError.js"; +import Server from "../../../Server.js"; +import ApiEndpoint from "../../../interfaces/ApiEndpoint.js"; +import UserDatabaseInterface from "../../../interfaces/UserDatabaseInterface.js"; + +class ApplicationsEndpoint extends ApiEndpoint { + + #users: UserDatabaseInterface; + + constructor (server: Server) { + super(server, { + name: 'Applications', + path: '/applications', + auth: true + }); + + this.#users = server.users; + + this.subpaths = [ + [ 'get', '/:id', this.getApp.bind(this), [ server.auth.createAuthoriser('administrator:applications', 5) ]], + [ 'post', '/:id/permissions', this.updatePermissions.bind(this), [ server.auth.createAuthoriser('administrator:applications', 5) ]] + ]; + + } + + async getApp (req: Request, res: Response): Promise { + const { id } = req.params; + const app = await this.#users.fetchApplication(id); + if (!app) + return void res.status(404).send('Invalid ID'); + res.json(app.json); + } + + async updatePermissions (req: Request, res: Response): Promise { + const { params, body } = req; + const { id } = params; + const app = await this.#users.fetchApplication(id); + if (!app) + return void res.status(404).send('Invalid ID'); + try { + await app.updatePermissions(body); + } catch (err) { + const error = err as Error; + if (err instanceof PermissionValidationError) + return void res.status(400).send(error.message); + this.logger.error(error.stack || error.message); + return void res.status(500).end(); + } + res.end(); + } + +} + +export default ApplicationsEndpoint; \ No newline at end of file diff --git a/src/server/endpoints/api/management/Users.ts b/src/server/endpoints/api/management/Users.ts index 9841c32..e95c13d 100644 --- a/src/server/endpoints/api/management/Users.ts +++ b/src/server/endpoints/api/management/Users.ts @@ -1,7 +1,3 @@ -// const { ApiEndpoint } = require("../../../interfaces"); -// const { PermissionValidationError } = require('../../../../util/errors'); -// const path = require('node:path'); - import path from 'path'; import Server from '../../../Server.js'; import { ApiEndpoint, UserDatabaseInterface } from '../../../interfaces/index.js'; diff --git a/src/server/interfaces/Entity.ts b/src/server/interfaces/Entity.ts index 0537db2..f53515a 100644 --- a/src/server/interfaces/Entity.ts +++ b/src/server/interfaces/Entity.ts @@ -42,7 +42,7 @@ class Entity { this.#_name = name; this.#_disabled = disabled ?? false; this.#_permissions = PermissionManager.merge(permissions || {}, PermissionManager.DefaultPermissions); - this.#_createdTimestamp = createdTimestamp || Date.now(); + this.#_createdTimestamp = createdTimestamp ?? Date.now(); this.#_cachedTimestamp = Date.now(); this.#_note = note ?? null; @@ -125,7 +125,7 @@ class Entity { } get createdTimestamp () { - return this.#_cachedTimestamp; + return this.#_createdTimestamp; } get cachedTimestamp () {