Add item_expense
This commit is contained in:
@@ -2,6 +2,7 @@ package validator
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"apskel-pos-be/internal/constants"
|
||||
@@ -39,14 +40,6 @@ func (v *ExpenseValidatorImpl) ValidateCreateExpenseRequest(req *contract.Create
|
||||
return errors.New("code_number is required"), constants.MissingFieldErrorCode
|
||||
}
|
||||
|
||||
if strings.TrimSpace(req.ChartOfAccountID) == "" {
|
||||
return errors.New("chart_of_account_id is required"), constants.MissingFieldErrorCode
|
||||
}
|
||||
|
||||
if _, err := uuid.Parse(req.ChartOfAccountID); err != nil {
|
||||
return errors.New("chart_of_account_id must be a valid UUID"), constants.MalformedFieldErrorCode
|
||||
}
|
||||
|
||||
if strings.TrimSpace(req.OutletID) == "" {
|
||||
return errors.New("outlet_id is required"), constants.MissingFieldErrorCode
|
||||
}
|
||||
@@ -63,6 +56,22 @@ func (v *ExpenseValidatorImpl) ValidateCreateExpenseRequest(req *contract.Create
|
||||
return errors.New("tax cannot be negative"), constants.MalformedFieldErrorCode
|
||||
}
|
||||
|
||||
if len(req.Items) == 0 {
|
||||
return errors.New("at least one item is required"), constants.MissingFieldErrorCode
|
||||
}
|
||||
|
||||
for i, item := range req.Items {
|
||||
if strings.TrimSpace(item.ChartOfAccountID) == "" {
|
||||
return fmt.Errorf("item %d: chart_of_account_id is required", i), constants.MissingFieldErrorCode
|
||||
}
|
||||
if _, err := uuid.Parse(item.ChartOfAccountID); err != nil {
|
||||
return fmt.Errorf("item %d: chart_of_account_id must be a valid UUID", i), constants.MalformedFieldErrorCode
|
||||
}
|
||||
if item.Amount <= 0 {
|
||||
return fmt.Errorf("item %d: amount must be greater than 0", i), constants.MalformedFieldErrorCode
|
||||
}
|
||||
}
|
||||
|
||||
return nil, ""
|
||||
}
|
||||
|
||||
@@ -79,15 +88,6 @@ func (v *ExpenseValidatorImpl) ValidateUpdateExpenseRequest(req *contract.Update
|
||||
return errors.New("code_number cannot be empty"), constants.MalformedFieldErrorCode
|
||||
}
|
||||
|
||||
if req.ChartOfAccountID != nil {
|
||||
if strings.TrimSpace(*req.ChartOfAccountID) == "" {
|
||||
return errors.New("chart_of_account_id cannot be empty"), constants.MalformedFieldErrorCode
|
||||
}
|
||||
if _, err := uuid.Parse(*req.ChartOfAccountID); err != nil {
|
||||
return errors.New("chart_of_account_id must be a valid UUID"), constants.MalformedFieldErrorCode
|
||||
}
|
||||
}
|
||||
|
||||
if req.OutletID != nil {
|
||||
if strings.TrimSpace(*req.OutletID) == "" {
|
||||
return errors.New("outlet_id cannot be empty"), constants.MalformedFieldErrorCode
|
||||
@@ -105,6 +105,22 @@ func (v *ExpenseValidatorImpl) ValidateUpdateExpenseRequest(req *contract.Update
|
||||
return errors.New("tax cannot be negative"), constants.MalformedFieldErrorCode
|
||||
}
|
||||
|
||||
if req.Items != nil {
|
||||
for i, item := range req.Items {
|
||||
if item.ChartOfAccountID != nil {
|
||||
if strings.TrimSpace(*item.ChartOfAccountID) == "" {
|
||||
return fmt.Errorf("item %d: chart_of_account_id cannot be empty", i), constants.MalformedFieldErrorCode
|
||||
}
|
||||
if _, err := uuid.Parse(*item.ChartOfAccountID); err != nil {
|
||||
return fmt.Errorf("item %d: chart_of_account_id must be a valid UUID", i), constants.MalformedFieldErrorCode
|
||||
}
|
||||
}
|
||||
if item.Amount != nil && *item.Amount <= 0 {
|
||||
return fmt.Errorf("item %d: amount must be greater than 0", i), constants.MalformedFieldErrorCode
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil, ""
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user