From d262aaac8bed64c4109467703f450a3223563c7b Mon Sep 17 00:00:00 2001 From: ionutboangiu Date: Wed, 19 Apr 2023 02:54:59 -0400 Subject: [PATCH] Revise CDR rerating If the reRate parameter is set to true, also set the refund to true. In case CostDetails is not populated, before attempting to refund try to retrieve it from StorDB. Now that the refund happens before the debit, revise the expected values for the testV1CDRsProcessEventWithRefund subtest within the apier/v1/cdrs_it_test.go file. --- apier/v1/cdrs_it_test.go | 5 ++--- engine/cdrs.go | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/apier/v1/cdrs_it_test.go b/apier/v1/cdrs_it_test.go index 8e2102d5c..7a957b56a 100644 --- a/apier/v1/cdrs_it_test.go +++ b/apier/v1/cdrs_it_test.go @@ -237,12 +237,11 @@ func testV1CDRsProcessEventWithRefund(t *testing.T) { } if err := cdrsRpc.Call(utils.APIerSv2GetAccount, acntAttrs, &acnt); err != nil { t.Error(err) - } else if blc1 := acnt.GetBalanceWithID(utils.MetaVoice, "BALANCE1"); blc1.Value != 120000000000 { // refund is done after debit + } else if blc1 := acnt.GetBalanceWithID(utils.MetaVoice, "BALANCE1"); blc1.Value != 60000000000 { t.Errorf("Balance1 is: %s", utils.ToIJSON(blc1)) - } else if blc2 := acnt.GetBalanceWithID(utils.MetaVoice, "BALANCE2"); blc2.Value != 120000000000 { + } else if blc2 := acnt.GetBalanceWithID(utils.MetaVoice, "BALANCE2"); blc2.Value != 180000000000 { t.Errorf("Balance2 is: %s", utils.ToIJSON(blc2)) } - return } func testV1CDRsRefundOutOfSessionCost(t *testing.T) { diff --git a/engine/cdrs.go b/engine/cdrs.go index 8665426d2..64a6a0583 100644 --- a/engine/cdrs.go +++ b/engine/cdrs.go @@ -439,6 +439,9 @@ func (cdrS *CDRServer) eeSProcessEvent(cgrEv *CGREventWithEeIDs) (err error) { // in case of partially executed, both error and evs will be returned func (cdrS *CDRServer) processEvents(evs []*utils.CGREvent, chrgS, attrS, refund, ralS, store, reRate, export, thdS, stS bool) (outEvs []*utils.EventWithFlags, err error) { + if reRate { + refund = true + } if attrS { for _, ev := range evs { if err = cdrS.attrSProcessEvent(ev); err != nil { @@ -513,6 +516,18 @@ func (cdrS *CDRServer) processEvents(evs []*utils.CGREvent, } if refund { for i, cdr := range cdrs { + if cdr.CostDetails == nil { // if CostDetails is not populated, look for it inside the previously stored cdr + var prevCDRs []*CDR // only one should be returned + if prevCDRs, _, err = cdrS.cdrDb.GetCDRs( + &utils.CDRsFilter{CGRIDs: []string{cdr.CGRID}, + RunIDs: []string{cdr.RunID}}, false); err != nil { + utils.Logger.Info( + fmt.Sprintf("<%s> could not retrieve previously stored CDR, error: <%s>", + utils.CDRs, err.Error())) + continue + } + cdr.CostDetails = prevCDRs[0].CostDetails + } if rfnd, errRfd := cdrS.refundEventCost(cdr.CostDetails, cdr.RequestType, cdr.ToR); errRfd != nil { utils.Logger.Warning( @@ -1088,7 +1103,7 @@ func (cdrS *CDRServer) V1RateCDRs(arg *ArgRateCDRs, reply *string) (err error) { cgrEvs[i] = cdr.AsCGREvent() cgrEvs[i].APIOpts = arg.APIOpts } - if _, err = cdrS.processEvents(cgrEvs, chrgS, attrS, true, + if _, err = cdrS.processEvents(cgrEvs, chrgS, attrS, false, true, store, true, export, thdS, statS); err != nil { return utils.NewErrServerError(err) }