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

Complete inactivity messages #200

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
64 changes: 63 additions & 1 deletion functions/src/services/trigger/triggerService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ describeWithEmulators('TriggerService', (env) => {
expect(vitalsMessage?.reference).to.be.undefined
expect(vitalsMessage?.completionDate).to.be.undefined
const inactiveMessage = patientMessagesData
.filter((message) => message.type === UserMessageType.vitals)
.filter((message) => message.type === UserMessageType.inactive)
.at(0)
expect(inactiveMessage).to.exist
expect(inactiveMessage?.reference).to.be.undefined
Expand Down Expand Up @@ -275,6 +275,68 @@ describeWithEmulators('TriggerService', (env) => {
expect(ownerMessage?.completionDate).to.be.undefined
})

it('should complete inactivity messages', async () => {
const clinicianId = await env.createUser({
type: UserType.clinician,
organization: 'stanford',
})

const patientId = await env.createUser({
type: UserType.patient,
organization: 'stanford',
clinician: clinicianId,
dateOfEnrollment: advanceDateByDays(new Date(), -2),
lastActiveDate: advanceDateByDays(new Date(), -8),
})

const triggerService = env.factory.trigger()
await triggerService.everyMorning()

const patientMessages = await env.collections
.userMessages(patientId)
.get()
expect(patientMessages.docs).to.have.length(2)
const patientMessagesData = patientMessages.docs.map((doc) => doc.data())
const vitalsMessage = patientMessagesData
.filter((message) => message.type === UserMessageType.vitals)
.at(0)
expect(vitalsMessage).to.exist
expect(vitalsMessage?.reference).to.be.undefined
expect(vitalsMessage?.completionDate).to.be.undefined
const inactiveMessage = patientMessagesData
.filter((message) => message.type === UserMessageType.inactive)
.at(0)
expect(inactiveMessage).to.exist
expect(inactiveMessage?.reference).to.be.undefined
expect(inactiveMessage?.completionDate).to.be.undefined

await triggerService.userObservationWritten(
patientId,
UserObservationCollection.heartRate,
)

const updatedPatientMessages = await env.collections
.userMessages(patientId)
.get()
expect(patientMessages.docs).to.have.length(2)
const updatedPatientMessagesData = updatedPatientMessages.docs.map(
(doc) => doc.data(),
)
const updatedVitalsMessage = updatedPatientMessagesData
.filter((message) => message.type === UserMessageType.vitals)
.at(0)
expect(updatedVitalsMessage).to.exist
expect(updatedVitalsMessage?.reference).to.be.undefined
expect(updatedVitalsMessage?.completionDate).to.be.undefined

const updatedInactiveMessage = updatedPatientMessagesData
.filter((message) => message.type === UserMessageType.inactive)
.at(0)
expect(updatedInactiveMessage).to.exist
expect(updatedInactiveMessage?.reference).to.be.undefined
expect(updatedInactiveMessage?.completionDate).to.exist
})

it('create no message about inactivity', async () => {
const ownerId = await env.createUser({
type: UserType.owner,
Expand Down
9 changes: 9 additions & 0 deletions functions/src/services/trigger/triggerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,15 @@ export class TriggerService {
)
}

try {
const messageSevice = this.factory.message()
await messageSevice.completeMessages(userId, UserMessageType.inactive)
} catch (error) {
logger.error(
`TriggerService.userObservationWritten(${userId}, ${collection}): Completing inactive messages failed due to ${String(error)}`,
)
}

try {
await this.updateRecommendationsForUser(userId)
} catch (error) {
Expand Down
Loading