Skip to content

Commit

Permalink
Seed clinician messages (#142)
Browse files Browse the repository at this point in the history
# Seed clinician messages

## ♻️ Current situation & Problem
As a part of dev process, it's great to see all possible messages. This
is very minimal implementation of clinician messages seed.


### Code of Conduct & Contributing Guidelines 

By submitting creating this pull request, you agree to follow our [Code
of
Conduct](https://github.com/StanfordBDHG/.github/blob/main/CODE_OF_CONDUCT.md)
and [Contributing
Guidelines](https://github.com/StanfordBDHG/.github/blob/main/CONTRIBUTING.md):
- [x] I agree to follow the [Code of
Conduct](https://github.com/StanfordBDHG/.github/blob/main/CODE_OF_CONDUCT.md)
and [Contributing
Guidelines](https://github.com/StanfordBDHG/.github/blob/main/CONTRIBUTING.md).
  • Loading branch information
arkadiuszbachorski authored Oct 1, 2024
1 parent 430235f commit 488c829
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 8 deletions.
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

0 comments on commit 488c829

Please sign in to comment.