Compare commits

..

4 commits

View file

@ -468,7 +468,7 @@ app.delete('/api/v1/admin/directory/:number', (req, res) => { // Delete a direct
});
app.get("/api/v1/admin/callLogs", (req, res) => {
if (!req.session.adminAuthenticated) {
if (!req.session.adminAuthenticated && (!req.headers['authorization'] || req.headers['authorization'] !== `${process.env.ADMIN_API_KEY}`) && (!req.query.apiKey || req.query.apiKey !== `${process.env.ADMIN_API_KEY}`)) {
res.status(401).json({ error: 'Unauthorized' });
return;
}
@ -478,8 +478,26 @@ app.get("/api/v1/admin/callLogs", (req, res) => {
// Get full count of call logs to calculate total pages
pool.getConnection().then(conn => {
conn.query("SELECT COUNT(*) as count FROM callLogs").then((rows) => {
const totalPages = Math.ceil(rows[0].count / 100);
const totalPages = Math.ceil(Number(rows[0].count) / 100);
conn.query("SELECT * FROM callLogs ORDER BY timestamp DESC LIMIT 100 OFFSET ?", [offset]).then((rows) => {
// Turn all values in rows to strings, prevents type issues with bigints.
rows = rows.map(row => {
const newRow = {};
for (const key in row) {
// Convert BigInt explicitly to avoid "Cannot mix BigInt and other types" errors,
// handle Dates and null/undefined safely, otherwise coerce to string.
if (typeof row[key] === 'bigint') {
newRow[key] = row[key].toString();
} else if (row[key] instanceof Date) {
newRow[key] = row[key].toISOString();
} else if (row[key] === null || row[key] === undefined) {
newRow[key] = null;
} else {
newRow[key] = String(row[key]);
}
}
return newRow;
});
res.json({ totalPages, page, data: rows });
}).catch(err => {
console.error('Error getting call logs:', err);