130 lines
3.0 KiB
Go
130 lines
3.0 KiB
Go
package balance
|
|
|
|
import (
|
|
"furtuna-be/internal/common/errors"
|
|
"furtuna-be/internal/entity"
|
|
"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.Balance
|
|
}
|
|
|
|
func (h *Handler) Route(group *gin.RouterGroup, jwt gin.HandlerFunc) {
|
|
route := group.Group("/balance")
|
|
|
|
route.GET("/partner", jwt, h.GetPartnerBalance)
|
|
route.POST("/withdraw/inquiry", jwt, h.WithdrawBalanceInquiry)
|
|
route.POST("/withdraw/execute", jwt, h.WithdrawBalanceExecute)
|
|
}
|
|
|
|
func NewHandler(service services.Balance) *Handler {
|
|
return &Handler{
|
|
service: service,
|
|
}
|
|
}
|
|
|
|
func (h *Handler) GetPartnerBalance(c *gin.Context) {
|
|
ctx := request.GetMyContext(c)
|
|
|
|
if !ctx.IsPartnerAdmin() {
|
|
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
|
return
|
|
}
|
|
|
|
updatedBranch, err := h.service.GetByID(ctx, *ctx.GetPartnerID())
|
|
if err != nil {
|
|
response.ErrorWrapper(c, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, response.BaseResponse{
|
|
Success: true,
|
|
Status: http.StatusOK,
|
|
Data: h.toBalanceResponse(updatedBranch),
|
|
})
|
|
}
|
|
|
|
func (h *Handler) WithdrawBalanceInquiry(c *gin.Context) {
|
|
var req request.BalanceReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
|
return
|
|
}
|
|
|
|
ctx := request.GetMyContext(c)
|
|
|
|
if !ctx.IsPartnerAdmin() {
|
|
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
|
return
|
|
}
|
|
|
|
inquiryResp, err := h.service.WithdrawInquiry(ctx, req.ToEntity(*ctx.GetPartnerID()))
|
|
if err != nil {
|
|
response.ErrorWrapper(c, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, response.BaseResponse{
|
|
Success: true,
|
|
Status: http.StatusOK,
|
|
Data: h.toBalanceInquiryResp(inquiryResp),
|
|
})
|
|
}
|
|
|
|
func (h *Handler) WithdrawBalanceExecute(c *gin.Context) {
|
|
var req request.BalanceReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
|
return
|
|
}
|
|
|
|
ctx := request.GetMyContext(c)
|
|
|
|
if !ctx.IsPartnerAdmin() {
|
|
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
|
return
|
|
}
|
|
|
|
inquiryResp, err := h.service.WithdrawExecute(ctx, req.ToEntityReq(*ctx.GetPartnerID()))
|
|
if err != nil {
|
|
response.ErrorWrapper(c, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, response.BaseResponse{
|
|
Success: true,
|
|
Status: http.StatusOK,
|
|
Data: h.toBalanceExecuteResp(inquiryResp),
|
|
})
|
|
}
|
|
|
|
func (h *Handler) toBalanceResponse(resp *entity.Balance) response.Balance {
|
|
return response.Balance{
|
|
PartnerID: resp.PartnerID,
|
|
Balance: resp.Balance,
|
|
AuthBalance: resp.AuthBalance,
|
|
}
|
|
}
|
|
|
|
func (h *Handler) toBalanceInquiryResp(resp *entity.BalanceWithdrawInquiryResponse) response.BalanceInquiryResponse {
|
|
return response.BalanceInquiryResponse{
|
|
PartnerID: resp.PartnerID,
|
|
Amount: resp.Amount,
|
|
Token: resp.Token,
|
|
Total: resp.Total,
|
|
Fee: resp.Fee,
|
|
}
|
|
}
|
|
|
|
func (h *Handler) toBalanceExecuteResp(resp *entity.WalletWithdrawResponse) response.BalanceExecuteResponse {
|
|
return response.BalanceExecuteResponse{
|
|
TransactionID: resp.TransactionID,
|
|
Status: resp.Status,
|
|
}
|
|
}
|