diff --git a/internal/repository/In_progress_orde_repo.go b/internal/repository/In_progress_orde_repo.go index bc9485c..af20dec 100644 --- a/internal/repository/In_progress_orde_repo.go +++ b/internal/repository/In_progress_orde_repo.go @@ -18,6 +18,7 @@ type InProgressOrderRepository interface { CreateOrderItems(ctx mycontext.Context, orderID int64, items []entity.OrderItem, tx *gorm.DB) error GetListByPartnerID(ctx mycontext.Context, partnerID int64, limit, offset int, status string) ([]*entity.Order, error) FindByIDAndPartnerID(ctx mycontext.Context, id int64, partnerID int64) (*entity.Order, error) + FindByIDWithTx(ctx mycontext.Context, id int64, tx *gorm.DB) (*entity.Order, error) } type inprogressOrderRepository struct { @@ -43,6 +44,21 @@ func (r *inprogressOrderRepository) FindByID(ctx mycontext.Context, id int64) (* return order, nil } +func (r *inprogressOrderRepository) FindByIDWithTx(ctx mycontext.Context, id int64, tx *gorm.DB) (*entity.Order, error) { + var orderDB models.OrderDB + + if err := tx.Preload("OrderItems").First(&orderDB, id).Error; err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return nil, errors.New("order not found") + } + return nil, errors.Wrap(err, "failed to find order") + } + + order := r.toDomainOrderModel(&orderDB) + + return order, nil +} + func (r *inprogressOrderRepository) CreateOrder(ctx mycontext.Context, order *entity.Order, tx *gorm.DB) (*entity.Order, error) { orderDB := r.toOrderDBModel(order) diff --git a/internal/repository/orde_repo.go b/internal/repository/orde_repo.go index 08e4634..1e827d7 100644 --- a/internal/repository/orde_repo.go +++ b/internal/repository/orde_repo.go @@ -34,6 +34,7 @@ type OrderRepository interface { UpdateOrderItem(ctx mycontext.Context, orderItemID int64, quantity int) error UpdateOrderTotals(ctx mycontext.Context, orderID int64, amount, tax, total float64) error UpdateOrderTotalsWithTx(ctx mycontext.Context, trx *gorm.DB, orderID int64, amount, tax, total float64) error + FindByIDWithTx(ctx mycontext.Context, id int64, tx *gorm.DB) (*entity.Order, error) } type orderRepository struct { @@ -123,6 +124,20 @@ func (r *orderRepository) FindByID(ctx mycontext.Context, id int64) (*entity.Ord return order, nil } +func (r *orderRepository) FindByIDWithTx(ctx mycontext.Context, id int64, tx *gorm.DB) (*entity.Order, error) { + var orderDB models.OrderDB + + if err := tx.Preload("OrderItems").First(&orderDB, id).Error; err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return nil, errors.New("order not found") + } + return nil, errors.Wrap(err, "failed to find order in transaction") + } + + order := r.toDomainOrderModel(&orderDB) + return order, nil +} + func (r *orderRepository) CreateInquiry(ctx mycontext.Context, inquiry *entity.OrderInquiry) (*entity.OrderInquiry, error) { inquiryDB := r.toOrderInquiryDBModel(inquiry) inquiryItems := make([]models.InquiryItemDB, 0, len(inquiry.OrderItems)) diff --git a/internal/services/v2/inprogress_order/in_progress_order.go b/internal/services/v2/inprogress_order/in_progress_order.go index c8b75b9..80dba79 100644 --- a/internal/services/v2/inprogress_order/in_progress_order.go +++ b/internal/services/v2/inprogress_order/in_progress_order.go @@ -25,6 +25,7 @@ type InProgressOrderService interface { type OrderRepository interface { FindByID(ctx mycontext.Context, id int64) (*entity.Order, error) + FindByIDWithTx(ctx mycontext.Context, id int64, tx *gorm.DB) (*entity.Order, error) CreateOrder(ctx mycontext.Context, order *entity.Order, tx *gorm.DB) (*entity.Order, error) CreateOrderItems(ctx mycontext.Context, orderID int64, items []entity.OrderItem, tx *gorm.DB) error GetListByPartnerID(ctx mycontext.Context, partnerID int64, limit, offset int, status string) ([]*entity.Order, error) @@ -147,7 +148,7 @@ func (s *inProgressOrderSvc) AddItems(ctx mycontext.Context, orderID int64, newI return nil, errors.Wrap(err, "failed to add order items") } - updatedOrder, err := s.repo.FindByID(ctx, existingOrder.ID) + updatedOrder, err := s.repo.FindByIDWithTx(ctx, existingOrder.ID, tx) if err != nil { s.trx.Rollback(tx) return nil, errors.Wrap(err, "failed to fetch updated order")