Skip to content

Commit 9df93c0

Browse files
authored
Merge pull request #26 from cliffkoome/feature/frontend-update-api
Feature/frontend update api
2 parents d5fb432 + 487dc28 commit 9df93c0

5 files changed

Lines changed: 1183 additions & 384 deletions

File tree

backend/src/app.js

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { ussdRouter } from "./delivery/ussd.js";
77
import { smsRouter } from "./routes/sms.js";
88
import { voiceRouter } from "./routes/voice.js";
99
import { usersRouter } from "./routes/users.js";
10+
import { User } from "./models/User.js";
11+
import { Notification } from "./models/Notification.js";
1012

1113
export const app = express();
1214

@@ -26,9 +28,54 @@ app.use("/voice", voiceRouter);
2628

2729
app.use("/api/alerts", alertsRouter);
2830
app.use("/api/bulletins", bulletinsRouter);
29-
3031
app.use("/api/users", usersRouter);
3132

33+
// 1. Fetch all registered beneficiaries
34+
app.get("/api/users", async (req, res) => {
35+
try {
36+
const users = await User.findAll({
37+
order: [["createdAt", "DESC"]],
38+
});
39+
40+
return res.status(200).json({
41+
success: true,
42+
data: users,
43+
});
44+
} catch (error) {
45+
console.error("[Admin] Error fetching users:", error);
46+
return res.status(500).json({ error: "Failed to retrieve users" });
47+
}
48+
});
49+
50+
// 2. Fetch all alert dispatch logs
51+
app.get("/api/logs", async (req, res) => {
52+
try {
53+
const logs = await Notification.findAll({
54+
order: [["updatedAt", "DESC"]],
55+
});
56+
57+
// Map the database model keys to match the frontend table schema
58+
const formattedLogs = logs.map((log) => ({
59+
timestamp: log.updatedAt || log.createdAt,
60+
alertKey: log.hazardType
61+
? `${log.hazardType}-${log.location}`
62+
: "Manual-Alert",
63+
phone: log.phoneNumber,
64+
smsSent: log.smsSent,
65+
callAttempts: log.callAttempts,
66+
callAnswered: log.callAnswered,
67+
}));
68+
69+
return res.status(200).json({
70+
success: true,
71+
data: formattedLogs,
72+
});
73+
} catch (error) {
74+
console.error("[Admin] Error fetching logs:", error);
75+
return res.status(500).json({ error: "Failed to retrieve dispatch logs" });
76+
}
77+
});
78+
3279
// Fallback for unmatched routes
3380
app.use((req, res) => {
3481
res.status(404).json({ error: "Not found" });

backend/src/routes/alerts.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { simplifyBulletin } from "../pipeline/simplify.js";
33
import { translateAlert } from "../pipeline/translate.js";
44
import { synthesizeSpeech } from "../delivery/tts.js";
55
import { sendSms } from "../delivery/sms.js";
6+
import { startVoiceCall } from "../delivery/voice.js";
67

78
export const alertsRouter = Router();
89

@@ -30,13 +31,26 @@ alertsRouter.post("/", async (req, res) => {
3031
const audioPath = `/audio/${fileName}`;
3132

3233
let deliveryResult = null;
34+
let voiceResult = null;
35+
3336
if (phoneNumber) {
37+
// Execute SMS dispatch
3438
deliveryResult = await sendSms({ to: phoneNumber, message: translated });
39+
40+
// Execute Voice dispatch internally (bypasses HTTP auth headers)
41+
try {
42+
voiceResult = await startVoiceCall({
43+
to: phoneNumber,
44+
clientRequestId: fileName,
45+
});
46+
} catch (voiceErr) {
47+
console.error("[alerts] voice call failed:", voiceErr.message);
48+
}
3549
}
3650

3751
res.json({
3852
stages: { raw: bulletinText, simplified, translated, audioPath },
39-
delivery: deliveryResult,
53+
delivery: { sms: deliveryResult, voice: voiceResult },
4054
});
4155
} catch (err) {
4256
console.error("[alerts] pipeline error:", err);

frontend/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
<meta charset="UTF-8" />
55
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
66
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<link
8+
href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:wght,FILL@100..700,0..1&display=swap"
9+
rel="stylesheet"
10+
/>
711
<title>frontend</title>
812
</head>
913
<body>

0 commit comments

Comments
 (0)