import { computed , observable } from 'mobx' ; import Firebase from 'common/services/firebase' ; import { Clients as ClientsFunctions } from 'common/abstractions/functions' ; import { ClientIntakeForm , AssessmentType , ClientAssessments , ClientAccountIded , ClientIntakeFormIded , } from 'common/models' ; import { TimeObservable } from 'common/utils/timeObservable' ; import { AssessmentsController as AssessmentsResponsesController } from 'common/controllers/AssessmentsController' ; import { IDisposable } from 'common/utils/unsubscriber' ; export interface IAssessmentsController { readonly nextFormTypeAvailable : AssessmentType | false ; readonly responses ? : AssessmentsResponsesController ; addResponse ( entry ) : Promise < ClientIntakeFormIded > ; isActive ( type ) : boolean ; } const FetchResponses = false ; export class AssessmentsController implements IAssessmentsController , IDisposable { @ observable . ref private _assessments : ClientAssessments ; private _clientCardId : string ; private _now = new TimeObservable ( 1000 * 30 ) ; private readonly _responses = FetchResponses ? new AssessmentsResponsesController ( ) : null ; @ computed public get nextFormTypeAvailable ( ) { if ( ! this . _assessments ) { return null ; } const nextActive = AssessmentType . EnabledTypes . value . find ( ( value ) => this . isActive ( value ) ) ; return nextActive ; } public get responses ( ) { return this . _responses ; } public isActive ( type ) { return ClientAssessments . getIsCooledDown ( this . _assessments , type , this . _now . nowMs ) ; } initialize ( account , clientUid ) { this . _clientCardId = account . id ; this . _assessments = account ?. assessments ; this . _responses ?. setAccount ( clientUid , account . id , account . coachId ) ; } async addResponse ( entry ) { if ( ! process . appFeatures . ASSESSMENTS_ENABLED ) { throw new Error ( 'IntakeForms feature is disabled' ) ; } try { const result = await Firebase . Instance . getFunction ( ClientsFunctions . AddIntakeFormResponse ) . execute ( { entry , accountId : this . _clientCardId } ) ; if ( result . account . assessments ) { this . _assessments = result . account . assessments ; } return result . entry ; } catch ( e ) { if ( e . code === 'failed-precondition' ) { this . _assessments [ entry . formType ] = { active : false } ; } return null ; } } dispose ( ) { this . _now . dispose ( ) ; this . _responses ?. dispose ( ) ; } }