import { OwnerModel, OwnerSchema, } from '@verb/src/libs/stream/host-metadata/versioning/v1/owner.model'; import { SchemaModel } from './base.model'; import { HostIdentificationSchema, IssuerMetadata, } from '@verb/src/libs/stream/host-metadata/versioning/v1/host-identification.model'; export class UserModel implements SchemaModel { public clientId = ''; public userId = ''; public email = ''; public familyName = ''; public givenName = ''; public nickname = ''; public picture = ''; public updatedAt = new Date(); public issuerMetadata: IssuerMetadata = {}; public constructor(schema?: HostIdentificationSchema) { if (schema) { this.fromSchema(schema); } } public fromSchema(schema: HostIdentificationSchema) { const splitSub = schema.sub.split('|'); const userId = splitSub.pop(); if (!userId) { throw new Error('userId not found in sub!'); } const clientId = splitSub.join('|'); if (!clientId) { throw new Error('clientId not found in sub!'); } this.clientId = clientId; this.userId = userId; this.email = schema.email; this.familyName = schema.family_name || ''; this.givenName = schema.given_name || ''; this.nickname = schema.nickname || ''; this.picture = schema.picture || ''; this.updatedAt = new Date(schema.updated_at || ''); this.issuerMetadata = schema.issuer_metadata || ''; } public toSchema(): HostIdentificationSchema { return { email: this.email, family_name: this.familyName, given_name: this.givenName, issuer_metadata: this.issuerMetadata, nickname: this.nickname, picture: this.picture, sub: `${this.clientId}|${this.userId}`, updated_at: this.updatedAt.toUTCString(), }; } public get owner(): OwnerModel { const ownerSchema: OwnerSchema = { client_id: this.clientId, user_id: this.userId, }; return new OwnerModel(ownerSchema); } public get sub() { return `${this.clientId}|${this.userId}`; } }