ADD & UPDATE: BelongingInventory and create Transaction handler
This commit is contained in:
@@ -1,9 +1,34 @@
|
||||
const Transaction = require('./transactionModel')
|
||||
const Belonging = require('../belonging/belongingModel')
|
||||
const AppError = require('../../globalErrorHandler/AppError')
|
||||
const mongoose = require('mongoose')
|
||||
const ObjectID = mongoose.Schema.Types.ObjectId
|
||||
const { client } = require('../../setup/typesense')
|
||||
const TsApiFeatures = require('../../utils/TsApiFeatures')
|
||||
|
||||
const createTransaction = async (data) => {
|
||||
if (data.type == 'withdraw') {
|
||||
// Aggregate transactions to calculate inventory
|
||||
const result = await Transaction.aggregate([
|
||||
{
|
||||
$match: {
|
||||
belongingId: ObjectID,
|
||||
status: 'finished',
|
||||
},
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
_id: '$belongingId',
|
||||
totalInventory: {
|
||||
$sum: '$value',
|
||||
},
|
||||
},
|
||||
},
|
||||
])
|
||||
|
||||
if (result[0].totalInventory <= 0) throw new AppError('Invalid Transaction', 404, {})
|
||||
}
|
||||
|
||||
return Transaction.create({
|
||||
type: data.type,
|
||||
status: data.status,
|
||||
@@ -20,4 +45,76 @@ const getUserTransaction = async (userId) => {
|
||||
return Transaction.find({ userId }, null, null)
|
||||
}
|
||||
|
||||
module.exports = { createTransaction, getUserTransaction }
|
||||
const getBelongingInventory = async (belongingId) => {
|
||||
const belonging = await Belonging.findById(belongingId, null, null)
|
||||
|
||||
if (!belonging) {
|
||||
throw new AppError('NoBelongingFound', 404, {})
|
||||
}
|
||||
|
||||
// Simplified aggregation to calculate inventory metrics
|
||||
const result = await Transaction.aggregate([
|
||||
{
|
||||
$match: {
|
||||
belongingId: ObjectID,
|
||||
$or: [{ status: 'finished' }, { status: 'pending' }],
|
||||
},
|
||||
},
|
||||
{
|
||||
$facet: {
|
||||
// Calculate total inventory from finished transactions
|
||||
finishedInventory: [
|
||||
{ $match: { status: 'finished' } },
|
||||
{
|
||||
$group: {
|
||||
_id: null,
|
||||
total: { $sum: '$value' },
|
||||
},
|
||||
},
|
||||
],
|
||||
// Calculate pending deposits
|
||||
pendingDepositsInventory: [
|
||||
{
|
||||
$match: {
|
||||
status: 'pending',
|
||||
type: 'deposit',
|
||||
},
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
_id: null,
|
||||
total: { $sum: '$value' },
|
||||
},
|
||||
},
|
||||
],
|
||||
// Calculate pending withdrawals
|
||||
pendingWithdrawsInventory: [
|
||||
{
|
||||
$match: {
|
||||
status: 'pending',
|
||||
type: 'withdraw',
|
||||
},
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
_id: null,
|
||||
total: { $sum: '$value' },
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
{
|
||||
// Restructure the output to get only the values
|
||||
$project: {
|
||||
finishedInventory: { $ifNull: [{ $arrayElemAt: ['$finishedInventory.total', 0] }, 0] },
|
||||
pendingDepositsInventory: { $ifNull: [{ $arrayElemAt: ['$pendingDepositsInventory.total', 0] }, 0] },
|
||||
pendingWithdrawsInventory: { $ifNull: [{ $arrayElemAt: ['$pendingWithdrawsInventory.total', 0] }, 0] },
|
||||
},
|
||||
},
|
||||
])
|
||||
|
||||
return { belongingCode: belonging.belongingCode, ...result[0] }
|
||||
}
|
||||
|
||||
module.exports = { createTransaction, getUserTransaction, getBelongingInventory }
|
||||
|
||||
Reference in New Issue
Block a user