feat(BE-119,135): add seeding and API documentation

- Implement project data seeding logic
- Add API documentation using Hoppscotch
This commit is contained in:
aguhh18
2025-10-20 12:55:19 +07:00
parent 748c959dbe
commit a1f579f616
2 changed files with 156 additions and 23 deletions
+74 -4
View File
@@ -92,6 +92,9 @@ func Run(db *gorm.DB) error {
if err := seedTransferStock(tx, adminID); err != nil { if err := seedTransferStock(tx, adminID); err != nil {
return err return err
} }
if err := seedChickin(tx, adminID); err != nil {
return err
}
fmt.Println("✅ Master data seeding completed") fmt.Println("✅ Master data seeding completed")
return nil return nil
@@ -981,6 +984,7 @@ func seedProductWarehouse(tx *gorm.DB, createdBy uint) error {
{ProductID: 1, WarehouseID: 1, Quantity: 100}, {ProductID: 1, WarehouseID: 1, Quantity: 100},
{ProductID: 2, WarehouseID: 2, Quantity: 200}, {ProductID: 2, WarehouseID: 2, Quantity: 200},
{ProductID: 2, WarehouseID: 1, Quantity: 300}, {ProductID: 2, WarehouseID: 1, Quantity: 300},
{ProductID: 1, WarehouseID: 3, Quantity: 5000},
} }
for _, seed := range seeds { for _, seed := range seeds {
@@ -1005,8 +1009,7 @@ func seedProductWarehouse(tx *gorm.DB, createdBy uint) error {
} }
func seedTransferStock(tx *gorm.DB, createdBy uint) error { func seedTransferStock(tx *gorm.DB, createdBy uint) error {
// Seeder Transfer Stock
// 1. Insert StockTransfer (header)
transfer := entity.StockTransfer{ transfer := entity.StockTransfer{
FromWarehouseId: 1, FromWarehouseId: 1,
ToWarehouseId: 2, ToWarehouseId: 2,
@@ -1019,7 +1022,6 @@ func seedTransferStock(tx *gorm.DB, createdBy uint) error {
return err return err
} }
// 2. Insert StockTransferDetail (detail)
details := []entity.StockTransferDetail{ details := []entity.StockTransferDetail{
{ {
StockTransferId: transfer.Id, StockTransferId: transfer.Id,
@@ -1038,7 +1040,6 @@ func seedTransferStock(tx *gorm.DB, createdBy uint) error {
} }
} }
// 3. Insert StockTransferDelivery (delivery)
deliveries := []entity.StockTransferDelivery{ deliveries := []entity.StockTransferDelivery{
{ {
StockTransferId: transfer.Id, StockTransferId: transfer.Id,
@@ -1082,6 +1083,75 @@ func seedTransferStock(tx *gorm.DB, createdBy uint) error {
return nil 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 { func ptr[T any](v T) *T {
return &v return &v
} }
@@ -77,16 +77,13 @@ func (s chickinService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity
} }
offset := (params.Page - 1) * params.Limit offset := (params.Page - 1) * params.Limit
chickins, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB { chickins, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
db = s.withRelations(db) db = s.withRelations(db)
if params.ProjectFlockKandangId != 0 { if params.ProjectFlockKandangId != 0 {
return db.Where("project_flock_kandang_id = ?", params.ProjectFlockKandangId) return db.Where("project_flock_kandang_id = ?", params.ProjectFlockKandangId)
} }
return db.Order("created_at DESC").Order("updated_at DESC") return db.Order("created_at DESC").Order("updated_at DESC")
}) })
if err != nil { if err != nil {
s.Log.Errorf("Failed to get chickins: %+v", err) s.Log.Errorf("Failed to get chickins: %+v", err)
return nil, 0, err return nil, 0, err
@@ -95,6 +92,7 @@ func (s chickinService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity
} }
func (s chickinService) GetOne(c *fiber.Ctx, id uint) (*entity.ProjectChickin, error) { func (s chickinService) GetOne(c *fiber.Ctx, id uint) (*entity.ProjectChickin, error) {
chickin, err := s.Repository.GetByID(c.Context(), id, s.withRelations) chickin, err := s.Repository.GetByID(c.Context(), id, s.withRelations)
if errors.Is(err, gorm.ErrRecordNotFound) { if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fiber.NewError(fiber.StatusNotFound, "Chickin not found") return nil, fiber.NewError(fiber.StatusNotFound, "Chickin not found")
@@ -111,20 +109,18 @@ func (s *chickinService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entit
return nil, err return nil, err
} }
// ambil salah satu kandang dari project_floc_id dari kandang repository
projectflockkandang, err := s.ProjectflockKandangRepo.GetByID(c.Context(), 1) projectflockkandang, err := s.ProjectflockKandangRepo.GetByID(c.Context(), 1)
if err != nil { if err != nil {
s.Log.Errorf("Failed to get projectflock kandang: %+v", err) s.Log.Errorf("Failed to get projectflock kandang: %+v", err)
return nil, err return nil, err
} }
// ambil warehouse dari kandangid
warehouse, err := s.WarehouseRepo.GetByKandangID(c.Context(), projectflockkandang.KandangId) warehouse, err := s.WarehouseRepo.GetByKandangID(c.Context(), projectflockkandang.KandangId)
if err != nil { if err != nil {
s.Log.Errorf("Failed to get warehouse: %+v", err) s.Log.Errorf("Failed to get warehouse: %+v", err)
return nil, err return nil, err
} }
// getprojectflock id with relation
projectFlock, err := s.ProjectFlockRepo.GetByID( projectFlock, err := s.ProjectFlockRepo.GetByID(
c.Context(), c.Context(),
projectflockkandang.ProjectFlockId, projectflockkandang.ProjectFlockId,
@@ -132,20 +128,19 @@ func (s *chickinService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entit
return db.Preload("ProductCategory") return db.Preload("ProductCategory")
}, },
) )
if err != nil { if err != nil {
s.Log.Errorf("Failed to get project flock: %+v", err) s.Log.Errorf("Failed to get project flock: %+v", err)
return nil, fiber.NewError(fiber.StatusNotFound, "Project Flock not found") return nil, fiber.NewError(fiber.StatusNotFound, "Project Flock not found")
} }
// ambil quantity
var productWarehouse entity.ProductWarehouse var productWarehouse entity.ProductWarehouse
err = s.ProductWarehouseRepo.DB().WithContext(c.Context()). err = s.ProductWarehouseRepo.DB().
WithContext(c.Context()).
Joins("JOIN products ON products.id = product_warehouses.product_id"). Joins("JOIN products ON products.id = product_warehouses.product_id").
Joins("JOIN product_categories ON product_categories.id = products.product_category_id"). Joins("JOIN product_categories ON product_categories.id = products.product_category_id").
Where("product_categories.code = ? AND product_warehouses.warehouse_id = ?", projectFlock.ProductCategory.Code, warehouse.Id). Where("product_categories.code = ? AND product_warehouses.warehouse_id = ?", projectFlock.ProductCategory.Code, warehouse.Id).
Order("created_at DESC"). Order("created_at DESC").
First(&productWarehouse).Error First(&productWarehouse).Error
if err != nil { if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) { if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fiber.NewError(fiber.StatusNotFound, "Product Warehouse not found for the given Project Flock and Warehouse") return nil, fiber.NewError(fiber.StatusNotFound, "Product Warehouse not found for the given Project Flock and Warehouse")
@@ -158,13 +153,11 @@ func (s *chickinService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entit
return nil, fiber.NewError(fiber.StatusBadRequest, "Insufficient product quantity in warehouse") return nil, fiber.NewError(fiber.StatusBadRequest, "Insufficient product quantity in warehouse")
} }
// masukan ke chic in
chickinDate, err := utils.ParseDateString(req.ChickInDate) chickinDate, err := utils.ParseDateString(req.ChickInDate)
if err != nil { if err != nil {
s.Log.Errorf("Failed to parse chickin date: %+v", err) s.Log.Errorf("Failed to parse chickin date: %+v", err)
return nil, fiber.NewError(fiber.StatusBadRequest, "Invalid ChickInDate format") return nil, fiber.NewError(fiber.StatusBadRequest, "Invalid ChickInDate format")
} }
newChickin := &entity.ProjectChickin{ newChickin := &entity.ProjectChickin{
ProjectFlockKandangId: projectflockkandang.ProjectFlockId, ProjectFlockKandangId: projectflockkandang.ProjectFlockId,
ChickInDate: chickinDate, ChickInDate: chickinDate,
@@ -172,14 +165,12 @@ func (s *chickinService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entit
Note: "", Note: "",
CreatedBy: 1, //todo: ganti dengan CreatedBy: 1, //todo: ganti dengan
} }
err = s.Repository.CreateOne(c.Context(), newChickin, nil) err = s.Repository.CreateOne(c.Context(), newChickin, nil)
if err != nil { if err != nil {
s.Log.Errorf("Failed to create chickin: %+v", err) s.Log.Errorf("Failed to create chickin: %+v", err)
return nil, err return nil, err
} }
// Kurangi quantity di product warehouse
updatedQuantity := productWarehouse.Quantity - newChickin.Quantity updatedQuantity := productWarehouse.Quantity - newChickin.Quantity
if updatedQuantity < 0 { if updatedQuantity < 0 {
updatedQuantity = 0 updatedQuantity = 0
@@ -191,15 +182,13 @@ func (s *chickinService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entit
s.Log.Errorf("Failed to update product warehouse quantity: %+v", err) s.Log.Errorf("Failed to update product warehouse quantity: %+v", err)
return nil, err return nil, err
} }
// masukan data nya ke project flock population
// check apakah sudah ada
existingPopulation, err := s.ProjectflockPopulationRepo.GetByProjectFlockKandangID(c.Context(), req.ProjectFlockKandangId) existingPopulation, err := s.ProjectflockPopulationRepo.GetByProjectFlockKandangID(c.Context(), req.ProjectFlockKandangId)
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
s.Log.Errorf("Failed to get project flock population: %+v", err) s.Log.Errorf("Failed to get project flock population: %+v", err)
return nil, err return nil, err
} }
if existingPopulation != nil { if existingPopulation != nil {
// update quantity
err = s.ProjectflockPopulationRepo.PatchOne(c.Context(), existingPopulation.Id, map[string]any{ err = s.ProjectflockPopulationRepo.PatchOne(c.Context(), existingPopulation.Id, map[string]any{
"reserved_quantity": newChickin.Quantity + existingPopulation.ReservedQuantity, "reserved_quantity": newChickin.Quantity + existingPopulation.ReservedQuantity,
@@ -209,7 +198,6 @@ func (s *chickinService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entit
return nil, err return nil, err
} }
} else { } else {
// create new population
newPopulation := &entity.ProjectFlockPopulation{ newPopulation := &entity.ProjectFlockPopulation{
ProjectFlockKandangId: req.ProjectFlockKandangId, ProjectFlockKandangId: req.ProjectFlockKandangId,
InitialQuantity: 0, InitialQuantity: 0,
@@ -253,6 +241,31 @@ func (s chickinService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
} }
func (s chickinService) DeleteOne(c *fiber.Ctx, id uint) error { func (s chickinService) DeleteOne(c *fiber.Ctx, id uint) error {
// todo: cek apakah chickin sudah di approve atau belum
chickin, err := s.Repository.GetByID(c.Context(), id, nil)
if errors.Is(err, gorm.ErrRecordNotFound) {
return fiber.NewError(fiber.StatusNotFound, "Chickin not found")
}
if err != nil {
s.Log.Errorf("Failed get chickin by id: %+v", err)
return err
}
population, err := s.ProjectflockPopulationRepo.GetByProjectFlockKandangID(c.Context(), chickin.ProjectFlockKandangId)
if err != nil {
s.Log.Errorf("Failed to get project flock population: %+v", err)
return err
}
err = s.ProjectflockPopulationRepo.PatchOne(c.Context(), population.Id, map[string]any{
"reserved_quantity": population.ReservedQuantity - chickin.Quantity,
}, nil)
if err != nil {
s.Log.Errorf("Failed to update project flock population: %+v", err)
return err
}
if err := s.Repository.DeleteOne(c.Context(), id); err != nil { if err := s.Repository.DeleteOne(c.Context(), id); err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) { if errors.Is(err, gorm.ErrRecordNotFound) {
return fiber.NewError(fiber.StatusNotFound, "Chickin not found") return fiber.NewError(fiber.StatusNotFound, "Chickin not found")
@@ -260,11 +273,62 @@ func (s chickinService) DeleteOne(c *fiber.Ctx, id uint) error {
s.Log.Errorf("Failed to delete chickin: %+v", err) s.Log.Errorf("Failed to delete chickin: %+v", err)
return err return err
} }
projectflockkandang, err := s.ProjectflockKandangRepo.GetByID(c.Context(), population.ProjectFlockKandangId)
if err != nil {
s.Log.Errorf("Failed to get projectflock kandang: %+v", err)
return err
}
warehouse, err := s.WarehouseRepo.GetByKandangID(c.Context(), projectflockkandang.KandangId)
if err != nil {
s.Log.Errorf("Failed to get warehouse: %+v", err)
return err
}
projectFlock, err := s.ProjectFlockRepo.GetByID(
c.Context(),
projectflockkandang.ProjectFlockId,
func(db *gorm.DB) *gorm.DB {
return db.Preload("ProductCategory")
},
)
if err != nil {
s.Log.Errorf("Failed to get project flock: %+v", err)
return fiber.NewError(fiber.StatusNotFound, "Project Flock not found")
}
var productWarehouse entity.ProductWarehouse
err = s.ProductWarehouseRepo.DB().WithContext(c.Context()).
Joins("JOIN products ON products.id = product_warehouses.product_id").
Joins("JOIN product_categories ON product_categories.id = products.product_category_id").
Where("product_categories.code = ? AND product_warehouses.warehouse_id = ?", projectFlock.ProductCategory.Code, warehouse.Id).
Order("created_at DESC").
First(&productWarehouse).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return fiber.NewError(fiber.StatusNotFound, "Product Warehouse not found for the given Project Flock and Warehouse")
}
s.Log.Errorf("Failed to get product warehouse: %+v", err)
return err
}
updatedQuantity := productWarehouse.Quantity + chickin.Quantity
err = s.ProductWarehouseRepo.PatchOne(c.Context(), productWarehouse.Id, map[string]any{
"quantity": updatedQuantity,
}, nil)
if err != nil {
s.Log.Errorf("Failed to update product warehouse quantity: %+v", err)
return err
}
return nil return nil
} }
func (s *chickinService) Approve(c *fiber.Ctx, id uint) error { func (s *chickinService) Approve(c *fiber.Ctx, id uint) error {
// todo: ini contoh akhir jika sudah approved
chickin, err := s.Repository.GetByID( chickin, err := s.Repository.GetByID(
c.Context(), c.Context(),
id, id,
@@ -278,7 +342,6 @@ func (s *chickinService) Approve(c *fiber.Ctx, id uint) error {
return err return err
} }
//pindahkan stock dari reserved ke actual stock pada table project flock population
population, err := s.ProjectflockPopulationRepo.GetByProjectFlockKandangID(c.Context(), chickin.ProjectFlockKandangId) population, err := s.ProjectflockPopulationRepo.GetByProjectFlockKandangID(c.Context(), chickin.ProjectFlockKandangId)
if err != nil { if err != nil {
s.Log.Errorf("Failed to get project flock population: %+v", err) s.Log.Errorf("Failed to get project flock population: %+v", err)