[FIX/BE-US] adjustment sso-location

This commit is contained in:
ragilap
2026-01-22 09:27:11 +07:00
parent a3e9017e29
commit 3a457ceee5
12 changed files with 158 additions and 133 deletions
+24 -2
View File
@@ -178,8 +178,6 @@ func uniqueUint(ids []uint) []uint {
return result return result
} }
func ApplyScopeFilter(db *gorm.DB, scope ScopeFilter, column string) *gorm.DB { func ApplyScopeFilter(db *gorm.DB, scope ScopeFilter, column string) *gorm.DB {
if db == nil || !scope.Restrict { if db == nil || !scope.Restrict {
return db return db
@@ -190,6 +188,30 @@ func ApplyScopeFilter(db *gorm.DB, scope ScopeFilter, column string) *gorm.DB {
return db.Where(column+" IN ?", scope.IDs) return db.Where(column+" IN ?", scope.IDs)
} }
func ApplyLocationScope(c *fiber.Ctx, db *gorm.DB, column string) (*gorm.DB, error) {
scopeDB := db
if db != nil {
scopeDB = db.Session(&gorm.Session{NewDB: true})
}
scope, err := ResolveLocationScope(c, scopeDB)
if err != nil {
return db, err
}
return ApplyScopeFilter(db, scope, column), nil
}
func ApplyAreaScope(c *fiber.Ctx, db *gorm.DB, column string) (*gorm.DB, error) {
scopeDB := db
if db != nil {
scopeDB = db.Session(&gorm.Session{NewDB: true})
}
scope, err := ResolveAreaScope(c, scopeDB)
if err != nil {
return db, err
}
return ApplyScopeFilter(db, scope, column), nil
}
func EnsureWarehouseAccess(c *fiber.Ctx, db *gorm.DB, warehouseID uint) error { func EnsureWarehouseAccess(c *fiber.Ctx, db *gorm.DB, warehouseID uint) error {
if warehouseID == 0 { if warehouseID == 0 {
return fiber.NewError(fiber.StatusBadRequest, "Invalid warehouse id") return fiber.NewError(fiber.StatusBadRequest, "Invalid warehouse id")
@@ -87,22 +87,22 @@ func (s expenseService) GetAll(c *fiber.Ctx, params *validation.Query) ([]expens
return nil, 0, err return nil, 0, err
} }
scope, err := middleware.ResolveLocationScope(c, s.Repository.DB()) var scopeErr error
if err != nil {
return nil, 0, err
}
offset := (params.Page - 1) * params.Limit offset := (params.Page - 1) * params.Limit
expenses, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB { expenses, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
db = s.withRelations(db) db = s.withRelations(db)
db = middleware.ApplyScopeFilter(db, scope, "location_id") db, scopeErr = middleware.ApplyLocationScope(c, db, "expenses.location_id")
if params.Search != "" { if params.Search != "" {
return db.Where("category ILIKE ?", "%"+params.Search+"%") return db.Where("category ILIKE ?", "%"+params.Search+"%")
} }
return db.Order("created_at DESC").Order("updated_at DESC") return db.Order("created_at DESC").Order("updated_at DESC")
}) })
if scopeErr != nil {
return nil, 0, scopeErr
}
if err != nil { if err != nil {
return nil, 0, err return nil, 0, err
@@ -123,16 +123,16 @@ func (s expenseService) GetAll(c *fiber.Ctx, params *validation.Query) ([]expens
} }
func (s expenseService) GetOne(c *fiber.Ctx, id uint) (*expenseDto.ExpenseDetailDTO, error) { func (s expenseService) GetOne(c *fiber.Ctx, id uint) (*expenseDto.ExpenseDetailDTO, error) {
scope, err := middleware.ResolveLocationScope(c, s.Repository.DB()) var scopeErr error
if err != nil {
return nil, err
}
expense, err := s.Repository.GetByID(c.Context(), id, func(db *gorm.DB) *gorm.DB { expense, err := s.Repository.GetByID(c.Context(), id, func(db *gorm.DB) *gorm.DB {
db = s.withRelations(db) db = s.withRelations(db)
db = middleware.ApplyScopeFilter(db, scope, "location_id") db, scopeErr = middleware.ApplyLocationScope(c, db, "expenses.location_id")
return db return db
}) })
if scopeErr != nil {
return nil, scopeErr
}
if err != nil { if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) { if errors.Is(err, gorm.ErrRecordNotFound) {
@@ -303,20 +303,12 @@ func (s *adjustmentService) AdjustmentHistory(c *fiber.Ctx, query *validation.Qu
return nil, 0, fiber.NewError(fiber.StatusNotFound, "Product not found") return nil, 0, fiber.NewError(fiber.StatusNotFound, "Product not found")
} }
var scopeErr error
stockLogs, total, err := s.StockLogsRepository.GetAll(c.Context(), offset, query.Limit, func(db *gorm.DB) *gorm.DB { stockLogs, total, err := s.StockLogsRepository.GetAll(c.Context(), offset, query.Limit, func(db *gorm.DB) *gorm.DB {
scope, err := m.ResolveLocationScope(c, s.StockLogsRepository.DB()) db = db.
if err != nil { Joins("JOIN product_warehouses pw ON pw.id = stock_logs.product_warehouse_id").
return db.Where("1 = 0") Joins("JOIN warehouses w ON w.id = pw.warehouse_id")
} db, scopeErr = m.ApplyLocationScope(c, db, "w.location_id")
if scope.Restrict {
if len(scope.IDs) == 0 {
return db.Where("1 = 0")
}
db = db.
Joins("JOIN product_warehouses pw ON pw.id = stock_logs.product_warehouse_id").
Joins("JOIN warehouses w ON w.id = pw.warehouse_id")
db = m.ApplyScopeFilter(db, scope, "w.location_id")
}
db = s.withRelations(db) db = s.withRelations(db)
db = db.Where("loggable_type = ?", string(utils.StockLogTypeAdjustment)) db = db.Where("loggable_type = ?", string(utils.StockLogTypeAdjustment))
@@ -329,6 +321,9 @@ func (s *adjustmentService) AdjustmentHistory(c *fiber.Ctx, query *validation.Qu
return db.Order("created_at DESC") return db.Order("created_at DESC")
}) })
if scopeErr != nil {
return nil, 0, scopeErr
}
if err != nil { if err != nil {
s.Log.Errorf("Failed to get adjustments: %+v", err) s.Log.Errorf("Failed to get adjustments: %+v", err)
return nil, 0, fiber.NewError(fiber.StatusInternalServerError, "Failed to get adjustment history") return nil, 0, fiber.NewError(fiber.StatusInternalServerError, "Failed to get adjustment history")
@@ -47,27 +47,22 @@ func (s areaService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.Ar
return nil, 0, err return nil, 0, err
} }
scope, err := m.ResolveAreaScope(c, s.Repository.DB()) var scopeErr error
if err != nil {
return nil, 0, err
}
offset := (params.Page - 1) * params.Limit offset := (params.Page - 1) * params.Limit
areas, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB { areas, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
db = s.withRelations(db) db = s.withRelations(db)
if scope.Restrict { db, scopeErr = m.ApplyAreaScope(c, db, "id")
if len(scope.IDs) == 0 {
return db.Where("1 = 0")
}
db = db.Where("id IN ?", scope.IDs)
}
if params.Search != "" { if params.Search != "" {
return db.Where("name ILIKE ?", "%"+params.Search+"%") return db.Where("name ILIKE ?", "%"+params.Search+"%")
} }
return db.Order("created_at DESC").Order("updated_at DESC") return db.Order("created_at DESC").Order("updated_at DESC")
}) })
if scopeErr != nil {
return nil, 0, scopeErr
}
if err != nil { if err != nil {
s.Log.Errorf("Failed to get areas: %+v", err) s.Log.Errorf("Failed to get areas: %+v", err)
return nil, 0, err return nil, 0, err
@@ -76,21 +71,16 @@ func (s areaService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.Ar
} }
func (s areaService) GetOne(c *fiber.Ctx, id uint) (*entity.Area, error) { func (s areaService) GetOne(c *fiber.Ctx, id uint) (*entity.Area, error) {
scope, err := m.ResolveAreaScope(c, s.Repository.DB()) var scopeErr error
if err != nil {
return nil, err
}
area, err := s.Repository.GetByID(c.Context(), id, func(db *gorm.DB) *gorm.DB { area, err := s.Repository.GetByID(c.Context(), id, func(db *gorm.DB) *gorm.DB {
db = s.withRelations(db) db = s.withRelations(db)
if scope.Restrict { db, scopeErr = m.ApplyAreaScope(c, db, "id")
if len(scope.IDs) == 0 {
return db.Where("1 = 0")
}
db = db.Where("id IN ?", scope.IDs)
}
return db return db
}) })
if scopeErr != nil {
return nil, scopeErr
}
if errors.Is(err, gorm.ErrRecordNotFound) { if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fiber.NewError(fiber.StatusNotFound, "Area not found") return nil, fiber.NewError(fiber.StatusNotFound, "Area not found")
} }
@@ -49,16 +49,13 @@ func (s kandangService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity
return nil, 0, err return nil, 0, err
} }
scope, err := m.ResolveLocationScope(c, s.Repository.DB()) var scopeErr error
if err != nil {
return nil, 0, err
}
offset := (params.Page - 1) * params.Limit offset := (params.Page - 1) * params.Limit
kandangs, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB { kandangs, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
db = s.withRelations(db) db = s.withRelations(db)
db = m.ApplyScopeFilter(db, scope, "location_id") db, scopeErr = m.ApplyLocationScope(c, db, "kandangs.location_id")
if params.Search != "" { if params.Search != "" {
return db.Where("name ILIKE ?", "%"+params.Search+"%") return db.Where("name ILIKE ?", "%"+params.Search+"%")
} }
@@ -71,6 +68,9 @@ func (s kandangService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity
return db.Order("created_at DESC").Order("updated_at DESC") return db.Order("created_at DESC").Order("updated_at DESC")
}) })
if scopeErr != nil {
return nil, 0, scopeErr
}
if err != nil { if err != nil {
s.Log.Errorf("Failed to get kandangs: %+v", err) s.Log.Errorf("Failed to get kandangs: %+v", err)
return nil, 0, err return nil, 0, err
@@ -79,16 +79,16 @@ func (s kandangService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity
} }
func (s kandangService) GetOne(c *fiber.Ctx, id uint) (*entity.Kandang, error) { func (s kandangService) GetOne(c *fiber.Ctx, id uint) (*entity.Kandang, error) {
scope, err := m.ResolveLocationScope(c, s.Repository.DB()) var scopeErr error
if err != nil {
return nil, err
}
kandang, err := s.Repository.GetByID(c.Context(), id, func(db *gorm.DB) *gorm.DB { kandang, err := s.Repository.GetByID(c.Context(), id, func(db *gorm.DB) *gorm.DB {
db = s.withRelations(db) db = s.withRelations(db)
db = m.ApplyScopeFilter(db, scope, "location_id") db, scopeErr = m.ApplyLocationScope(c, db, "kandangs.location_id")
return db return db
}) })
if scopeErr != nil {
return nil, scopeErr
}
if errors.Is(err, gorm.ErrRecordNotFound) { if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fiber.NewError(fiber.StatusNotFound, "Kandang not found") return nil, fiber.NewError(fiber.StatusNotFound, "Kandang not found")
} }
@@ -47,21 +47,13 @@ func (s locationService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entit
return nil, 0, err return nil, 0, err
} }
scope, err := m.ResolveLocationScope(c, s.Repository.DB()) var scopeErr error
if err != nil {
return nil, 0, err
}
offset := (params.Page - 1) * params.Limit offset := (params.Page - 1) * params.Limit
locations, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB { locations, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
db = s.withRelations(db) db = s.withRelations(db)
if scope.Restrict { db, scopeErr = m.ApplyLocationScope(c, db, "locations.id")
if len(scope.IDs) == 0 {
return db.Where("1 = 0")
}
db = db.Where("id IN ?", scope.IDs)
}
if params.Search != "" { if params.Search != "" {
db = db.Where("name ILIKE ?", "%"+params.Search+"%") db = db.Where("name ILIKE ?", "%"+params.Search+"%")
} }
@@ -71,6 +63,9 @@ func (s locationService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entit
return db.Order("created_at DESC").Order("updated_at DESC") return db.Order("created_at DESC").Order("updated_at DESC")
}) })
if scopeErr != nil {
return nil, 0, scopeErr
}
if err != nil { if err != nil {
s.Log.Errorf("Failed to get locations: %+v", err) s.Log.Errorf("Failed to get locations: %+v", err)
return nil, 0, err return nil, 0, err
@@ -79,21 +74,16 @@ func (s locationService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entit
} }
func (s locationService) GetOne(c *fiber.Ctx, id uint) (*entity.Location, error) { func (s locationService) GetOne(c *fiber.Ctx, id uint) (*entity.Location, error) {
scope, err := m.ResolveLocationScope(c, s.Repository.DB()) var scopeErr error
if err != nil {
return nil, err
}
location, err := s.Repository.GetByID(c.Context(), id, func(db *gorm.DB) *gorm.DB { location, err := s.Repository.GetByID(c.Context(), id, func(db *gorm.DB) *gorm.DB {
db = s.withRelations(db) db = s.withRelations(db)
if scope.Restrict { db, scopeErr = m.ApplyLocationScope(c, db, "locations.id")
if len(scope.IDs) == 0 {
return db.Where("1 = 0")
}
db = db.Where("id IN ?", scope.IDs)
}
return db return db
}) })
if scopeErr != nil {
return nil, scopeErr
}
if errors.Is(err, gorm.ErrRecordNotFound) { if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fiber.NewError(fiber.StatusNotFound, "Location not found") return nil, fiber.NewError(fiber.StatusNotFound, "Location not found")
} }
@@ -48,16 +48,13 @@ func (s warehouseService) GetAll(c *fiber.Ctx, params *validation.Query) ([]enti
return nil, 0, err return nil, 0, err
} }
scope, err := m.ResolveAreaScope(c, s.Repository.DB()) var scopeErr error
if err != nil {
return nil, 0, err
}
offset := (params.Page - 1) * params.Limit offset := (params.Page - 1) * params.Limit
warehouses, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB { warehouses, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
db = s.withRelations(db) db = s.withRelations(db)
db = m.ApplyScopeFilter(db, scope, "area_id") db, scopeErr = m.ApplyAreaScope(c, db, "warehouses.area_id")
if params.Search != "" { if params.Search != "" {
db = db.Where("warehouses.name ILIKE ?", "%"+params.Search+"%") db = db.Where("warehouses.name ILIKE ?", "%"+params.Search+"%")
} }
@@ -84,6 +81,9 @@ func (s warehouseService) GetAll(c *fiber.Ctx, params *validation.Query) ([]enti
return db.Order("created_at DESC").Order("updated_at DESC") return db.Order("created_at DESC").Order("updated_at DESC")
}) })
if scopeErr != nil {
return nil, 0, scopeErr
}
if err != nil { if err != nil {
s.Log.Errorf("Failed to get warehouses: %+v", err) s.Log.Errorf("Failed to get warehouses: %+v", err)
return nil, 0, err return nil, 0, err
@@ -92,16 +92,16 @@ func (s warehouseService) GetAll(c *fiber.Ctx, params *validation.Query) ([]enti
} }
func (s warehouseService) GetOne(c *fiber.Ctx, id uint) (*entity.Warehouse, error) { func (s warehouseService) GetOne(c *fiber.Ctx, id uint) (*entity.Warehouse, error) {
scope, err := m.ResolveAreaScope(c, s.Repository.DB()) var scopeErr error
if err != nil {
return nil, err
}
warehouse, err := s.Repository.GetByID(c.Context(), id, func(db *gorm.DB) *gorm.DB { warehouse, err := s.Repository.GetByID(c.Context(), id, func(db *gorm.DB) *gorm.DB {
db = s.withRelations(db) db = s.withRelations(db)
db = m.ApplyScopeFilter(db, scope, "area_id") db, scopeErr = m.ApplyAreaScope(c, db, "warehouses.area_id")
return db return db
}) })
if scopeErr != nil {
return nil, scopeErr
}
if errors.Is(err, gorm.ErrRecordNotFound) { if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fiber.NewError(fiber.StatusNotFound, "Warehouse not found") return nil, fiber.NewError(fiber.StatusNotFound, "Warehouse not found")
} }
@@ -7,6 +7,7 @@ import (
"strconv" "strconv"
"strings" "strings"
warehouseDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/dto"
"gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/dto" "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/dto"
service "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/services" service "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/services"
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/validations" validation "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/validations"
@@ -278,14 +279,22 @@ func (u *ProjectflockController) LookupProjectFlockKandang(c *fiber.Ctx) error {
if err != nil { if err != nil {
return err return err
} }
_ = availableStock
dtoResult := dto.ToProjectFlockKandangDTO(*result) dtoResult := dto.ToProjectFlockKandangDTO(*result)
dtoResult.AvailableQuantity = float64(availableStock) if population, err := u.ProjectflockService.GetProjectFlockKandangPopulation(c, result.Id); err != nil {
return err
} else {
dtoResult.AvailableQuantity = population
}
if warehouse, werr := u.ProjectflockService.GetWarehouseByKandangID(c, result.KandangId); werr != nil {
return werr
} else if warehouse != nil {
mapped := warehouseDTO.ToWarehouseRelationDTO(*warehouse)
dtoResult.Warehouse = &mapped
}
if withPopulation { if withPopulation {
population, err := u.ProjectflockService.GetProjectFlockKandangPopulation(c, result.Id) population := dtoResult.AvailableQuantity
if err != nil {
return err
}
dtoResult.Population = &population dtoResult.Population = &population
} }
@@ -7,6 +7,7 @@ import (
kandangDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/dto" kandangDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/dto"
locationDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/locations/dto" locationDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/locations/dto"
productionStandardDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/production-standards/dto" productionStandardDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/production-standards/dto"
warehouseDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/dto"
userDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto" userDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto"
) )
@@ -17,24 +18,26 @@ type KandangWithPivotDTO struct {
type ProjectFlockWithPivotDTO struct { type ProjectFlockWithPivotDTO struct {
ProjectFlockRelationDTO ProjectFlockRelationDTO
Area *areaDTO.AreaRelationDTO `json:"area,omitempty"` Area *areaDTO.AreaRelationDTO `json:"area,omitempty"`
Category string `json:"category"` Category string `json:"category"`
Fcr *fcrDTO.FcrRelationDTO `json:"fcr,omitempty"` Fcr *fcrDTO.FcrRelationDTO `json:"fcr,omitempty"`
ProductionStandard *productionStandardDTO.ProductionStandardRelationDTO `json:"production_standard,omitempty"` ProductionStandard *productionStandardDTO.ProductionStandardRelationDTO `json:"production_standard,omitempty"`
Location *locationDTO.LocationRelationDTO `json:"location,omitempty"` ProductionStandardId uint `json:"production_standard_id"`
Kandangs []KandangWithPivotDTO `json:"kandangs,omitempty"` Location *locationDTO.LocationRelationDTO `json:"location,omitempty"`
CreatedUser *userDTO.UserRelationDTO `json:"created_user,omitempty"` Kandangs []KandangWithPivotDTO `json:"kandangs,omitempty"`
CreatedUser *userDTO.UserRelationDTO `json:"created_user,omitempty"`
} }
type ProjectFlockKandangDTO struct { type ProjectFlockKandangDTO struct {
Id uint `json:"id"` Id uint `json:"id"`
ProjectFlockKandangId uint `json:"project_flock_kandang_id"` ProjectFlockKandangId uint `json:"project_flock_kandang_id"`
ProjectFlockId uint `json:"project_flock_id"` ProjectFlockId uint `json:"project_flock_id"`
KandangId uint `json:"kandang_id"` KandangId uint `json:"kandang_id"`
Kandang *kandangDTO.KandangRelationDTO `json:"kandang,omitempty"` Kandang *kandangDTO.KandangRelationDTO `json:"kandang,omitempty"`
ProjectFlock *ProjectFlockWithPivotDTO `json:"project_flock,omitempty"` Warehouse *warehouseDTO.WarehouseRelationDTO `json:"warehouse,omitempty"`
AvailableQuantity float64 `json:"available_quantity"` ProjectFlock *ProjectFlockWithPivotDTO `json:"project_flock,omitempty"`
Population *float64 `json:"population,omitempty"` AvailableQuantity float64 `json:"available_quantity"`
Population *float64 `json:"population,omitempty"`
} }
func ToProjectFlockKandangDTO(e entity.ProjectFlockKandang) ProjectFlockKandangDTO { func ToProjectFlockKandangDTO(e entity.ProjectFlockKandang) ProjectFlockKandangDTO {
@@ -53,7 +56,8 @@ func ToProjectFlockKandangDTO(e entity.ProjectFlockKandang) ProjectFlockKandangD
Period: e.Period, Period: e.Period,
FlockName: e.ProjectFlock.FlockName, FlockName: e.ProjectFlock.FlockName,
}, },
Category: e.ProjectFlock.Category, Category: e.ProjectFlock.Category,
ProductionStandardId: e.ProjectFlock.ProductionStandardId,
} }
if e.ProjectFlock.Area.Id != 0 { if e.ProjectFlock.Area.Id != 0 {
@@ -38,6 +38,7 @@ type ProjectflockService interface {
GetOne(ctx *fiber.Ctx, id uint) (*entity.ProjectFlock, *flockDTO.FlockRelationDTO, error) GetOne(ctx *fiber.Ctx, id uint) (*entity.ProjectFlock, *flockDTO.FlockRelationDTO, error)
CreateOne(ctx *fiber.Ctx, req *validation.Create) (*entity.ProjectFlock, error) CreateOne(ctx *fiber.Ctx, req *validation.Create) (*entity.ProjectFlock, error)
GetAvailableDocQuantity(ctx *fiber.Ctx, kandangID uint) (float64, error) GetAvailableDocQuantity(ctx *fiber.Ctx, kandangID uint) (float64, error)
GetWarehouseByKandangID(ctx *fiber.Ctx, kandangID uint) (*entity.Warehouse, error)
DeleteOne(ctx *fiber.Ctx, id uint) error DeleteOne(ctx *fiber.Ctx, id uint) error
GetProjectFlockKandangByProjectAndKandang(ctx *fiber.Ctx, projectFlockID uint, kandangID uint) (*entity.ProjectFlockKandang, float64, error) GetProjectFlockKandangByProjectAndKandang(ctx *fiber.Ctx, projectFlockID uint, kandangID uint) (*entity.ProjectFlockKandang, float64, error)
GetProjectFlockKandangPopulation(ctx *fiber.Ctx, projectFlockKandangID uint) (float64, error) GetProjectFlockKandangPopulation(ctx *fiber.Ctx, projectFlockKandangID uint) (float64, error)
@@ -532,6 +533,31 @@ func (s projectflockService) GetAvailableDocQuantity(ctx *fiber.Ctx, kandangID u
return total, nil return total, nil
} }
func (s projectflockService) GetWarehouseByKandangID(ctx *fiber.Ctx, kandangID uint) (*entity.Warehouse, error) {
if kandangID == 0 || s.WarehouseRepo == nil {
return nil, nil
}
var warehouse entity.Warehouse
err := s.WarehouseRepo.DB().WithContext(ctx.Context()).
Preload("Area").
Preload("Location").
Preload("Kandang").
Where("kandang_id = ?", kandangID).
Where("deleted_at IS NULL").
Order("id DESC").
First(&warehouse).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
if err != nil {
s.Log.Errorf("Failed to fetch warehouse for kandang %d: %+v", kandangID, err)
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch warehouse")
}
return &warehouse, nil
}
func (s projectflockService) GetProjectPeriods(c *fiber.Ctx, projectIDs []uint) (map[uint]int, error) { func (s projectflockService) GetProjectPeriods(c *fiber.Ctx, projectIDs []uint) (map[uint]int, error) {
if len(projectIDs) == 0 { if len(projectIDs) == 0 {
return map[uint]int{}, nil return map[uint]int{}, nil
@@ -101,10 +101,7 @@ func (s recordingService) GetAll(c *fiber.Ctx, params *validation.Query) ([]enti
return nil, 0, err return nil, 0, err
} }
scope, err := m.ResolveLocationScope(c, s.Repository.DB()) var scopeErr error
if err != nil {
return nil, 0, err
}
if params.ProjectFlockKandangId != 0 { if params.ProjectFlockKandangId != 0 {
if err := m.EnsureProjectFlockKandangAccess(c, s.Repository.DB(), 0, params.ProjectFlockKandangId); err != nil { if err := m.EnsureProjectFlockKandangAccess(c, s.Repository.DB(), 0, params.ProjectFlockKandangId); err != nil {
return nil, 0, err return nil, 0, err
@@ -123,21 +120,19 @@ func (s recordingService) GetAll(c *fiber.Ctx, params *validation.Query) ([]enti
recordings, total, err := s.Repository.GetAll(c.Context(), offset, limit, func(db *gorm.DB) *gorm.DB { recordings, total, err := s.Repository.GetAll(c.Context(), offset, limit, func(db *gorm.DB) *gorm.DB {
db = s.Repository.WithRelations(db) db = s.Repository.WithRelations(db)
if scope.Restrict { db = db.
if len(scope.IDs) == 0 { Joins("JOIN project_flock_kandangs pfk ON pfk.id = recordings.project_flock_kandangs_id").
return db.Where("1 = 0") Joins("JOIN project_flocks pf ON pf.id = pfk.project_flock_id")
} db, scopeErr = m.ApplyLocationScope(c, db, "pf.location_id")
db = db.
Joins("JOIN project_flock_kandangs pfk ON pfk.id = recordings.project_flock_kandangs_id").
Joins("JOIN project_flocks pf ON pf.id = pfk.project_flock_id")
db = m.ApplyScopeFilter(db, scope, "pf.location_id")
}
if params.ProjectFlockKandangId != 0 { if params.ProjectFlockKandangId != 0 {
db = db.Where("project_flock_kandangs_id = ?", params.ProjectFlockKandangId) db = db.Where("project_flock_kandangs_id = ?", params.ProjectFlockKandangId)
} }
return db.Order("record_datetime DESC").Order("created_at DESC") return db.Order("record_datetime DESC").Order("created_at DESC")
}) })
if scopeErr != nil {
return nil, 0, scopeErr
}
if err != nil { if err != nil {
s.Log.Errorf("Failed to get recordings: %+v", err) s.Log.Errorf("Failed to get recordings: %+v", err)
return nil, 0, err return nil, 0, err
@@ -962,7 +957,6 @@ type eggTotals struct {
Weight float64 Weight float64
} }
func stocksMatch(existing []entity.RecordingStock, incoming []validation.Stock) bool { func stocksMatch(existing []entity.RecordingStock, incoming []validation.Stock) bool {
hasPending := false hasPending := false
for _, item := range incoming { for _, item := range incoming {
@@ -87,25 +87,20 @@ func (s uniformityService) GetAll(c *fiber.Ctx, params *validation.Query) ([]ent
return nil, 0, err return nil, 0, err
} }
scope, err := m.ResolveLocationScope(c, s.Repository.DB()) var scopeErr error
if err != nil {
return nil, 0, err
}
offset := (params.Page - 1) * params.Limit offset := (params.Page - 1) * params.Limit
uniformitys, total, err := s.Repository.GetAllWithFilters(c.Context(), offset, params.Limit, params, func(db *gorm.DB) *gorm.DB { uniformitys, total, err := s.Repository.GetAllWithFilters(c.Context(), offset, params.Limit, params, func(db *gorm.DB) *gorm.DB {
if scope.Restrict { db = db.
if len(scope.IDs) == 0 { Joins("JOIN project_flock_kandangs pfk ON pfk.id = project_flock_kandang_uniformities.project_flock_kandang_id").
return db.Where("1 = 0") Joins("JOIN project_flocks pf ON pf.id = pfk.project_flock_id")
} db, scopeErr = m.ApplyLocationScope(c, db, "pf.location_id")
db = db.
Joins("JOIN project_flock_kandangs pfk ON pfk.id = project_flock_kandang_uniformities.project_flock_kandang_id").
Joins("JOIN project_flocks pf ON pf.id = pfk.project_flock_id")
db = m.ApplyScopeFilter(db, scope, "pf.location_id")
}
return db return db
}) })
if scopeErr != nil {
return nil, 0, scopeErr
}
if err != nil { if err != nil {
s.Log.Errorf("Failed to get uniformitys: %+v", err) s.Log.Errorf("Failed to get uniformitys: %+v", err)
return nil, 0, err return nil, 0, err