Init commit!
This commit is contained in:
commit
086d682592
34 changed files with 19249 additions and 0 deletions
42
server/api/transactions/bulk.put.js
Normal file
42
server/api/transactions/bulk.put.js
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
import { getCollection } from '../../utils/db.js'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
const transactions = await readBody(event)
|
||||
const collection = await getCollection('transactions')
|
||||
|
||||
// Clear existing transactions and insert new ones
|
||||
await collection.deleteMany({})
|
||||
|
||||
if (transactions.length > 0) {
|
||||
// Convert transactions for MongoDB
|
||||
const mongoTransactions = transactions.map(transaction => ({
|
||||
...transaction,
|
||||
_id: transaction.id,
|
||||
id: undefined,
|
||||
date: new Date(transaction.date),
|
||||
endDate: transaction.endDate ? new Date(transaction.endDate) : null,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date()
|
||||
}))
|
||||
|
||||
const result = await collection.insertMany(mongoTransactions)
|
||||
|
||||
return {
|
||||
success: true,
|
||||
insertedCount: result.insertedCount
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
insertedCount: 0
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error bulk updating transactions:', error)
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
statusMessage: 'Failed to bulk update transactions'
|
||||
})
|
||||
}
|
||||
})
|
||||
Loading…
Add table
Add a link
Reference in a new issue