40 lines
No EOL
916 B
JavaScript
40 lines
No EOL
916 B
JavaScript
import { getCollection } from '../../utils/db.js'
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
try {
|
|
const id = getRouterParam(event, 'id')
|
|
const body = await readBody(event)
|
|
const collection = await getCollection('transactions')
|
|
|
|
// Update document
|
|
const updateData = {
|
|
...body,
|
|
_id: id,
|
|
id: undefined,
|
|
updatedAt: new Date()
|
|
}
|
|
|
|
const result = await collection.replaceOne(
|
|
{ _id: id },
|
|
updateData
|
|
)
|
|
|
|
if (result.matchedCount === 0) {
|
|
throw createError({
|
|
statusCode: 404,
|
|
statusMessage: 'Transaction not found'
|
|
})
|
|
}
|
|
|
|
return {
|
|
success: true,
|
|
modifiedCount: result.modifiedCount
|
|
}
|
|
} catch (error) {
|
|
console.error('Error updating transaction:', error)
|
|
throw createError({
|
|
statusCode: 500,
|
|
statusMessage: 'Failed to update transaction'
|
|
})
|
|
}
|
|
}) |