logingest/services/logService.js

61 lines
1.6 KiB
JavaScript
Raw Normal View History

2025-06-15 14:47:15 +00:00
export async function createLogEntry(client, data) {
const now = new Date();
const epochTime = Math.floor(now.getTime() / 1000);
// Create log entry with defaults
const logEntry = {
2025-06-18 12:05:24 +00:00
id: epochTime.toString(),
body: data.body, // Changed from body to message
2025-06-15 14:47:15 +00:00
project: data.project || 'Project 1',
type: data.type || 'Info',
date: now.toISOString().split('T')[0], // YYYY-MM-DD format
avatar_src: data.avatar_src || '/rectangle-15.png',
owner: data.owner || 'N/A',
description: data.message, // Full message
created_at: now.toISOString(),
status: data.status || 'Pending'
};
// Insert into database
const query = `
INSERT INTO logs (id, body, project, type, date, avatar_src, owner, description, created_at, status)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
RETURNING *
`;
const values = [
logEntry.id,
logEntry.body,
logEntry.project,
logEntry.type,
logEntry.date,
logEntry.avatar_src,
logEntry.owner,
logEntry.description,
logEntry.created_at,
logEntry.status
];
try {
2025-06-18 12:05:24 +00:00
// Log the query and values before execution
console.log('Executing SQL:', query);
console.log('With values:', values);
2025-06-15 14:47:15 +00:00
const result = await client.query(query, values);
const insertedData = result.rows[0];
console.log('Log entry created:', {
id: insertedData.id,
project: insertedData.project,
type: insertedData.type,
timestamp: insertedData.created_at
});
return insertedData;
} catch (error) {
console.error('Database error:', error);
throw new Error(`Failed to insert log entry: ${error.message}`);
}
2025-06-18 12:05:24 +00:00
}