diff --git a/functions/data/debug/users.json b/functions/data/debug/users.json index b78b69d7..34fd9f6c 100644 --- a/functions/data/debug/users.json +++ b/functions/data/debug/users.json @@ -187,6 +187,7 @@ "user": { "type": "patient", "organization": "stanford", + "clinician": "engagehf-clinician0-stanford.edu", "dateOfEnrollment": "1970-01-01T00:00:00.000Z", "lastActiveDate": "1970-01-01T00:00:00.000Z", "invitationCode": "SEEDING0" @@ -202,6 +203,7 @@ "user": { "type": "patient", "organization": "stanford", + "clinician": "engagehf-clinician1-stanford.edu", "dateOfEnrollment": "1970-01-01T00:00:00.000Z", "lastActiveDate": "1970-01-01T00:00:00.000Z", "invitationCode": "SEEDING1" @@ -232,6 +234,7 @@ "user": { "type": "patient", "organization": "jhu", + "clinician": "engagehf-clinician0-jhu.edu", "dateOfEnrollment": "1970-01-01T00:00:00.000Z", "lastActiveDate": "1970-01-01T00:00:00.000Z", "invitationCode": "SEEDING3" @@ -247,6 +250,7 @@ "user": { "type": "patient", "organization": "jhu", + "clinician": "engagehf-clinician0-jhu.edu", "dateOfEnrollment": "1970-01-01T00:00:00.000Z", "lastActiveDate": "1970-01-01T00:00:00.000Z", "invitationCode": "SEEDING4" @@ -262,6 +266,7 @@ "user": { "type": "patient", "organization": "jhu", + "clinician": "engagehf-clinician0-jhu.edu", "dateOfEnrollment": "1970-01-01T00:00:00.000Z", "lastActiveDate": "1970-01-01T00:00:00.000Z", "invitationCode": "SEEDING5" diff --git a/functions/src/functions/defaultSeed.ts b/functions/src/functions/defaultSeed.ts index 3e8e3d46..0bf2b5da 100644 --- a/functions/src/functions/defaultSeed.ts +++ b/functions/src/functions/defaultSeed.ts @@ -23,6 +23,24 @@ 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 + patients: Array<{ + id: string + name: string | undefined + }> + components: UserDebugDataComponent[] +}): Promise { + const promises: Array> = [] + if (input.components.includes(UserDebugDataComponent.messages)) + promises.push( + input.debugData.seedClinicianMessages(input.userId, input.patients), + ) + await Promise.all(promises) +} + async function _seedPatientCollections(input: { debugData: DebugDataService trigger: TriggerService @@ -105,17 +123,40 @@ 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 clinicianPatients = allPatients.filter( + (patient) => patient.content.clinician === user.id, + ) + const patients = await Promise.all( + clinicianPatients.map(async (patient) => { + const patientAuth = await userService.getAuth(patient.id) + return { + name: patientAuth.displayName, + id: patient.id, + } + }), + ) + await _seedClinicianCollections({ + debugData: debugDataService, + trigger: triggerService, + userId, + components: data.onlyUserCollections, + patients, + }) + } } 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..85756faa 100644 --- a/functions/src/services/seeding/debugData/debugDataService.ts +++ b/functions/src/services/seeding/debugData/debugDataService.ts @@ -175,6 +175,38 @@ export class DebugDataService extends SeedingService { ) } + async seedClinicianMessages( + userId: string, + patients: Array<{ + id: string + name: string | undefined + }>, + ) { + const values = patients.flatMap((patient) => [ + UserMessage.createInactiveForClinician({ + userId: patient.id, + userName: patient.name, + }), + UserMessage.createMedicationUptitrationForClinician({ + userId: patient.id, + userName: patient.name, + }), + UserMessage.createPreAppointmentForClinician({ + userId: patient.id, + userName: patient.name, + reference: '', + }), + UserMessage.createWeightGainForClinician({ + userId: patient.id, + userName: patient.name, + }), + ]) + 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