Adds the system payment method for paying with EnakPoint (docs/prd-point-coin.md F9, §8, §10.5, PC-303). Migration 000094 allows the point type, keeps one per organization with a partial unique index, creates it for every existing organization, and adds a trigger that creates it for new ones, as the walk-in customer is. It adds payments.points_used and point_value. Their CHECK is written so it can never be NULL: the PRD form, (both NULL) OR (both > 0), is NULL for points_used with a NULL point_value, which a CHECK lets through, so a payment could have lost the value a refund depends on. A test caught it. The API cannot create, delete or retype the EnakPoint method, nor turn another method into one; that answers 400. Renaming it is allowed. The method list takes the outlet from ?outlet_id= or the user's outlet and leaves EnakPoint out when that outlet does not accept it, filtered in the query so the count stays right. The organization-wide active list is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
109 lines
3.1 KiB
Go
109 lines
3.1 KiB
Go
package validator
|
|
|
|
import (
|
|
"apskel-pos-be/internal/constants"
|
|
"apskel-pos-be/internal/contract"
|
|
|
|
"github.com/go-playground/validator/v10"
|
|
)
|
|
|
|
type PaymentMethodValidator interface {
|
|
ValidateCreatePaymentMethodRequest(req *contract.CreatePaymentMethodRequest) (error, string)
|
|
ValidateUpdatePaymentMethodRequest(req *contract.UpdatePaymentMethodRequest) (error, string)
|
|
ValidateListPaymentMethodsRequest(req *contract.ListPaymentMethodsRequest) (error, string)
|
|
}
|
|
|
|
type PaymentMethodValidatorImpl struct {
|
|
validate *validator.Validate
|
|
}
|
|
|
|
func NewPaymentMethodValidator() PaymentMethodValidator {
|
|
return &PaymentMethodValidatorImpl{
|
|
validate: validator.New(),
|
|
}
|
|
}
|
|
|
|
func (v *PaymentMethodValidatorImpl) ValidateCreatePaymentMethodRequest(req *contract.CreatePaymentMethodRequest) (error, string) {
|
|
if err := v.validate.Struct(req); err != nil {
|
|
return err, constants.ValidationErrorCode
|
|
}
|
|
|
|
if req.Name == "" {
|
|
return constants.ErrPaymentMethodNameRequired, constants.MissingFieldErrorCode
|
|
}
|
|
|
|
if req.Type == "" {
|
|
return constants.ErrPaymentMethodTypeRequired, constants.MissingFieldErrorCode
|
|
}
|
|
|
|
// Validate payment method type
|
|
if !v.isValidPaymentMethodType(req.Type) {
|
|
return constants.ErrInvalidPaymentMethodType, constants.InvalidFieldErrorCode
|
|
}
|
|
|
|
return nil, ""
|
|
}
|
|
|
|
func (v *PaymentMethodValidatorImpl) ValidateUpdatePaymentMethodRequest(req *contract.UpdatePaymentMethodRequest) (error, string) {
|
|
if err := v.validate.Struct(req); err != nil {
|
|
return err, constants.ValidationErrorCode
|
|
}
|
|
|
|
// Additional business logic validation
|
|
if req.Name != nil && *req.Name == "" {
|
|
return constants.ErrPaymentMethodNameRequired, constants.MissingFieldErrorCode
|
|
}
|
|
|
|
if req.Type != nil && *req.Type == "" {
|
|
return constants.ErrPaymentMethodTypeRequired, constants.MissingFieldErrorCode
|
|
}
|
|
|
|
// Validate payment method type if provided
|
|
if req.Type != nil && !v.isValidPaymentMethodType(*req.Type) {
|
|
return constants.ErrInvalidPaymentMethodType, constants.InvalidFieldErrorCode
|
|
}
|
|
|
|
return nil, ""
|
|
}
|
|
|
|
func (v *PaymentMethodValidatorImpl) ValidateListPaymentMethodsRequest(req *contract.ListPaymentMethodsRequest) (error, string) {
|
|
if err := v.validate.Struct(req); err != nil {
|
|
return err, constants.ValidationErrorCode
|
|
}
|
|
|
|
// Additional business logic validation
|
|
if req.Page < 1 {
|
|
return constants.ErrInvalidPageNumber, constants.InvalidFieldErrorCode
|
|
}
|
|
|
|
if req.Limit < 1 || req.Limit > 100 {
|
|
return constants.ErrInvalidLimit, constants.InvalidFieldErrorCode
|
|
}
|
|
|
|
// Validate payment method type if provided
|
|
if req.Type != nil && !v.isValidPaymentMethodType(*req.Type) {
|
|
return constants.ErrInvalidPaymentMethodType, constants.InvalidFieldErrorCode
|
|
}
|
|
|
|
return nil, ""
|
|
}
|
|
|
|
func (v *PaymentMethodValidatorImpl) isValidPaymentMethodType(paymentMethodType string) bool {
|
|
validTypes := []string{
|
|
string(constants.PaymentMethodTypeCash),
|
|
string(constants.PaymentMethodTypeCard),
|
|
string(constants.PaymentMethodTypeDigitalWallet),
|
|
string(constants.PaymentMethodTypeQR),
|
|
string(constants.PaymentMethodTypeEDC),
|
|
string(constants.PaymentMethodTypePoint),
|
|
}
|
|
|
|
for _, validType := range validTypes {
|
|
if paymentMethodType == validType {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|