feat(model): add new static method load to create ressources from partial entry

This commit is contained in:
Julien Oculi 2024-06-18 14:05:45 +02:00
parent 873f4d23dc
commit 28a517323b
6 changed files with 105 additions and 0 deletions

View file

@ -1,6 +1,7 @@
import type { Base64String, ToJson, UUID } from '@/types.ts' import type { Base64String, ToJson, UUID } from '@/types.ts'
import { Ressource } from '@models/ressource.ts' import { Ressource } from '@models/ressource.ts'
import type { Ref } from '@models' import type { Ref } from '@models'
import { Avatar } from '@/src/models/utils/avatar.ts'
export class Credential<T extends CredentialCategory> extends Ressource { export class Credential<T extends CredentialCategory> extends Ressource {
static fromJSON<T extends CredentialCategory>( static fromJSON<T extends CredentialCategory>(
@ -27,6 +28,20 @@ export class Credential<T extends CredentialCategory> extends Ressource {
}) })
} }
static load<T extends CredentialCategory>({
category,
store,
name,
avatar = Avatar.fromEmoji('🔑'),
}: {
name: Credential<T>['name']
category: T
store: Credential<T>['store']
avatar?: Credential<T>['avatar']
}): Credential<T> {
return this.create({ category, store, name, avatar })
}
#category: T #category: T
#store: Readonly<CredentialStore<T>> #store: Readonly<CredentialStore<T>>

View file

@ -1,6 +1,7 @@
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' import { Ref } from '@models'
import { Avatar } from '@/src/models/utils/avatar.ts'
export class Group extends Ressource { export class Group extends Ressource {
static fromJSON( static fromJSON(
@ -32,6 +33,22 @@ export class Group extends Ressource {
}) })
} }
static load({
name,
groups = [],
permissions = {},
posix,
avatar = Avatar.fromEmoji('👥'),
}: {
name: Group['name']
permissions?: Group['permissions']
posix?: Group['posix']
avatar?: Group['avatar']
groups?: Group['groups']
}): Group {
return this.create({ name, groups, permissions, posix, avatar })
}
#posix?: Posix #posix?: Posix
#permissions: Readonly<{ #permissions: Readonly<{
[serviceOrMachine: UUID]: { [serviceOrMachine: UUID]: {

View file

@ -2,6 +2,7 @@ import { Ref } from '@/src/models/utils/ref.ts'
import type { ToJson, UrlString } from '@/types.ts' import type { ToJson, UrlString } from '@/types.ts'
import type { Group } from '@models/group.ts' import type { Group } from '@models/group.ts'
import { Ressource } from '@models/ressource.ts' import { Ressource } from '@models/ressource.ts'
import { Avatar } from '@/src/models/utils/avatar.ts'
export class Machine extends Ressource { export class Machine extends Ressource {
static fromJSON( static fromJSON(
@ -32,6 +33,24 @@ export class Machine extends Ressource {
}) })
} }
static load({
name,
avatar = Avatar.fromEmoji('🖨️'),
tags = [],
url,
status = 'unknown',
groups = [],
}: {
name: Machine['name']
avatar?: Machine['avatar']
tags?: Machine['tags']
url: Machine['url']
status?: Machine['status']
groups?: Machine['groups']
}): Machine {
return this.create({ name, avatar, tags, url, status, groups })
}
#tags: readonly string[] #tags: readonly string[]
#url: UrlString #url: UrlString
#status: #status:

View file

@ -25,6 +25,10 @@ export class Ressource {
return new Ressource({ name, avatar, uuid, createdAt, updatedAt }) return new Ressource({ name, avatar, uuid, createdAt, updatedAt })
} }
static load(_options: never): Ressource {
throw new Error('not implemented')
}
#name: string #name: string
#uuid: UUID #uuid: UUID
#createdAt: DateString #createdAt: DateString

View file

@ -2,6 +2,7 @@ import { Ref } from '@/src/models/utils/ref.ts'
import type { ToJson, UrlString } from '@/types.ts' import type { ToJson, UrlString } from '@/types.ts'
import type { Group } from '@models/group.ts' import type { Group } from '@models/group.ts'
import { Ressource } from '@models/ressource.ts' import { Ressource } from '@models/ressource.ts'
import { Avatar } from '@/src/models/utils/avatar.ts'
export class Service extends Ressource { export class Service extends Ressource {
static fromJSON( static fromJSON(
@ -33,6 +34,24 @@ export class Service extends Ressource {
}) })
} }
static load({
name,
avatar = Avatar.fromEmoji('⚙️'),
tags = [],
url,
category,
groups = [],
}: {
name: Service['name']
url: Service['url']
avatar?: Service['avatar']
tags?: Service['tags']
category: Service['category']
groups?: Service['groups']
}): Service {
return this.create({ name, avatar, tags, url, category, groups })
}
#category: 'web' | 'fs' | 'git' #category: 'web' | 'fs' | 'git'
#url: UrlString #url: UrlString
#groups: readonly Ref<Group>[] #groups: readonly Ref<Group>[]

View file

@ -4,6 +4,7 @@ import { regex, toLogin } from '@/utils.ts'
import type { Credential, CredentialCategory } from '@models/credential.ts' import type { Credential, CredentialCategory } from '@models/credential.ts'
import type { Group } from '@models/group.ts' import type { Group } from '@models/group.ts'
import { Ressource } from '@models/ressource.ts' import { Ressource } from '@models/ressource.ts'
import { Avatar } from '@/src/models/utils/avatar.ts'
export class User extends Ressource { export class User extends Ressource {
static fromJSON(json: ToJson<User>): User { static fromJSON(json: ToJson<User>): User {
@ -66,6 +67,36 @@ export class User extends Ressource {
return new User({ name, avatar, uuid, createdAt, updatedAt, ...props }) return new User({ name, avatar, uuid, createdAt, updatedAt, ...props })
} }
static load({
lastname,
firstname,
mail,
groups = [],
posix,
avatar = Avatar.fromEmoji('👤'),
credentials = [],
}: {
lastname: User['lastname']
firstname: User['firstname']
mail: User['mail']
groups?: User['groups']
posix?: User['posix']
avatar?: User['avatar']
credentials?: User['credentials']
}): User {
const name = `${firstname} ${lastname}`
return this.create({
name,
lastname,
firstname,
mail,
groups,
posix,
avatar,
credentials,
})
}
#lastname: string #lastname: string
#firstname: string #firstname: string
#login: Login #login: Login