61 lines
1.6 KiB
JavaScript
61 lines
1.6 KiB
JavaScript
export async function createLogEntry(client, data) {
|
|
const now = new Date();
|
|
const epochTime = Math.floor(now.getTime() / 1000);
|
|
|
|
// Create log entry with defaults
|
|
const logEntry = {
|
|
id: epochTime.toString(),
|
|
body: data.body, // Changed from body to message
|
|
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 {
|
|
// Log the query and values before execution
|
|
console.log('Executing SQL:', query);
|
|
console.log('With values:', values);
|
|
|
|
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}`);
|
|
}
|
|
}
|
|
|