From 957c1ae53da0e2825161c39bab684485320424a4 Mon Sep 17 00:00:00 2001 From: Efril Date: Mon, 25 May 2026 20:28:24 +0700 Subject: [PATCH] update order response --- internal/contract/order_contract.go | 2 ++ internal/mappers/order_mapper.go | 8 +++++++ internal/models/order.go | 2 ++ internal/processor/order_processor.go | 28 +++-------------------- internal/repository/order_repository.go | 3 +++ internal/transformer/order_transformer.go | 4 ++++ 6 files changed, 22 insertions(+), 25 deletions(-) diff --git a/internal/contract/order_contract.go b/internal/contract/order_contract.go index 028dfc4..0895ff8 100644 --- a/internal/contract/order_contract.go +++ b/internal/contract/order_contract.go @@ -98,6 +98,8 @@ type OrderItemResponse struct { ProductName string `json:"product_name"` ProductVariantID *uuid.UUID `json:"product_variant_id"` ProductVariantName *string `json:"product_variant_name,omitempty"` + CategoryID *uuid.UUID `json:"category_id,omitempty"` + CategoryName *string `json:"category_name,omitempty"` Quantity int `json:"quantity"` UnitPrice float64 `json:"unit_price"` TotalPrice float64 `json:"total_price"` diff --git a/internal/mappers/order_mapper.go b/internal/mappers/order_mapper.go index 5f3c4da..4e336d5 100644 --- a/internal/mappers/order_mapper.go +++ b/internal/mappers/order_mapper.go @@ -134,6 +134,14 @@ func OrderItemEntityToResponse(item *entities.OrderItem) *models.OrderItemRespon if item.Product.ID != uuid.Nil { response.ProductName = item.Product.Name + if item.Product.CategoryID != uuid.Nil { + categoryID := item.Product.CategoryID + response.CategoryID = &categoryID + } + if item.Product.Category.ID != uuid.Nil { + categoryName := item.Product.Category.Name + response.CategoryName = &categoryName + } } if item.ProductVariant != nil { diff --git a/internal/models/order.go b/internal/models/order.go index 39fddea..3a456af 100644 --- a/internal/models/order.go +++ b/internal/models/order.go @@ -188,6 +188,8 @@ type OrderItemResponse struct { ProductName string ProductVariantID *uuid.UUID ProductVariantName *string + CategoryID *uuid.UUID + CategoryName *string Quantity int UnitPrice float64 TotalPrice float64 diff --git a/internal/processor/order_processor.go b/internal/processor/order_processor.go index 26ee447..d146785 100644 --- a/internal/processor/order_processor.go +++ b/internal/processor/order_processor.go @@ -1,7 +1,6 @@ package processor import ( - "apskel-pos-be/internal/constants" "context" "errors" "fmt" @@ -388,31 +387,10 @@ func (p *OrderProcessorImpl) AddToOrder(ctx context.Context, orderID uuid.UUID, return nil, fmt.Errorf("failed to create order item: %w", err) } - itemResponse := models.OrderItemResponse{ - ID: orderItem.ID, - OrderID: orderItem.OrderID, - ProductID: orderItem.ProductID, - ProductVariantID: orderItem.ProductVariantID, - Quantity: orderItem.Quantity, - UnitPrice: orderItem.UnitPrice, - TotalPrice: orderItem.TotalPrice, - UnitCost: orderItem.UnitCost, - TotalCost: orderItem.TotalCost, - RefundAmount: orderItem.RefundAmount, - RefundQuantity: orderItem.RefundQuantity, - IsPartiallyRefunded: orderItem.IsPartiallyRefunded, - IsFullyRefunded: orderItem.IsFullyRefunded, - RefundReason: orderItem.RefundReason, - RefundedAt: orderItem.RefundedAt, - RefundedBy: orderItem.RefundedBy, - Modifiers: []map[string]interface{}(orderItem.Modifiers), - Notes: orderItem.Notes, - Metadata: map[string]interface{}(orderItem.Metadata), - Status: constants.OrderItemStatus(orderItem.Status), - CreatedAt: orderItem.CreatedAt, - UpdatedAt: orderItem.UpdatedAt, + itemResponse := mappers.OrderItemEntityToResponse(orderItem) + if itemResponse != nil { + addedItemResponses = append(addedItemResponses, *itemResponse) } - addedItemResponses = append(addedItemResponses, itemResponse) } orderWithRelations, err := p.orderRepo.GetWithRelations(ctx, orderID) diff --git a/internal/repository/order_repository.go b/internal/repository/order_repository.go index 97ca112..2a60aa9 100644 --- a/internal/repository/order_repository.go +++ b/internal/repository/order_repository.go @@ -60,6 +60,7 @@ func (r *OrderRepositoryImpl) GetWithRelations(ctx context.Context, id uuid.UUID Preload("User"). Preload("OrderItems"). Preload("OrderItems.Product"). + Preload("OrderItems.Product.Category"). Preload("OrderItems.ProductVariant"). Preload("Payments"). Preload("Payments.PaymentMethod"). @@ -139,6 +140,7 @@ func (r *OrderRepositoryImpl) List(ctx context.Context, filters map[string]inter Preload("User"). Preload("OrderItems"). Preload("OrderItems.Product"). + Preload("OrderItems.Product.Category"). Preload("OrderItems.ProductVariant"). Preload("Payments"). Preload("Payments.PaymentMethod"). @@ -155,6 +157,7 @@ func (r *OrderRepositoryImpl) ListBySessionID(ctx context.Context, sessionID str Preload("User"). Preload("OrderItems"). Preload("OrderItems.Product"). + Preload("OrderItems.Product.Category"). Preload("OrderItems.ProductVariant"). Preload("Payments"). Preload("Payments.PaymentMethod"). diff --git a/internal/transformer/order_transformer.go b/internal/transformer/order_transformer.go index 72abbe7..7edbe3b 100644 --- a/internal/transformer/order_transformer.go +++ b/internal/transformer/order_transformer.go @@ -100,6 +100,8 @@ func OrderModelToContract(resp *models.OrderResponse) *contract.OrderResponse { ProductName: item.ProductName, ProductVariantID: item.ProductVariantID, ProductVariantName: item.ProductVariantName, + CategoryID: item.CategoryID, + CategoryName: item.CategoryName, Quantity: item.Quantity, UnitPrice: item.UnitPrice, TotalPrice: item.TotalPrice, @@ -168,6 +170,8 @@ func AddToOrderModelToContract(resp *models.AddToOrderResponse) *contract.AddToO ProductName: item.ProductName, ProductVariantID: item.ProductVariantID, ProductVariantName: item.ProductVariantName, + CategoryID: item.CategoryID, + CategoryName: item.CategoryName, Quantity: item.Quantity, UnitPrice: item.UnitPrice, TotalPrice: item.TotalPrice,