114 lines
3.4 KiB
Go
114 lines
3.4 KiB
Go
|
|
package validator
|
||
|
|
|
||
|
|
import (
|
||
|
|
"errors"
|
||
|
|
"strings"
|
||
|
|
|
||
|
|
"apskel-pos-be/internal/constants"
|
||
|
|
"apskel-pos-be/internal/contract"
|
||
|
|
|
||
|
|
"github.com/google/uuid"
|
||
|
|
)
|
||
|
|
|
||
|
|
type InventoryValidator interface {
|
||
|
|
ValidateCreateInventoryRequest(req *contract.CreateInventoryRequest) (error, string)
|
||
|
|
ValidateUpdateInventoryRequest(req *contract.UpdateInventoryRequest) (error, string)
|
||
|
|
ValidateListInventoryRequest(req *contract.ListInventoryRequest) (error, string)
|
||
|
|
ValidateAdjustInventoryRequest(req *contract.AdjustInventoryRequest) (error, string)
|
||
|
|
}
|
||
|
|
|
||
|
|
type InventoryValidatorImpl struct{}
|
||
|
|
|
||
|
|
func NewInventoryValidator() *InventoryValidatorImpl {
|
||
|
|
return &InventoryValidatorImpl{}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (v *InventoryValidatorImpl) ValidateCreateInventoryRequest(req *contract.CreateInventoryRequest) (error, string) {
|
||
|
|
if req == nil {
|
||
|
|
return errors.New("request body is required"), constants.MissingFieldErrorCode
|
||
|
|
}
|
||
|
|
|
||
|
|
if req.OutletID == uuid.Nil {
|
||
|
|
return errors.New("outlet_id is required"), constants.MissingFieldErrorCode
|
||
|
|
}
|
||
|
|
|
||
|
|
if req.ProductID == uuid.Nil {
|
||
|
|
return errors.New("product_id is required"), constants.MissingFieldErrorCode
|
||
|
|
}
|
||
|
|
|
||
|
|
if req.Quantity < 0 {
|
||
|
|
return errors.New("quantity must be non-negative"), constants.MalformedFieldErrorCode
|
||
|
|
}
|
||
|
|
|
||
|
|
if req.ReorderLevel < 0 {
|
||
|
|
return errors.New("reorder_level must be non-negative"), constants.MalformedFieldErrorCode
|
||
|
|
}
|
||
|
|
|
||
|
|
return nil, ""
|
||
|
|
}
|
||
|
|
|
||
|
|
func (v *InventoryValidatorImpl) ValidateUpdateInventoryRequest(req *contract.UpdateInventoryRequest) (error, string) {
|
||
|
|
if req == nil {
|
||
|
|
return errors.New("request body is required"), constants.MissingFieldErrorCode
|
||
|
|
}
|
||
|
|
|
||
|
|
// At least one field should be provided for update
|
||
|
|
if req.Quantity == nil && req.ReorderLevel == nil {
|
||
|
|
return errors.New("at least one field must be provided for update"), constants.MissingFieldErrorCode
|
||
|
|
}
|
||
|
|
|
||
|
|
if req.Quantity != nil && *req.Quantity < 0 {
|
||
|
|
return errors.New("quantity must be non-negative"), constants.MalformedFieldErrorCode
|
||
|
|
}
|
||
|
|
|
||
|
|
if req.ReorderLevel != nil && *req.ReorderLevel < 0 {
|
||
|
|
return errors.New("reorder_level must be non-negative"), constants.MalformedFieldErrorCode
|
||
|
|
}
|
||
|
|
|
||
|
|
return nil, ""
|
||
|
|
}
|
||
|
|
|
||
|
|
func (v *InventoryValidatorImpl) ValidateListInventoryRequest(req *contract.ListInventoryRequest) (error, string) {
|
||
|
|
if req == nil {
|
||
|
|
return errors.New("request is required"), constants.MissingFieldErrorCode
|
||
|
|
}
|
||
|
|
|
||
|
|
if req.Page < 1 {
|
||
|
|
return errors.New("page must be at least 1"), constants.MalformedFieldErrorCode
|
||
|
|
}
|
||
|
|
|
||
|
|
if req.Limit < 1 || req.Limit > 100 {
|
||
|
|
return errors.New("limit must be between 1 and 100"), constants.MalformedFieldErrorCode
|
||
|
|
}
|
||
|
|
|
||
|
|
return nil, ""
|
||
|
|
}
|
||
|
|
|
||
|
|
func (v *InventoryValidatorImpl) ValidateAdjustInventoryRequest(req *contract.AdjustInventoryRequest) (error, string) {
|
||
|
|
if req == nil {
|
||
|
|
return errors.New("request body is required"), constants.MissingFieldErrorCode
|
||
|
|
}
|
||
|
|
|
||
|
|
if req.ProductID == uuid.Nil {
|
||
|
|
return errors.New("product_id is required"), constants.MissingFieldErrorCode
|
||
|
|
}
|
||
|
|
|
||
|
|
if req.OutletID == uuid.Nil {
|
||
|
|
return errors.New("outlet_id is required"), constants.MissingFieldErrorCode
|
||
|
|
}
|
||
|
|
|
||
|
|
if req.Delta == 0 {
|
||
|
|
return errors.New("delta cannot be zero"), constants.MalformedFieldErrorCode
|
||
|
|
}
|
||
|
|
|
||
|
|
if strings.TrimSpace(req.Reason) == "" {
|
||
|
|
return errors.New("reason is required"), constants.MissingFieldErrorCode
|
||
|
|
}
|
||
|
|
|
||
|
|
if len(req.Reason) < 1 || len(req.Reason) > 255 {
|
||
|
|
return errors.New("reason must be between 1 and 255 characters"), constants.MalformedFieldErrorCode
|
||
|
|
}
|
||
|
|
|
||
|
|
return nil, ""
|
||
|
|
}
|