[FIX/BE-US]add feature restrict by location and areas in roles

This commit is contained in:
ragilap
2026-01-14 13:27:52 +07:00
parent 1847e5590a
commit dff9e73ab1
30 changed files with 1258 additions and 37 deletions
@@ -5,6 +5,7 @@ import (
"strconv"
"strings"
m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
"gitlab.com/mbugroup/lti-api.git/internal/modules/repports/dto"
service "gitlab.com/mbugroup/lti-api.git/internal/modules/repports/services"
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/repports/validations"
@@ -49,6 +50,21 @@ func (c *RepportController) GetExpense(ctx *fiber.Ctx) error {
RealizationDate: ctx.Query("realization_date", ""),
}
locationScope, err := m.ResolveLocationScope(ctx, c.RepportService.DB())
if err != nil {
return err
}
areaScope, err := m.ResolveAreaScope(ctx, c.RepportService.DB())
if err != nil {
return err
}
if locationScope.Restrict {
query.AllowedLocationIDs = toInt64Slice(locationScope.IDs)
}
if areaScope.Restrict {
query.AllowedAreaIDs = toInt64Slice(areaScope.IDs)
}
if query.Page < 1 || query.Limit < 1 {
return fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0")
}
@@ -130,6 +146,14 @@ func (c *RepportController) GetPurchaseSupplier(ctx *fiber.Ctx) error {
FilterBy: ctx.Query("filter_by", ""),
}
areaScope, err := m.ResolveAreaScope(ctx, c.RepportService.DB())
if err != nil {
return err
}
if areaScope.Restrict {
query.AllowedAreaIDs = toInt64Slice(areaScope.IDs)
}
if query.Page < 1 || query.Limit < 1 {
return fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0")
}
@@ -306,3 +330,14 @@ 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
}
@@ -53,10 +53,18 @@ func (r *purchaseSupplierRepositoryImpl) baseSupplierQuery(ctx context.Context,
Where("products.product_category_id = ?", filters.ProductCategoryId)
}
if filters.AreaId > 0 {
db = db.
Joins("JOIN warehouses ON warehouses.id = purchase_items.warehouse_id").
Where("warehouses.area_id = ?", filters.AreaId)
if filters.AreaId > 0 || filters.AllowedAreaIDs != nil {
db = db.Joins("JOIN warehouses ON warehouses.id = purchase_items.warehouse_id")
if filters.AreaId > 0 {
db = db.Where("warehouses.area_id = ?", filters.AreaId)
}
if filters.AllowedAreaIDs != nil {
if len(filters.AllowedAreaIDs) == 0 {
db = db.Where("1 = 0")
} else {
db = db.Where("warehouses.area_id IN ?", filters.AllowedAreaIDs)
}
}
}
if filters.StartDate != "" {
@@ -164,10 +172,18 @@ func (r *purchaseSupplierRepositoryImpl) GetItemsBySuppliers(ctx context.Context
Where("products.product_category_id = ?", filters.ProductCategoryId)
}
if filters.AreaId > 0 {
db = db.
Joins("JOIN warehouses ON warehouses.id = purchase_items.warehouse_id").
Where("warehouses.area_id = ?", filters.AreaId)
if filters.AreaId > 0 || filters.AllowedAreaIDs != nil {
db = db.Joins("JOIN warehouses ON warehouses.id = purchase_items.warehouse_id")
if filters.AreaId > 0 {
db = db.Where("warehouses.area_id = ?", filters.AreaId)
}
if filters.AllowedAreaIDs != nil {
if len(filters.AllowedAreaIDs) == 0 {
db = db.Where("1 = 0")
} else {
db = db.Where("warehouses.area_id IN ?", filters.AllowedAreaIDs)
}
}
}
if filters.StartDate != "" {
@@ -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 == "" {
@@ -13,6 +13,8 @@ type ExpenseQuery struct {
AreaId int64 `query:"area_id" validate:"omitempty"`
LocationId int64 `query:"location_id" validate:"omitempty"`
RealizationDate string `query:"realization_date" validate:"omitempty"`
AllowedAreaIDs []int64 `query:"-"`
AllowedLocationIDs []int64 `query:"-"`
}
type MarketingQuery struct {
@@ -42,6 +44,7 @@ type PurchaseSupplierQuery struct {
EndDate string `query:"end_date" validate:"omitempty"`
SortBy string `query:"sort_by" validate:"omitempty"`
FilterBy string `query:"filter_by" validate:"omitempty"`
AllowedAreaIDs []int64 `query:"-"`
}
type DebtSupplierQuery struct {