mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-25 07:45:44 +00:00
Feat[BE]: enhance transfer laying functionality with comprehensive filtering options and improved DTO structures
This commit is contained in:
+94
@@ -2,6 +2,7 @@ package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
@@ -12,6 +13,9 @@ type TransferLayingRepository interface {
|
||||
repository.BaseRepository[entity.LayingTransfer]
|
||||
GetByTransferNumber(ctx context.Context, transferNumber string) (*entity.LayingTransfer, error)
|
||||
IdExists(ctx context.Context, id uint) (bool, error)
|
||||
|
||||
// Tambah method baru untuk query dengan filter lengkap
|
||||
GetAllWithFilters(ctx context.Context, offset int, limit int, params *GetAllFilterParams) ([]entity.LayingTransfer, int64, error)
|
||||
}
|
||||
|
||||
type TransferLayingRepositoryImpl struct {
|
||||
@@ -40,3 +44,93 @@ func (r *TransferLayingRepositoryImpl) GetByTransferNumber(ctx context.Context,
|
||||
}
|
||||
return &transfer, nil
|
||||
}
|
||||
|
||||
type GetAllFilterParams struct {
|
||||
Search string
|
||||
StartDate string
|
||||
EndDate string
|
||||
FlockSource []uint
|
||||
FlockDestination []uint
|
||||
Status []string
|
||||
}
|
||||
|
||||
func (r *TransferLayingRepositoryImpl) GetAllWithFilters(ctx context.Context, offset int, limit int, params *GetAllFilterParams) ([]entity.LayingTransfer, int64, error) {
|
||||
var records []entity.LayingTransfer
|
||||
var total int64
|
||||
|
||||
q := r.db.WithContext(ctx).Model(&entity.LayingTransfer{})
|
||||
|
||||
if params.Search != "" {
|
||||
searchPattern := "%" + params.Search + "%"
|
||||
q = q.Joins("LEFT JOIN project_flocks AS pf_from ON laying_transfers.from_project_flock_id = pf_from.id").
|
||||
Joins("LEFT JOIN project_flocks AS pf_to ON laying_transfers.to_project_flock_id = pf_to.id").
|
||||
Where("laying_transfers.transfer_number ILIKE ? OR laying_transfers.notes ILIKE ? OR pf_from.flock_name ILIKE ? OR pf_to.flock_name ILIKE ?",
|
||||
searchPattern, searchPattern, searchPattern, searchPattern)
|
||||
}
|
||||
|
||||
if params.StartDate != "" && params.EndDate != "" {
|
||||
q = q.Where("transfer_date::date >= ?::date AND transfer_date::date <= ?::date",
|
||||
params.StartDate, params.EndDate)
|
||||
} else if params.StartDate != "" {
|
||||
q = q.Where("transfer_date::date >= ?::date", params.StartDate)
|
||||
} else if params.EndDate != "" {
|
||||
q = q.Where("transfer_date::date <= ?::date", params.EndDate)
|
||||
}
|
||||
|
||||
if len(params.FlockSource) > 0 {
|
||||
q = q.Where("from_project_flock_id IN ?", params.FlockSource)
|
||||
}
|
||||
|
||||
if len(params.FlockDestination) > 0 {
|
||||
q = q.Where("to_project_flock_id IN ?", params.FlockDestination)
|
||||
}
|
||||
|
||||
if len(params.Status) > 0 {
|
||||
statusConditions := []string{}
|
||||
statusValues := []interface{}{}
|
||||
|
||||
for _, status := range params.Status {
|
||||
switch status {
|
||||
case "PENDING":
|
||||
statusConditions = append(statusConditions,
|
||||
"NOT EXISTS (SELECT 1 FROM approvals WHERE approvable_type = 'TRANSFER_TO_LAYINGS' AND approvable_id = laying_transfers.id)")
|
||||
|
||||
case "APPROVED":
|
||||
statusConditions = append(statusConditions,
|
||||
"EXISTS (SELECT 1 FROM approvals WHERE approvable_type = 'TRANSFER_TO_LAYINGS' AND approvable_id = laying_transfers.id AND action = 'APPROVED' ORDER BY created_at DESC LIMIT 1)")
|
||||
|
||||
case "REJECTED":
|
||||
statusConditions = append(statusConditions,
|
||||
"EXISTS (SELECT 1 FROM approvals WHERE approvable_type = 'TRANSFER_TO_LAYINGS' AND approvable_id = laying_transfers.id AND action = 'REJECTED' ORDER BY created_at DESC LIMIT 1)")
|
||||
}
|
||||
}
|
||||
|
||||
if len(statusConditions) > 0 {
|
||||
q = q.Where("("+strings.Join(statusConditions, " OR ")+")", statusValues...)
|
||||
}
|
||||
}
|
||||
|
||||
if err := q.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
q = q.Offset(offset).Limit(limit).
|
||||
Preload("FromProjectFlock").
|
||||
Preload("ToProjectFlock").
|
||||
Preload("CreatedUser").
|
||||
Preload("Sources").
|
||||
Preload("Sources.SourceProjectFlockKandang").
|
||||
Preload("Sources.SourceProjectFlockKandang.Kandang").
|
||||
Preload("Sources.ProductWarehouse").
|
||||
Preload("Targets").
|
||||
Preload("Targets.TargetProjectFlockKandang").
|
||||
Preload("Targets.TargetProjectFlockKandang.Kandang").
|
||||
Preload("Targets.ProductWarehouse").
|
||||
Order("laying_transfers.created_at DESC")
|
||||
|
||||
if err := q.Find(&records).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return records, total, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user