import { createLogger } from 'common/logger' ; import { CoachProfile , IntelligentInsightFeedback , IntelligentInsightFeedbackIded } from 'common/models' ; import Identify from 'common/models/Identify' ; import RepoError from './RepoError' ; import Collections from 'common/database/collections' ; import DBProvider , { serverOnly , documentSnapshot , querySnapshot , getIdentify , UnsubscribeSnapshot , DocumentSnapshotCallback , QuerySnapshotCallback , } from './dbProvider' ; import { PromptsLibraryIded , ClientLibraryStateIded , PromptsLibrary , ClientLibraryState } from 'common/models/prompts' ; const logger = createLogger ( '[CoachRepo]' , false ) ; type CoachProfileIded = Identify < CoachProfile > ; class CoachRepo { constructor ( protected readonly db ) { } private get collection ( ) { return this . db . collection ( Collections . Coaches ) ; } private get serverCollection ( ) { return serverOnly ( this . db ) . collection ( Collections . Coaches ) ; } getCoach ( uid ) ; getCoach ( uid , cb ) ; async getCoach ( uid , cb = null ) { if ( ! uid ) { throw RepoError . InvalidArg ( 'uid' ) ; } return documentSnapshot ( this . db , this . collection . doc ( uid ) , cb ) ; } async ensureAndUpdate ( uid , profile ) { if ( ! uid ) { throw RepoError . InvalidArg ( 'uid' ) ; } const doc = this . serverCollection . doc ( uid ) ; await doc . set ( profile , { merge : true } ) ; const resDoc = await doc . get ( ) ; return getIdentify < CoachProfileIded > ( resDoc ) ; } private getInsightsCollection ( coachId , clientCardId ) { return this . db . collection ( Collections . insightsFeedback ( coachId , clientCardId ) ) ; } getInsightsFeedback ( uid , clientCardId ) ; getInsightsFeedback ( uid , clientCardId , cb ) ; getInsightsFeedback ( uid , clientCardId , cb ? ) { const q = this . getInsightsCollection ( uid , clientCardId ) ; return querySnapshot ( this . db , q , cb ) ; } async saveInsightFeedback ( uid , clientCardId , item ) { const docRef = this . getInsightsCollection ( uid , clientCardId ) . doc ( ) ; await docRef . set ( item ) ; const doc = await docRef . get ( ) ; return getIdentify < CoachProfileIded > ( doc ) ; } getPromptsLibrary ( coachUid ) ; getPromptsLibrary ( coachUid , cb ) ; getPromptsLibrary ( coachUid , cb ? ) { if ( ! coachUid ) { throw new Error ( 'invalid args' ) ; } const docRef = this . db . doc ( Collections . promptsLibraryDoc ( coachUid ) ) ; return documentSnapshot ( this . db , docRef , cb ) ; } async updatePromptsLibrary ( coachUid , diff ) { if ( ! coachUid ) { throw new Error ( 'invalid args' ) ; } const docRef = serverOnly ( this . db ) . doc ( Collections . promptsLibraryDoc ( coachUid ) ) ; await docRef . set ( diff , { merge : true } ) ; const res = await docRef . get ( ) ; return getIdentify < PromptsLibraryIded > ( res ) ; } getClientPrompts ( coachUid , clientUid ) ; getClientPrompts ( coachUid , clientUid , cb ) ; getClientPrompts ( coachUid , clientUid , cb ? ) { if ( ! coachUid || ! clientUid ) { throw new Error ( 'invalid args' ) ; } const docRef = this . db . doc ( Collections . clientPromptsDoc ( coachUid , clientUid ) ) ; return documentSnapshot ( this . db , docRef , cb ) ; } async updateClientPrompts ( coachUid , clientUid , diff ) { if ( ! coachUid || ! clientUid ) { throw new Error ( 'invalid args' ) ; } const docRef = serverOnly ( this . db ) . doc ( Collections . clientPromptsDoc ( coachUid , clientUid ) ) ; await docRef . set ( diff , { merge : true } ) ; const res = await docRef . get ( ) ; return getIdentify < ClientLibraryStateIded > ( res ) ; } } export default CoachRepo ;