update config
This commit is contained in:
@@ -2,6 +2,7 @@ package auth
|
||||
|
||||
import (
|
||||
"enaklo-pos-be/internal/constants/role"
|
||||
"enaklo-pos-be/internal/services/member"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
@@ -15,7 +16,8 @@ import (
|
||||
)
|
||||
|
||||
type AuthHandler struct {
|
||||
service services.Auth
|
||||
service services.Auth
|
||||
memberSvc member.RegistrationService
|
||||
}
|
||||
|
||||
func (a *AuthHandler) Route(group *gin.RouterGroup, jwt gin.HandlerFunc) {
|
||||
|
||||
@@ -0,0 +1,168 @@
|
||||
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),
|
||||
})
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
package customerauth
|
||||
|
||||
import (
|
||||
"enaklo-pos-be/internal/entity"
|
||||
auth2 "enaklo-pos-be/internal/handlers/request"
|
||||
"enaklo-pos-be/internal/services/member"
|
||||
"enaklo-pos-be/internal/services/v2/auth"
|
||||
"enaklo-pos-be/internal/services/v2/customer"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
@@ -11,23 +13,24 @@ import (
|
||||
|
||||
"enaklo-pos-be/internal/common/errors"
|
||||
"enaklo-pos-be/internal/handlers/response"
|
||||
"enaklo-pos-be/internal/services"
|
||||
)
|
||||
|
||||
type AuthHandler struct {
|
||||
service auth.Service
|
||||
userService services.User
|
||||
customerSvc customer.Service
|
||||
service auth.Service
|
||||
memberSvc member.RegistrationService
|
||||
}
|
||||
|
||||
func (a *AuthHandler) Route(group *gin.RouterGroup, jwt gin.HandlerFunc) {
|
||||
authRoute := group.Group("/auth")
|
||||
authRoute.POST("/login", a.AuthLogin)
|
||||
authRoute.POST("/register", a.Registration)
|
||||
authRoute.POST("/verify-otp", a.VerifyOTP)
|
||||
}
|
||||
|
||||
func NewAuthHandler(service auth.Service) *AuthHandler {
|
||||
func NewAuthHandler(service auth.Service, memberSvc member.RegistrationService) *AuthHandler {
|
||||
return &AuthHandler{
|
||||
service: service,
|
||||
service: service,
|
||||
memberSvc: memberSvc,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,3 +63,75 @@ func (h *AuthHandler) AuthLogin(c *gin.Context) {
|
||||
Data: resp,
|
||||
})
|
||||
}
|
||||
|
||||
func (h *AuthHandler) Registration(c *gin.Context) {
|
||||
ctx := auth2.GetMyContext(c)
|
||||
userID := ctx.RequestedBy()
|
||||
|
||||
var req auth2.InitiateRegistrationRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
validate := validator.New()
|
||||
if err := validate.Struct(req); err != nil {
|
||||
response.ErrorWrapper(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
birthDate, err := req.GetBirthdate()
|
||||
if err != nil {
|
||||
response.ErrorWrapper(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
memberReq := &entity.MemberRegistrationRequest{
|
||||
Name: req.Name,
|
||||
Email: req.Email,
|
||||
Phone: req.Phone,
|
||||
BirthDate: birthDate,
|
||||
CashierID: userID,
|
||||
Password: req.Password,
|
||||
}
|
||||
|
||||
result, err := h.memberSvc.InitiateRegistration(ctx, memberReq)
|
||||
if err != nil {
|
||||
response.ErrorWrapper(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response.BaseResponse{
|
||||
Success: true,
|
||||
Status: http.StatusOK,
|
||||
Data: response.MapToMemberRegistrationResponse(result),
|
||||
})
|
||||
}
|
||||
|
||||
func (h *AuthHandler) VerifyOTP(c *gin.Context) {
|
||||
ctx := auth2.GetMyContext(c)
|
||||
|
||||
var req auth2.VerifyOTPRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
validate := validator.New()
|
||||
if err := validate.Struct(req); err != nil {
|
||||
response.ErrorWrapper(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := h.memberSvc.VerifyOTP(ctx, req.Token, req.OTP)
|
||||
if err != nil {
|
||||
response.ErrorWrapper(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response.BaseResponse{
|
||||
Success: true,
|
||||
Status: http.StatusOK,
|
||||
Data: response.MapToMemberVerificationResponse(result.Auth),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -99,7 +99,7 @@ func (h *MemberHandler) VerifyOTP(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, response.BaseResponse{
|
||||
Success: true,
|
||||
Status: http.StatusOK,
|
||||
Data: response.MapToMemberVerificationResponse(result),
|
||||
Data: response.MapToMemberVerificationResponse(result.Auth),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -5,10 +5,12 @@ import (
|
||||
)
|
||||
|
||||
type InitiateRegistrationRequest struct {
|
||||
Name string `json:"name" validate:"required"`
|
||||
Email string `json:"email" validate:"required,email"`
|
||||
Phone string `json:"phone" validate:"required"`
|
||||
BirthDate string `json:"birth_date" validate:"required"`
|
||||
Name string `json:"name" validate:"required"`
|
||||
Email string `json:"email" validate:"required,email"`
|
||||
Phone string `json:"phone" validate:"required"`
|
||||
BirthDate string `json:"birthDate" validate:"required"`
|
||||
Password string `json:"password" validate:"required"`
|
||||
ConfirmPassword string `json:"confirmPassword" validate:"required"`
|
||||
}
|
||||
|
||||
func (i *InitiateRegistrationRequest) GetBirthdate() (time.Time, error) {
|
||||
|
||||
@@ -61,15 +61,15 @@ func MapToMemberRegistrationResponse(entity *entity.MemberRegistrationResponse)
|
||||
}
|
||||
}
|
||||
|
||||
func MapToMemberVerificationResponse(entity *entity.MemberVerificationResponse) MemberVerificationResponse {
|
||||
return MemberVerificationResponse{
|
||||
CustomerID: entity.CustomerID,
|
||||
Name: entity.Name,
|
||||
Email: entity.Email,
|
||||
Phone: entity.Phone,
|
||||
Points: entity.Points,
|
||||
Status: entity.Status,
|
||||
func MapToMemberVerificationResponse(user *entity.AuthenticateUser) LoginResponseCustoemr {
|
||||
resp := LoginResponseCustoemr{
|
||||
ID: user.ID,
|
||||
Token: user.Token,
|
||||
Name: user.Name,
|
||||
ResetPassword: user.ResetPassword,
|
||||
}
|
||||
|
||||
return resp
|
||||
}
|
||||
|
||||
func MapToMemberRegistrationStatus(entity *entity.MemberRegistrationStatus) MemberRegistrationStatus {
|
||||
|
||||
@@ -189,15 +189,16 @@ type OrderDetailItem struct {
|
||||
}
|
||||
|
||||
type OrderHistoryResponse struct {
|
||||
ID int64 `json:"id"`
|
||||
CustomerName string `json:"customer_name"`
|
||||
Status string `json:"status"`
|
||||
Amount float64 `json:"amount"`
|
||||
Total float64 `json:"total"`
|
||||
PaymentType string `json:"payment_type"`
|
||||
TableNumber string `json:"table_number"`
|
||||
OrderType string `json:"order_type"`
|
||||
OrderItems []OrderItemResponse `json:"order_items"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
Tax float64 `json:"tax"`
|
||||
ID int64 `json:"id"`
|
||||
CustomerName string `json:"customer_name"`
|
||||
Status string `json:"status"`
|
||||
Amount float64 `json:"amount"`
|
||||
Total float64 `json:"total"`
|
||||
PaymentType string `json:"payment_type"`
|
||||
TableNumber string `json:"table_number"`
|
||||
OrderType string `json:"order_type"`
|
||||
OrderItems []OrderItemResponse `json:"order_items"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
Tax float64 `json:"tax"`
|
||||
RestaurantName string `json:"restaurant_name"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user