ADD: updateTransaction service
This commit is contained in:
@@ -6,6 +6,15 @@ const ObjectID = mongoose.Schema.Types.ObjectId
|
||||
const { client } = require('../../setup/typesense')
|
||||
const TsApiFeatures = require('../../utils/TsApiFeatures')
|
||||
|
||||
const filterObj = (obj, ...allowedFields) => {
|
||||
const newObj = {}
|
||||
Object.keys(obj).forEach((el) => {
|
||||
if (allowedFields.includes(el)) newObj[el] = obj[el]
|
||||
})
|
||||
|
||||
return newObj
|
||||
}
|
||||
|
||||
const createTransaction = async (data) => {
|
||||
if (data.type == 'withdraw') {
|
||||
// Aggregate transactions to calculate inventory
|
||||
@@ -117,4 +126,15 @@ const getBelongingInventory = async (belongingId) => {
|
||||
return { belongingCode: belonging.belongingCode, ...result[0] }
|
||||
}
|
||||
|
||||
module.exports = { createTransaction, getUserTransaction, getBelongingInventory }
|
||||
const updateTransaction = async (transactionId, data) => {
|
||||
const filteredBody = filterObj(data, 'status')
|
||||
const transaction = await Transaction.findOneAndUpdate({ _id: transactionId }, filteredBody, { new: true })
|
||||
|
||||
if (!transaction) {
|
||||
throw new AppError('NoTransactionFound', 404, {})
|
||||
}
|
||||
|
||||
return transaction
|
||||
}
|
||||
|
||||
module.exports = { createTransaction, getUserTransaction, getBelongingInventory, updateTransaction }
|
||||
|
||||
Reference in New Issue
Block a user