mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
Revert "Revert "[FIX/BE-US]add feature restrict by location and areas in roles""
This reverts commit 26bf7f165e.
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/modules/repports/dto"
|
||||
repportRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/repports/repositories"
|
||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/repports/validations"
|
||||
@@ -40,6 +41,7 @@ type RepportService interface {
|
||||
GetDebtSupplier(ctx *fiber.Ctx, params *validation.DebtSupplierQuery) ([]dto.DebtSupplierDTO, int64, error)
|
||||
GetHppPerKandang(ctx *fiber.Ctx) (*dto.HppPerKandangResponseData, *dto.HppPerKandangMetaDTO, error)
|
||||
GetProductionResult(ctx *fiber.Ctx, params *validation.ProductionResultQuery) ([]dto.ProductionResultDTO, int64, error)
|
||||
DB() *gorm.DB
|
||||
}
|
||||
|
||||
type repportService struct {
|
||||
@@ -95,6 +97,10 @@ func NewRepportService(
|
||||
}
|
||||
}
|
||||
|
||||
func (s *repportService) DB() *gorm.DB {
|
||||
return s.ExpenseRealizationRepo.DB()
|
||||
}
|
||||
|
||||
func (s *repportService) GetExpense(c *fiber.Ctx, params *validation.ExpenseQuery) ([]dto.RepportExpenseListDTO, int64, error) {
|
||||
if err := s.Validate.Struct(params); err != nil {
|
||||
return nil, 0, err
|
||||
@@ -1303,6 +1309,36 @@ func (s *repportService) parseHppPerKandangQuery(ctx *fiber.Ctx) (*validation.Hp
|
||||
return nil, dto.HppPerKandangFiltersDTO{}, fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
locationScope, err := m.ResolveLocationScope(ctx, s.ExpenseRealizationRepo.DB())
|
||||
if err != nil {
|
||||
return nil, dto.HppPerKandangFiltersDTO{}, err
|
||||
}
|
||||
areaScope, err := m.ResolveAreaScope(ctx, s.ExpenseRealizationRepo.DB())
|
||||
if err != nil {
|
||||
return nil, dto.HppPerKandangFiltersDTO{}, err
|
||||
}
|
||||
|
||||
if locationScope.Restrict {
|
||||
allowed := toInt64Slice(locationScope.IDs)
|
||||
if len(allowed) == 0 {
|
||||
locationIDs = []int64{-1}
|
||||
} else if len(locationIDs) > 0 {
|
||||
locationIDs = intersectInt64(locationIDs, allowed)
|
||||
} else {
|
||||
locationIDs = allowed
|
||||
}
|
||||
}
|
||||
if areaScope.Restrict {
|
||||
allowed := toInt64Slice(areaScope.IDs)
|
||||
if len(allowed) == 0 {
|
||||
areaIDs = []int64{-1}
|
||||
} else if len(areaIDs) > 0 {
|
||||
areaIDs = intersectInt64(areaIDs, allowed)
|
||||
} else {
|
||||
areaIDs = allowed
|
||||
}
|
||||
}
|
||||
|
||||
weightMin, err := parseOptionalFloat64(rawWeightMin)
|
||||
if err != nil {
|
||||
return nil, dto.HppPerKandangFiltersDTO{}, fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
@@ -1366,6 +1402,34 @@ func parseCommaSeparatedInt64s(raw string) ([]int64, error) {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func toInt64Slice(ids []uint) []int64 {
|
||||
if len(ids) == 0 {
|
||||
return nil
|
||||
}
|
||||
out := make([]int64, 0, len(ids))
|
||||
for _, id := range ids {
|
||||
out = append(out, int64(id))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func intersectInt64(a, b []int64) []int64 {
|
||||
if len(a) == 0 || len(b) == 0 {
|
||||
return nil
|
||||
}
|
||||
set := make(map[int64]struct{}, len(b))
|
||||
for _, id := range b {
|
||||
set[id] = struct{}{}
|
||||
}
|
||||
out := make([]int64, 0, len(a))
|
||||
for _, id := range a {
|
||||
if _, ok := set[id]; ok {
|
||||
out = append(out, id)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func parseOptionalFloat64(raw string) (*float64, error) {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
|
||||
Reference in New Issue
Block a user