From 6d08698d10bdb018c69a21cec26b62091df3f01a Mon Sep 17 00:00:00 2001 From: Julien Oculi Date: Mon, 17 Jun 2024 18:10:07 +0200 Subject: [PATCH] feat(model): :sparkles: refactor `Group` model and add `groups` attributes for inheritance --- src/models/src/group.ts | 44 +++++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/src/models/src/group.ts b/src/models/src/group.ts index 8a27af4..9acd75c 100644 --- a/src/models/src/group.ts +++ b/src/models/src/group.ts @@ -1,32 +1,49 @@ import type { Posix, ToJson, UUID } from '@/types.ts' import { Ressource } from '@models/ressource.ts' +import { Ref } from '@models' export class Group extends Ressource { static fromJSON( - { posix, ...json }: ToJson, + { posix, groups, ...json }: ToJson, ): Group { - return new Group({ ...json, posix: posix ?? undefined }) + return new Group({ + ...json, + posix: posix ?? undefined, + groups: groups.map((group) => Ref.fromString(group)), + }) } static create( - { posix, permissions, name }: Pick, + { posix, permissions, name, groups }: Pick< + Group, + 'posix' | 'permissions' | 'name' | 'groups' + >, ): Group { const { uuid, createdAt, updatedAt } = super.create({ name }) - return new Group({ uuid, createdAt, updatedAt, name, posix, permissions }) + return new Group({ + uuid, + createdAt, + updatedAt, + name, + posix, + permissions, + groups, + }) } #posix?: Posix - #permissions: { + #permissions: Readonly<{ [serviceOrMachine: UUID]: { read: boolean write: boolean execute: boolean } - } = {} + }> + #groups: readonly Ref[] private constructor( - { posix, permissions, ...props }: - & Pick + { posix, permissions, groups, ...props }: + & Pick & Pick, ) { super(props) @@ -50,7 +67,8 @@ export class Group extends Ressource { this.#posix = posix } - this.permissions = Object.freeze(permissions) + this.#permissions = Object.freeze(permissions) + this.#groups = Object.freeze(groups) } get type(): 'group' { @@ -62,7 +80,11 @@ export class Group extends Ressource { } get permissions() { - return Object.freeze(this.#permissions) + return this.#permissions + } + + get groups() { + return this.#groups } update(props: Partial>): Group { @@ -77,6 +99,7 @@ export class Group extends Ressource { type: this.type, posix: this.posix ?? null, permissions: this.permissions, + groups: this.groups.map((group) => group.toJSON()), } as const } @@ -88,6 +111,7 @@ export class Group extends Ressource { export interface Group extends Ressource { type: 'group' posix: Posix | undefined + groups: readonly Ref[] permissions: Readonly<{ [serviceOrMachine: UUID]: { read: boolean