import { AxiosInstance } from 'axios'; import { AuthorizationHeader, Id, Methods, UserData, FormData, CredentialsData, PersonData, } from '../types'; import { AddressWithRelations } from './address'; import { CreditWithRelations } from './credit'; import { hasMany } from './decorators'; import { FormWithRelations } from './form'; import { BaseRequestModelInterface } from './interfaces'; import { KycWithRelations } from './kyc'; import { LoanPartWithRelations } from './loan-part'; import { BaseRequestModel } from './models'; import { PersonWithRelations } from './person'; import { TinkLoanWithRelations } from './tink-loan'; import { TinkAccountWithRelations } from './tink-account'; export type FormInitOptions = { ignoreTink?: boolean; ignoreRoaring?: boolean }; @hasMany({ urlPath: 'forms', classProp: 'forms' }) @hasMany({ urlPath: 'persons', classProp: 'persons' }) @hasMany({ urlPath: 'addresses', classProp: 'addresses' }) @hasMany({ urlPath: 'loan-parts', classProp: 'loanParts' }) @hasMany({ urlPath: 'credits', classProp: 'credits' }) @hasMany({ urlPath: 'kycs', classProp: 'kycs' }) @hasMany({ urlPath: 'tink-loans', classProp: 'tinkLoans' }) @hasMany({ urlPath: 'tink-accounts', classProp: 'tinkAccounts' }) export class User extends BaseRequestModel implements BaseRequestModelInterface { constructor(api: AxiosInstance) { super(api, '/users'); this.login = this.login.bind(this); this.anonymousLogin = this.anonymousLogin.bind(this); this.formInit = this.formInit.bind(this); this.lookUpPerson = this.lookUpPerson.bind(this); } async login(credentials: CredentialsData) { const result = await this.request<{ token: string; user: UserData }>({ url: `${this.basePath}/login`, method: Methods.POST, data: credentials, }); if (result && result.token) { this.api.defaults.headers.common[ AuthorizationHeader ] = `Bearer ${result.token}`; } return result; } async anonymousLogin() { const result = await this.request<{ token: string }>({ url: `${this.basePath}/login/anonymous`, method: Methods.POST, }); if (result && result.token) { this.api.defaults.headers.common[ AuthorizationHeader ] = `Bearer ${result.token}`; } return result; } async formInit(userId: Id, opts: FormInitOptions = {}) { return this.request({ url: `${this.basePath}/${userId}/forms/init`, method: Methods.POST, params: { ignoreTink: opts.ignoreTink, ignoreRoaring: opts.ignoreRoaring, }, }); } async lookUpPerson(userId: Id, ssn: string) { return this.request({ url: `${this.basePath}/${userId}/roaring-perosns/fetch`, method: Methods.POST, params: { ssn, }, }); } } export interface UserRelations { forms?: FormWithRelations[]; persons?: PersonWithRelations[]; addresses?: AddressWithRelations[]; loanParts?: LoanPartWithRelations[]; tinkLoans?: TinkLoanWithRelations[]; tinkAccounts?: TinkAccountWithRelations[]; credits?: CreditWithRelations[]; kycs?: KycWithRelations[]; } export type UserWithRelations = User & UserRelations;