From 3c43426fbc7362b49aecce27762fac9daae3f204 Mon Sep 17 00:00:00 2001 From: giovanni Date: Mon, 9 Mar 2026 15:58:57 +0700 Subject: [PATCH] adjust employee --- ...123_alter_table_employee_kandangs.down.sql | 54 +++++++++++++++++++ ...85123_alter_table_employee_kandangs.up.sql | 41 ++++++++++++++ internal/entities/employee.go | 4 +- .../master/employees/dto/employees.dto.go | 18 +++---- .../employees/services/employees.service.go | 6 +-- 5 files changed, 109 insertions(+), 14 deletions(-) create mode 100644 internal/database/migrations/20260309085123_alter_table_employee_kandangs.down.sql create mode 100644 internal/database/migrations/20260309085123_alter_table_employee_kandangs.up.sql diff --git a/internal/database/migrations/20260309085123_alter_table_employee_kandangs.down.sql b/internal/database/migrations/20260309085123_alter_table_employee_kandangs.down.sql new file mode 100644 index 00000000..d5566a6d --- /dev/null +++ b/internal/database/migrations/20260309085123_alter_table_employee_kandangs.down.sql @@ -0,0 +1,54 @@ +BEGIN; + +ALTER TABLE employee_kandangs + DROP CONSTRAINT IF EXISTS fk_employee_kandangs_kandang; + +ALTER TABLE employee_kandangs + DROP CONSTRAINT IF EXISTS uq_employee_kandangs; + +CREATE TEMP TABLE tmp_kandang_group_to_kandang_map ( + kandang_group_id BIGINT PRIMARY KEY, + kandang_id BIGINT NOT NULL +) ON COMMIT DROP; + +INSERT INTO tmp_kandang_group_to_kandang_map (kandang_group_id, kandang_id) +SELECT + k.kandang_group_id, + MIN(k.id) AS kandang_id +FROM kandangs k +WHERE k.kandang_group_id IS NOT NULL +GROUP BY k.kandang_group_id; + +DO $$ +BEGIN + IF EXISTS ( + SELECT 1 + FROM employee_kandangs ek + LEFT JOIN tmp_kandang_group_to_kandang_map m ON m.kandang_group_id = ek.kandang_id + WHERE m.kandang_id IS NULL + ) THEN + RAISE EXCEPTION 'Cannot rollback employee_kandangs migration: kandang_group_id has no kandang mapping'; + END IF; +END $$; + +UPDATE employee_kandangs ek +SET kandang_id = m.kandang_id, + updated_at = NOW() +FROM tmp_kandang_group_to_kandang_map m +WHERE m.kandang_group_id = ek.kandang_id; + +DELETE FROM employee_kandangs ek1 +USING employee_kandangs ek2 +WHERE ek1.id > ek2.id + AND ek1.employee_id = ek2.employee_id + AND ek1.kandang_id = ek2.kandang_id; + +ALTER TABLE employee_kandangs + ADD CONSTRAINT fk_employee_kandangs_kandang + FOREIGN KEY (kandang_id) REFERENCES kandangs (id) + ON DELETE CASCADE; + +ALTER TABLE employee_kandangs + ADD CONSTRAINT uq_employee_kandangs UNIQUE (employee_id, kandang_id); + +COMMIT; diff --git a/internal/database/migrations/20260309085123_alter_table_employee_kandangs.up.sql b/internal/database/migrations/20260309085123_alter_table_employee_kandangs.up.sql new file mode 100644 index 00000000..999d5372 --- /dev/null +++ b/internal/database/migrations/20260309085123_alter_table_employee_kandangs.up.sql @@ -0,0 +1,41 @@ +BEGIN; + +ALTER TABLE employee_kandangs + DROP CONSTRAINT IF EXISTS fk_employee_kandangs_kandang; + +ALTER TABLE employee_kandangs + DROP CONSTRAINT IF EXISTS uq_employee_kandangs; + +DO $$ +BEGIN + IF EXISTS ( + SELECT 1 + FROM employee_kandangs ek + LEFT JOIN kandangs k ON k.id = ek.kandang_id + WHERE k.kandang_group_id IS NULL + ) THEN + RAISE EXCEPTION 'Cannot migrate employee_kandangs: kandang_id has no kandang_group_id mapping'; + END IF; +END $$; + +UPDATE employee_kandangs ek +SET kandang_id = k.kandang_group_id, + updated_at = NOW() +FROM kandangs k +WHERE k.id = ek.kandang_id; + +DELETE FROM employee_kandangs ek1 +USING employee_kandangs ek2 +WHERE ek1.id > ek2.id + AND ek1.employee_id = ek2.employee_id + AND ek1.kandang_id = ek2.kandang_id; + +ALTER TABLE employee_kandangs + ADD CONSTRAINT fk_employee_kandangs_kandang + FOREIGN KEY (kandang_id) REFERENCES kandang_groups (id) + ON DELETE CASCADE; + +ALTER TABLE employee_kandangs + ADD CONSTRAINT uq_employee_kandangs UNIQUE (employee_id, kandang_id); + +COMMIT; diff --git a/internal/entities/employee.go b/internal/entities/employee.go index a93cbb46..b1cef008 100644 --- a/internal/entities/employee.go +++ b/internal/entities/employee.go @@ -26,6 +26,6 @@ type EmployeeKandang struct { CreatedAt time.Time `gorm:"autoCreateTime"` UpdatedAt time.Time `gorm:"autoUpdateTime"` - Employee Employee `gorm:"foreignKey:EmployeeId;references:Id"` - Kandang Kandang `gorm:"foreignKey:KandangId;references:Id"` + Employee Employee `gorm:"foreignKey:EmployeeId;references:Id"` + Kandang KandangGroup `gorm:"foreignKey:KandangId;references:Id"` } diff --git a/internal/modules/master/employees/dto/employees.dto.go b/internal/modules/master/employees/dto/employees.dto.go index 65b1b5ca..6fb99217 100644 --- a/internal/modules/master/employees/dto/employees.dto.go +++ b/internal/modules/master/employees/dto/employees.dto.go @@ -4,7 +4,7 @@ import ( "time" entity "gitlab.com/mbugroup/lti-api.git/internal/entities" - kandangDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/dto" + kandangGroupDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandang-groups/dto" ) // === DTO Structs === @@ -15,12 +15,12 @@ type EmployeesRelationDTO struct { } type EmployeesListDTO struct { - Id uint `json:"id"` - Name string `json:"name"` - IsActive bool `json:"is_active"` - Kandangs []kandangDTO.KandangRelationDTO `json:"kandangs"` - CreatedAt time.Time `json:"created_at"` - UpdatedAt time.Time `json:"updated_at"` + Id uint `json:"id"` + Name string `json:"name"` + IsActive bool `json:"is_active"` + Kandangs []kandangGroupDTO.KandangGroupRelationDTO `json:"kandangs"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` } type EmployeesDetailDTO struct { @@ -37,12 +37,12 @@ func ToEmployeesRelationDTO(e entity.Employees) EmployeesRelationDTO { } func ToEmployeesListDTO(e entity.Employees) EmployeesListDTO { - kandangs := make([]kandangDTO.KandangRelationDTO, 0, len(e.EmployeeKandangs)) + kandangs := make([]kandangGroupDTO.KandangGroupRelationDTO, 0, len(e.EmployeeKandangs)) for _, rel := range e.EmployeeKandangs { if rel.Kandang.Id == 0 { continue } - kandangs = append(kandangs, kandangDTO.ToKandangRelationDTO(rel.Kandang)) + kandangs = append(kandangs, kandangGroupDTO.ToKandangGroupRelationDTO(rel.Kandang)) } return EmployeesListDTO{ diff --git a/internal/modules/master/employees/services/employees.service.go b/internal/modules/master/employees/services/employees.service.go index 2817a9fa..a7a6cef3 100644 --- a/internal/modules/master/employees/services/employees.service.go +++ b/internal/modules/master/employees/services/employees.service.go @@ -52,7 +52,7 @@ func (s employeesService) ensureEmployeeAccess(c *fiber.Ctx, employeeID uint) er 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 kandang_groups 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). @@ -79,7 +79,7 @@ func (s employeesService) ensureKandangIDsAccess(c *fiber.Ctx, kandangIDs []uint } db := s.Repository.DB().WithContext(c.Context()). - Table("kandangs k"). + Table("kandang_groups 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) @@ -109,7 +109,7 @@ 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 kandang_groups 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