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

Owner Messages #165

Merged
merged 4 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 6 additions & 3 deletions functions/models/src/types/userMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export class UserMessage {
creationDate?: Date
userId: string
userName?: string
reference: string
}): UserMessage {
return new UserMessage({
creationDate: input.creationDate ?? new Date(),
Expand All @@ -103,7 +104,7 @@ export class UserMessage {
action: `users/${input.userId}`,
type: UserMessageType.inactive,
isDismissible: true,
reference: `users/${input.userId}`,
reference: input.reference,
})
}

Expand Down Expand Up @@ -153,6 +154,7 @@ export class UserMessage {
creationDate?: Date
userId: string
userName?: string
reference: string
}): UserMessage {
return new UserMessage({
creationDate: input.creationDate ?? new Date(),
Expand All @@ -162,7 +164,7 @@ export class UserMessage {
description: new LocalizedText({
en: `${input.userName ?? 'Patient'} may be eligible for med changes. You can review med information on the user detail page.`,
}),
reference: `users/${input.userId}`,
reference: input.reference,
action: `users/${input.userId}/medications`,
type: UserMessageType.medicationUptitration,
isDismissible: true,
Expand Down Expand Up @@ -271,6 +273,7 @@ export class UserMessage {
creationDate?: Date
userId: string
userName?: string
reference: string
}): UserMessage {
return new UserMessage({
creationDate: input.creationDate ?? new Date(),
Expand All @@ -281,7 +284,7 @@ export class UserMessage {
en: `Weight increase over 3 lbs for ${input.userName ?? 'patient'}.`,
}),
action: `users/${input.userId}/medications`,
reference: `users/${input.userId}`,
reference: input.reference,
type: UserMessageType.weightGain,
isDismissible: true,
})
Expand Down
62 changes: 38 additions & 24 deletions functions/src/services/message/defaultMessageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export class DefaultMessageService implements MessageService {
notify: boolean
user?: User | null
},
): Promise<void> {
): Promise<Document<UserMessage> | undefined> {
const newMessage = await this.databaseService.runTransaction(
async (collections, transaction) => {
logger.debug(
Expand Down Expand Up @@ -180,30 +180,13 @@ export class DefaultMessageService implements MessageService {
`DatabaseMessageService.addMessage(user: ${userId}): System will notify user unless user settings prevent it`,
)

const user =
options.user ?? (await this.userService.getUser(userId))?.content
if (!user) return

switch (message.type) {
case UserMessageType.medicationChange:
if (!user.receivesMedicationUpdates) return
case UserMessageType.weightGain:
if (!user.receivesWeightAlerts) return
case UserMessageType.medicationUptitration:
if (!user.receivesRecommendationUpdates) return
case UserMessageType.welcome:
break
case UserMessageType.vitals:
if (!user.receivesVitalsReminders) return
case UserMessageType.symptomQuestionnaire:
if (!user.receivesQuestionnaireReminders) return
case UserMessageType.preAppointment:
if (!user.receivesAppointmentReminders) return
}

await this.sendNotification(userId, newMessage, {
language: user.language,
await this.sendNotificationIfNeeded({
userId: userId,
user: options.user ?? null,
message: newMessage,
})

return newMessage
}
}

Expand Down Expand Up @@ -340,6 +323,37 @@ export class DefaultMessageService implements MessageService {

// Helpers - Notifications

private async sendNotificationIfNeeded(input: {
userId: string
user: User | null
message: Document<UserMessage>
}) {
const user =
input.user ?? (await this.userService.getUser(input.userId))?.content
if (!user) return

switch (input.message.content.type) {
case UserMessageType.medicationChange:
if (!user.receivesMedicationUpdates) return
case UserMessageType.weightGain:
if (!user.receivesWeightAlerts) return
case UserMessageType.medicationUptitration:
if (!user.receivesRecommendationUpdates) return
case UserMessageType.welcome:
break
case UserMessageType.vitals:
if (!user.receivesVitalsReminders) return
case UserMessageType.symptomQuestionnaire:
if (!user.receivesQuestionnaireReminders) return
case UserMessageType.preAppointment:
if (!user.receivesAppointmentReminders) return
}

await this.sendNotification(input.userId, input.message, {
language: user.language,
})
}

private async sendNotification(
userId: string,
message: Document<UserMessage>,
Expand Down
3 changes: 2 additions & 1 deletion functions/src/services/message/messageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
type UserMessage,
type UserMessageType,
} from '@stanfordbdhg/engagehf-models'
import { type Document } from '../database/databaseService'

export interface MessageService {
// Notifications
Expand All @@ -33,7 +34,7 @@ export interface MessageService {
notify: boolean
user?: User | null
},
): Promise<void>
): Promise<Document<UserMessage> | undefined>

completeMessages(
userId: string,
Expand Down
3 changes: 3 additions & 0 deletions functions/src/services/seeding/debugData/debugDataService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,12 @@ export class DebugDataService extends SeedingService {
UserMessage.createInactiveForClinician({
userId: patient.id,
userName: patient.name,
reference: '',
}),
UserMessage.createMedicationUptitrationForClinician({
userId: patient.id,
userName: patient.name,
reference: '',
}),
UserMessage.createPreAppointmentForClinician({
userId: patient.id,
Expand All @@ -197,6 +199,7 @@ export class DebugDataService extends SeedingService {
UserMessage.createWeightGainForClinician({
userId: patient.id,
userName: patient.name,
reference: '',
}),
])
await this.replaceCollection(
Expand Down
106 changes: 89 additions & 17 deletions functions/src/services/trigger/triggerService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ import { describeWithEmulators } from '../../tests/functions/testEnvironment.js'
describeWithEmulators('TriggerService', (env) => {
describe('every15Minutes', () => {
it('should create a message for an upcoming appointment', async () => {
const ownerId = await env.createUser({
type: UserType.owner,
organization: 'stanford',
})

const clinicianId = await env.createUser({
type: UserType.clinician,
organization: 'stanford',
Expand Down Expand Up @@ -64,11 +69,22 @@ describeWithEmulators('TriggerService', (env) => {
expect(clinicianMessages.docs).to.have.length(1)
const clinicianMessage = clinicianMessages.docs.at(0)?.data()
expect(clinicianMessage?.type).to.equal(UserMessageType.preAppointment)
expect(clinicianMessage?.reference).to.equal(appointmentRef.path)
expect(clinicianMessage?.reference).to.equal(
patientMessages.docs.at(0)?.ref.path,
)
expect(clinicianMessage?.completionDate).to.be.undefined

const ownerMessages = await env.collections.userMessages(ownerId).get()
expect(ownerMessages.docs).to.have.length(1)
const ownerMessage = clinicianMessages.docs.at(0)?.data()
expect(ownerMessage?.type).to.equal(UserMessageType.preAppointment)
expect(ownerMessage?.reference).to.equal(
patientMessages.docs.at(0)?.ref.path,
)
expect(ownerMessage?.completionDate).to.be.undefined
})

it('should create a complete a message for a past appointment', async () => {
it('should complete a message for a past appointment', async () => {
const clinicianId = await env.createUser({
type: UserType.clinician,
organization: 'stanford',
Expand All @@ -91,36 +107,44 @@ describeWithEmulators('TriggerService', (env) => {
const appointmentRef = env.collections.userAppointments(patientId).doc()
await appointmentRef.set(appointment)

const message = UserMessage.createPreAppointment({
const patientMessage = UserMessage.createPreAppointment({
reference: appointmentRef.path,
})
const patientMessageRef = env.collections.userMessages(patientId).doc()
await patientMessageRef.set(message)
await patientMessageRef.set(patientMessage)

const clinicianMessage = UserMessage.createPreAppointmentForClinician({
userId: patientId,
userName: 'Mock',
reference: patientMessageRef.path,
})

const clinicianMessageRef = env.collections
.userMessages(clinicianId)
.doc()
await clinicianMessageRef.set(message)
await clinicianMessageRef.set(clinicianMessage)

await env.factory.trigger().every15Minutes()

const patientMessages = await env.collections
.userMessages(patientId)
.get()
expect(patientMessages.docs).to.have.length(1)
const patientMessage = patientMessages.docs.at(0)?.data()
expect(patientMessage?.type).to.equal(UserMessageType.preAppointment)
expect(patientMessage?.reference).to.equal(appointmentRef.path)
expect(patientMessage?.completionDate).to.exist
const patientMessageData = patientMessages.docs.at(0)?.data()
expect(patientMessageData?.type).to.equal(UserMessageType.preAppointment)
expect(patientMessageData?.reference).to.equal(appointmentRef.path)
expect(patientMessageData?.completionDate).to.exist

const clinicianMessages = await env.collections
.userMessages(patientId)
.userMessages(clinicianId)
.get()
expect(clinicianMessages.docs).to.have.length(1)
const clinicianMessage = clinicianMessages.docs.at(0)?.data()
expect(clinicianMessage?.type).to.equal(UserMessageType.preAppointment)
expect(clinicianMessage?.reference).to.equal(appointmentRef.path)
expect(clinicianMessage?.completionDate).to.exist
const clinicianMessageData = clinicianMessages.docs.at(0)?.data()
expect(clinicianMessageData?.type).to.equal(
UserMessageType.preAppointment,
)
expect(clinicianMessageData?.reference).to.equal(patientMessageRef.path)
expect(clinicianMessageData?.completionDate).to.be.undefined // this message will need to be manually completed
})
})

Expand Down Expand Up @@ -182,6 +206,11 @@ describeWithEmulators('TriggerService', (env) => {
})

it('create a message about inactivity', async () => {
const ownerId = await env.createUser({
type: UserType.owner,
organization: 'stanford',
})

const clinicianId = await env.createUser({
type: UserType.clinician,
organization: 'stanford',
Expand Down Expand Up @@ -221,11 +250,31 @@ describeWithEmulators('TriggerService', (env) => {
expect(clinicianMessages.docs).to.have.length(1)
const clinicianMessage = clinicianMessages.docs.at(0)?.data()
expect(clinicianMessage?.type).to.equal(UserMessageType.inactive)
expect(clinicianMessage?.reference).to.equal(`users/${patientId}`)
expect(clinicianMessage?.reference).to.equal(
patientMessages.docs
.filter((doc) => doc.data().type === UserMessageType.inactive)
.at(0)?.ref.path,
)
expect(clinicianMessage?.completionDate).to.be.undefined

const ownerMessages = await env.collections.userMessages(ownerId).get()
expect(ownerMessages.docs).to.have.length(1)
const ownerMessage = clinicianMessages.docs.at(0)?.data()
expect(ownerMessage?.type).to.equal(UserMessageType.inactive)
expect(ownerMessage?.reference).to.equal(
patientMessages.docs
.filter((doc) => doc.data().type === UserMessageType.inactive)
.at(0)?.ref.path,
)
expect(ownerMessage?.completionDate).to.be.undefined
})

it('create no message about inactivity', async () => {
const ownerId = await env.createUser({
type: UserType.owner,
organization: 'stanford',
})

const clinicianId = await env.createUser({
type: UserType.clinician,
organization: 'stanford',
Expand Down Expand Up @@ -253,6 +302,9 @@ describeWithEmulators('TriggerService', (env) => {
.userMessages(clinicianId)
.get()
expect(clinicianMessages.docs).to.have.length(0)

const ownerMessages = await env.collections.userMessages(ownerId).get()
expect(ownerMessages.docs).to.have.length(0)
})
})

Expand All @@ -261,6 +313,11 @@ describeWithEmulators('TriggerService', (env) => {
it('should create a weight gain message for a user', async () => {
const triggerService = env.factory.trigger()

const ownerId = await env.createUser({
type: UserType.owner,
organization: 'stanford',
})

const clinicianId = await env.createUser({
type: UserType.clinician,
organization: 'stanford',
Expand Down Expand Up @@ -304,6 +361,9 @@ describeWithEmulators('TriggerService', (env) => {
.get()
expect(clinicianMessages0.docs).to.have.length(0)

const ownerMessages0 = await env.collections.userMessages(ownerId).get()
expect(ownerMessages0.docs).to.have.length(0)

const slightlyHigherWeight = FHIRObservation.createSimple({
id: 'observation-10',
code: LoincCode.bodyWeight,
Expand All @@ -328,13 +388,19 @@ describeWithEmulators('TriggerService', (env) => {
expect(patientMessage1?.completionDate).to.be.undefined

const clinicianMessages1 = await env.collections
.userMessages(patientId)
.userMessages(clinicianId)
.get()
expect(clinicianMessages1.docs, 'clinicianMessages1').to.have.length(1)
const clinicianMessage1 = clinicianMessages1.docs.at(0)?.data()
expect(clinicianMessage1?.type).to.equal(UserMessageType.weightGain)
expect(clinicianMessage1?.completionDate).to.be.undefined

const ownerMessages1 = await env.collections.userMessages(ownerId).get()
expect(ownerMessages1.docs, 'ownerMessages1').to.have.length(1)
const ownerMessage1 = clinicianMessages1.docs.at(0)?.data()
expect(ownerMessage1?.type).to.equal(UserMessageType.weightGain)
expect(ownerMessage1?.completionDate).to.be.undefined

const actuallyHigherWeight = FHIRObservation.createSimple({
id: 'observation-11',
code: LoincCode.bodyWeight,
Expand All @@ -359,12 +425,18 @@ describeWithEmulators('TriggerService', (env) => {
expect(patientMessage2?.completionDate).to.be.undefined

const clinicianMessages2 = await env.collections
.userMessages(patientId)
.userMessages(clinicianId)
.get()
expect(clinicianMessages2.docs, 'clinicianMessages2').to.have.length(1)
const clinicianMessage2 = clinicianMessages1.docs.at(0)?.data()
expect(clinicianMessage2?.type).to.equal(UserMessageType.weightGain)
expect(clinicianMessage2?.completionDate).to.be.undefined

const ownerMessages2 = await env.collections.userMessages(ownerId).get()
expect(ownerMessages2.docs, 'ownerMessages2').to.have.length(1)
const ownerMessage2 = ownerMessages2.docs.at(0)?.data()
expect(ownerMessage2?.type).to.equal(UserMessageType.weightGain)
expect(ownerMessage2?.completionDate).to.be.undefined
})
})
})
Expand Down
Loading
Loading