feat(model): refactor Group model and add groups attributes for inheritance

This commit is contained in:
Julien Oculi 2024-06-17 18:10:07 +02:00
parent fda7b03e59
commit 6d08698d10

View file

@ -1,32 +1,49 @@
import type { Posix, ToJson, UUID } from '@/types.ts' import type { Posix, ToJson, UUID } from '@/types.ts'
import { Ressource } from '@models/ressource.ts' import { Ressource } from '@models/ressource.ts'
import { Ref } from '@models'
export class Group extends Ressource { export class Group extends Ressource {
static fromJSON( static fromJSON(
{ posix, ...json }: ToJson<Group>, { posix, groups, ...json }: ToJson<Group>,
): Group { ): Group {
return new Group({ ...json, posix: posix ?? undefined }) return new Group({
...json,
posix: posix ?? undefined,
groups: groups.map((group) => Ref.fromString<Group>(group)),
})
} }
static create( static create(
{ posix, permissions, name }: Pick<Group, 'posix' | 'permissions' | 'name'>, { posix, permissions, name, groups }: Pick<
Group,
'posix' | 'permissions' | 'name' | 'groups'
>,
): Group { ): Group {
const { uuid, createdAt, updatedAt } = super.create({ name }) 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 #posix?: Posix
#permissions: { #permissions: Readonly<{
[serviceOrMachine: UUID]: { [serviceOrMachine: UUID]: {
read: boolean read: boolean
write: boolean write: boolean
execute: boolean execute: boolean
} }
} = {} }>
#groups: readonly Ref<Group>[]
private constructor( private constructor(
{ posix, permissions, ...props }: { posix, permissions, groups, ...props }:
& Pick<Group, 'posix' | 'permissions'> & Pick<Group, 'posix' | 'permissions' | 'groups'>
& Pick<Ressource, 'name' | 'uuid' | 'createdAt' | 'updatedAt'>, & Pick<Ressource, 'name' | 'uuid' | 'createdAt' | 'updatedAt'>,
) { ) {
super(props) super(props)
@ -50,7 +67,8 @@ export class Group extends Ressource {
this.#posix = posix this.#posix = posix
} }
this.permissions = Object.freeze(permissions) this.#permissions = Object.freeze(permissions)
this.#groups = Object.freeze(groups)
} }
get type(): 'group' { get type(): 'group' {
@ -62,7 +80,11 @@ export class Group extends Ressource {
} }
get permissions() { get permissions() {
return Object.freeze(this.#permissions) return this.#permissions
}
get groups() {
return this.#groups
} }
update(props: Partial<Omit<Group, 'type' | 'uuid' | 'createdAt'>>): Group { update(props: Partial<Omit<Group, 'type' | 'uuid' | 'createdAt'>>): Group {
@ -77,6 +99,7 @@ export class Group extends Ressource {
type: this.type, type: this.type,
posix: this.posix ?? null, posix: this.posix ?? null,
permissions: this.permissions, permissions: this.permissions,
groups: this.groups.map((group) => group.toJSON()),
} as const } as const
} }
@ -88,6 +111,7 @@ export class Group extends Ressource {
export interface Group extends Ressource { export interface Group extends Ressource {
type: 'group' type: 'group'
posix: Posix | undefined posix: Posix | undefined
groups: readonly Ref<Group>[]
permissions: Readonly<{ permissions: Readonly<{
[serviceOrMachine: UUID]: { [serviceOrMachine: UUID]: {
read: boolean read: boolean