mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
[FIX/BE-US] feat adjustment location and area
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"strings"
|
||||
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
|
||||
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/master/employees/repositories"
|
||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/master/employees/validations"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
@@ -43,6 +44,61 @@ func (s employeesService) withRelations(db *gorm.DB) *gorm.DB {
|
||||
Where("employees.deleted_at IS NULL")
|
||||
}
|
||||
|
||||
func (s employeesService) ensureEmployeeAccess(c *fiber.Ctx, employeeID uint) error {
|
||||
if employeeID == 0 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid employee id")
|
||||
}
|
||||
|
||||
db := s.Repository.DB().WithContext(c.Context()).
|
||||
Table("employees e").
|
||||
Joins("JOIN employee_kandangs ek ON ek.employee_id = e.id").
|
||||
Joins("JOIN kandangs k ON k.id = ek.kandang_id").
|
||||
Joins("JOIN locations loc ON loc.id = k.location_id").
|
||||
Joins("JOIN areas a ON a.id = loc.area_id").
|
||||
Where("e.id = ?", employeeID).
|
||||
Where("e.deleted_at IS NULL")
|
||||
|
||||
scopedDB, err := m.ApplyLocationAreaScope(c, db, "loc.id", "a.id")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var count int64
|
||||
if err := scopedDB.Count(&count).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if count == 0 {
|
||||
return fiber.NewError(fiber.StatusNotFound, "Employees not found")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s employeesService) ensureKandangIDsAccess(c *fiber.Ctx, kandangIDs []uint) error {
|
||||
if len(kandangIDs) == 0 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "kandang_ids must contain at least one valid id")
|
||||
}
|
||||
|
||||
db := s.Repository.DB().WithContext(c.Context()).
|
||||
Table("kandangs k").
|
||||
Joins("JOIN locations loc ON loc.id = k.location_id").
|
||||
Joins("JOIN areas a ON a.id = loc.area_id").
|
||||
Where("k.id IN ?", kandangIDs)
|
||||
|
||||
scopedDB, err := m.ApplyLocationAreaScope(c, db, "loc.id", "a.id")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var count int64
|
||||
if err := scopedDB.Count(&count).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if count != int64(len(kandangIDs)) {
|
||||
return fiber.NewError(fiber.StatusNotFound, "Kandang not found")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s employeesService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.Employees, int64, error) {
|
||||
if err := s.Validate.Struct(params); err != nil {
|
||||
return nil, 0, err
|
||||
@@ -52,17 +108,29 @@ func (s employeesService) GetAll(c *fiber.Ctx, params *validation.Query) ([]enti
|
||||
|
||||
employeess, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
|
||||
db = s.withRelations(db)
|
||||
db = db.Joins("JOIN employee_kandangs ek ON ek.employee_id = employees.id").
|
||||
Joins("JOIN kandangs k ON k.id = ek.kandang_id").
|
||||
Joins("JOIN locations loc ON loc.id = k.location_id").
|
||||
Joins("JOIN areas a ON a.id = loc.area_id")
|
||||
var scopeErr error
|
||||
db, scopeErr = m.ApplyLocationAreaScope(c, db, "loc.id", "a.id")
|
||||
if scopeErr != nil {
|
||||
return db.Where("1 = 0")
|
||||
}
|
||||
if params.Search != "" {
|
||||
db = db.Where("employees.name ILIKE ?", "%"+params.Search+"%")
|
||||
}
|
||||
if params.KandangId != nil {
|
||||
db = db.Joins("JOIN employee_kandangs ek ON ek.employee_id = employees.id").
|
||||
Where("ek.kandang_id = ?", *params.KandangId)
|
||||
db = db.Where("ek.kandang_id = ?", *params.KandangId)
|
||||
}
|
||||
if params.IsActive != nil {
|
||||
db = db.Where("employees.is_active = ?", *params.IsActive)
|
||||
}
|
||||
return db.Order("employees.created_at DESC").Order("employees.updated_at DESC")
|
||||
return db.
|
||||
Select("employees.id, employees.name, employees.is_active, employees.created_at, employees.updated_at").
|
||||
Group("employees.id, employees.name, employees.is_active, employees.created_at, employees.updated_at").
|
||||
Order("employees.created_at DESC").
|
||||
Order("employees.updated_at DESC")
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
@@ -73,6 +141,9 @@ func (s employeesService) GetAll(c *fiber.Ctx, params *validation.Query) ([]enti
|
||||
}
|
||||
|
||||
func (s employeesService) GetOne(c *fiber.Ctx, id uint) (*entity.Employees, error) {
|
||||
if err := s.ensureEmployeeAccess(c, id); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
employees, err := s.Repository.GetByID(c.Context(), id, s.withRelations)
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, "Employees not found")
|
||||
@@ -98,6 +169,9 @@ func (s *employeesService) CreateOne(c *fiber.Ctx, req *validation.Create) (*ent
|
||||
if len(kandangIDs) == 0 {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "kandang_ids must contain at least one valid id")
|
||||
}
|
||||
if err := s.ensureKandangIDsAccess(c, kandangIDs); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if _, err := s.Repository.First(c.Context(), func(db *gorm.DB) *gorm.DB {
|
||||
return db.Where("LOWER(name) = ?", strings.ToLower(name))
|
||||
@@ -147,6 +221,9 @@ func (s employeesService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uin
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.ensureEmployeeAccess(c, id); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
updateBody := make(map[string]any)
|
||||
var (
|
||||
@@ -181,6 +258,9 @@ func (s employeesService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uin
|
||||
if len(ids) == 0 {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "kandang_ids must contain at least one valid id")
|
||||
}
|
||||
if err := s.ensureKandangIDsAccess(c, ids); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
kandangIDs = ids
|
||||
needKandangUpdate = true
|
||||
@@ -234,6 +314,9 @@ func (s employeesService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uin
|
||||
}
|
||||
|
||||
func (s employeesService) DeleteOne(c *fiber.Ctx, id uint) error {
|
||||
if err := s.ensureEmployeeAccess(c, id); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.Repository.DeleteOne(c.Context(), id); err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fiber.NewError(fiber.StatusNotFound, "Employees not found")
|
||||
|
||||
@@ -3,11 +3,13 @@ package controller
|
||||
import (
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/dto"
|
||||
service "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/services"
|
||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/validations"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/response"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
@@ -23,6 +25,11 @@ func NewWarehouseController(warehouseService service.WarehouseService) *Warehous
|
||||
}
|
||||
|
||||
func (u *WarehouseController) GetAll(c *fiber.Ctx) error {
|
||||
excludeIDs, err := parseCommaSeparatedUint(c.Query("exclude_id", ""))
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
query := &validation.Query{
|
||||
Page: c.QueryInt("page", 1),
|
||||
Limit: c.QueryInt("limit", 10),
|
||||
@@ -30,6 +37,8 @@ func (u *WarehouseController) GetAll(c *fiber.Ctx) error {
|
||||
AreaId: c.QueryInt("area_id", 0),
|
||||
LocationId: c.QueryInt("location_id", 0),
|
||||
ActiveProjectFlockOnly: c.QueryBool("active_project_flock", false),
|
||||
TransferContext: c.Query(utils.TransferContextKey, ""),
|
||||
ExcludeIDs: excludeIDs,
|
||||
}
|
||||
|
||||
if query.Page < 1 || query.Limit < 1 {
|
||||
@@ -56,6 +65,28 @@ func (u *WarehouseController) GetAll(c *fiber.Ctx) error {
|
||||
})
|
||||
}
|
||||
|
||||
func parseCommaSeparatedUint(raw string) ([]uint, error) {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
parts := strings.Split(raw, ",")
|
||||
out := make([]uint, 0, len(parts))
|
||||
for _, part := range parts {
|
||||
part = strings.TrimSpace(part)
|
||||
if part == "" {
|
||||
continue
|
||||
}
|
||||
value, err := strconv.ParseUint(part, 10, 64)
|
||||
if err != nil {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "invalid exclude_id")
|
||||
}
|
||||
out = append(out, uint(value))
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (u *WarehouseController) GetOne(c *fiber.Ctx) error {
|
||||
param := c.Params("id")
|
||||
|
||||
|
||||
@@ -54,7 +54,14 @@ func (s warehouseService) GetAll(c *fiber.Ctx, params *validation.Query) ([]enti
|
||||
|
||||
warehouses, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
|
||||
db = s.withRelations(db)
|
||||
db, scopeErr = m.ApplyAreaScope(c, db, "warehouses.area_id")
|
||||
applyScope := true
|
||||
if params.TransferContext == utils.TransferContextInventoryTransfer {
|
||||
applyScope = !m.HasPermission(c, m.P_TransferCreateOne)
|
||||
}
|
||||
|
||||
if applyScope {
|
||||
db, scopeErr = m.ApplyLocationAreaScope(c, db, "warehouses.location_id", "warehouses.area_id")
|
||||
}
|
||||
if params.Search != "" {
|
||||
db = db.Where("warehouses.name ILIKE ?", "%"+params.Search+"%")
|
||||
}
|
||||
@@ -81,6 +88,9 @@ func (s warehouseService) GetAll(c *fiber.Ctx, params *validation.Query) ([]enti
|
||||
)
|
||||
`, "Aktif")
|
||||
}
|
||||
if len(params.ExcludeIDs) > 0 {
|
||||
db = db.Where("warehouses.id NOT IN ?", params.ExcludeIDs)
|
||||
}
|
||||
return db.Order("created_at DESC").Order("updated_at DESC")
|
||||
})
|
||||
|
||||
@@ -99,7 +109,7 @@ func (s warehouseService) GetOne(c *fiber.Ctx, id uint) (*entity.Warehouse, erro
|
||||
|
||||
warehouse, err := s.Repository.GetByID(c.Context(), id, func(db *gorm.DB) *gorm.DB {
|
||||
db = s.withRelations(db)
|
||||
db, scopeErr = m.ApplyAreaScope(c, db, "warehouses.area_id")
|
||||
db, scopeErr = m.ApplyLocationAreaScope(c, db, "warehouses.location_id", "warehouses.area_id")
|
||||
return db
|
||||
})
|
||||
if scopeErr != nil {
|
||||
|
||||
@@ -23,4 +23,6 @@ type Query struct {
|
||||
AreaId int `query:"area_id" validate:"omitempty,number,gt=0"`
|
||||
LocationId int `query:"location_id" validate:"omitempty,number,gt=0"`
|
||||
ActiveProjectFlockOnly bool `query:"active_project_flock"`
|
||||
ExcludeIDs []uint `query:"-" validate:"omitempty,dive,gt=0"`
|
||||
TransferContext string `query:"transfer_context" validate:"omitempty,oneof=inventory_transfer"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user