mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-24 23:35:43 +00:00
Feat[BE-222]: creating update DO(Unfinished)
This commit is contained in:
@@ -25,7 +25,7 @@ type DeliveryOrdersService interface {
|
||||
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]dto.DeliveryOrdersListDTO, int64, error)
|
||||
GetOne(ctx *fiber.Ctx, id uint) (*dto.DeliveryOrdersListDTO, error)
|
||||
CreateOne(ctx *fiber.Ctx, req *validation.Create) (*dto.DeliveryOrdersListDTO, error)
|
||||
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*entity.DeliveryOrders, error)
|
||||
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*dto.DeliveryOrdersListDTO, error)
|
||||
DeleteOne(ctx *fiber.Ctx, id uint) error
|
||||
Approval(ctx *fiber.Ctx, req *validation.Approve) ([]entity.DeliveryOrders, error)
|
||||
}
|
||||
@@ -91,18 +91,11 @@ func (s deliveryOrdersService) GetAll(c *fiber.Ctx, params *validation.Query) ([
|
||||
// Load delivery products untuk setiap marketing
|
||||
result := make([]dto.DeliveryOrdersListDTO, len(marketings))
|
||||
for i, marketing := range marketings {
|
||||
// Get marketing delivery products
|
||||
var allDeliveryProducts []entity.MarketingDeliveryProduct
|
||||
if err := s.Repository.DB().WithContext(c.Context()).
|
||||
Preload("MarketingProduct").
|
||||
Where("marketing_product_id IN (?)",
|
||||
s.Repository.DB().WithContext(c.Context()).
|
||||
Model(&entity.MarketingProduct{}).
|
||||
Select("id").
|
||||
Where("marketing_id = ?", marketing.Id)).
|
||||
Find(&allDeliveryProducts).Error; err != nil {
|
||||
// Get marketing delivery products menggunakan repository method
|
||||
allDeliveryProducts, err := s.MarketingDeliveryProductRepo.GetByMarketingId(c.Context(), marketing.Id)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to load delivery products for marketing %d: %+v", marketing.Id, err)
|
||||
// Continue without products
|
||||
allDeliveryProducts = []entity.MarketingDeliveryProduct{} // Set empty slice jika gagal
|
||||
}
|
||||
|
||||
// Build response DTO
|
||||
@@ -125,7 +118,8 @@ func (s deliveryOrdersService) GetOne(c *fiber.Ctx, id uint) (*dto.DeliveryOrder
|
||||
return db.Preload("CreatedUser").
|
||||
Preload("Customer").
|
||||
Preload("SalesPerson").
|
||||
Preload("Products.ProductWarehouse")
|
||||
Preload("Products.ProductWarehouse.Product").
|
||||
Preload("Products.ProductWarehouse.Warehouse")
|
||||
})
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, "Marketing not found")
|
||||
@@ -135,26 +129,52 @@ func (s deliveryOrdersService) GetOne(c *fiber.Ctx, id uint) (*dto.DeliveryOrder
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Get marketing delivery products
|
||||
var allDeliveryProducts []entity.MarketingDeliveryProduct
|
||||
if err := s.Repository.DB().WithContext(c.Context()).
|
||||
Preload("MarketingProduct").
|
||||
Where("marketing_product_id IN (?)",
|
||||
s.Repository.DB().WithContext(c.Context()).
|
||||
Model(&entity.MarketingProduct{}).
|
||||
Select("id").
|
||||
Where("marketing_id = ?", marketing.Id)).
|
||||
Find(&allDeliveryProducts).Error; err != nil {
|
||||
s.Log.Errorf("Failed to load delivery products for marketing %d: %+v", marketing.Id, err)
|
||||
// Continue without products
|
||||
// Get marketing delivery products menggunakan repository method
|
||||
allDeliveryProducts, err := s.MarketingDeliveryProductRepo.GetByMarketingId(c.Context(), id)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to load delivery products for marketing %d: %+v", id, err)
|
||||
allDeliveryProducts = []entity.MarketingDeliveryProduct{} // Set empty slice jika gagal
|
||||
}
|
||||
|
||||
// Build response DTO
|
||||
// Debug: Log jumlah delivery products
|
||||
s.Log.Infof("Found %d delivery products for marketing %d", len(allDeliveryProducts), id)
|
||||
|
||||
// Jika tidak ada delivery products, buat dummy data untuk testing
|
||||
if len(allDeliveryProducts) == 0 && len(marketing.Products) > 0 {
|
||||
s.Log.Infof("Creating dummy delivery products for testing")
|
||||
for i, product := range marketing.Products {
|
||||
deliveryDate := marketing.SoDate.AddDate(0, 0, i+7) // 7 hari setelah SO
|
||||
dummyDeliveryProduct := entity.MarketingDeliveryProduct{
|
||||
Id: uint(i + 1),
|
||||
MarketingProductId: product.Id,
|
||||
Qty: product.Qty / 2, // Setengah dari qty asli
|
||||
UnitPrice: product.UnitPrice,
|
||||
TotalWeight: product.TotalWeight / 2,
|
||||
AvgWeight: product.AvgWeight,
|
||||
TotalPrice: (product.Qty / 2) * product.UnitPrice,
|
||||
DeliveryDate: &deliveryDate,
|
||||
VehicleNumber: fmt.Sprintf("B%04d%s", (i+1)*1000, "ABC"),
|
||||
}
|
||||
allDeliveryProducts = append(allDeliveryProducts, dummyDeliveryProduct)
|
||||
}
|
||||
s.Log.Infof("Created %d dummy delivery products", len(allDeliveryProducts))
|
||||
}
|
||||
|
||||
// Build response DTO dengan timestamps yang benar
|
||||
deliveryOrderResponse := &entity.DeliveryOrders{
|
||||
MarketingId: marketing.Id,
|
||||
CreatedUser: &marketing.CreatedUser,
|
||||
Marketing: marketing,
|
||||
DeliveryProducts: allDeliveryProducts,
|
||||
CreatedAt: marketing.CreatedAt,
|
||||
UpdatedAt: marketing.UpdatedAt,
|
||||
}
|
||||
|
||||
// Set delivery_date dari delivery products atau fallback ke marketing date
|
||||
if len(allDeliveryProducts) > 0 && allDeliveryProducts[0].DeliveryDate != nil {
|
||||
deliveryOrderResponse.DeliveryDate = *allDeliveryProducts[0].DeliveryDate
|
||||
} else {
|
||||
deliveryOrderResponse.DeliveryDate = marketing.SoDate
|
||||
}
|
||||
|
||||
responseDTO := dto.ToDeliveryOrdersListDTOWithProducts(*deliveryOrderResponse, allDeliveryProducts)
|
||||
@@ -255,6 +275,27 @@ func (s *deliveryOrdersService) CreateOne(c *fiber.Ctx, req *validation.Create)
|
||||
s.Log.Infof("Updated delivery product %d: qty=%v, unitPrice=%v, totalPrice=%v", deliveryProduct.Id, requestedProduct.Qty, requestedProduct.UnitPrice, requestedProduct.TotalPrice)
|
||||
}
|
||||
|
||||
approvalSvcTx := commonSvc.NewApprovalService(commonRepo.NewApprovalRepository(dbTransaction))
|
||||
actorID := uint(1) // TODO: ambil dari auth context
|
||||
approvalAction := entity.ApprovalActionCreated
|
||||
var notes *string
|
||||
if req.Notes != "" {
|
||||
notes = &req.Notes
|
||||
}
|
||||
if _, err := approvalSvcTx.CreateApproval(
|
||||
c.Context(),
|
||||
utils.ApprovalWorkflowMarketing,
|
||||
req.MarketingId,
|
||||
utils.MarketingDeliveryOrder,
|
||||
&approvalAction,
|
||||
actorID,
|
||||
notes); err != nil {
|
||||
if !errors.Is(err, gorm.ErrDuplicatedKey) {
|
||||
s.Log.Errorf("Failed to create delivery order approval: %+v", err)
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to create delivery order approval")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
@@ -262,7 +303,6 @@ func (s *deliveryOrdersService) CreateOne(c *fiber.Ctx, req *validation.Create)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Fetch marketing dengan delivery products yang sudah di-updated
|
||||
marketing, err := s.MarketingRepo.GetByID(c.Context(), req.MarketingId, func(db *gorm.DB) *gorm.DB {
|
||||
return db.Preload("CreatedUser").Preload("Products.DeliveryProduct")
|
||||
})
|
||||
@@ -271,18 +311,11 @@ func (s *deliveryOrdersService) CreateOne(c *fiber.Ctx, req *validation.Create)
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch updated marketing")
|
||||
}
|
||||
|
||||
// Get marketing delivery products
|
||||
var allDeliveryProducts []entity.MarketingDeliveryProduct
|
||||
if err := s.MarketingDeliveryProductRepo.DB().WithContext(c.Context()).
|
||||
Preload("MarketingProduct").
|
||||
Where("marketing_product_id IN (?)",
|
||||
s.MarketingProductRepo.DB().WithContext(c.Context()).
|
||||
Model(&entity.MarketingProduct{}).
|
||||
Select("id").
|
||||
Where("marketing_id = ?", req.MarketingId)).
|
||||
Find(&allDeliveryProducts).Error; err != nil {
|
||||
// Get marketing delivery products menggunakan repository method
|
||||
allDeliveryProducts, err := s.MarketingDeliveryProductRepo.GetByMarketingId(c.Context(), req.MarketingId)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to load delivery products: %+v", err)
|
||||
// Continue tanpa delivery products
|
||||
allDeliveryProducts = []entity.MarketingDeliveryProduct{} // Set empty slice jika gagal
|
||||
}
|
||||
|
||||
// Build response DTO
|
||||
@@ -298,38 +331,132 @@ func (s *deliveryOrdersService) CreateOne(c *fiber.Ctx, req *validation.Create)
|
||||
return &responseDTO, nil
|
||||
}
|
||||
|
||||
func (s deliveryOrdersService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*entity.DeliveryOrders, error) {
|
||||
func (s deliveryOrdersService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*dto.DeliveryOrdersListDTO, error) {
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
updateBody := make(map[string]any)
|
||||
|
||||
if req.DeliveryDate != "" {
|
||||
deliveryDate, err := time.Parse("2006-01-02", req.DeliveryDate)
|
||||
if err != nil {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Invalid delivery date format")
|
||||
}
|
||||
updateBody["delivery_date"] = deliveryDate
|
||||
}
|
||||
|
||||
if req.Notes != "" {
|
||||
updateBody["notes"] = req.Notes
|
||||
}
|
||||
|
||||
if len(updateBody) == 0 {
|
||||
return s.Repository.GetByID(c.Context(), id, s.withRelations)
|
||||
}
|
||||
|
||||
if err := s.Repository.PatchOne(c.Context(), id, updateBody, nil); err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, "DeliveryOrders not found")
|
||||
}
|
||||
s.Log.Errorf("Failed to update deliveryOrders: %+v", err)
|
||||
// Validate bahwa marketing ID yang di-update ada (id parameter adalah marketing_id untuk delivery orders)
|
||||
if err := commonSvc.EnsureRelations(c.Context(),
|
||||
commonSvc.RelationCheck{Name: "Marketing", ID: &id, Exists: s.MarketingRepo.IdExists},
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s.Repository.GetByID(c.Context(), id, s.withRelations)
|
||||
// Validate delivery products jika ada
|
||||
if len(req.DeliveryProducts) > 0 {
|
||||
for _, requestedProduct := range req.DeliveryProducts {
|
||||
if err := commonSvc.EnsureRelations(c.Context(),
|
||||
commonSvc.RelationCheck{Name: "MarketingProduct", ID: &requestedProduct.MarketingProductId, Exists: s.MarketingProductRepo.IdExists},
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
err := s.Repository.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
|
||||
marketingProductRepositoryTx := marketingRepo.NewMarketingProductRepository(dbTransaction)
|
||||
marketingDeliveryProductRepositoryTx := marketingDeliveryProductRepo.NewMarketingDeliveryProductRepository(dbTransaction)
|
||||
|
||||
// Update delivery products jika ada dalam request
|
||||
if len(req.DeliveryProducts) > 0 {
|
||||
for _, requestedProduct := range req.DeliveryProducts {
|
||||
// Validate bahwa marketing product ada untuk marketing ini
|
||||
allMarketingProducts, err := marketingProductRepositoryTx.GetByMarketingID(c.Context(), id)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("No marketing products found for marketing %d", id))
|
||||
}
|
||||
s.Log.Errorf("Failed to fetch marketing products: %+v", err)
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch marketing products")
|
||||
}
|
||||
|
||||
var foundMarketingProduct *entity.MarketingProduct
|
||||
for i := range allMarketingProducts {
|
||||
if allMarketingProducts[i].Id == requestedProduct.MarketingProductId {
|
||||
foundMarketingProduct = &allMarketingProducts[i]
|
||||
break
|
||||
}
|
||||
}
|
||||
if foundMarketingProduct == nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("Marketing product %d not found for this marketing", requestedProduct.MarketingProductId))
|
||||
}
|
||||
|
||||
// Get existing delivery product
|
||||
deliveryProduct, err := marketingDeliveryProductRepositoryTx.GetByMarketingProductID(c.Context(), foundMarketingProduct.Id)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("Delivery product for marketing product %d not found", requestedProduct.MarketingProductId))
|
||||
}
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch delivery product")
|
||||
}
|
||||
|
||||
// Parse delivery date
|
||||
var itemDeliveryDate time.Time
|
||||
if requestedProduct.DeliveryDate != "" {
|
||||
parsedDate, err := utils.ParseDateString(requestedProduct.DeliveryDate)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Invalid delivery date format for product %d: %v", requestedProduct.MarketingProductId, err))
|
||||
}
|
||||
itemDeliveryDate = parsedDate
|
||||
} else if deliveryProduct.DeliveryDate != nil {
|
||||
itemDeliveryDate = *deliveryProduct.DeliveryDate
|
||||
} else {
|
||||
itemDeliveryDate = time.Now()
|
||||
}
|
||||
|
||||
// Update delivery product
|
||||
deliveryProduct.Qty = requestedProduct.Qty
|
||||
deliveryProduct.UnitPrice = requestedProduct.UnitPrice
|
||||
deliveryProduct.AvgWeight = requestedProduct.AvgWeight
|
||||
deliveryProduct.TotalWeight = requestedProduct.TotalWeight
|
||||
deliveryProduct.TotalPrice = requestedProduct.TotalPrice
|
||||
deliveryProduct.DeliveryDate = &itemDeliveryDate
|
||||
deliveryProduct.VehicleNumber = requestedProduct.VehicleNumber
|
||||
|
||||
if err := marketingDeliveryProductRepositoryTx.UpdateOne(c.Context(), deliveryProduct.Id, deliveryProduct, nil); err != nil {
|
||||
s.Log.Errorf("Failed to update marketing delivery product: %+v", err)
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to update delivery product")
|
||||
}
|
||||
|
||||
s.Log.Infof("Updated delivery product %d: qty=%v, unitPrice=%v, totalPrice=%v", deliveryProduct.Id, requestedProduct.Qty, requestedProduct.UnitPrice, requestedProduct.TotalPrice)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Fetch updated marketing with delivery products
|
||||
marketing, err := s.MarketingRepo.GetByID(c.Context(), id, func(db *gorm.DB) *gorm.DB {
|
||||
return db.Preload("CreatedUser").Preload("Products.DeliveryProduct")
|
||||
})
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to fetch marketing after update: %+v", err)
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch updated marketing")
|
||||
}
|
||||
|
||||
// Get marketing delivery products menggunakan repository method
|
||||
allDeliveryProducts, err := s.MarketingDeliveryProductRepo.GetByMarketingId(c.Context(), id)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to load delivery products: %+v", err)
|
||||
allDeliveryProducts = []entity.MarketingDeliveryProduct{} // Set empty slice jika gagal
|
||||
}
|
||||
|
||||
// Build response DTO
|
||||
deliveryOrderResponse := &entity.DeliveryOrders{
|
||||
MarketingId: id,
|
||||
Notes: req.Notes,
|
||||
CreatedUser: &marketing.CreatedUser,
|
||||
Marketing: marketing,
|
||||
DeliveryProducts: allDeliveryProducts,
|
||||
}
|
||||
|
||||
responseDTO := dto.ToDeliveryOrdersListDTOWithProducts(*deliveryOrderResponse, allDeliveryProducts)
|
||||
return &responseDTO, nil
|
||||
}
|
||||
|
||||
func (s deliveryOrdersService) Approval(c *fiber.Ctx, req *validation.Approve) ([]entity.DeliveryOrders, error) {
|
||||
|
||||
Reference in New Issue
Block a user