169 lines
3.8 KiB
Go
169 lines
3.8 KiB
Go
package http
|
|
|
|
import (
|
|
"enaklo-pos-be/internal/common/errors"
|
|
"enaklo-pos-be/internal/entity"
|
|
"enaklo-pos-be/internal/handlers/request"
|
|
"enaklo-pos-be/internal/handlers/response"
|
|
"enaklo-pos-be/internal/services/v2/order"
|
|
"github.com/gin-gonic/gin"
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
type CustomerOrderHandler struct {
|
|
service order.Service
|
|
}
|
|
|
|
func NewCustomerOrderHandler(service order.Service) *CustomerOrderHandler {
|
|
return &CustomerOrderHandler{
|
|
service: service,
|
|
}
|
|
}
|
|
|
|
func (h *CustomerOrderHandler) Route(group *gin.RouterGroup, jwt gin.HandlerFunc) {
|
|
route := group.Group("/order")
|
|
|
|
route.GET("/history", jwt, h.GetOrderHistory)
|
|
route.GET("/detail/:id", jwt, h.GetOrderID)
|
|
|
|
}
|
|
|
|
func (h *CustomerOrderHandler) GetOrderHistory(c *gin.Context) {
|
|
ctx := request.GetMyContext(c)
|
|
userID := ctx.RequestedBy()
|
|
|
|
limitStr := c.Query("limit")
|
|
offsetStr := c.Query("offset")
|
|
status := c.Query("status")
|
|
startDateStr := c.Query("start_date")
|
|
endDateStr := c.Query("end_date")
|
|
|
|
searchReq := entity.SearchRequest{}
|
|
|
|
if status != "" {
|
|
searchReq.Status = status
|
|
}
|
|
|
|
limit := 10
|
|
if limitStr != "" {
|
|
parsedLimit, err := strconv.Atoi(limitStr)
|
|
if err == nil && parsedLimit > 0 {
|
|
limit = parsedLimit
|
|
}
|
|
}
|
|
|
|
if limit > 20 {
|
|
limit = 20
|
|
}
|
|
|
|
searchReq.Limit = limit
|
|
|
|
offset := 0
|
|
if offsetStr != "" {
|
|
parsedOffset, err := strconv.Atoi(offsetStr)
|
|
if err == nil && parsedOffset >= 0 {
|
|
offset = parsedOffset
|
|
}
|
|
}
|
|
searchReq.Offset = offset
|
|
|
|
if startDateStr != "" {
|
|
startDate, err := time.Parse(time.RFC3339, startDateStr)
|
|
if err == nil {
|
|
searchReq.Start = startDate
|
|
}
|
|
}
|
|
|
|
if endDateStr != "" {
|
|
endDate, err := time.Parse(time.RFC3339, endDateStr)
|
|
if err == nil {
|
|
searchReq.End = endDate
|
|
}
|
|
}
|
|
|
|
orders, total, err := h.service.GetCustomerOrderHistory(ctx, userID, searchReq)
|
|
if err != nil {
|
|
response.ErrorWrapper(c, err)
|
|
return
|
|
}
|
|
|
|
responseData := []response.OrderHistoryResponse{}
|
|
for _, order := range orders {
|
|
var orderItems []response.OrderItemResponse
|
|
for _, item := range order.OrderItems {
|
|
orderItems = append(orderItems, response.OrderItemResponse{
|
|
ProductID: item.ItemID,
|
|
ProductName: item.ItemName,
|
|
Price: item.Price,
|
|
Quantity: item.Quantity,
|
|
Subtotal: item.Price * float64(item.Quantity),
|
|
})
|
|
}
|
|
|
|
responseData = append(responseData, response.OrderHistoryResponse{
|
|
ID: order.ID,
|
|
CustomerName: order.CustomerName,
|
|
Status: order.Status,
|
|
Amount: order.Amount,
|
|
Total: order.Total,
|
|
PaymentType: h.formatPayment(order.PaymentType, order.PaymentProvider),
|
|
TableNumber: order.TableNumber,
|
|
OrderType: order.OrderType,
|
|
OrderItems: orderItems,
|
|
CreatedAt: order.CreatedAt.Format("2006-01-02T15:04:05Z"),
|
|
Tax: order.Tax,
|
|
RestaurantName: "Bakso 343",
|
|
})
|
|
}
|
|
|
|
c.JSON(http.StatusOK, response.BaseResponse{
|
|
Success: true,
|
|
Status: http.StatusOK,
|
|
Data: responseData,
|
|
PagingMeta: &response.PagingMeta{
|
|
Page: offset + 1,
|
|
Total: int64(total),
|
|
Limit: limit,
|
|
},
|
|
})
|
|
}
|
|
|
|
func (h *CustomerOrderHandler) formatPayment(payment, provider string) string {
|
|
if payment == "CASH" {
|
|
return payment
|
|
}
|
|
|
|
return payment + " " + provider
|
|
}
|
|
|
|
func (h *CustomerOrderHandler) GetOrderID(c *gin.Context) {
|
|
ctx := request.GetMyContext(c)
|
|
|
|
var req GetOrderParam
|
|
if err := c.ShouldBindQuery(&req); err != nil {
|
|
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
|
return
|
|
}
|
|
|
|
id := c.Param("id")
|
|
orderID, err := strconv.ParseInt(id, 10, 64)
|
|
if err != nil {
|
|
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
|
return
|
|
}
|
|
|
|
order, err := h.service.GetOrderByOrderAndCustomerID(ctx, ctx.RequestedBy(), orderID)
|
|
if err != nil {
|
|
response.ErrorWrapper(c, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, response.BaseResponse{
|
|
Success: true,
|
|
Status: http.StatusOK,
|
|
Data: MapToOrderCreateResponse(order),
|
|
})
|
|
}
|