ADD: updateTransaction service

This commit is contained in:
MohammadHMazarei
2025-04-17 14:00:24 +03:30
parent fd8ac3d16a
commit 21ab53ad6a
+21 -1
View File
@@ -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 }