124 lines
2.9 KiB
Go
124 lines
2.9 KiB
Go
package http
|
|
|
|
import (
|
|
"enaklo-pos-be/internal/common/errors"
|
|
"enaklo-pos-be/internal/handlers/request"
|
|
"enaklo-pos-be/internal/handlers/response"
|
|
"enaklo-pos-be/internal/services/v2/cashier_session"
|
|
"github.com/gin-gonic/gin"
|
|
"net/http"
|
|
"strconv"
|
|
)
|
|
|
|
type CashierSessionHandler struct {
|
|
service cashier_session.Service
|
|
}
|
|
|
|
func NewCashierSession(service cashier_session.Service) *CashierSessionHandler {
|
|
return &CashierSessionHandler{service: service}
|
|
}
|
|
|
|
func (h *CashierSessionHandler) Route(group *gin.RouterGroup, jwt gin.HandlerFunc) {
|
|
route := group.Group("/cashier-sessions")
|
|
route.Use(jwt)
|
|
|
|
route.POST("/open", h.OpenSession)
|
|
route.POST("/close/:id", h.CloseSession)
|
|
route.GET("/open", h.GetOpenSession)
|
|
route.GET("/report/:id", h.GetSessionReport)
|
|
}
|
|
|
|
func (h *CashierSessionHandler) OpenSession(c *gin.Context) {
|
|
ctx := request.GetMyContext(c)
|
|
|
|
var req request.OpenCashierSessionRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
|
return
|
|
}
|
|
|
|
session, err := h.service.OpenSession(ctx, req.ToEntity(ctx.RequestedBy()))
|
|
if err != nil {
|
|
response.ErrorWrapper(c, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, response.BaseResponse{
|
|
Success: true,
|
|
Status: http.StatusOK,
|
|
Data: response.MapToCashierSessionResponse(session),
|
|
})
|
|
}
|
|
|
|
func (h *CashierSessionHandler) CloseSession(c *gin.Context) {
|
|
ctx := request.GetMyContext(c)
|
|
idStr := c.Param("id")
|
|
sessionID, err := strconv.ParseInt(idStr, 10, 64)
|
|
if err != nil {
|
|
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
|
return
|
|
}
|
|
|
|
var body struct {
|
|
ClosingAmount float64 `json:"closing_amount"`
|
|
}
|
|
|
|
if err := c.ShouldBindJSON(&body); err != nil {
|
|
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
|
return
|
|
}
|
|
|
|
report, err := h.service.CloseSession(ctx, sessionID, body.ClosingAmount)
|
|
if err != nil {
|
|
response.ErrorWrapper(c, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, response.BaseResponse{
|
|
Success: true,
|
|
Status: http.StatusOK,
|
|
Data: response.MapToCashierSessionReport(report),
|
|
})
|
|
}
|
|
|
|
func (h *CashierSessionHandler) GetOpenSession(c *gin.Context) {
|
|
ctx := request.GetMyContext(c)
|
|
|
|
cashierID := ctx.RequestedBy()
|
|
|
|
session, err := h.service.GetOpenSession(ctx, cashierID)
|
|
if err != nil {
|
|
response.ErrorWrapper(c, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, response.BaseResponse{
|
|
Success: true,
|
|
Status: http.StatusOK,
|
|
Data: response.MapToCashierSessionResponse(session),
|
|
})
|
|
}
|
|
|
|
func (h *CashierSessionHandler) GetSessionReport(c *gin.Context) {
|
|
ctx := request.GetMyContext(c)
|
|
idStr := c.Param("id")
|
|
|
|
sessionID, err := strconv.ParseInt(idStr, 10, 64)
|
|
if err != nil {
|
|
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
|
return
|
|
}
|
|
|
|
report, err := h.service.GetSessionReport(ctx, sessionID)
|
|
if err != nil {
|
|
response.ErrorWrapper(c, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, response.BaseResponse{
|
|
Success: true,
|
|
Status: http.StatusOK,
|
|
Data: response.MapToCashierSessionReport(report),
|
|
})
|
|
}
|