Created
December 8, 2019 11:53
-
-
Save kartikpandey2/867f46d2f491809f5be142d4368dd5f2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const useZingCash = (userId, amountToPay) => { | |
//Find all zingcash document which {userId: userId, isExpired: false, status: ACTIVE, transactionType: CREDIT} and sort in ascending order by expiryDate | |
data = Date from db | |
let remainingAmountToPay = amountToPay | |
const transactionToUpdate = [] // will contain all transaction id which will be completely used to pay amountToPay | |
const breakTransaction = { _id: null, updateBody: {}} | |
for (let i=0; i<data.length; ++i) { | |
const {amount} = data[i] | |
if(remainingAmountToPay === 0) { | |
// Break the for loop if remainingAmountToPay becomes 0 before for loop ends | |
break; | |
} | |
if(remainingAmountToPay >= amount) { | |
remainingAmountToPay = remainingAmountToPay - amount | |
transactionToUpdate.push(data[i]._id) | |
}else { | |
// Transaction to break | |
breakTransaction._id = data[i]._id | |
breakTransaction.updateBody.amount = amount - remainingAmountToPay | |
remainingAmountToPay = 0 | |
} | |
} | |
// Update all transactionToUpdate with updateBody = {amount: 0} | |
if(breakTransaction._id) { | |
// Update break transaction with update body = breakTransaction.body | |
} | |
const zingCashUsed = amountToPay - remainingAmountToPay | |
// Add new DEBIT transaction for amountUsed = zingCashUsed | |
return {remainingAmountToPay, zinCashUsed } | |
} | |
// CRON JOB | |
const zingCashExpireCron = () => { | |
// Find all documents in zingcash with { status: ACTIVE, isExpired: false, expiryDate: { $lte: Date.now() } } | |
const data = [] // zingcash data from db | |
for(let i=0; i< data.length; ++i) { | |
const {userId, amount} = data[i] | |
// Add DEBIT transaction entry | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment