Update Tranaction
This commit is contained in:
@@ -18,6 +18,7 @@ func (h *TransactionHandler) Route(group *gin.RouterGroup, jwt gin.HandlerFunc)
|
||||
route := group.Group("/transaction")
|
||||
|
||||
route.GET("/search", jwt, h.Search)
|
||||
route.POST("/approval", jwt, h.Approval)
|
||||
}
|
||||
|
||||
func New(service services.Transaction) *TransactionHandler {
|
||||
@@ -63,6 +64,36 @@ func (h *TransactionHandler) Search(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
func (h *TransactionHandler) Approval(c *gin.Context) {
|
||||
var req request.ApprovalRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
||||
return
|
||||
}
|
||||
ctx := request.GetMyContext(c)
|
||||
|
||||
if !ctx.IsAdmin() {
|
||||
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if err := req.Validate(); err != nil {
|
||||
response.ErrorWrapper(c, errors.NewError(errors.ErrorBadRequest.ErrorType(), err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
err := h.service.Approval(ctx, req.ToEntity())
|
||||
if err != nil {
|
||||
response.ErrorWrapper(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response.BaseResponse{
|
||||
Success: true,
|
||||
Status: http.StatusOK,
|
||||
})
|
||||
}
|
||||
|
||||
func (h *TransactionHandler) ToTransactionListResponse(transactions []*entity.TransactionList, totalCount, limit, offset int) response.TransactionListResponse {
|
||||
responseItems := make([]response.TransactionListItem, len(transactions))
|
||||
for i, transaction := range transactions {
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"furtuna-be/internal/common/mycontext"
|
||||
"furtuna-be/internal/constants/transaction"
|
||||
"furtuna-be/internal/entity"
|
||||
"github.com/go-playground/validator/v10"
|
||||
)
|
||||
|
||||
type Transaction struct {
|
||||
@@ -19,9 +20,41 @@ type TransactionSearch struct {
|
||||
Type string `form:"type,default="`
|
||||
}
|
||||
|
||||
type ApprovalRequest struct {
|
||||
Status string `json:"status" validate:"required,approvalStatus"`
|
||||
TransactionID string `json:"transaction_id" validate:"required"`
|
||||
}
|
||||
|
||||
func (a *ApprovalRequest) ToEntity() *entity.TransactionApproval {
|
||||
return &entity.TransactionApproval{
|
||||
Status: a.Status,
|
||||
TransactionID: a.TransactionID,
|
||||
}
|
||||
}
|
||||
|
||||
func approvalStatus(fl validator.FieldLevel) bool {
|
||||
status := fl.Field().String()
|
||||
return status == "APPROVE" || status == "REJECT"
|
||||
}
|
||||
|
||||
func (a *ApprovalRequest) Validate() error {
|
||||
validate := validator.New()
|
||||
err := validate.RegisterValidation("approvalStatus", approvalStatus)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := validate.Struct(a); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *TransactionSearch) ToEntity(ctx mycontext.Context) entity.TransactionSearch {
|
||||
return entity.TransactionSearch{
|
||||
PartnerID: ctx.GetPartnerID(),
|
||||
SiteID: ctx.GetSiteID(),
|
||||
Type: t.Type,
|
||||
Status: t.Status,
|
||||
Limit: t.Limit,
|
||||
|
||||
Reference in New Issue
Block a user