55 lines
1.5 KiB
JavaScript
55 lines
1.5 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,
|
||
|
body: data.message.substring(0, 200), // First 200 characters
|
||
|
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 {
|
||
|
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}`);
|
||
|
}
|
||
|
}
|