Skip to content

Commit

Permalink
Add clinician seeding
Browse files Browse the repository at this point in the history
  • Loading branch information
arkadiuszbachorski committed Sep 23, 2024
1 parent cd56c71 commit 29e3ec7
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 8 deletions.
54 changes: 46 additions & 8 deletions functions/src/functions/defaultSeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
const promises: Array<Promise<void>> = []
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
Expand Down Expand Up @@ -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)}`)
}
Expand Down
30 changes: 30 additions & 0 deletions functions/src/services/seeding/debugData/debugDataService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 29e3ec7

Please sign in to comment.