Add admin authenticated API to get call logs

This commit is contained in:
Christopher Cookman 2024-12-16 17:17:30 -07:00
parent a1fdfbf8e0
commit 4e2f1e6495

View file

@ -359,6 +359,33 @@ app.delete('/api/v1/admin/directory/:number', (req, res) => { // Delete a direct
});
});
app.get("/api/v1/admin/callLogs", (req, res) => {
if (!req.session.adminAuthenticated) {
res.status(401).json({ error: 'Unauthorized' });
return;
}
// if ?page is set, return 100 results from that page, if no page assume page 1
const page = Number(req.query.page) || 1;
const offset = (page - 1) * 100;
// Get full count of call logs to calculate total pages
db.get("SELECT COUNT(*) as count FROM callLogs", [], (err, row) => {
if (err) {
console.error('Error getting call log count:', err);
res.status(500).json({ error: 'Internal server error' });
return;
}
const totalPages = Math.ceil(row.count / 100);
db.all("SELECT * FROM callLogs ORDER BY timestamp DESC LIMIT 100 OFFSET ?", [offset], (err, rows) => {
if (err) {
console.error('Error getting call logs:', err);
res.status(500).json({ error: 'Internal server error' });
return;
}
res.json({ totalPages, page, data: rows });
});
});
});
// == END ADMIN ROUTES ==
// == User routes == // allows someone to log in with their API key and add entries to the Directory (as long as the number is within their block range)
@ -625,7 +652,7 @@ app.get("/api/healthcheck", (req, res) => {
// logCall function (caller, callee)
const logCall = (caller, callee) => {
db.run('INSERT INTO callLogs (caller, callee, timestamp) VALUES (?, ?, ?)',
[caller, callee, Math.floor(Date.now() / 1000)],
[caller, callee, Math.floor(Date.now())],
(err) => {
if (err) console.error('Error logging call:', err);
});