Skip to content

Commit

Permalink
Add SMS notification when a reminder is already set.
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanWEdwards committed Jan 29, 2023
1 parent 15352ea commit 8e01c53
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@startuml
skinparam sequenceMessageAlign center

User -> Server: 01-AB-23456
Server -> User: We found case "01-AB-23456" on 7/28/2022 1:00 PM @ Family/Criminal Courtroom 2A Chittenden, VT.\nReply with YES if you would like a courtesy reminder the day before or reply with NO to start over.
User -> Server: Yes
Server -> User: A reminder to notify you on case "01-AB-23456" taking place on 7/28/2022 1:00 PM @ Family/Criminal\n Courtroom 2A Chittenden, VT has already been set.
@enduml
3 changes: 3 additions & 0 deletions docs/use-cases.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

[![User finds a case but doesn't sign up for notification](https://tinyurl.com/2e5hkbzp)](https://tinyurl.com/2e5hkbzp)<!--![User finds a case but doesn't sign up for notification](./diagrams/case-found-no-reminder-set.puml)-->

## User signs up to receive a reminder the user has already signed up to receive a notification on
[![User has already signed up to receive a reminder](https://tinyurl.com/2375cj3f)]<!--[![User has already signed up to receive a reminder](./diagrams/case-found-sign-up-for-reminder-reminder-already-set.puml)]-->

## User finds multiple cases

[![User finds multiple cases](https://tinyurl.com/2nab39j3)](https://tinyurl.com/2nab39j3)<!--![User finds multiple cases](./diagrams/multiple-cases.puml)-->
Expand Down
105 changes: 79 additions & 26 deletions pages/api/sms/[instance]/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,23 +135,50 @@ const handleText = async (req:NextApiRequest, res:NextApiResponse, input:string,
// let's check for a yes
if (response.toLowerCase() === 'yes') {
let c = cases[0];
await ReminderDao.create({

// check if a reminder is already active
const reminders = await ReminderDao.find({
active: true,
uid: c.uid,
number: c.number,
phone,
});
}).exec();

logger.info(`${phone} (${instance})[${state}]: reminder set`, { metadata: {
service: `/api/sms/${instance}`,
cookies,
instance,
input,
phone,
case: c,
state,
result: 'reminder set',
}});
res.send(smsResponse.reminderYes(c).toString());
if (reminders.length === 0) {
// create a new reminder document
// if no 'active' reminder documents are found
// matching the case uid, docket number, and phone number
await ReminderDao.create({
uid: c.uid,
number: c.number,
phone,
});
logger.info(`${phone} (${instance})[${state}]: reminder set`, { metadata: {
service: `/api/sms/${instance}`,
cookies,
instance,
input,
phone,
case: c,
state,
result: 'reminder set',
}});
res.send(smsResponse.reminderYes(c).toString());
} else {
// at least one reminder document in the collection already exists
// for the phone number `phone` to be reminded about case docket `c.number`
logger.info(`${phone} (${instance})[${state}]: reminder already set`, { metadata: {
service: `/api/sms/${instance}`,
cookies,
instance,
input,
phone,
case: c,
state,
result: 'reminder already set',
}});
res.send(smsResponse.reminderActive(c).toString(), instanceMethods.getTimezone());
}
}
// send help due to unexpected response
else {
Expand Down Expand Up @@ -179,23 +206,49 @@ const handleText = async (req:NextApiRequest, res:NextApiResponse, input:string,
// if a number was given lets check to see if it maps to a case index
if (response === parseInt(response).toString() && index >= 0 && index < cases.length) {
let c = cases[index];
await ReminderDao.create({
// check if a reminder is already active
const reminders = await ReminderDao.find({
active: true,
uid: c.uid,
number: c.number,
phone,
});
}).exec();

logger.info(`${phone} (${instance})[${state}]: reminder set`, { metadata: {
service: `/api/sms/${instance}`,
cookies,
instance,
input,
phone,
case: c,
state,
result: 'reminder set',
}});
res.send(smsResponse.reminderYes(c).toString());
if (reminders.length === 0) {
// create a new reminder document
// if no 'active' reminder documents are found
// matching the case uid, docket number, and phone number
await ReminderDao.create({
uid: c.uid,
number: c.number,
phone,
});
logger.info(`${phone} (${instance})[${state}]: reminder set`, { metadata: {
service: `/api/sms/${instance}`,
cookies,
instance,
input,
phone,
case: c,
state,
result: 'reminder set',
}});
res.send(smsResponse.reminderYes(c).toString());
} else {
// at least one reminder document in the collection already exists
// for the phone number `phone` to be reminded about case docket `c.number`
logger.info(`${phone} (${instance})[${state}]: reminder already set`, { metadata: {
service: `/api/sms/${instance}`,
cookies,
instance,
input,
phone,
case: c,
state,
result: 'reminder already set',
}});
res.send(smsResponse.reminderActive(c).toString(), instanceMethods.getTimezone());
}
}
// send help due to unexpected response
else {
Expand Down
7 changes: 7 additions & 0 deletions utils/sms-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,18 @@ const reminderNo = (website:string) => {
return resp;
};

const reminderActive = (c:Case, timezone = 'America/New_York')) => {
var resp = new MessagingResponse();
resp.message(`A reminder to notify you on case "${c.number}" taking place on ${moment(c.date).tz(timezone).format('l LT')} @ ${c.address} has already been set.`);
return resp;
}

export default {
caseNotFound,
caseFound,
error,
help,
reminderActive,
reminderNo,
reminderYes,
};

0 comments on commit 8e01c53

Please sign in to comment.