From 91fd8a253bae28486425bde9850279bbf3c2eb4a Mon Sep 17 00:00:00 2001 From: aguhh18 Date: Tue, 30 Dec 2025 20:16:40 +0700 Subject: [PATCH] feat(BE): update foreign key constraints for project_chickins and adjust service logic for project flock kandang retrieval --- ...alter_project_chickins_fk_cascade.down.sql | 20 +++++++++++++++++++ ...9_alter_project_chickins_fk_cascade.up.sql | 20 +++++++++++++++++++ .../services/adjustment.service.go | 7 +++---- 3 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 internal/database/migrations/20251230201439_alter_project_chickins_fk_cascade.down.sql create mode 100644 internal/database/migrations/20251230201439_alter_project_chickins_fk_cascade.up.sql diff --git a/internal/database/migrations/20251230201439_alter_project_chickins_fk_cascade.down.sql b/internal/database/migrations/20251230201439_alter_project_chickins_fk_cascade.down.sql new file mode 100644 index 00000000..1314087c --- /dev/null +++ b/internal/database/migrations/20251230201439_alter_project_chickins_fk_cascade.down.sql @@ -0,0 +1,20 @@ +-- Drop CASCADE constraint +DO $$ +BEGIN + IF EXISTS ( + SELECT 1 + FROM pg_constraint + WHERE conname = 'fk_project_chickins_kandang' + AND conrelid = 'project_chickins'::regclass + ) THEN + ALTER TABLE project_chickins + DROP CONSTRAINT fk_project_chickins_kandang; + END IF; +END $$; + +-- Recreate foreign key constraint with RESTRICT (original behavior) +ALTER TABLE project_chickins +ADD CONSTRAINT fk_project_chickins_kandang +FOREIGN KEY (project_flock_kandang_id) +REFERENCES project_flock_kandangs(id) +ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/internal/database/migrations/20251230201439_alter_project_chickins_fk_cascade.up.sql b/internal/database/migrations/20251230201439_alter_project_chickins_fk_cascade.up.sql new file mode 100644 index 00000000..ad07b8e0 --- /dev/null +++ b/internal/database/migrations/20251230201439_alter_project_chickins_fk_cascade.up.sql @@ -0,0 +1,20 @@ +-- Drop existing foreign key constraint with RESTRICT +DO $$ +BEGIN + IF EXISTS ( + SELECT 1 + FROM pg_constraint + WHERE conname = 'fk_project_chickins_kandang' + AND conrelid = 'project_chickins'::regclass + ) THEN + ALTER TABLE project_chickins + DROP CONSTRAINT fk_project_chickins_kandang; + END IF; +END $$; + +-- Add new foreign key constraint with CASCADE delete +ALTER TABLE project_chickins +ADD CONSTRAINT fk_project_chickins_kandang +FOREIGN KEY (project_flock_kandang_id) +REFERENCES project_flock_kandangs(id) +ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/internal/modules/inventory/adjustments/services/adjustment.service.go b/internal/modules/inventory/adjustments/services/adjustment.service.go index edf5f72b..f15f37df 100644 --- a/internal/modules/inventory/adjustments/services/adjustment.service.go +++ b/internal/modules/inventory/adjustments/services/adjustment.service.go @@ -118,10 +118,9 @@ func (s *adjustmentService) Adjustment(c *fiber.Ctx, req *validation.Create) (*e var createdLogId uint var projectFlockKandangID *uint - pfk, err := s.ProjectFlockKandangRepo.GetActiveByKandangID(ctx, uint(req.WarehouseID)) - if err == nil && pfk != nil { - idCopy := uint(pfk.Id) - projectFlockKandangID = &idCopy + pfkID, err := s.getActiveProjectFlockKandangID(ctx, uint(req.WarehouseID)) + if err == nil && pfkID > 0 { + projectFlockKandangID = &pfkID } pw, err := s.ProductWarehouseRepo.FindByProductWarehouseAndPfk(