mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-24 07:15:43 +00:00
feat(BE-117): implement CRUD endpoints for project
This commit is contained in:
@@ -31,28 +31,30 @@ type ChickinService interface {
|
||||
}
|
||||
|
||||
type chickinService struct {
|
||||
Log *logrus.Logger
|
||||
Validate *validator.Validate
|
||||
Repository repository.ProjectChickinRepository
|
||||
KandangRepo KandangRepo.KandangRepository
|
||||
WarehouseRepo rWarehouse.WarehouseRepository
|
||||
ProductWarehouseRepo rProductWarehouse.ProductWarehouseRepository
|
||||
ProjectFlockRepo rProjectFlock.ProjectflockRepository
|
||||
AuditLogRepo AuditLogRepo.AuditLogRepository
|
||||
ProjectflockKandangRepo rProjectFlockKandang.ProjectFlockKandangRepository
|
||||
Log *logrus.Logger
|
||||
Validate *validator.Validate
|
||||
Repository repository.ProjectChickinRepository
|
||||
KandangRepo KandangRepo.KandangRepository
|
||||
WarehouseRepo rWarehouse.WarehouseRepository
|
||||
ProductWarehouseRepo rProductWarehouse.ProductWarehouseRepository
|
||||
ProjectFlockRepo rProjectFlock.ProjectflockRepository
|
||||
AuditLogRepo AuditLogRepo.AuditLogRepository
|
||||
ProjectflockKandangRepo rProjectFlockKandang.ProjectFlockKandangRepository
|
||||
ProjectflockPopulationRepo rProjectFlock.ProjectFlockPopulationRepository
|
||||
}
|
||||
|
||||
func NewChickinService(repo repository.ProjectChickinRepository, kandangRepo KandangRepo.KandangRepository, warehouseRepo rWarehouse.WarehouseRepository, productWarehouseRepo rProductWarehouse.ProductWarehouseRepository, projectFlockRepo rProjectFlock.ProjectflockRepository, auditLogRepo AuditLogRepo.AuditLogRepository, projectflockkandangRepo rProjectFlockKandang.ProjectFlockKandangRepository, validate *validator.Validate) ChickinService {
|
||||
func NewChickinService(repo repository.ProjectChickinRepository, kandangRepo KandangRepo.KandangRepository, warehouseRepo rWarehouse.WarehouseRepository, productWarehouseRepo rProductWarehouse.ProductWarehouseRepository, projectFlockRepo rProjectFlock.ProjectflockRepository, auditLogRepo AuditLogRepo.AuditLogRepository, projectflockkandangRepo rProjectFlockKandang.ProjectFlockKandangRepository, projectflockpopulationRepo rProjectFlock.ProjectFlockPopulationRepository, validate *validator.Validate) ChickinService {
|
||||
return &chickinService{
|
||||
Log: utils.Log,
|
||||
Validate: validate,
|
||||
Repository: repo,
|
||||
KandangRepo: kandangRepo,
|
||||
WarehouseRepo: warehouseRepo,
|
||||
ProductWarehouseRepo: productWarehouseRepo,
|
||||
ProjectFlockRepo: projectFlockRepo,
|
||||
AuditLogRepo: auditLogRepo,
|
||||
ProjectflockKandangRepo: projectflockkandangRepo,
|
||||
Log: utils.Log,
|
||||
Validate: validate,
|
||||
Repository: repo,
|
||||
KandangRepo: kandangRepo,
|
||||
WarehouseRepo: warehouseRepo,
|
||||
ProductWarehouseRepo: productWarehouseRepo,
|
||||
ProjectFlockRepo: projectFlockRepo,
|
||||
AuditLogRepo: auditLogRepo,
|
||||
ProjectflockKandangRepo: projectflockkandangRepo,
|
||||
ProjectflockPopulationRepo: projectflockpopulationRepo,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,8 +80,9 @@ func (s chickinService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity
|
||||
|
||||
chickins, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
|
||||
db = s.withRelations(db)
|
||||
if params.Search != "" {
|
||||
return db.Where("name LIKE ?", "%"+params.Search+"%")
|
||||
|
||||
if params.ProjectFlockKandangId != 0 {
|
||||
return db.Where("project_flock_kandang_id = ?", params.ProjectFlockKandangId)
|
||||
}
|
||||
return db.Order("created_at DESC").Order("updated_at DESC")
|
||||
})
|
||||
@@ -163,11 +166,11 @@ func (s *chickinService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entit
|
||||
}
|
||||
|
||||
newChickin := &entity.ProjectChickin{
|
||||
ProjectFlocKandangId: projectflockkandang.ProjectFlockId,
|
||||
ChickInDate: chickinDate,
|
||||
Quantity: productWarehouse.Quantity,
|
||||
Note: "",
|
||||
CreatedBy: 1, //todo: ganti dengan
|
||||
ProjectFlockKandangId: projectflockkandang.ProjectFlockId,
|
||||
ChickInDate: chickinDate,
|
||||
Quantity: productWarehouse.Quantity,
|
||||
Note: "",
|
||||
CreatedBy: 1, //todo: ganti dengan
|
||||
}
|
||||
|
||||
err = s.Repository.CreateOne(c.Context(), newChickin, nil)
|
||||
@@ -188,45 +191,37 @@ func (s *chickinService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entit
|
||||
s.Log.Errorf("Failed to update product warehouse quantity: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// masukan check apakah stock availability ada, jika ada update, jika tidak buat baru
|
||||
stockAvailability := &entity.StockAvailability{
|
||||
EntityType: entity.EntityTypeProjectFlockKandang,
|
||||
ReservedQuantity: productWarehouse.Quantity,
|
||||
EntityId: projectflockkandang.Id,
|
||||
ProductId: productWarehouse.ProductId,
|
||||
// masukan data nya ke project flock population
|
||||
// check apakah sudah ada
|
||||
existingPopulation, err := s.ProjectflockPopulationRepo.GetByProjectFlockKandangID(c.Context(), req.ProjectFlockKandangId)
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
s.Log.Errorf("Failed to get project flock population: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
if existingPopulation != nil {
|
||||
// update quantity
|
||||
|
||||
var existingStockAvailability entity.StockAvailability
|
||||
err = s.ProductWarehouseRepo.DB().WithContext(c.Context()).
|
||||
Where("entity_type = ? AND entity_id = ? AND product_id = ?", stockAvailability.EntityType, stockAvailability.EntityId, stockAvailability.ProductId).
|
||||
First(&existingStockAvailability).Error
|
||||
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
// buat baru
|
||||
stockAvailability.ReservedQuantity = newChickin.Quantity
|
||||
stockAvailability.Quantity = 0
|
||||
err = s.ProductWarehouseRepo.DB().WithContext(c.Context()).Create(stockAvailability).Error
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to create stock availability: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
s.Log.Errorf("Failed to get stock availability: %+v", err)
|
||||
err = s.ProjectflockPopulationRepo.PatchOne(c.Context(), existingPopulation.Id, map[string]any{
|
||||
"reserved_quantity": newChickin.Quantity + existingPopulation.ReservedQuantity,
|
||||
}, nil)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to update project flock population: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
// update existing
|
||||
newQuantity := existingStockAvailability.ReservedQuantity + newChickin.Quantity
|
||||
err = s.ProductWarehouseRepo.DB().WithContext(c.Context()).
|
||||
Model(&existingStockAvailability).
|
||||
Update("reserved_quantity", newQuantity).Error
|
||||
// create new population
|
||||
newPopulation := &entity.ProjectFlockPopulation{
|
||||
ProjectFlockKandangId: req.ProjectFlockKandangId,
|
||||
InitialQuantity: 0,
|
||||
CurrentQuantity: 0,
|
||||
ReservedQuantity: newChickin.Quantity,
|
||||
CreatedBy: 1, // todo: ganti dengan user login
|
||||
}
|
||||
err = s.ProjectflockPopulationRepo.CreateOne(c.Context(), newPopulation, nil)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to update stock availability: %+v", err)
|
||||
s.Log.Errorf("Failed to create project flock population: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return s.GetOne(c, newChickin.Id)
|
||||
@@ -283,20 +278,21 @@ func (s *chickinService) Approve(c *fiber.Ctx, id uint) error {
|
||||
return err
|
||||
}
|
||||
|
||||
//pindahkan stock dari reserved ke actual stock
|
||||
// get stock avaibility untuk di update
|
||||
var stockAvailability entity.StockAvailability
|
||||
err = s.ProductWarehouseRepo.DB().WithContext(c.Context()).
|
||||
Where("entity_type = ? AND entity_id = ? ", entity.EntityTypeProjectFlockKandang, chickin.ProjectFlocKandangId).
|
||||
First(&stockAvailability).Error
|
||||
//pindahkan stock dari reserved ke actual stock pada table project flock population
|
||||
population, err := s.ProjectflockPopulationRepo.GetByProjectFlockKandangID(c.Context(), chickin.ProjectFlockKandangId)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to get stock availability: %+v", err)
|
||||
s.Log.Errorf("Failed to get project flock population: %+v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
newReservedQuantity := stockAvailability.ReservedQuantity - chickin.Quantity
|
||||
if newReservedQuantity < 0 {
|
||||
newReservedQuantity = 0
|
||||
err = s.ProjectflockPopulationRepo.PatchOne(c.Context(), population.Id, map[string]any{
|
||||
"reserved_quantity": population.ReservedQuantity - chickin.Quantity,
|
||||
"initial_quantity": population.InitialQuantity + chickin.Quantity,
|
||||
"current_quantity": population.CurrentQuantity + chickin.Quantity,
|
||||
}, nil)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to update project flock population: %+v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user