title: Transactions With Mongoose and NodeJs, calling an external function
body: <p>I have some code that have implemented Transaction and it works perfectly, but now I need to call a function inside this code(it has a comment in my code example), both parts have to have success or then nothing have to be done. It is not working properly. How can I integrate the &quot;sendCashback, function&quot; to work perfectly with my Transaction?</p>
  <p>This is my code:</p>
  <pre><code>var transaction: any 
    let cashback_sent: any
    try{
      const transactiontimectrl = Number(process.env.TRANSACTION_TIME_STOP)   
      let dt = new Date();
      dt.setSeconds(dt.getSeconds() - transactiontimectrl);
      const timectrl = dt.toISOString(); 
  
      console.log('teste transaction1')
      console.log(timectrl)
  
      const session = await mongoose.startSession();
  
      await session.withTransaction(async () =&gt; {
        const transactionRes = await Transaction.findOne(
          {
            from: from,
            //to: to,
            //value: valuetodebit,
            createdAt: {$gt: timectrl}
          }).session(session)
          
        
        if(!transactionRes){
          transaction = await Transaction.create([
            {
              from,
              to,
              value: value,
              division_factor: destinationUserGroup.division_factor,
              title: title || null,
              description: description || null,
              type: type || null,
              hasCashback: hasCashback,
              realmoney: realmoney,
              valuetodebit: valuetodebit
            }],{ session }) 
        }else{
          throw new Error('Erro, uma transação semelhante foi realizada recentemente')
        }
  
        if (!transaction) {
          throw new Error('Erro, tente novamente mais tarde') 
        }
      
        console.log('@sadihjaisvq3')
  
        let fromBalance
        //Saldo real
        if (realmoney) {
          fromBalance = await Balance.findOne({
            type: IBalanceType.GENERAL,
            user: from
          }).session(session)
        }
        //Saldo cashback
        else {
          fromBalance = await Balance.findOne({
            type: IBalanceType.CASHBACK,
            user: from
          }).session(session)
        }
  
        console.log('****criar balance2')
  
        let toBalance = await Balance.findOne({
          type: IBalanceType.GENERAL,
          user: to
        }).session(session)
        console.log('****criar balance1', toBalance)
  
        if (!toBalance) {
          console.log('****criar balance')
          toBalance = await Balance.create(
          {
            user: to
          })
        }
  
        
        let toLivetPay = await Balance.findOne({
          type: IBalanceType.GENERAL,
          user: toLivet
        }).session(session)
  
        if (!fromBalance || !toBalance || !toLivetPay) throw new Error()
  
        if (!credit) {
          fromBalance.value = fromBalance.value + parseFloat(valuetodebit) * -1
        }
        // fromBalance.value = fromBalance.value + parseFloat(valuetodebit) * -1
        toBalance.value = toBalance.value + parseFloat(value)
  
        let valorCash = Number(value) / 0.75
        let valorLivet = Number(valorCash) * 0.25
        toLivetPay.value = toLivetPay.value + parseFloat(String(valorLivet))
  
        await fromBalance.save()
        await toBalance.save()
        await toLivetPay.save()
  
        
        if (hasCashback) {
          if (!saque) {
            //EXTERNAL FUNCTION---------------------------------------------------------
            cashback_sent = await sendCashback(
              transaction[0],
              body.originUser!,
              body.destinationUser!
            )
            //------------------------------------------------------------------------
          } 
           
          if (!cashback_sent) {
            throw new Error('Erro ao distribuir cashback') 
          }
          console.log('depois do cashback')
        }
  
      })
  </code></pre>