added base examples and drills
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
# notifications original
|
||||
|
||||
Messy reminder logic mixing quiet-hours rules, channel checks, time, logging, mutation, and file I/O.
|
||||
@@ -0,0 +1,33 @@
|
||||
const fs = require("fs");
|
||||
|
||||
function sendReminder(filePath, userId, channel) {
|
||||
const raw = fs.readFileSync(filePath, "utf8");
|
||||
const state = JSON.parse(raw);
|
||||
const user = state.users.find((item) => item.id === userId);
|
||||
|
||||
if (!user) {
|
||||
throw new Error("User not found");
|
||||
}
|
||||
|
||||
if (!user.remindersEnabled) {
|
||||
return { sent: false, reason: "disabled" };
|
||||
}
|
||||
|
||||
const hour = new Date().getHours();
|
||||
|
||||
if (hour < 8 || hour > 20) {
|
||||
return { sent: false, reason: "quiet-hours" };
|
||||
}
|
||||
|
||||
if (channel === "sms" && !user.phoneNumber) {
|
||||
return { sent: false, reason: "missing-phone" };
|
||||
}
|
||||
|
||||
user.lastReminderAt = new Date().toISOString();
|
||||
fs.writeFileSync(filePath, JSON.stringify(state, null, 2));
|
||||
console.log("Sent reminder", userId, channel);
|
||||
|
||||
return { sent: true, channel };
|
||||
}
|
||||
|
||||
module.exports = { sendReminder };
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"users": [
|
||||
{
|
||||
"id": "user-1",
|
||||
"remindersEnabled": true,
|
||||
"phoneNumber": "+15555555555",
|
||||
"lastReminderAt": null
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user