diff --git a/src/models/src/credential.ts b/src/models/src/credential.ts index f325bf6..7641d0a 100644 --- a/src/models/src/credential.ts +++ b/src/models/src/credential.ts @@ -10,13 +10,21 @@ export class Credential extends Ressource { } static create( - { category, store, name }: Pick< + { category, store, name, avatar }: Pick< Credential, - 'category' | 'store' | 'name' + 'category' | 'store' | 'name' | 'avatar' >, ): Credential { - const { uuid, createdAt, updatedAt } = super.create({ name }) - return new Credential({ uuid, createdAt, updatedAt, name, category, store }) + const { uuid, createdAt, updatedAt } = super.create({ name, avatar }) + return new Credential({ + uuid, + createdAt, + updatedAt, + name, + category, + store, + avatar, + }) } #category: T @@ -25,7 +33,7 @@ export class Credential extends Ressource { private constructor( { category, store, ...props }: & Pick, 'category' | 'store'> - & Pick, + & Pick, ) { super(props) diff --git a/src/models/src/group.ts b/src/models/src/group.ts index 658cff9..fb33415 100644 --- a/src/models/src/group.ts +++ b/src/models/src/group.ts @@ -14,14 +14,15 @@ export class Group extends Ressource { } static create( - { posix, permissions, name, groups }: Pick< + { posix, permissions, name, avatar, groups }: Pick< Group, - 'posix' | 'permissions' | 'name' | 'groups' + 'posix' | 'permissions' | 'name' | 'avatar' | 'groups' >, ): Group { - const { uuid, createdAt, updatedAt } = super.create({ name }) + const { uuid, createdAt, updatedAt } = super.create({ name, avatar }) return new Group({ uuid, + avatar, createdAt, updatedAt, name, @@ -44,7 +45,7 @@ export class Group extends Ressource { private constructor( { posix, permissions, groups, ...props }: & Pick - & Pick, + & Pick, ) { super(props) diff --git a/src/models/src/machine.ts b/src/models/src/machine.ts index 7d4859c..8ce6d10 100644 --- a/src/models/src/machine.ts +++ b/src/models/src/machine.ts @@ -5,19 +5,19 @@ import { Ressource } from '@models/ressource.ts' export class Machine extends Ressource { static fromJSON( - { url, avatar, ...json }: ToJson, + { url, ...json }: ToJson, ): Machine { const groups = json.groups.map((group) => Ref.fromString(group)) - return new Machine({ ...json, url, avatar, groups }) + return new Machine({ ...json, url, groups }) } static create( - { tags, url, status, avatar, groups, ...props }: + { tags, url, status, groups, ...props }: & Pick - & Pick, + & Pick, ): Machine { - return new Machine({ ...props, tags, url, status, avatar, groups }) + return new Machine({ ...props, tags, url, status, groups }) } #tags: readonly string[] @@ -29,13 +29,12 @@ export class Machine extends Ressource { | 'discontinued' | 'error' | 'unknown' - #avatar: UrlString #groups: readonly Ref[] private constructor( - { tags, url, status, avatar, groups, ...props }: + { tags, url, status, groups, ...props }: & Pick - & Pick, + & Pick, ) { super(props) @@ -47,7 +46,6 @@ export class Machine extends Ressource { ) } this.#status = status - this.#avatar = new URL(avatar).href as UrlString this.#groups = Object.freeze(groups) } @@ -63,9 +61,6 @@ export class Machine extends Ressource { get status() { return this.#status } - get avatar() { - return this.#avatar - } get groups() { return this.#groups } @@ -85,7 +80,6 @@ export class Machine extends Ressource { tags: this.tags, url: this.url, status: this.status, - avatar: this.avatar, groups: Object.freeze(this.groups.map((group) => group.toJSON())), } as const } @@ -110,6 +104,5 @@ export interface Machine extends Ressource { | 'discontinued' | 'error' | 'unknown' - avatar: UrlString groups: readonly Ref[] } diff --git a/src/models/src/ressource.ts b/src/models/src/ressource.ts index e073519..b1b77a3 100644 --- a/src/models/src/ressource.ts +++ b/src/models/src/ressource.ts @@ -1,5 +1,5 @@ import { Ref } from '@/src/models/utils/ref.ts' -import type { DateString, ToJson, UUID } from '@/types.ts' +import type { DateString, ToJson, UrlString, UUID } from '@/types.ts' import { regex } from '@/utils.ts' export class Ressource { @@ -8,29 +8,33 @@ export class Ressource { ): Ressource { return new Ressource({ name: json.name, + avatar: json.avatar, uuid: json.uuid, createdAt: json.createdAt, updatedAt: json.updatedAt, }) } - static create({ name }: Pick): Ressource { + static create( + { name, avatar }: Pick, + ): Ressource { const uuid = crypto.randomUUID() as UUID const createdAt = new Date().toISOString() as DateString const updatedAt = createdAt - return new Ressource({ name, uuid, createdAt, updatedAt }) + return new Ressource({ name, avatar, uuid, createdAt, updatedAt }) } #name: string #uuid: UUID #createdAt: DateString #updatedAt: DateString + #avatar: UrlString protected constructor( - { name, uuid, createdAt, updatedAt }: Pick< + { name, avatar, uuid, createdAt, updatedAt }: Pick< Ressource, - 'name' | 'uuid' | 'createdAt' | 'updatedAt' + 'name' | 'avatar' | 'uuid' | 'createdAt' | 'updatedAt' >, ) { this.#name = name @@ -39,6 +43,7 @@ export class Ressource { `the following uuid "${uuid}" is not a valid UUID V4`, ) } + this.#avatar = new URL(avatar).href as UrlString this.#uuid = uuid if (!regex.isoDateString.test(createdAt)) { throw new SyntaxError( @@ -63,6 +68,9 @@ export class Ressource { get name(): string { return this.#name } + get avatar(): UrlString { + return this.#avatar + } get createdAt(): DateString { return this.#createdAt } @@ -83,6 +91,7 @@ export class Ressource { type: this.type, uuid: this.uuid, name: this.name, + avatar: this.avatar, createdAt: this.createdAt, updatedAt: this.updatedAt, } as const @@ -101,6 +110,7 @@ export interface Ressource { type: 'user' | 'machine' | 'service' | 'group' | 'credential' uuid: UUID name: string + avatar: UrlString createdAt: DateString updatedAt: DateString } diff --git a/src/models/src/service.ts b/src/models/src/service.ts index a9f9162..5d03a7c 100644 --- a/src/models/src/service.ts +++ b/src/models/src/service.ts @@ -19,7 +19,7 @@ export class Service extends Ressource { 'category' | 'tags' | 'url' | 'groups' | 'avatar' | 'name' >, ): Service { - const { uuid, createdAt, updatedAt } = super.create({ name }) + const { uuid, createdAt, updatedAt } = super.create({ name, avatar }) return new Service({ uuid, createdAt, @@ -36,7 +36,6 @@ export class Service extends Ressource { #category: 'web' | 'fs' | 'git' #url: UrlString #groups: readonly Ref[] - #avatar: UrlString #tags: readonly string[] private constructor({ @@ -44,7 +43,6 @@ export class Service extends Ressource { tags, url, groups, - avatar, ...ressource }: Pick< Service, @@ -68,7 +66,6 @@ export class Service extends Ressource { this.#category = category this.#url = new URL(url).href as UrlString this.#groups = Object.freeze(groups) - this.#avatar = new URL(avatar).href as UrlString this.#tags = Object.freeze(tags) } @@ -92,10 +89,6 @@ export class Service extends Ressource { return this.#groups } - get avatar() { - return this.#avatar - } - update( props: Partial>, ): Service { @@ -112,7 +105,6 @@ export class Service extends Ressource { tags: this.tags, url: this.url, groups: this.groups.map((group) => group.toJSON()), - avatar: this.avatar, } as const } @@ -131,5 +123,4 @@ export interface Service extends Ressource { tags: readonly string[] url: UrlString groups: readonly Ref[] - avatar: UrlString } diff --git a/src/models/src/user.ts b/src/models/src/user.ts index cfe9aab..cd42bd4 100644 --- a/src/models/src/user.ts +++ b/src/models/src/user.ts @@ -1,14 +1,12 @@ import { Ref } from '@/src/models/utils/ref.ts' -import type { Login, MailAddress, Posix, ToJson, UrlString } from '@/types.ts' +import type { Login, MailAddress, Posix, ToJson } from '@/types.ts' import { regex, toLogin } from '@/utils.ts' import type { Credential, CredentialCategory } from '@models/credential.ts' import type { Group } from '@models/group.ts' import { Ressource } from '@models/ressource.ts' export class User extends Ressource { - static fromJSON( - json: ToJson, - ): User { + static fromJSON(json: ToJson): User { if (json.type !== 'user') { throw new TypeError(`type is "${json.type}" but "user" is required`) } @@ -22,7 +20,10 @@ export class User extends Ressource { `firstname type is "${typeof json.firstname}" but "string" is required`, ) } - if (typeof json.login !== 'string' && /(\w|_)+\.(\w|_)+/.test(json.login)) { + if ( + typeof json.login !== 'string' && + /(\w|_)+\.(\w|_)+/.test(json.login) + ) { throw new TypeError( `login is "${json.login}" but "\${string}.\${string}" is required`, ) @@ -48,6 +49,7 @@ export class User extends Ressource { static create({ name, + avatar, ...props }: Pick< User, @@ -60,8 +62,8 @@ export class User extends Ressource { | 'avatar' | 'credentials' >): User { - const { uuid, createdAt, updatedAt } = super.create({ name }) - return new User({ name, uuid, createdAt, updatedAt, ...props }) + const { uuid, createdAt, updatedAt } = super.create({ name, avatar }) + return new User({ name, avatar, uuid, createdAt, updatedAt, ...props }) } #lastname: string @@ -70,7 +72,6 @@ export class User extends Ressource { #mail: MailAddress #groups: readonly Ref[] #posix?: Posix - #avatar: UrlString #credentials: readonly Ref>[] private constructor({ @@ -79,7 +80,6 @@ export class User extends Ressource { mail, groups, posix, - avatar, credentials, ...ressource }: Pick< @@ -112,7 +112,6 @@ export class User extends Ressource { } } this.#posix = posix - this.#avatar = new URL(avatar).href as UrlString this.#login = toLogin({ firstname, lastname }) this.#credentials = Object.freeze(credentials) } @@ -138,9 +137,6 @@ export class User extends Ressource { get posix() { return this.#posix } - get avatar() { - return this.#avatar - } get credentials() { return this.#credentials } @@ -182,6 +178,5 @@ export interface User extends Ressource { mail: MailAddress groups: readonly Ref[] posix: Posix | undefined - avatar: UrlString credentials: readonly Ref>[] }