diff --git a/docs/diagrams/case-found-sign-up-for-reminder-reminder-already-set.puml b/docs/diagrams/case-found-sign-up-for-reminder-reminder-already-set.puml new file mode 100755 index 0000000..d4698f5 --- /dev/null +++ b/docs/diagrams/case-found-sign-up-for-reminder-reminder-already-set.puml @@ -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 \ No newline at end of file diff --git a/pages/api/sms/[instance]/index.ts b/pages/api/sms/[instance]/index.ts index 7e0afb9..aee0d85 100644 --- a/pages/api/sms/[instance]/index.ts +++ b/pages/api/sms/[instance]/index.ts @@ -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 { @@ -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 { diff --git a/utils/sms-response.ts b/utils/sms-response.ts index db58fdb..7c864cc 100644 --- a/utils/sms-response.ts +++ b/utils/sms-response.ts @@ -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, }; \ No newline at end of file