Compare commits

..
5 changed files with 26 additions and 48 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
# 1) Build stage # 1) Build stage
FROM golang:1.21-alpine AS build FROM golang:1.24-alpine AS build
RUN apk --no-cache add ca-certificates tzdata git curl RUN apk --no-cache add ca-certificates tzdata git curl
WORKDIR /src WORKDIR /src
COPY go.mod go.sum ./ COPY go.mod go.sum ./
-1
View File
@@ -140,7 +140,6 @@ func (h *OrderHandler) ListOrders(c *gin.Context) {
if modelReq.OutletID == nil && contextInfo.OutletID != uuid.Nil { if modelReq.OutletID == nil && contextInfo.OutletID != uuid.Nil {
modelReq.OutletID = &contextInfo.OutletID modelReq.OutletID = &contextInfo.OutletID
} }
response, err := h.orderService.ListOrders(c.Request.Context(), modelReq) response, err := h.orderService.ListOrders(c.Request.Context(), modelReq)
if err != nil { if err != nil {
util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{contract.NewResponseError("internal_error", "OrderHandler::ListOrders", err.Error())}), "OrderHandler::ListOrders") util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{contract.NewResponseError("internal_error", "OrderHandler::ListOrders", err.Error())}), "OrderHandler::ListOrders")
+2 -7
View File
@@ -11,7 +11,6 @@ import (
"apskel-pos-be/internal/service" "apskel-pos-be/internal/service"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/google/uuid"
) )
type AuthMiddleware struct { type AuthMiddleware struct {
@@ -46,13 +45,9 @@ func (m *AuthMiddleware) RequireAuth() gin.HandlerFunc {
setKeyInContext(c, appcontext.OrganizationIDKey, userResponse.OrganizationID.String()) setKeyInContext(c, appcontext.OrganizationIDKey, userResponse.OrganizationID.String())
setKeyInContext(c, appcontext.UserIDKey, userResponse.ID.String()) setKeyInContext(c, appcontext.UserIDKey, userResponse.ID.String())
// Always override OutletID from token to prevent header injection. if userResponse.Role != "superadmin" {
// Set empty string if user has no outlet, so PopulateContext header value is ignored. setKeyInContext(c, appcontext.OutletIDKey, userResponse.OutletID.String())
outletIDStr := ""
if userResponse.OutletID != nil && *userResponse.OutletID != uuid.Nil {
outletIDStr = userResponse.OutletID.String()
} }
setKeyInContext(c, appcontext.OutletIDKey, outletIDStr)
logger.FromContext(c.Request.Context()).Infof("AuthMiddleware::RequireAuth -> User authenticated: %s", userResponse.Email) logger.FromContext(c.Request.Context()).Infof("AuthMiddleware::RequireAuth -> User authenticated: %s", userResponse.Email)
c.Next() c.Next()
+22 -38
View File
@@ -98,42 +98,7 @@ func (r *OrderRepositoryImpl) List(ctx context.Context, filters map[string]inter
var orders []*entities.Order var orders []*entities.Order
var total int64 var total int64
// organization_id is mandatory to prevent cross-org data leaks query := r.db.WithContext(ctx).Model(&entities.Order{}).
organizationID, ok := filters["organization_id"]
if !ok {
return nil, 0, fmt.Errorf("organization_id is required for listing orders")
}
baseQuery := r.db.WithContext(ctx).Model(&entities.Order{}).
Where("organization_id = ?", organizationID)
// outlet_id is optional — if present, scope to that outlet; otherwise return all outlets in the org
if outletID, exists := filters["outlet_id"]; exists {
baseQuery = baseQuery.Where("outlet_id = ?", outletID)
}
for key, value := range filters {
switch key {
case "organization_id", "outlet_id":
// already handled above
case "search":
searchValue := "%" + value.(string) + "%"
baseQuery = baseQuery.Where("order_number ILIKE ?", searchValue)
case "date_from":
baseQuery = baseQuery.Where("created_at >= ?", value)
case "date_to":
baseQuery = baseQuery.Where("created_at <= ?", value)
default:
baseQuery = baseQuery.Where(key+" = ?", value)
}
}
// Use separate queries for count and find to avoid GORM state mutation issues
if err := baseQuery.Count(&total).Error; err != nil {
return nil, 0, err
}
err := baseQuery.
Preload("Organization"). Preload("Organization").
Preload("Outlet"). Preload("Outlet").
Preload("User"). Preload("User").
@@ -142,8 +107,27 @@ func (r *OrderRepositoryImpl) List(ctx context.Context, filters map[string]inter
Preload("OrderItems.ProductVariant"). Preload("OrderItems.ProductVariant").
Preload("Payments"). Preload("Payments").
Preload("Payments.PaymentMethod"). Preload("Payments.PaymentMethod").
Preload("Payments.PaymentOrderItems"). Preload("Payments.PaymentOrderItems")
Limit(limit).Offset(offset).Order("created_at DESC").Find(&orders).Error
for key, value := range filters {
switch key {
case "search":
searchValue := "%" + value.(string) + "%"
query = query.Where("order_number ILIKE ?", searchValue)
case "date_from":
query = query.Where("created_at >= ?", value)
case "date_to":
query = query.Where("created_at <= ?", value)
default:
query = query.Where(key+" = ?", value)
}
}
if err := query.Count(&total).Error; err != nil {
return nil, 0, err
}
err := query.Limit(limit).Offset(offset).Order("created_at DESC").Find(&orders).Error
return orders, total, err return orders, total, err
} }
@@ -105,7 +105,7 @@ func (r *OrganizationRepositoryImpl) GetTotalOmset(ctx context.Context, organiza
var total float64 var total float64
err := r.db.WithContext(ctx). err := r.db.WithContext(ctx).
Table("orders"). Table("orders").
Where("organization_id = ? AND payment_status = ? AND is_void = ? AND is_refund = ?", organizationID, "completed", false, false). Where("organization_id = ? AND payment_status = ?", organizationID, "completed").
Select("COALESCE(SUM(total_amount), 0)"). Select("COALESCE(SUM(total_amount), 0)").
Scan(&total).Error Scan(&total).Error
return total, err return total, err