Merge pull request 'feat: add printer types' (#40) from fix/point-payment-method-types into main

Reviewed-on: https://gits.altru.id/apksel-dev/apskel-pos-backend/pulls/40
This commit was merged in pull request #40.
This commit is contained in:
2026-10-01 17:43:53 +02:00
25 changed files with 2048 additions and 66 deletions
+17 -5
View File
@@ -59,8 +59,8 @@ func (v *ProductValidatorImpl) ValidateCreateProductRequest(req *contract.Create
return errors.New("image_url cannot exceed 500 characters"), constants.MalformedFieldErrorCode
}
if req.PrinterType != nil && len(*req.PrinterType) > 50 {
return errors.New("printer_type cannot exceed 50 characters"), constants.MalformedFieldErrorCode
if err, code := validatePrinterTypes(req.PrinterTypes); err != nil {
return err, code
}
if err, code := validateSellBy(req.SellBy, req.UnitID); err != nil {
@@ -70,6 +70,18 @@ func (v *ProductValidatorImpl) ValidateCreateProductRequest(req *contract.Create
return nil, ""
}
// validatePrinterTypes holds each printer to the 50 characters the single printer_type
// column allowed.
func validatePrinterTypes(printerTypes []string) (error, string) {
for _, printerType := range printerTypes {
if len(strings.TrimSpace(printerType)) > 50 {
return errors.New("each printer_types entry cannot exceed 50 characters"), constants.MalformedFieldErrorCode
}
}
return nil, ""
}
// validateSellBy checks how a product is sold and that it carries what that choice
// needs. A weight-based product without a unit would produce order lines with no unit
// to print, so the receipt could show "4,2" with no idea of what.
@@ -100,7 +112,7 @@ func (v *ProductValidatorImpl) ValidateUpdateProductRequest(req *contract.Update
// At least one field should be provided for update
if req.CategoryID == nil && req.SKU == nil && req.Name == nil && req.Description == nil &&
req.Price == nil && req.Cost == nil && req.BusinessType == nil && req.ImageURL == nil &&
req.PrinterType == nil && req.PrintToChecker == nil && req.UnitID == nil &&
req.PrinterTypes == nil && req.PrintToChecker == nil && req.UnitID == nil &&
req.SellBy == nil && req.Metadata == nil && req.IsActive == nil {
return errors.New("at least one field must be provided for update"), constants.MissingFieldErrorCode
}
@@ -134,8 +146,8 @@ func (v *ProductValidatorImpl) ValidateUpdateProductRequest(req *contract.Update
return errors.New("image_url cannot exceed 500 characters"), constants.MalformedFieldErrorCode
}
if req.PrinterType != nil && len(*req.PrinterType) > 50 {
return errors.New("printer_type cannot exceed 50 characters"), constants.MalformedFieldErrorCode
if err, code := validatePrinterTypes(req.PrinterTypes); err != nil {
return err, code
}
// Only the value is checked here. Whether the product ends up with a unit depends on
@@ -0,0 +1,35 @@
package validator
import (
"strings"
"testing"
"apskel-pos-be/internal/contract"
)
func TestValidateProductRequestPrinterTypes(t *testing.T) {
v := NewProductValidator()
tooLong := strings.Repeat("a", 51)
create := baseCreateRequest()
create.PrinterTypes = []string{"kitchen", "bar"}
if err, _ := v.ValidateCreateProductRequest(create); err != nil {
t.Fatalf("kitchen and bar should be accepted, got: %v", err)
}
create.PrinterTypes = []string{"kitchen", tooLong}
if err, _ := v.ValidateCreateProductRequest(create); err == nil {
t.Fatal("expected an error for a printer longer than 50 characters")
}
err, _ := v.ValidateUpdateProductRequest(&contract.UpdateProductRequest{PrinterTypes: []string{tooLong}})
if err == nil {
t.Fatal("expected an error for a printer longer than 50 characters on update")
}
// An update that only changes the printers is a real update.
err, _ = v.ValidateUpdateProductRequest(&contract.UpdateProductRequest{PrinterTypes: []string{"kitchen", "bar"}})
if err != nil {
t.Fatalf("an update of only printer_types should be accepted, got: %v", err)
}
}