From 9f6df93d9fd7077ac1d539f65ed68dbc39b53ebf Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Mon, 17 Jul 2023 18:52:25 +0300 Subject: [PATCH] flag delete functionality --- src/server/components/FlagManager.ts | 22 ++++++++++++----- src/server/endpoints/api/management/Flags.ts | 25 ++++++++++++-------- src/server/structures/Flag.ts | 16 ++++++++++++- 3 files changed, 46 insertions(+), 17 deletions(-) diff --git a/src/server/components/FlagManager.ts b/src/server/components/FlagManager.ts index 65bf644..1e04727 100644 --- a/src/server/components/FlagManager.ts +++ b/src/server/components/FlagManager.ts @@ -59,11 +59,14 @@ class FlagManager } - async #flagUpdate (incoming: { origin: number, flag: FlagData }) + #flagUpdate (incoming: { origin: number, flag: FlagData, delete: string }): void { - const { flag: data, origin } = incoming; + const { flag: data, origin, delete: del } = incoming; if (origin === this.#server.shardId) return; + if (del) + return void this.#flags.delete(del); + this.#logger.info(`Incoming flag update for ${data.name}`); const flag = this.#flags.get(data._id as string); if (!flag) @@ -134,9 +137,9 @@ class FlagManager data._id = (new ObjectId()).toString(); const flag = new Flag(this, data); - await this.#mongo.insertOne(this.#collectionName, flag.json); + await this.#mongo.insertOne(this.#collectionName, flag.jsonPrivate); this.#flags.set(flag.id, flag); - this.#broker?.publish('flagUpdates', { origin: this.#server.shardId, flag: flag.json }); + this.#broker?.publish('flagUpdates', { origin: this.#server.shardId, flag: flag.jsonPrivate }); return flag; } @@ -144,13 +147,20 @@ class FlagManager async updateFlag (flag: Flag): Promise { - const json = flag.json as { _id?: string }; + const json = flag.jsonPrivate as { _id?: string }; delete json._id; await this.#mongo.updateOne(this.#collectionName, { _id: flag.id }, json, true); - this.#broker?.publish('flagUpdates', { origin: this.#server.shardId, flag: flag.json }); + this.#broker?.publish('flagUpdates', { origin: this.#server.shardId, flag: flag.jsonPrivate }); } + async deleteFlag (flag: Flag) + { + const { id } = flag; + await this.#mongo.deleteOne(this.#collectionName, { _id: id }); + this.#broker?.publish('flagUpdates', { origin: this.#server.shardId, delete: id }); + } + } export default FlagManager; \ No newline at end of file diff --git a/src/server/endpoints/api/management/Flags.ts b/src/server/endpoints/api/management/Flags.ts index 5289a56..f3bbd94 100644 --- a/src/server/endpoints/api/management/Flags.ts +++ b/src/server/endpoints/api/management/Flags.ts @@ -29,11 +29,12 @@ class Flags extends ApiEndpoint this.methods = [ [ 'get', this.getFlags.bind(this) ], - [ 'post', this.createFlag.bind(this), [ server.auth.createAuthoriser('administrator:flags:create', 5), UtilityMiddleware.requireBody ]] + [ 'post', this.createFlag.bind(this), [ server.auth.createAuthoriser('administrator:flags:create', 5), UtilityMiddleware.requireBody ]], ]; this.subpaths = [ - [ 'patch', '/:id', this.updateFlag.bind(this), [ server.auth.createAuthoriser('administrator:flags:modify', 5), UtilityMiddleware.requireBody ]] + [ 'patch', '/:id', this.updateFlag.bind(this), [ server.auth.createAuthoriser('administrator:flags:modify', 5), UtilityMiddleware.requireBody ]], + [ 'delete', '/:id', this.deleteFlag.bind(this), [ server.auth.createAuthoriser('administrator:flags:delete', 5) ]] ]; this.middleware = [ server.auth.createAuthoriser('administrator:flags', 5) ]; @@ -81,13 +82,7 @@ class Flags extends ApiEndpoint const start = all ? 0 : (page - 1) * amount; const end = all ? flags.length : start + amount; res.json({ - flags: flags.slice(start, end).map(flag => - { - const { json } = flag; - json.id = flag.id; - delete json._id; - return { type: flag.type, ...json }; - }), + flags: flags.slice(start, end).map(flag => flag.json), page, pages: maxPage, tags @@ -110,9 +105,19 @@ class Flags extends ApiEndpoint const error = err as Error; return void res.status(400).send(error.message); } - + this.logger.info(`${req.user.name} created a flag ${flag.name}`); res.json(flag.json); + } + async deleteFlag (req: Request, res: Response): Promise + { + const { params } = req; + const { id } = params; + const flag = this.#flagManager.getFlag({ id }); + if (!flag) + return void res.status(404).end(); + await flag.delete(); + res.end(); } async updateFlag (req: Request, res: Response): Promise diff --git a/src/server/structures/Flag.ts b/src/server/structures/Flag.ts index 6aa84a4..23b09d5 100644 --- a/src/server/structures/Flag.ts +++ b/src/server/structures/Flag.ts @@ -60,6 +60,11 @@ class Flag return this.#manager.updateFlag(this); } + delete (): Promise + { + return this.#manager.deleteFlag(this); + } + get name () { return this.#name; @@ -122,7 +127,7 @@ class Flag this.#hierarchy = val; } - get json (): FlagData + get jsonPrivate (): FlagData { return { _id: this.id, @@ -134,6 +139,15 @@ class Flag }; } + get json () + { + const json = this.jsonPrivate; + json.id = json._id; + delete json._id; + return { ...json, type: this.type }; + } + + // get client () { // return (this.#env & BITS.Client) === BITS.Client; // }