diff --git a/functions/src/functions/defaultSeed.ts b/functions/src/functions/defaultSeed.ts index 3e8e3d46..5d27a403 100644 --- a/functions/src/functions/defaultSeed.ts +++ b/functions/src/functions/defaultSeed.ts @@ -23,6 +23,26 @@ import { type ServiceFactory } from '../services/factory/serviceFactory.js' import { type DebugDataService } from '../services/seeding/debugData/debugDataService.js' import { type TriggerService } from '../services/trigger/triggerService.js' +async function _seedClinicianCollections(input: { + debugData: DebugDataService + trigger: TriggerService + userId: string + patientId: string + patientName: string | undefined + components: UserDebugDataComponent[] +}): Promise { + const promises: Array> = [] + if (input.components.includes(UserDebugDataComponent.messages)) + promises.push( + input.debugData.seedClinicianMessages( + input.userId, + input.patientId, + input.patientName, + ), + ) + await Promise.all(promises) +} + async function _seedPatientCollections(input: { debugData: DebugDataService trigger: TriggerService @@ -105,17 +125,35 @@ export async function _defaultSeed( const userIds = await debugDataService.seedUsers() const userService = factory.user() + const allPatients = await userService.getAllPatients() + for (const userId of userIds) { try { const user = await userService.getUser(userId) - if (user?.content.type !== UserType.patient) continue - await _seedPatientCollections({ - debugData: debugDataService, - trigger: triggerService, - userId, - components: data.onlyUserCollections, - date: data.date, - }) + if (user?.content.type === UserType.patient) { + await _seedPatientCollections({ + debugData: debugDataService, + trigger: triggerService, + userId, + components: data.onlyUserCollections, + date: data.date, + }) + } else if (user?.content.type === UserType.clinician) { + const patient = allPatients.find( + (patient) => + patient.content.organization === user.content.organization, + ) + if (!patient) continue + const patientAuth = await userService.getAuth(user.id) + await _seedClinicianCollections({ + debugData: debugDataService, + trigger: triggerService, + userId, + components: data.onlyUserCollections, + patientName: patientAuth.displayName, + patientId: patient.id, + }) + } } catch (error) { logger.error(`Failed to seed user ${userId}: ${String(error)}`) } diff --git a/functions/src/services/seeding/debugData/debugDataService.ts b/functions/src/services/seeding/debugData/debugDataService.ts index 0392c9df..81cba14f 100644 --- a/functions/src/services/seeding/debugData/debugDataService.ts +++ b/functions/src/services/seeding/debugData/debugDataService.ts @@ -175,6 +175,36 @@ export class DebugDataService extends SeedingService { ) } + async seedClinicianMessages( + userId: string, + patientId: string, + patientName: string | undefined, + ) { + const values = [ + UserMessage.createInactiveForClinician({ + userId: patientId, + userName: patientName, + }), + UserMessage.createMedicationUptitrationForClinician({ + userId: patientId, + userName: patientName, + }), + UserMessage.createPreAppointmentForClinician({ + userId: patientId, + userName: patientName, + reference: '', + }), + UserMessage.createWeightGainForClinician({ + userId: patientId, + userName: patientName, + }), + ] + await this.replaceCollection( + (collections) => collections.userMessages(userId), + values, + ) + } + async seedUserBloodPressureObservations(userId: string, date: Date) { // This is just a list of pseudo-random numbers that is used to generate // the different user collections