fix
This commit is contained in:
@@ -3,6 +3,7 @@ package validator
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"apskel-pos-be/internal/constants"
|
||||
"apskel-pos-be/internal/contract"
|
||||
@@ -37,15 +38,26 @@ func (v *PurchaseOrderValidatorImpl) ValidateCreatePurchaseOrderRequest(req *con
|
||||
return errors.New("po_number must be between 1 and 50 characters"), constants.MalformedFieldErrorCode
|
||||
}
|
||||
|
||||
if req.TransactionDate.IsZero() {
|
||||
// Validate transaction date
|
||||
if strings.TrimSpace(req.TransactionDate) == "" {
|
||||
return errors.New("transaction_date is required"), constants.MissingFieldErrorCode
|
||||
}
|
||||
|
||||
if req.DueDate.IsZero() {
|
||||
return errors.New("due_date is required"), constants.MissingFieldErrorCode
|
||||
transactionDate, err := time.Parse("2006-01-02", req.TransactionDate)
|
||||
if err != nil {
|
||||
return errors.New("transaction_date must be in YYYY-MM-DD format"), constants.MalformedFieldErrorCode
|
||||
}
|
||||
|
||||
if req.DueDate.Before(req.TransactionDate) {
|
||||
// Validate due date
|
||||
if strings.TrimSpace(req.DueDate) == "" {
|
||||
return errors.New("due_date is required"), constants.MissingFieldErrorCode
|
||||
}
|
||||
dueDate, err := time.Parse("2006-01-02", req.DueDate)
|
||||
if err != nil {
|
||||
return errors.New("due_date must be in YYYY-MM-DD format"), constants.MalformedFieldErrorCode
|
||||
}
|
||||
|
||||
// Check if due date is after transaction date
|
||||
if dueDate.Before(transactionDate) {
|
||||
return errors.New("due_date must be after transaction_date"), constants.MalformedFieldErrorCode
|
||||
}
|
||||
|
||||
@@ -88,9 +100,22 @@ func (v *PurchaseOrderValidatorImpl) ValidateUpdatePurchaseOrderRequest(req *con
|
||||
}
|
||||
}
|
||||
|
||||
// Validate dates if both are provided
|
||||
if req.TransactionDate != nil && req.DueDate != nil {
|
||||
if req.DueDate.Before(*req.TransactionDate) {
|
||||
return errors.New("due_date must be after transaction_date"), constants.MalformedFieldErrorCode
|
||||
if *req.TransactionDate != "" && *req.DueDate != "" {
|
||||
transactionDate, err := time.Parse("2006-01-02", *req.TransactionDate)
|
||||
if err != nil {
|
||||
return errors.New("transaction_date must be in YYYY-MM-DD format"), constants.MalformedFieldErrorCode
|
||||
}
|
||||
|
||||
dueDate, err := time.Parse("2006-01-02", *req.DueDate)
|
||||
if err != nil {
|
||||
return errors.New("due_date must be in YYYY-MM-DD format"), constants.MalformedFieldErrorCode
|
||||
}
|
||||
|
||||
if dueDate.Before(transactionDate) {
|
||||
return errors.New("due_date must be after transaction_date"), constants.MalformedFieldErrorCode
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user