Skip to content

Instantly share code, notes, and snippets.

@mulieriq
Created November 25, 2021 12:51
Show Gist options
  • Save mulieriq/c967862fbbb7e9dfeb91fac2698a0e4d to your computer and use it in GitHub Desktop.
Save mulieriq/c967862fbbb7e9dfeb91fac2698a0e4d to your computer and use it in GitHub Desktop.
add cash to wallet
function addMoneyToWallet(userDataTransactionsOrderId, data) {
let serviceFeeCal = Math.ceil(parseInt(data.data().amountToPay.toString()) * 0.05)
let netAmount = parseInt(data.data().amountToPay.toString()) - serviceFeeCal
console.log(`WALLET -------------------${serviceFeeCal} -- ${netAmount} - ${JSON.stringify(data.data())}`)
admin.firestore().collection("wallet").doc(data.data().recepientId.toString()).collection("transactions").add(
{
tenancyTransactionId: userDataTransactionsOrderId,
date: data.data().payedDate,
hash: "Genesis",
prevHash: "PrevHash",
gross: data.data().amountToPay.toString(),
serviceRate: "5%",
serviceFee: serviceFeeCal.toString(),
net: netAmount.toString(),
transactionType: "Deposit",
}).then((result) => {
console.log(`TRANSACTION ADD RESULTS -------------${JSON.stringify( result.data)}`)
admin.firestore().collection("wallet").doc(data.data().recepientId.toString()).get().then((query) => {
if (!query.exists) {
//creat a wallet
admin.firestore().collection("wallet").doc(data.data().recepientId.toString()).set(
{
tenancyTransactionId: userDataTransactionsOrderId,
updatedOn: "updated on", //to add da
net: netAmount.toString(),
prevHash: "result.data().hash",
hash: "GenesisWalletHash",
lastTransactionId: result.id
}
).catch((e) => console.log(`Error Adding data to wallet${e}`));
} else {
let walletAmount = query.data().net
let newWallet = netAmount + parseInt(walletAmount)
//update existing wallet
admin.firestore().collection("wallet").doc(data.data().recepientId.toString()).update(
{
tenancyTransactionId: userDataTransactionsOrderId,
updatedOn:" result.data().payedDate",
net: newWallet.toString(),
prevHash: "result.data().hash",
hash: "GenesisWalletHash",
lastTransactionId: result.id
}
).catch((e) => console.log(`Error Adding data to wallet${e}`));
}
})
console.log('Creating Wallet')
sendNotification('money added', 'Your Wallet account has been credited.', data.data().recepientId.toString(), 'wallet')
}).catch((e) => console.log(`Error Adding data to wallet${e}`));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment