github.com/mundipagg/boleto-api@v0.0.0-20230620145841-3f9ec742599f/api/validator.go (about)

     1  package api
     2  
     3  import (
     4  	"github.com/gin-gonic/gin"
     5  	"github.com/mundipagg/boleto-api/models"
     6  )
     7  
     8  //validateRegisterV1 Middleware de validação das requisições de registro de boleto na rota V1
     9  func validateRegisterV1(c *gin.Context) {
    10  	rules := getBoletoFromContext(c).Title.Rules
    11  	bn := getBankFromContext(c).GetBankNumber()
    12  
    13  	errorResponse := models.BoletoResponse{
    14  		Errors: models.NewErrors(),
    15  	}
    16  
    17  	if rules != nil {
    18  		errorResponse.Errors.Append("MP400", "title.rules not available in this version")
    19  		c.AbortWithStatusJSON(400, errorResponse)
    20  		return
    21  	}
    22  
    23  	if bn == models.Stone {
    24  		errorResponse.Errors.Append("MP400", "bank Stone not available in this version")
    25  		c.AbortWithStatusJSON(400, errorResponse)
    26  		return
    27  	}
    28  }
    29  
    30  //validateRegisterV2 Middleware de validação das requisições de registro de boleto na rota V2
    31  func validateRegisterV2(c *gin.Context) {
    32  	t := getBoletoFromContext(c).Title
    33  	bn := getBankFromContext(c).GetBankNumber()
    34  
    35  	errorResponse := models.BoletoResponse{
    36  		Errors: models.NewErrors(),
    37  	}
    38  
    39  	if t.HasFees() && !isBankNumberAcceptFees(bn) {
    40  		errorResponse.Errors.Append("MP400", "title.fees not available for this bank")
    41  		c.AbortWithStatusJSON(400, errorResponse)
    42  		return
    43  	}
    44  
    45  	if t.HasRules() && !isBankNumberAcceptRules(bn) {
    46  		errorResponse.Errors.Append("MP400", "title.rules not available for this bank")
    47  		c.AbortWithStatusJSON(400, errorResponse)
    48  		return
    49  	}
    50  }
    51  
    52  func isBankNumberAcceptFees(b models.BankNumber) bool {
    53  	return b == models.Caixa || b == models.Stone
    54  }
    55  
    56  func isBankNumberAcceptRules(b models.BankNumber) bool {
    57  	return b == models.Caixa || b == models.Stone
    58  }