import { SchemaModel } from '@verb/src/libs/shared/base/base.model'; import { HostPaymentModel, HostPaymentSchema, hostPaymentValidationSchema, } from './host-payment.model'; import { SubSchema } from '@verb/src/libs/shared/json-schema-validator/schema-types'; export class HostBillingModel implements SchemaModel { public accountType: AccountType = 'free'; public endDate: Date | null = null; public minutes = 0; public payments: HostPaymentModel[] = []; public constructor(hostCreditSchema?: HostBillingSchema) { if (hostCreditSchema) { this.fromSchema(hostCreditSchema); } } public fromSchema(schema: HostBillingSchema): void { this.accountType = schema.account_type; this.endDate = schema.end_date ? new Date(schema.end_date) : null; this.minutes = schema.minutes; this.payments = schema.payments.map(payment => new HostPaymentModel(payment)); } public toSchema(): HostBillingSchema { return { account_type: this.accountType, end_date: this.endDate ? this.endDate.toUTCString() : null, minutes: this.minutes, payments: this.payments.map(payment => payment.toSchema()), }; } } export type AccountType = 'free' | 'single' | 'subscription'; export interface HostBillingSchema { account_type: AccountType; end_date: string | null; minutes: number; payments: HostPaymentSchema[]; } export const hostBillingValidationSchema: SubSchema = { additionalProperties: false, description: 'Host Billing Validation Schema', properties: { account_type: { description: 'Host Billing Account Type', enum: ['free', 'single', 'subscription'], minLength: 1, type: 'string', }, end_date: { description: 'End date of current subscription', type: 'string', }, minutes: { description: 'Host Minutes Remaining', type: 'number', }, payments: { description: 'Host payment history', items: hostPaymentValidationSchema, type: 'array', }, }, required: ['account_type', 'minutes', 'payments'], type: 'object', };