import { SchemaModel } from '@verb/src/libs/shared/base/base.model'; import { UserModel } from '@verb/src/libs/shared/base/user.model'; import { BASE_DEFINITIONS, BaseSchema, } from '@verb/src/libs/shared/json-schema-validator/schema-types'; import { CoreConfigurationModel, CoreConfigurationSchema, coreConfigurationValidationSchema, } from './core-configuration.model'; import { HostBillingModel, HostBillingSchema, hostBillingValidationSchema, } from './host-billing.model'; import { HostCredentialSchema, hostCredentialValidationSchema } from './host-credential.model'; import { HostIdentificationModel, HostIdentificationSchema, hostIdentificationValidationSchema, } from './host-identification.model'; import { OwnerModel, OwnerSchema, ownerValidationSchema } from './owner.model'; import { StreamConfigurationModel, StreamConfigurationSchema, streamConfigurationValidationSchema, } from './stream-configuration.model'; export class HostModel implements SchemaModel { public billing = new HostBillingModel(); public coreConfiguration = new CoreConfigurationModel(); public createdAt = new Date(); public identification = new HostIdentificationModel(); public owner = new OwnerModel(); public schemaVersion = 1; public streamConfiguration = new StreamConfigurationModel(); public credentials: Map = new Map(); public constructor(hostSchema?: HostSchema) { if (hostSchema) { this.fromSchema(hostSchema); } } public fromSchema(hostSchema: HostSchema) { this.billing = new HostBillingModel(hostSchema.billing); this.coreConfiguration = new CoreConfigurationModel(hostSchema.core_configuration); this.createdAt = new Date(hostSchema.created_at); this.owner = new OwnerModel(hostSchema.owner); this.identification = new HostIdentificationModel(hostSchema.identification); this.schemaVersion = hostSchema.schema_version; this.streamConfiguration = new StreamConfigurationModel(hostSchema.stream_configuration); this.credentials = new Map(); hostSchema.credentials?.forEach((credential: HostCredentialSchema) => { if (this.credentials.has(credential.key)) { throw new Error(`Duplicate credentials for key ${credential.key}`); } this.credentials.set(credential.key, credential); }); } public toSchema(): HostSchema { const credentials: HostCredentialSchema[] = []; for (const credential of this.credentials) { credentials.push(credential[1]); } return { credentials, billing: this.billing.toSchema(), core_configuration: this.coreConfiguration.toSchema(), created_at: this.createdAt.toUTCString(), identification: this.identification.toSchema(), owner: this.owner.toSchema(), schema_version: this.schemaVersion, stream_configuration: this.streamConfiguration.toSchema(), }; } public isOwner(userModel: UserModel) { return userModel.clientId === this.owner.clientId && userModel.userId === this.owner.userId; } public getCredential(key: string) { return this.credentials.get(key); } public setCredential(key: string, credential: HostCredentialSchema) { this.credentials.set(key, credential); } public deleteCredential(key: string) { this.credentials.delete(key); } public validate() { return this.identification.validate(); } } export interface HostSchema { billing: HostBillingSchema; core_configuration: CoreConfigurationSchema; created_at: string; credentials: HostCredentialSchema[]; identification: HostIdentificationSchema; owner: OwnerSchema; schema_version: number; stream_configuration: StreamConfigurationSchema; } export const hostValidationSchema: BaseSchema = { $id: 'http://myverb.com/host-metadata.schema.json', $schema: 'http://json-schema.org/draft-07/schema#', additionalProperties: false, definitions: BASE_DEFINITIONS, description: 'Validation Schema for Host Configuration', properties: { billing: hostBillingValidationSchema, core_configuration: coreConfigurationValidationSchema, created_at: { description: 'Create date Host Configuration', type: 'string', }, credentials: { description: 'Host Credentials', items: hostCredentialValidationSchema, type: 'array', }, identification: hostIdentificationValidationSchema, owner: ownerValidationSchema, schema_version: { description: 'Schema version', type: 'number', }, stream_configuration: streamConfigurationValidationSchema, }, required: ['created_at', 'identification', 'owner', 'stream_configuration'], title: 'Host Configuration Schema', type: 'object', };