This commit is contained in:
Efril 2026-05-28 15:30:18 +07:00
parent 23ac572e3f
commit 84222fc7f4
8 changed files with 33 additions and 15 deletions

View File

@ -110,6 +110,7 @@ type OrderItemResponse struct {
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
PrinterType string `json:"printer_type"`
PrintToChecker bool `json:"print_to_checker"`
PaidQuantity int `json:"paid_quantity"`
}

View File

@ -26,13 +26,14 @@ type Product struct {
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
Organization Organization `gorm:"foreignKey:OrganizationID" json:"organization,omitempty"`
Category Category `gorm:"foreignKey:CategoryID" json:"category,omitempty"`
Unit *Unit `gorm:"foreignKey:UnitID" json:"unit,omitempty"`
ProductVariants []ProductVariant `gorm:"foreignKey:ProductID" json:"variants,omitempty"`
ProductRecipes []ProductRecipe `gorm:"foreignKey:ProductID" json:"product_recipes,omitempty"`
Inventory []Inventory `gorm:"foreignKey:ProductID" json:"inventory,omitempty"`
OrderItems []OrderItem `gorm:"foreignKey:ProductID" json:"order_items,omitempty"`
Organization Organization `gorm:"foreignKey:OrganizationID" json:"organization,omitempty"`
Category Category `gorm:"foreignKey:CategoryID" json:"category,omitempty"`
Unit *Unit `gorm:"foreignKey:UnitID" json:"unit,omitempty"`
ProductVariants []ProductVariant `gorm:"foreignKey:ProductID" json:"variants,omitempty"`
ProductRecipes []ProductRecipe `gorm:"foreignKey:ProductID" json:"product_recipes,omitempty"`
Inventory []Inventory `gorm:"foreignKey:ProductID" json:"inventory,omitempty"`
OrderItems []OrderItem `gorm:"foreignKey:ProductID" json:"order_items,omitempty"`
ProductOutletPrices []ProductOutletPrice `gorm:"foreignKey:ProductID" json:"product_outlet_prices,omitempty"`
}
func (p *Product) BeforeCreate(tx *gorm.DB) error {

View File

@ -82,7 +82,7 @@ func OrderEntityToResponse(order *entities.Order) *models.OrderResponse {
}
for i, item := range order.OrderItems {
resp := OrderItemEntityToResponse(&item)
resp := OrderItemEntityToResponse(&item, order.OutletID)
if resp != nil {
resp.PaidQuantity = paidQtyByOrderItem[item.ID]
response.OrderItems[i] = *resp
@ -101,11 +101,20 @@ func OrderEntityToResponse(order *entities.Order) *models.OrderResponse {
return response
}
func OrderItemEntityToResponse(item *entities.OrderItem) *models.OrderItemResponse {
func OrderItemEntityToResponse(item *entities.OrderItem, outletID uuid.UUID) *models.OrderItemResponse {
if item == nil {
return nil
}
// Resolve print_to_checker from preloaded outlet prices
printToChecker := true // default
for _, op := range item.Product.ProductOutletPrices {
if op.OutletID == outletID {
printToChecker = op.PrintToChecker
break
}
}
response := &models.OrderItemResponse{
ID: item.ID,
OrderID: item.OrderID,
@ -130,6 +139,7 @@ func OrderItemEntityToResponse(item *entities.OrderItem) *models.OrderItemRespon
CreatedAt: item.CreatedAt,
UpdatedAt: item.UpdatedAt,
PrinterType: item.Product.PrinterType,
PrintToChecker: printToChecker,
}
if item.Product.ID != uuid.Nil {
@ -324,14 +334,14 @@ func OrderEntitiesToResponses(orders []*entities.Order) []models.OrderResponse {
return responses
}
func OrderItemEntitiesToResponses(items []*entities.OrderItem) []models.OrderItemResponse {
func OrderItemEntitiesToResponses(items []*entities.OrderItem, outletID uuid.UUID) []models.OrderItemResponse {
if items == nil {
return nil
}
responses := make([]models.OrderItemResponse, len(items))
for i, item := range items {
response := OrderItemEntityToResponse(item)
response := OrderItemEntityToResponse(item, outletID)
if response != nil {
responses[i] = *response
}

View File

@ -45,7 +45,7 @@ func TestOrderItemEntityToResponse_WithProductNames(t *testing.T) {
}
// Act
result := OrderItemEntityToResponse(orderItem)
result := OrderItemEntityToResponse(orderItem, uuid.Nil)
// Assert
assert.NotNil(t, result)
@ -89,7 +89,7 @@ func TestOrderItemEntityToResponse_WithoutProductVariant(t *testing.T) {
}
// Act
result := OrderItemEntityToResponse(orderItem)
result := OrderItemEntityToResponse(orderItem, uuid.Nil)
// Assert
assert.NotNil(t, result)
@ -129,7 +129,7 @@ func TestOrderItemEntityToResponse_WithoutProductPreload(t *testing.T) {
}
// Act
result := OrderItemEntityToResponse(orderItem)
result := OrderItemEntityToResponse(orderItem, uuid.Nil)
// Assert
assert.NotNil(t, result)

View File

@ -209,6 +209,7 @@ type OrderItemResponse struct {
CreatedAt time.Time
UpdatedAt time.Time
PrinterType string
PrintToChecker bool
PaidQuantity int
}

View File

@ -387,7 +387,7 @@ func (p *OrderProcessorImpl) AddToOrder(ctx context.Context, orderID uuid.UUID,
return nil, fmt.Errorf("failed to create order item: %w", err)
}
itemResponse := mappers.OrderItemEntityToResponse(orderItem)
itemResponse := mappers.OrderItemEntityToResponse(orderItem, order.OutletID)
if itemResponse != nil {
addedItemResponses = append(addedItemResponses, *itemResponse)
}

View File

@ -61,6 +61,7 @@ func (r *OrderRepositoryImpl) GetWithRelations(ctx context.Context, id uuid.UUID
Preload("OrderItems").
Preload("OrderItems.Product").
Preload("OrderItems.Product.Category").
Preload("OrderItems.Product.ProductOutletPrices").
Preload("OrderItems.ProductVariant").
Preload("Payments").
Preload("Payments.PaymentMethod").
@ -141,6 +142,7 @@ func (r *OrderRepositoryImpl) List(ctx context.Context, filters map[string]inter
Preload("OrderItems").
Preload("OrderItems.Product").
Preload("OrderItems.Product.Category").
Preload("OrderItems.Product.ProductOutletPrices").
Preload("OrderItems.ProductVariant").
Preload("Payments").
Preload("Payments.PaymentMethod").
@ -158,6 +160,7 @@ func (r *OrderRepositoryImpl) ListBySessionID(ctx context.Context, sessionID str
Preload("OrderItems").
Preload("OrderItems.Product").
Preload("OrderItems.Product.Category").
Preload("OrderItems.Product.ProductOutletPrices").
Preload("OrderItems.ProductVariant").
Preload("Payments").
Preload("Payments.PaymentMethod").

View File

@ -112,6 +112,7 @@ func OrderModelToContract(resp *models.OrderResponse) *contract.OrderResponse {
CreatedAt: item.CreatedAt,
UpdatedAt: item.UpdatedAt,
PrinterType: item.PrinterType,
PrintToChecker: item.PrintToChecker,
PaidQuantity: item.PaidQuantity,
}
}
@ -181,6 +182,7 @@ func AddToOrderModelToContract(resp *models.AddToOrderResponse) *contract.AddToO
Status: string(item.Status),
CreatedAt: item.CreatedAt,
UpdatedAt: item.UpdatedAt,
PrintToChecker: item.PrintToChecker,
}
}
return &contract.AddToOrderResponse{