Add callback and update user role
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
package mdtrns
|
||||
|
||||
import (
|
||||
"furtuna-be/internal/handlers/request"
|
||||
"furtuna-be/internal/handlers/response"
|
||||
"furtuna-be/internal/services"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
service services.Order
|
||||
}
|
||||
|
||||
func (h *Handler) Route(group *gin.RouterGroup, jwt gin.HandlerFunc) {
|
||||
route := group.Group("/midtrans")
|
||||
|
||||
route.POST("/callback", h.Callback)
|
||||
}
|
||||
|
||||
func NewHandler(service services.Order) *Handler {
|
||||
return &Handler{
|
||||
service: service,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) Callback(c *gin.Context) {
|
||||
var callbackData request.MidtransCallbackRequest
|
||||
if err := c.ShouldBindJSON(&callbackData); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
validStatuses := []string{"settlement", "expire", "deny", "cancel", "capture", "failure"}
|
||||
|
||||
isValidStatus := false
|
||||
for _, status := range validStatuses {
|
||||
if callbackData.TransactionStatus == status {
|
||||
isValidStatus = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !isValidStatus {
|
||||
c.JSON(http.StatusOK, response.BaseResponse{
|
||||
Success: true,
|
||||
Status: http.StatusOK,
|
||||
Message: "",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
err := h.service.ProcessCallback(c, callbackData.ToEntity())
|
||||
|
||||
if err != nil {
|
||||
c.JSON(http.StatusUnauthorized, response.BaseResponse{
|
||||
Success: false,
|
||||
Status: http.StatusBadRequest,
|
||||
Message: err.Error(),
|
||||
Data: nil,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response.BaseResponse{
|
||||
Success: true,
|
||||
Status: http.StatusOK,
|
||||
Message: "order",
|
||||
})
|
||||
|
||||
}
|
||||
@@ -108,6 +108,7 @@ func MapOrderToCreateOrderResponse(orderResponse *entity.OrderResponse) response
|
||||
ItemID: item.ItemID,
|
||||
Quantity: item.Quantity,
|
||||
Price: item.Price,
|
||||
Name: item.Product.Name,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -133,6 +134,7 @@ func MapOrderToExecuteOrderResponse(orderResponse *entity.ExecuteOrderResponse)
|
||||
ItemID: item.ItemID,
|
||||
Quantity: item.Quantity,
|
||||
Price: item.Price,
|
||||
Name: item.Product.Name,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ func (h *Handler) Route(group *gin.RouterGroup, jwt gin.HandlerFunc) {
|
||||
route := group.Group("/product")
|
||||
|
||||
route.POST("/", jwt, h.Create)
|
||||
route.GET("/pos", jwt, h.GetPOSProduct)
|
||||
route.GET("/list", jwt, h.GetAll)
|
||||
route.PUT("/:id", jwt, h.Update)
|
||||
route.GET("/:id", jwt, h.GetByID)
|
||||
@@ -157,6 +158,37 @@ func (h *Handler) GetAll(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
func (h *Handler) GetPOSProduct(c *gin.Context) {
|
||||
ctx := request.GetMyContext(c)
|
||||
|
||||
var req request.ProductParam
|
||||
if err := c.ShouldBindQuery(&req); err != nil {
|
||||
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if !ctx.IsCasheer() {
|
||||
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
products, err := h.service.GetProductPOS(c.Request.Context(), entity.ProductPOS{
|
||||
PartnerID: *ctx.GetPartnerID(),
|
||||
SiteID: *ctx.GetSiteID(),
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
response.ErrorWrapper(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response.BaseResponse{
|
||||
Success: true,
|
||||
Status: http.StatusOK,
|
||||
Data: h.toProductResponseList(products, int64(len(products)), req),
|
||||
})
|
||||
}
|
||||
|
||||
// Delete handles the deletion of a product by ID.
|
||||
// @Summary Delete a product by ID
|
||||
// @Description Delete a product based on the provided ID.
|
||||
@@ -240,14 +272,18 @@ func (h *Handler) GetByID(c *gin.Context) {
|
||||
|
||||
func (h *Handler) toProductResponse(resp *entity.Product) response.Product {
|
||||
return response.Product{
|
||||
ID: resp.ID,
|
||||
Name: resp.Name,
|
||||
Type: resp.Type,
|
||||
Price: resp.Price,
|
||||
Status: resp.Status,
|
||||
Description: resp.Description,
|
||||
CreatedAt: resp.CreatedAt.Format(time.RFC3339),
|
||||
UpdatedAt: resp.CreatedAt.Format(time.RFC3339),
|
||||
ID: resp.ID,
|
||||
Name: resp.Name,
|
||||
Type: resp.Type,
|
||||
Price: resp.Price,
|
||||
Status: resp.Status,
|
||||
Description: resp.Description,
|
||||
CreatedAt: resp.CreatedAt.Format(time.RFC3339),
|
||||
UpdatedAt: resp.CreatedAt.Format(time.RFC3339),
|
||||
PartnerID: resp.PartnerID,
|
||||
SiteID: resp.SiteID,
|
||||
IsSeasonTicket: resp.IsSeasonTicket,
|
||||
IsWeekendTicket: resp.IsWeekendTicket,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"furtuna-be/internal/constants/role"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
@@ -64,6 +65,11 @@ func (h *Handler) Create(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
if req.RoleID == role.Casheer && req.SiteID == nil {
|
||||
response.ErrorWrapper(c, errors.NewServiceException("site id is required for cashier"))
|
||||
return
|
||||
}
|
||||
|
||||
res, err := h.service.Create(ctx, req.ToEntity())
|
||||
if err != nil {
|
||||
response.ErrorWrapper(c, err)
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package request
|
||||
|
||||
import "furtuna-be/internal/entity"
|
||||
|
||||
type MidtransCallbackRequest struct {
|
||||
VANumbers []VANumber `json:"va_numbers"`
|
||||
TransactionTime string `json:"transaction_time"`
|
||||
TransactionStatus string `json:"transaction_status"`
|
||||
TransactionID string `json:"transaction_id"`
|
||||
StatusMessage string `json:"status_message"`
|
||||
StatusCode string `json:"status_code"`
|
||||
SignatureKey string `json:"signature_key"`
|
||||
SettlementTime string `json:"settlement_time"`
|
||||
PaymentType string `json:"payment_type"`
|
||||
OrderID string `json:"order_id"`
|
||||
MerchantID string `json:"merchant_id"`
|
||||
GrossAmount string `json:"gross_amount"`
|
||||
FraudStatus string `json:"fraud_status"`
|
||||
ExpiryTime string `json:"expiry_time"`
|
||||
Currency string `json:"currency"`
|
||||
}
|
||||
|
||||
type VANumber struct {
|
||||
VANumber string `json:"va_number"`
|
||||
Bank string `json:"bank"`
|
||||
}
|
||||
|
||||
type MidtransCallbackBank struct {
|
||||
Bank string `json:"bank"`
|
||||
VaNumber string `json:"va_number"`
|
||||
BillerCode string `json:"biller_code"`
|
||||
}
|
||||
|
||||
func (m *MidtransCallbackRequest) ToEntity() *entity.CallbackRequest {
|
||||
return &entity.CallbackRequest{
|
||||
TransactionID: m.OrderID,
|
||||
TransactionStatus: m.TransactionStatus,
|
||||
}
|
||||
}
|
||||
@@ -8,14 +8,15 @@ import (
|
||||
)
|
||||
|
||||
type User struct {
|
||||
Name string `json:"name" validate:"required"`
|
||||
Email string `json:"email" validate:"required"`
|
||||
Password string `json:"password" validate:"required"`
|
||||
PartnerID *int64 `json:"partner_id"`
|
||||
RoleID int64 `json:"role_id" validate:"required"`
|
||||
NIK string `json:"nik"`
|
||||
UserType string `json:"user_type"`
|
||||
PhoneNumber string `json:"phone_number"`
|
||||
Name string `json:"name" validate:"required"`
|
||||
Email string `json:"email" validate:"required"`
|
||||
Password string `json:"password" validate:"required"`
|
||||
PartnerID *int64 `json:"partner_id"`
|
||||
SiteID *int64 `json:"site_id"`
|
||||
RoleID role.Role `json:"role_id" validate:"required"`
|
||||
NIK string `json:"nik"`
|
||||
UserType string `json:"user_type"`
|
||||
PhoneNumber string `json:"phone_number"`
|
||||
}
|
||||
|
||||
func (e *User) Validate() error {
|
||||
@@ -34,6 +35,7 @@ func (u *User) ToEntity() *entity.User {
|
||||
Password: u.Password,
|
||||
RoleID: role.Role(u.RoleID),
|
||||
PartnerID: u.PartnerID,
|
||||
SiteID: u.SiteID,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -82,4 +82,5 @@ type CreateOrderItemResponse struct {
|
||||
ItemID int64 `json:"item_id"`
|
||||
Quantity int64 `json:"quantity"`
|
||||
Price float64 `json:"price"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user