54 lines
1.6 KiB
TypeScript
54 lines
1.6 KiB
TypeScript
|
|
import { requireAuth } from '@/server/utils/auth';
|
||
|
|
import { getExpenseById } from '@/server/utils/nocodb';
|
||
|
|
import { processExpenseWithCurrency } from '@/server/utils/currency';
|
||
|
|
|
||
|
|
export default defineEventHandler(async (event) => {
|
||
|
|
await requireAuth(event);
|
||
|
|
|
||
|
|
const query = getQuery(event);
|
||
|
|
const { id } = query;
|
||
|
|
|
||
|
|
if (!id || typeof id !== 'string') {
|
||
|
|
throw createError({
|
||
|
|
statusCode: 400,
|
||
|
|
statusMessage: 'Expense ID is required'
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
console.log('[get-expense-by-id] Fetching expense ID:', id);
|
||
|
|
|
||
|
|
try {
|
||
|
|
const expense = await getExpenseById(id);
|
||
|
|
|
||
|
|
// Process expense with currency conversion
|
||
|
|
const processedExpense = await processExpenseWithCurrency(expense);
|
||
|
|
|
||
|
|
// Transform the response to include additional computed data
|
||
|
|
const transformedExpense = {
|
||
|
|
...processedExpense,
|
||
|
|
// Format the date for easier frontend consumption
|
||
|
|
FormattedDate: new Date(expense.Time).toLocaleDateString(),
|
||
|
|
FormattedTime: new Date(expense.Time).toLocaleTimeString(),
|
||
|
|
FormattedDateTime: new Date(expense.Time).toLocaleString()
|
||
|
|
};
|
||
|
|
|
||
|
|
console.log('[get-expense-by-id] Successfully fetched expense:', transformedExpense.Id);
|
||
|
|
|
||
|
|
return transformedExpense;
|
||
|
|
} catch (error: any) {
|
||
|
|
console.error('[get-expense-by-id] Error fetching expense:', error);
|
||
|
|
|
||
|
|
if (error.statusCode === 404 || error.status === 404) {
|
||
|
|
throw createError({
|
||
|
|
statusCode: 404,
|
||
|
|
statusMessage: `Expense with ID ${id} not found`
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
throw createError({
|
||
|
|
statusCode: 500,
|
||
|
|
statusMessage: 'Failed to fetch expense'
|
||
|
|
});
|
||
|
|
}
|
||
|
|
});
|