mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Revenue protection through Min-/MaxCost filters to GetLCR APIs, fixes #411
This commit is contained in:
@@ -32,7 +32,7 @@ func (self *ApierV1) GetLcr(lcrReq engine.LcrRequest, lcrReply *engine.LcrReply)
|
||||
return err
|
||||
}
|
||||
var lcrQried engine.LCRCost
|
||||
if err := self.Responder.GetLCR(&engine.AttrGetLcr{CallDescriptor: cd, Paginator: lcrReq.Paginator}, &lcrQried); err != nil {
|
||||
if err := self.Responder.GetLCR(&engine.AttrGetLcr{CallDescriptor: cd, LCRFilter: lcrReq.LCRFilter, Paginator: lcrReq.Paginator}, &lcrQried); err != nil {
|
||||
return utils.NewErrServerError(err)
|
||||
}
|
||||
if lcrQried.Entry == nil {
|
||||
@@ -65,7 +65,7 @@ func (self *ApierV1) GetLcrSuppliers(lcrReq engine.LcrRequest, suppliers *string
|
||||
return err
|
||||
}
|
||||
var lcrQried engine.LCRCost
|
||||
if err := self.Responder.GetLCR(&engine.AttrGetLcr{CallDescriptor: cd, Paginator: lcrReq.Paginator}, &lcrQried); err != nil {
|
||||
if err := self.Responder.GetLCR(&engine.AttrGetLcr{CallDescriptor: cd, LCRFilter: lcrReq.LCRFilter, Paginator: lcrReq.Paginator}, &lcrQried); err != nil {
|
||||
return utils.NewErrServerError(err)
|
||||
}
|
||||
if lcrQried.HasErrors() {
|
||||
|
||||
@@ -966,7 +966,7 @@ func (cd *CallDescriptor) GetLCRFromStorage() (*LCR, error) {
|
||||
return nil, utils.ErrNotFound
|
||||
}
|
||||
|
||||
func (cd *CallDescriptor) GetLCR(stats rpcclient.RpcClientConnection, p *utils.Paginator) (*LCRCost, error) {
|
||||
func (cd *CallDescriptor) GetLCR(stats rpcclient.RpcClientConnection, lcrFltr *LCRFilter, p *utils.Paginator) (*LCRCost, error) {
|
||||
cd.account = nil // make sure it's not cached
|
||||
lcr, err := cd.GetLCRFromStorage()
|
||||
if err != nil {
|
||||
@@ -1259,6 +1259,14 @@ func (cd *CallDescriptor) GetLCR(stats rpcclient.RpcClientConnection, p *utils.P
|
||||
})
|
||||
continue
|
||||
} else {
|
||||
if lcrFltr != nil {
|
||||
if lcrFltr.MinCost != nil && cc.Cost < *lcrFltr.MinCost {
|
||||
continue // MinCost not reached, ignore the supplier
|
||||
}
|
||||
if lcrFltr.MaxCost != nil && cc.Cost >= *lcrFltr.MaxCost {
|
||||
continue // Equal or higher than MaxCost allowed, ignore the supplier
|
||||
}
|
||||
}
|
||||
supplCost := &LCRSupplierCost{
|
||||
Supplier: fullSupplier,
|
||||
Cost: cc.Cost,
|
||||
|
||||
@@ -58,9 +58,15 @@ type LcrRequest struct {
|
||||
Duration string
|
||||
IgnoreErrors bool
|
||||
ExtraFields map[string]string
|
||||
*LCRFilter
|
||||
*utils.Paginator
|
||||
}
|
||||
|
||||
type LCRFilter struct {
|
||||
MinCost *float64
|
||||
MaxCost *float64
|
||||
}
|
||||
|
||||
func (self *LcrRequest) AsCallDescriptor(timezone string) (*CallDescriptor, error) {
|
||||
if len(self.Account) == 0 || len(self.Destination) == 0 {
|
||||
return nil, utils.ErrMandatoryIeMissing
|
||||
|
||||
@@ -204,7 +204,7 @@ func TestLcrGet(t *testing.T) {
|
||||
Account: "rif",
|
||||
Subject: "rif",
|
||||
}
|
||||
lcr, err := cd.GetLCR(nil, nil)
|
||||
lcr, err := cd.GetLCR(nil, nil, nil)
|
||||
if err != nil || lcr == nil {
|
||||
t.Errorf("Bad lcr: %+v, %v", lcr, err)
|
||||
}
|
||||
@@ -222,7 +222,7 @@ func TestLcrGetPrefix(t *testing.T) {
|
||||
Account: "rif",
|
||||
Subject: "rifus",
|
||||
}
|
||||
lcr, err := cd.GetLCR(nil, nil)
|
||||
lcr, err := cd.GetLCR(nil, nil, nil)
|
||||
if err != nil || lcr == nil {
|
||||
t.Errorf("Bad lcr: %+v, %v", lcr, err)
|
||||
}
|
||||
|
||||
@@ -43,6 +43,7 @@ type SessionRun struct {
|
||||
|
||||
type AttrGetLcr struct {
|
||||
*CallDescriptor
|
||||
*LCRFilter
|
||||
*utils.Paginator
|
||||
}
|
||||
|
||||
@@ -543,7 +544,7 @@ func (rs *Responder) GetLCR(attrs *AttrGetLcr, reply *LCRCost) error {
|
||||
rs.getCache().Cache(cacheKey, &cache2go.CacheItem{Err: err})
|
||||
return err
|
||||
}
|
||||
lcrCost, err := attrs.CallDescriptor.GetLCR(rs.Stats, attrs.Paginator)
|
||||
lcrCost, err := attrs.CallDescriptor.GetLCR(rs.Stats, attrs.LCRFilter, attrs.Paginator)
|
||||
if err != nil {
|
||||
rs.getCache().Cache(cacheKey, &cache2go.CacheItem{Err: err})
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user