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 14, 2023
1 parent 15352ea commit 218c55a
Show file tree
Hide file tree
Showing 3 changed files with 94 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
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());
}
}
// 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());
}
}
// 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) => {
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 218c55a

Please sign in to comment.