FIX[BE]: if project flocs deleted kandangs reset to non_active and add filter get all project_flock by area,kandangs,period and location

This commit is contained in:
ragilap
2025-10-19 23:24:56 +07:00
parent c9b4b3008e
commit f15e0d62e3
13 changed files with 663 additions and 51 deletions
@@ -4,6 +4,8 @@ import (
"context"
"errors"
"fmt"
"strings"
"time"
commonRepo "gitlab.com/mbugroup/lti-api.git/internal/common/repository"
common "gitlab.com/mbugroup/lti-api.git/internal/common/service"
@@ -35,6 +37,7 @@ type projectflockService struct {
Repository repository.ProjectflockRepository
FlockRepo flockRepository.FlockRepository
KandangRepo kandangRepository.KandangRepository
PivotRepo repository.ProjectFlockKandangRepository
}
type FlockPeriodSummary struct {
@@ -46,6 +49,7 @@ func NewProjectflockService(
repo repository.ProjectflockRepository,
flockRepo flockRepository.FlockRepository,
kandangRepo kandangRepository.KandangRepository,
pivotRepo repository.ProjectFlockKandangRepository,
validate *validator.Validate,
) ProjectflockService {
return &projectflockService{
@@ -54,6 +58,7 @@ func NewProjectflockService(
Repository: repo,
FlockRepo: flockRepo,
KandangRepo: kandangRepo,
PivotRepo: pivotRepo,
}
}
@@ -73,11 +78,81 @@ func (s projectflockService) GetAll(c *fiber.Ctx, params *validation.Query) ([]e
return nil, 0, err
}
if params.Page <= 0 {
params.Page = 1
}
if params.Limit <= 0 {
params.Limit = 10
}
offset := (params.Page - 1) * params.Limit
projectflocks, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
db = s.withRelations(db)
return db.Order("created_at DESC").Order("updated_at DESC")
if params.AreaId > 0 {
db = db.Where("project_flocks.area_id = ?", params.AreaId)
}
if params.LocationId > 0 {
db = db.Where("project_flocks.location_id = ?", params.LocationId)
}
if params.Period > 0 {
db = db.Where("project_flocks.period = ?", params.Period)
}
if len(params.KandangIds) > 0 {
db = db.Where("EXISTS (SELECT 1 FROM kandangs WHERE kandangs.project_flock_id = project_flocks.id AND kandangs.id IN ?)", params.KandangIds)
}
if params.Search != "" {
normalizedSearch := strings.ToLower(strings.TrimSpace(params.Search))
if normalizedSearch == "" {
for _, expr := range s.buildOrderExpressions(params.SortBy, params.SortOrder) {
db = db.Order(expr)
}
return db
}
likeQuery := "%" + normalizedSearch + "%"
db = db.
Joins("LEFT JOIN flocks ON flocks.id = project_flocks.flock_id").
Joins("LEFT JOIN areas ON areas.id = project_flocks.area_id").
Joins("LEFT JOIN product_categories ON product_categories.id = project_flocks.product_category_id").
Joins("LEFT JOIN fcrs ON fcrs.id = project_flocks.fcr_id").
Joins("LEFT JOIN locations ON locations.id = project_flocks.location_id").
Joins("LEFT JOIN users AS created_users ON created_users.id = project_flocks.created_by").
Where(`
LOWER(flocks.name) LIKE ?
OR LOWER(areas.name) LIKE ?
OR LOWER(product_categories.name) LIKE ?
OR LOWER(product_categories.code) LIKE ?
OR LOWER(fcrs.name) LIKE ?
OR LOWER(locations.name) LIKE ?
OR LOWER(locations.address) LIKE ?
OR LOWER(created_users.name) LIKE ?
OR LOWER(created_users.email) LIKE ?
OR LOWER(CAST(project_flocks.period AS TEXT)) LIKE ?
OR EXISTS (
SELECT 1 FROM kandangs
WHERE kandangs.project_flock_id = project_flocks.id
AND LOWER(kandangs.name) LIKE ?
)
`,
likeQuery,
likeQuery,
likeQuery,
likeQuery,
likeQuery,
likeQuery,
likeQuery,
likeQuery,
likeQuery,
likeQuery,
likeQuery,
)
}
for _, expr := range s.buildOrderExpressions(params.SortBy, params.SortOrder) {
db = db.Order(expr)
}
return db
})
if err != nil {
@@ -167,12 +242,10 @@ func (s *projectflockService) CreateOne(c *fiber.Ctx, req *validation.Create) (*
return nil, err
}
if err := tx.Model(&entity.Kandang{}).
Where("id IN ?", kandangIDs).
Updates(map[string]any{"project_flock_id": createBody.Id}).Error; err != nil {
if err := s.attachKandangs(c.Context(), tx, createBody.Id, kandangIDs, createBody.CreatedBy); err != nil {
tx.Rollback()
s.Log.Errorf("Failed to assign kandangs to projectflock: %+v", err)
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to assign kandangs")
s.Log.Errorf("Failed to attach kandangs to projectflock %d: %+v", createBody.Id, err)
return nil, err
}
if err := tx.Commit().Error; err != nil {
@@ -315,22 +388,18 @@ func (s projectflockService) UpdateOne(c *fiber.Ctx, req *validation.Update, id
}
if len(toDetach) > 0 {
if err := tx.Model(&entity.Kandang{}).
Where("id IN ?", toDetach).
Updates(map[string]any{"project_flock_id": nil}).Error; err != nil {
if err := s.detachKandangs(c.Context(), tx, id, toDetach, false); err != nil {
tx.Rollback()
s.Log.Errorf("Failed to detach kandangs: %+v", err)
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to update kandangs")
s.Log.Errorf("Failed to detach kandangs from projectflock %d: %+v", id, err)
return nil, err
}
}
if len(toAttach) > 0 {
if err := tx.Model(&entity.Kandang{}).
Where("id IN ?", toAttach).
Updates(map[string]any{"project_flock_id": id}).Error; err != nil {
if err := s.attachKandangs(c.Context(), tx, id, toAttach, existing.CreatedBy); err != nil {
tx.Rollback()
s.Log.Errorf("Failed to attach kandangs: %+v", err)
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to update kandangs")
s.Log.Errorf("Failed to attach kandangs to projectflock %d: %+v", id, err)
return nil, err
}
}
}
@@ -363,12 +432,10 @@ func (s projectflockService) DeleteOne(c *fiber.Ctx, id uint) error {
for i, k := range existing.Kandangs {
ids[i] = k.Id
}
if err := tx.Model(&entity.Kandang{}).
Where("id IN ?", ids).
Updates(map[string]any{"project_flock_id": nil}).Error; err != nil {
if err := s.detachKandangs(c.Context(), tx, id, ids, true); err != nil {
tx.Rollback()
s.Log.Errorf("Failed to detach kandangs before delete: %+v", err)
return fiber.NewError(fiber.StatusInternalServerError, "Failed to update kandangs")
s.Log.Errorf("Failed to detach kandangs before deleting projectflock %d: %+v", id, err)
return err
}
}
@@ -431,3 +498,93 @@ func relationExistsChecker[T any](db *gorm.DB) func(context.Context, uint) (bool
return commonRepo.Exists[T](ctx, db, id)
}
}
func (s projectflockService) buildOrderExpressions(sortBy, sortOrder string) []string {
direction := "ASC"
if strings.ToLower(sortOrder) == "desc" {
direction = "DESC"
}
switch sortBy {
case "area":
return []string{
fmt.Sprintf("(SELECT name FROM areas WHERE areas.id = project_flocks.area_id) %s", direction),
fmt.Sprintf("project_flocks.id %s", direction),
}
case "location":
return []string{
fmt.Sprintf("(SELECT name FROM locations WHERE locations.id = project_flocks.location_id) %s", direction),
fmt.Sprintf("project_flocks.id %s", direction),
}
case "kandangs":
return []string{
fmt.Sprintf("(SELECT COUNT(*) FROM kandangs WHERE kandangs.project_flock_id = project_flocks.id) %s", direction),
fmt.Sprintf("project_flocks.id %s", direction),
}
case "period":
return []string{
fmt.Sprintf("project_flocks.period %s", direction),
fmt.Sprintf("project_flocks.id %s", direction),
}
default:
return []string{
"project_flocks.created_at DESC",
"project_flocks.updated_at DESC",
}
}
}
func (s projectflockService) attachKandangs(ctx context.Context, tx *gorm.DB, projectFlockID uint, kandangIDs []uint, createdBy uint) error {
if len(kandangIDs) == 0 {
return nil
}
if err := tx.Model(&entity.Kandang{}).
Where("id IN ?", kandangIDs).
Updates(map[string]any{"project_flock_id": projectFlockID}).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, "Failed to update kandangs")
}
pivotRepo := s.pivotRepoWithTx(tx)
records := make([]*entity.ProjectFlockKandang, len(kandangIDs))
for i, id := range kandangIDs {
records[i] = &entity.ProjectFlockKandang{
ProjectFlockId: projectFlockID,
KandangId: id,
CreatedBy: createdBy,
}
}
if err := pivotRepo.CreateMany(ctx, records); err != nil {
return fiber.NewError(fiber.StatusInternalServerError, "Failed to persist project flock history")
}
return nil
}
func (s projectflockService) detachKandangs(ctx context.Context, tx *gorm.DB, projectFlockID uint, kandangIDs []uint, resetStatus bool) error {
if len(kandangIDs) == 0 {
return nil
}
updates := map[string]any{"project_flock_id": nil}
if resetStatus {
updates["status"] = string(utils.KandangStatusNonActive)
}
if err := tx.Model(&entity.Kandang{}).
Where("id IN ?", kandangIDs).
Updates(updates).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, "Failed to update kandangs")
}
if err := s.pivotRepoWithTx(tx).MarkDetached(ctx, projectFlockID, kandangIDs, time.Now()); err != nil {
return fiber.NewError(fiber.StatusInternalServerError, "Failed to persist project flock history")
}
return nil
}
func (s projectflockService) pivotRepoWithTx(tx *gorm.DB) repository.ProjectFlockKandangRepository {
if s.PivotRepo == nil {
return repository.NewProjectFlockKandangRepository(tx)
}
return s.PivotRepo.WithTx(tx)
}