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

Messaging client: handle last unfollowed message date/time when outside communication is sent #78

Merged
merged 1 commit into from
Nov 10, 2023
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
90 changes: 64 additions & 26 deletions src/components/MessagingView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -453,22 +453,29 @@ export default class MessagingView extends React.Component<
// @ts-ignore
const patient: Patient = context.patient;
const matchedCR = patient?.nextScheduledMessageDateTime
? this.state.scheduledCommunicationRequests?.find((cr: CommunicationRequest) => {
const crDate = new Date(cr.occurrenceDateTime);
const patientExtensionDate = new Date(patient?.nextScheduledMessageDateTime);
if (isNaN(crDate.getTime()) || isNaN(patientExtensionDate.getTime()))
return false;
return (
CommunicationRequest.isScheduledOutgoingMessage(cr) &&
cr.status === "active" &&
crDate.getFullYear() === patientExtensionDate.getFullYear() &&
crDate.getMonth() === patientExtensionDate.getMonth() &&
crDate.getDate() === patientExtensionDate.getDate() &&
crDate.getHours() === patientExtensionDate.getHours() &&
crDate.getSeconds() === patientExtensionDate.getSeconds() &&
crDate.getTime() >= (new Date()).getTime()
);
})
? this.state.scheduledCommunicationRequests?.find(
(cr: CommunicationRequest) => {
const crDate = new Date(cr.occurrenceDateTime);
const patientExtensionDate = new Date(
patient?.nextScheduledMessageDateTime
);
if (
isNaN(crDate.getTime()) ||
isNaN(patientExtensionDate.getTime())
)
return false;
return (
CommunicationRequest.isScheduledOutgoingMessage(cr) &&
cr.status === "active" &&
crDate.getFullYear() === patientExtensionDate.getFullYear() &&
crDate.getMonth() === patientExtensionDate.getMonth() &&
crDate.getDate() === patientExtensionDate.getDate() &&
crDate.getHours() === patientExtensionDate.getHours() &&
crDate.getSeconds() === patientExtensionDate.getSeconds() &&
crDate.getTime() >= new Date().getTime()
);
}
)
: null;
if (!matchedCR) return null;
return (
Expand Down Expand Up @@ -924,6 +931,7 @@ export default class MessagingView extends React.Component<
client
.update(targetEntry)
.then(() => {
this.handleLastUnfollowedDateTime(targetEntry);
const existingEntryIndex =
this.state.communications?.findIndex(
(item) => item.id === targetEntry.id
Expand Down Expand Up @@ -1075,18 +1083,48 @@ export default class MessagingView extends React.Component<
);
}

private handleLastUnfollowedDateTime(communication: Communication) {
// @ts-ignore
const context: FhirClientContextType = this.context;
const patient: Patient = context.patient;
const isOutsideCommunication = !!communication.category?.find(
(c: ICodeableConcept) =>
c.coding.find((coding: ICoding) =>
IsaccMessageCategory.isaccNonSMSMessage.equals(coding)
)
);
// if an outside communication is sent to the recipient
if (isOutsideCommunication && communication.sent) {
const existingLastUnfollowedMessageDateTime =
patient.lastUnfollowedMessageDateTime;
// if date/time of outside communication > existing last unfollowed message date/time
// means the provider has responded to the recipient's last message
if (
existingLastUnfollowedMessageDateTime &&
new Date(communication.sent) >
new Date(existingLastUnfollowedMessageDateTime)
) {
// set to a future date, this is so that `Time Since Reply` can be sorted
patient.lastUnfollowedMessageDateTime = Patient.UNSET_LAST_UNFOLLOWED_DATETIME;
const client = context.client;
if (client) {
// @ts-ignore
client.update(patient);
}
}
}
}

private saveNonSMSMessage() {
// @ts-ignore
let context: FhirClientContextType = this.context;
this.setState({ saveLoading: true });
const messageType = this.state.activeMessage?.type;
const messageStatus = this.state.activeMessage?.status;
const sentDate =
this.state.activeMessage?.status === "sent"
? this.state.activeMessage.date
: null;
messageStatus === "sent" ? this.state.activeMessage.date : null;
const receivedDate =
this.state.activeMessage?.status === "received"
? this.state.activeMessage.date
: null;
messageStatus === "received" ? this.state.activeMessage.date : null;
const currentPractitioner = context.practitioner;
const userName = getUserName(context.client);
const practitionerName = currentPractitioner
Expand All @@ -1100,7 +1138,7 @@ export default class MessagingView extends React.Component<
: userName
? `entered by ${userName}`
: "staff-entered";
const noteAboutCommunication = `${this.state.activeMessage?.type}, ${enteredByText}`;
const noteAboutCommunication = `${messageType}, ${enteredByText}`;
// new communication
// TODO implement sender, requires Practitioner resource set for the user
const newCommunication = Communication.create(
Expand All @@ -1109,19 +1147,19 @@ export default class MessagingView extends React.Component<
context.currentCarePlan,
sentDate,
receivedDate,
this.state.activeMessage?.type === "comment"
messageType === "comment"
? IsaccMessageCategory.isaccComment
: IsaccMessageCategory.isaccNonSMSMessage,
noteAboutCommunication,
currentPractitioner
);
this._save(newCommunication, (savedResult: IResource) => {
console.log("Saved new communication:", savedResult);
const currentMessageType = this.state.activeMessage.type;
this.handleLastUnfollowedDateTime(newCommunication);
this.setState({
activeMessage: {
...defaultMessage,
type: currentMessageType,
type: messageType,
},
error: null,
});
Expand Down
1 change: 1 addition & 0 deletions src/model/CodeSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export const ExtensionUrl = {
studyStatusUrl: "isacc.app/study-status",
pronounsUrl: "http://hl7.org/fhir/StructureDefinition/individual-genderIdentity",
nextScheduledgMessageDateTimeUrl: "http://isacc.app/date-time-of-next-outgoing-message",
lastUnfollowedMessageDateTimeUrl: "http://isacc.app/time-of-last-unfollowedup-message"
}

export const SystemURL = {
Expand Down
27 changes: 27 additions & 0 deletions src/model/Patient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export default class Patient implements IPatient {
}

static TEST_PATIENT_SECURITY_CODE = "HTEST";
static UNSET_LAST_UNFOLLOWED_DATETIME = "2025-01-01T00:00:00Z";

get smsContactPoint(): string {
let p = this.telecom?.filter(
Expand Down Expand Up @@ -397,6 +398,32 @@ export default class Patient implements IPatient {
existingDateTime.valueDateTime = value;
}

get lastUnfollowedMessageDateTime(): string {
let o = this.extension?.filter(
(i: IExtension) => i.url === ExtensionUrl.lastUnfollowedMessageDateTimeUrl
)[0];
return o ? o.valueDateTime : null;
}

set lastUnfollowedMessageDateTime(value: string) {
if (!value) {
return;
}
if (!this.extension) {
this.extension = [];
}
let existingDateTime = this.extension?.filter(
(i: IExtension) => i.url === ExtensionUrl.lastUnfollowedMessageDateTimeUrl
)[0];
if (!existingDateTime) {
existingDateTime = {
url: ExtensionUrl.lastUnfollowedMessageDateTimeUrl,
};
this.extension.push(existingDateTime);
}
existingDateTime.valueDateTime = value;
}


get isTest(): boolean {
let o = this.meta?.security?.filter(
Expand Down
Loading