Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Seed clinician messages #142

Merged
merged 3 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions functions/data/debug/users.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand Down
57 changes: 49 additions & 8 deletions functions/src/functions/defaultSeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
const promises: Array<Promise<void>> = []
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
Expand Down Expand Up @@ -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)}`)
}
Expand Down
32 changes: 32 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,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
Expand Down
Loading