Files
refactor-kata/kata/notifications/original/notificationService.js
T
2026-05-20 17:42:01 -06:00

34 lines
834 B
JavaScript

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 };