42 lines
No EOL
1.1 KiB
JavaScript
42 lines
No EOL
1.1 KiB
JavaScript
import { getCollection } from '../../utils/db.js'
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
try {
|
|
const collection = await getCollection('balances')
|
|
const balances = await collection.findOne({ type: 'current' })
|
|
|
|
if (!balances) {
|
|
// Return default balance structure if none exists
|
|
return {
|
|
currentBalance: 0,
|
|
accountBalances: {
|
|
manual: {
|
|
rbc_cad: 0,
|
|
td_cad: 0,
|
|
millennium_eur: 0
|
|
},
|
|
wise: {
|
|
jennie: [],
|
|
henry: []
|
|
},
|
|
lastWiseFetch: null,
|
|
lastManualUpdate: null
|
|
}
|
|
}
|
|
}
|
|
|
|
// Ensure timestamp fields exist
|
|
if (balances.accountBalances && !balances.accountBalances.lastWiseFetch && !balances.accountBalances.lastManualUpdate) {
|
|
balances.accountBalances.lastWiseFetch = null;
|
|
balances.accountBalances.lastManualUpdate = null;
|
|
}
|
|
|
|
return balances
|
|
} catch (error) {
|
|
console.error('Error fetching balances:', error)
|
|
throw createError({
|
|
statusCode: 500,
|
|
statusMessage: 'Failed to fetch balances'
|
|
})
|
|
}
|
|
}) |