Merge branch 'feat/BE/US-75/chick-in-doc' into 'development-before-sso'

fix(BE): add missing product json in transfer get all & support flag param...

See merge request mbugroup/lti-api!25
This commit is contained in:
Hafizh A. Y.
2025-10-21 06:54:59 +00:00
35 changed files with 1383 additions and 59 deletions
@@ -0,0 +1 @@
DROP TABLE IF EXISTS project_chickins;
@@ -0,0 +1,36 @@
CREATE TABLE IF NOT EXISTS project_chickins (
id BIGSERIAL PRIMARY KEY,
project_flock_kandang_id BIGINT NOT NULL,
chick_in_date DATE NOT NULL,
quantity NUMERIC(15, 3) NOT NULL,
note TEXT,
created_by BIGINT NOT NULL,
created_at TIMESTAMPTZ DEFAULT now(),
updated_at TIMESTAMPTZ DEFAULT now(),
deleted_at TIMESTAMPTZ
);
-- FOREIGN KEYS (dijalankan setelah semua tabel parent ada)
DO $$
BEGIN
IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'project_flock_kandangs') THEN
ALTER TABLE project_chickins
ADD CONSTRAINT fk_project_flock_kandang_id
FOREIGN KEY (project_flock_kandang_id)
REFERENCES project_flock_kandangs(id)
ON DELETE RESTRICT ON UPDATE CASCADE;
END IF;
IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'users') THEN
ALTER TABLE project_chickins
ADD CONSTRAINT fk_created_by
FOREIGN KEY (created_by)
REFERENCES users(id)
ON DELETE RESTRICT ON UPDATE CASCADE;
END IF;
END $$;
-- INDEXES
CREATE INDEX IF NOT EXISTS idx_project_chickins_project_flock_kandang_id ON project_chickins (project_flock_kandang_id);
CREATE INDEX IF NOT EXISTS idx_project_chickins_created_by ON project_chickins (created_by);
@@ -0,0 +1 @@
DROP TABLE IF EXISTS project_flock_populations;
@@ -0,0 +1,36 @@
CREATE TABLE IF NOT EXISTS project_flock_populations (
id BIGSERIAL PRIMARY KEY,
project_flock_kandang_id BIGINT NOT NULL,
initial_quantity NUMERIC(15, 3) NOT NULL,
current_quantity NUMERIC(15, 3) NOT NULL,
reserved_quantity NUMERIC(15, 3),
created_by BIGINT NOT NULL,
created_at TIMESTAMPTZ DEFAULT now(),
updated_at TIMESTAMPTZ DEFAULT now(),
deleted_at TIMESTAMPTZ
);
-- FOREIGN KEYS (dijalankan setelah semua tabel parent ada)
DO $$
BEGIN
IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'project_flock_kandangs') THEN
ALTER TABLE project_flock_populations
ADD CONSTRAINT fk_project_flock_kandang_id
FOREIGN KEY (project_flock_kandang_id)
REFERENCES project_flock_kandangs(id)
ON DELETE RESTRICT ON UPDATE CASCADE;
END IF;
IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'users') THEN
ALTER TABLE project_flock_populations
ADD CONSTRAINT fk_created_by
FOREIGN KEY (created_by)
REFERENCES users(id)
ON DELETE RESTRICT ON UPDATE CASCADE;
END IF;
END $$;
-- INDEXES
CREATE INDEX IF NOT EXISTS idx_project_flock_populations_project_flock_kandang_id ON project_flock_populations (project_flock_kandang_id);
CREATE INDEX IF NOT EXISTS idx_project_flock_populations_created_by ON project_flock_populations (created_by);
+74 -4
View File
@@ -92,6 +92,9 @@ func Run(db *gorm.DB) error {
if err := seedTransferStock(tx, adminID); err != nil {
return err
}
if err := seedChickin(tx, adminID); err != nil {
return err
}
fmt.Println("✅ Master data seeding completed")
return nil
@@ -975,6 +978,7 @@ func seedProductWarehouse(tx *gorm.DB, createdBy uint) error {
{ProductID: 1, WarehouseID: 1, Quantity: 100},
{ProductID: 2, WarehouseID: 2, Quantity: 200},
{ProductID: 2, WarehouseID: 1, Quantity: 300},
{ProductID: 1, WarehouseID: 3, Quantity: 5000},
}
for _, seed := range seeds {
@@ -999,8 +1003,7 @@ func seedProductWarehouse(tx *gorm.DB, createdBy uint) error {
}
func seedTransferStock(tx *gorm.DB, createdBy uint) error {
// Seeder Transfer Stock
// 1. Insert StockTransfer (header)
transfer := entity.StockTransfer{
FromWarehouseId: 1,
ToWarehouseId: 2,
@@ -1013,7 +1016,6 @@ func seedTransferStock(tx *gorm.DB, createdBy uint) error {
return err
}
// 2. Insert StockTransferDetail (detail)
details := []entity.StockTransferDetail{
{
StockTransferId: transfer.Id,
@@ -1032,7 +1034,6 @@ func seedTransferStock(tx *gorm.DB, createdBy uint) error {
}
}
// 3. Insert StockTransferDelivery (delivery)
deliveries := []entity.StockTransferDelivery{
{
StockTransferId: transfer.Id,
@@ -1076,6 +1077,75 @@ func seedTransferStock(tx *gorm.DB, createdBy uint) error {
return nil
}
func seedChickin(tx *gorm.DB, createdBy uint) error {
seeds := []struct {
ProjectFlockKandangId uint
ChickInDate string
Quantity float64
Note string
}{
{ProjectFlockKandangId: 1, ChickInDate: "2025-10-20", Quantity: 100, Note: "Seeder chickin 1"},
{ProjectFlockKandangId: 2, ChickInDate: "2025-10-21", Quantity: 200, Note: "Seeder chickin 2"},
}
for _, seed := range seeds {
chickinDate, err := time.Parse("2006-01-02", seed.ChickInDate)
if err != nil {
return err
}
// Insert ProjectChickin jika belum ada
var chickin entity.ProjectChickin
err = tx.Where("project_flock_kandang_id = ? AND chick_in_date = ?", seed.ProjectFlockKandangId, chickinDate).
First(&chickin).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
chickin = entity.ProjectChickin{
ProjectFlockKandangId: seed.ProjectFlockKandangId,
ChickInDate: chickinDate,
Quantity: seed.Quantity,
Note: seed.Note,
CreatedBy: createdBy,
}
if err := tx.Create(&chickin).Error; err != nil {
return err
}
} else if err != nil {
return err
}
// Update/Insert ProjectFlockPopulation
var population entity.ProjectFlockPopulation
err = tx.Where("project_flock_kandang_id = ?", seed.ProjectFlockKandangId).First(&population).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
population = entity.ProjectFlockPopulation{
ProjectFlockKandangId: seed.ProjectFlockKandangId,
InitialQuantity: seed.Quantity,
CurrentQuantity: seed.Quantity,
ReservedQuantity: 0,
CreatedBy: createdBy,
}
if err := tx.Create(&population).Error; err != nil {
return err
}
} else if err != nil {
return err
} else {
// Update population quantities
if err := tx.Model(&entity.ProjectFlockPopulation{}).
Where("id = ?", population.Id).
Updates(map[string]any{
"initial_quantity": population.InitialQuantity + seed.Quantity,
"current_quantity": population.CurrentQuantity + seed.Quantity,
"reserved_quantity": 0,
}).Error; err != nil {
return err
}
}
}
return nil
}
func ptr[T any](v T) *T {
return &v
}