Init commit!
This commit is contained in:
commit
086d682592
34 changed files with 19249 additions and 0 deletions
42
server/api/balances/index.get.js
Normal file
42
server/api/balances/index.get.js
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
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'
|
||||
})
|
||||
}
|
||||
})
|
||||
43
server/api/balances/index.put.js
Normal file
43
server/api/balances/index.put.js
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
import { getCollection } from '../../utils/db.js'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
const body = await readBody(event)
|
||||
const collection = await getCollection('balances')
|
||||
|
||||
// Get existing balance document to preserve timestamp data
|
||||
const existingBalance = await collection.findOne({ type: 'current' })
|
||||
|
||||
const balanceData = {
|
||||
type: 'current',
|
||||
currentBalance: body.currentBalance,
|
||||
accountBalances: {
|
||||
manual: body.accountBalances?.manual || {},
|
||||
wise: body.accountBalances?.wise || { jennie: [], henry: [] },
|
||||
// Preserve existing timestamps if not explicitly provided
|
||||
lastWiseFetch: body.accountBalances?.lastWiseFetch || existingBalance?.accountBalances?.lastWiseFetch || null,
|
||||
lastManualUpdate: body.accountBalances?.lastManualUpdate || existingBalance?.accountBalances?.lastManualUpdate || null
|
||||
},
|
||||
updatedAt: new Date()
|
||||
}
|
||||
|
||||
// Upsert the balance record
|
||||
const result = await collection.replaceOne(
|
||||
{ type: 'current' },
|
||||
balanceData,
|
||||
{ upsert: true }
|
||||
)
|
||||
|
||||
return {
|
||||
success: true,
|
||||
modifiedCount: result.modifiedCount,
|
||||
upsertedCount: result.upsertedCount
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error updating balances:', error)
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
statusMessage: 'Failed to update balances'
|
||||
})
|
||||
}
|
||||
})
|
||||
Loading…
Add table
Add a link
Reference in a new issue