diff --git a/engine/suppliers.go b/engine/suppliers.go index c348a90b9..7462e652e 100644 --- a/engine/suppliers.go +++ b/engine/suppliers.go @@ -416,6 +416,16 @@ func (spS *SupplierService) sortedSuppliersForEvent(args *ArgsGetSuppliers) (sor if err != nil { return nil, err } + if args.Paginator.Offset != nil { + if *args.Paginator.Offset <= len(sortedSuppliers.SortedSuppliers) { + sortedSuppliers.SortedSuppliers = sortedSuppliers.SortedSuppliers[*args.Paginator.Offset:] + } + } + if args.Paginator.Limit != nil { + if *args.Paginator.Limit <= len(sortedSuppliers.SortedSuppliers) { + sortedSuppliers.SortedSuppliers = sortedSuppliers.SortedSuppliers[:*args.Paginator.Limit] + } + } return sortedSuppliers, nil } @@ -423,6 +433,7 @@ type ArgsGetSuppliers struct { IgnoreErrors bool MaxCost string // toDo: try with interface{} here utils.CGREvent + utils.Paginator *utils.ArgDispatcher } diff --git a/engine/suppliers_test.go b/engine/suppliers_test.go index b023055ae..72e505a19 100644 --- a/engine/suppliers_test.go +++ b/engine/suppliers_test.go @@ -492,6 +492,92 @@ func TestSuppliersSortedForEvent(t *testing.T) { } } +func TestSuppliersSortedForEventWithLimit(t *testing.T) { + eFirstSupplierProfile := &SortedSuppliers{ + ProfileID: "SupplierProfile2", + Sorting: utils.MetaWeight, + SortedSuppliers: []*SortedSupplier{ + { + SupplierID: "supplier1", + SortingData: map[string]interface{}{ + "Weight": 30.0, + }, + SupplierParameters: "param1", + }, + { + SupplierID: "supplier2", + SortingData: map[string]interface{}{ + "Weight": 20.0, + }, + SupplierParameters: "param2", + }, + }, + } + argsGetSuppliers[1].Paginator = utils.Paginator{ + Limit: utils.IntPointer(2), + } + sprf, err := splService.sortedSuppliersForEvent(argsGetSuppliers[1]) + if err != nil { + t.Errorf("Error: %+v", err) + } + if !reflect.DeepEqual(eFirstSupplierProfile, sprf) { + t.Errorf("Expecting: %+v, received: %+v", eFirstSupplierProfile, sprf) + } +} + +func TestSuppliersSortedForEventWithOffset(t *testing.T) { + eFirstSupplierProfile := &SortedSuppliers{ + ProfileID: "SupplierProfile2", + Sorting: utils.MetaWeight, + SortedSuppliers: []*SortedSupplier{ + { + SupplierID: "supplier3", + SortingData: map[string]interface{}{ + "Weight": 10.0, + }, + SupplierParameters: "param3", + }, + }, + } + argsGetSuppliers[1].Paginator = utils.Paginator{ + Offset: utils.IntPointer(2), + } + sprf, err := splService.sortedSuppliersForEvent(argsGetSuppliers[1]) + if err != nil { + t.Errorf("Error: %+v", err) + } + if !reflect.DeepEqual(eFirstSupplierProfile, sprf) { + t.Errorf("Expecting: %+v,received: %+v", utils.ToJSON(eFirstSupplierProfile), utils.ToJSON(sprf)) + } +} + +func TestSuppliersSortedForEventWithLimitAndOffset(t *testing.T) { + eFirstSupplierProfile := &SortedSuppliers{ + ProfileID: "SupplierProfile2", + Sorting: utils.MetaWeight, + SortedSuppliers: []*SortedSupplier{ + { + SupplierID: "supplier2", + SortingData: map[string]interface{}{ + "Weight": 20.0, + }, + SupplierParameters: "param2", + }, + }, + } + argsGetSuppliers[1].Paginator = utils.Paginator{ + Limit: utils.IntPointer(1), + Offset: utils.IntPointer(1), + } + sprf, err := splService.sortedSuppliersForEvent(argsGetSuppliers[1]) + if err != nil { + t.Errorf("Error: %+v", err) + } + if !reflect.DeepEqual(eFirstSupplierProfile, sprf) { + t.Errorf("Expecting: %+v,received: %+v", utils.ToJSON(eFirstSupplierProfile), utils.ToJSON(sprf)) + } +} + func TestSuppliersAsOptsGetSuppliers(t *testing.T) { s := &ArgsGetSuppliers{ IgnoreErrors: true, diff --git a/sessions/sessions.go b/sessions/sessions.go index 986c6610e..d941a331f 100644 --- a/sessions/sessions.go +++ b/sessions/sessions.go @@ -1720,6 +1720,7 @@ func (sS *SessionS) BiRPCv1AuthorizeEvent(clnt rpcclient.RpcClientConnection, IgnoreErrors: args.SuppliersIgnoreErrors, MaxCost: args.SuppliersMaxCost, CGREvent: *cgrEv, + Paginator: args.Paginator, ArgDispatcher: args.ArgDispatcher, } if err = sS.splS.Call(utils.SupplierSv1GetSuppliers,