flag delete functionality

This commit is contained in:
Erik 2023-07-17 18:52:25 +03:00
parent f908620c46
commit 9f6df93d9f
Signed by: Navy.gif
GPG Key ID: 2532FBBB61C65A68
3 changed files with 46 additions and 17 deletions

View File

@ -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) if (origin === this.#server.shardId)
return; return;
if (del)
return void this.#flags.delete(del);
this.#logger.info(`Incoming flag update for ${data.name}`); this.#logger.info(`Incoming flag update for ${data.name}`);
const flag = this.#flags.get(data._id as string); const flag = this.#flags.get(data._id as string);
if (!flag) if (!flag)
@ -134,9 +137,9 @@ class FlagManager
data._id = (new ObjectId()).toString(); data._id = (new ObjectId()).toString();
const flag = new Flag(this, data); 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.#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; return flag;
} }
@ -144,13 +147,20 @@ class FlagManager
async updateFlag (flag: Flag): Promise<void> async updateFlag (flag: Flag): Promise<void>
{ {
const json = flag.json as { _id?: string }; const json = flag.jsonPrivate as { _id?: string };
delete json._id; delete json._id;
await this.#mongo.updateOne(this.#collectionName, { _id: flag.id }, json, true); 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; export default FlagManager;

View File

@ -29,11 +29,12 @@ class Flags extends ApiEndpoint
this.methods = [ this.methods = [
[ 'get', this.getFlags.bind(this) ], [ '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 = [ 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) ]; this.middleware = [ server.auth.createAuthoriser('administrator:flags', 5) ];
@ -81,13 +82,7 @@ class Flags extends ApiEndpoint
const start = all ? 0 : (page - 1) * amount; const start = all ? 0 : (page - 1) * amount;
const end = all ? flags.length : start + amount; const end = all ? flags.length : start + amount;
res.json({ res.json({
flags: flags.slice(start, end).map(flag => flags: flags.slice(start, end).map(flag => flag.json),
{
const { json } = flag;
json.id = flag.id;
delete json._id;
return { type: flag.type, ...json };
}),
page, page,
pages: maxPage, pages: maxPage,
tags tags
@ -110,9 +105,19 @@ class Flags extends ApiEndpoint
const error = err as Error; const error = err as Error;
return void res.status(400).send(error.message); return void res.status(400).send(error.message);
} }
this.logger.info(`${req.user.name} created a flag ${flag.name}`);
res.json(flag.json); res.json(flag.json);
}
async deleteFlag (req: Request, res: Response): Promise<void>
{
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<void> async updateFlag (req: Request, res: Response): Promise<void>

View File

@ -60,6 +60,11 @@ class Flag
return this.#manager.updateFlag(this); return this.#manager.updateFlag(this);
} }
delete (): Promise<void>
{
return this.#manager.deleteFlag(this);
}
get name () get name ()
{ {
return this.#name; return this.#name;
@ -122,7 +127,7 @@ class Flag
this.#hierarchy = val; this.#hierarchy = val;
} }
get json (): FlagData get jsonPrivate (): FlagData
{ {
return { return {
_id: this.id, _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 () { // get client () {
// return (this.#env & BITS.Client) === BITS.Client; // return (this.#env & BITS.Client) === BITS.Client;
// } // }