feat: add printer types

This commit is contained in:
efrilm
2026-10-01 22:26:44 +07:00
parent 892575202b
commit b0ef226af1
25 changed files with 2048 additions and 66 deletions
+1 -1
View File
@@ -149,7 +149,7 @@ func OrderItemEntityToResponse(item *entities.OrderItem, outletID uuid.UUID) *mo
Status: constants.OrderItemStatus(item.Status),
CreatedAt: item.CreatedAt,
UpdatedAt: item.UpdatedAt,
PrinterType: item.Product.PrinterType,
PrinterTypes: item.Product.GetPrinterTypes(),
PrintToChecker: printToChecker,
}
+13 -12
View File
@@ -24,7 +24,7 @@ func ProductEntityToModel(entity *entities.Product) *models.Product {
Cost: entity.Cost,
BusinessType: constants.BusinessType(entity.BusinessType),
ImageURL: entity.ImageURL,
PrinterType: entity.PrinterType,
PrinterTypes: entity.GetPrinterTypes(),
UnitID: entity.UnitID,
SellBy: entity.SellBy,
HasIngredients: entity.HasIngredients,
@@ -51,7 +51,7 @@ func ProductModelToEntity(model *models.Product) *entities.Product {
Cost: model.Cost,
BusinessType: string(model.BusinessType),
ImageURL: model.ImageURL,
PrinterType: model.PrinterType,
PrinterTypes: entities.StringSlice(model.PrinterTypes),
UnitID: model.UnitID,
SellBy: model.SellBy,
HasIngredients: model.HasIngredients,
@@ -77,11 +77,6 @@ func CreateProductRequestToEntity(req *models.CreateProductRequest) *entities.Pr
businessType = string(req.BusinessType)
}
printerType := "kitchen"
if req.PrinterType != nil && *req.PrinterType != "" {
printerType = *req.PrinterType
}
sellBy := constants.SellByUnit
if constants.IsValidSellBy(req.SellBy) {
sellBy = req.SellBy
@@ -92,7 +87,7 @@ func CreateProductRequestToEntity(req *models.CreateProductRequest) *entities.Pr
metadata = entities.Metadata(req.Metadata)
}
return &entities.Product{
product := &entities.Product{
OrganizationID: req.OrganizationID,
CategoryID: req.CategoryID,
SKU: req.SKU,
@@ -102,12 +97,18 @@ func CreateProductRequestToEntity(req *models.CreateProductRequest) *entities.Pr
Cost: cost,
BusinessType: businessType,
ImageURL: req.ImageURL,
PrinterType: printerType,
UnitID: req.UnitID,
SellBy: sellBy,
Metadata: metadata,
IsActive: true, // Default to active
}
product.SetPrinterTypes(req.PrinterTypes)
if len(product.PrinterTypes) == 0 {
product.SetPrinterTypes([]string{"kitchen"})
}
return product
}
func ProductEntityToResponse(entity *entities.Product) *models.ProductResponse {
@@ -152,7 +153,7 @@ func ProductEntityToResponse(entity *entities.Product) *models.ProductResponse {
Cost: entity.Cost,
BusinessType: constants.BusinessType(entity.BusinessType),
ImageURL: entity.ImageURL,
PrinterType: entity.PrinterType,
PrinterTypes: entity.GetPrinterTypes(),
UnitID: entity.UnitID,
SellBy: entity.SellBy,
Metadata: map[string]interface{}(entity.Metadata),
@@ -196,8 +197,8 @@ func UpdateProductEntityFromRequest(entity *entities.Product, req *models.Update
entity.ImageURL = req.ImageURL
}
if req.PrinterType != nil {
entity.PrinterType = *req.PrinterType
if req.PrinterTypes != nil {
entity.SetPrinterTypes(req.PrinterTypes)
}
if req.UnitID != nil {
@@ -0,0 +1,102 @@
package mappers
import (
"testing"
"apskel-pos-be/internal/entities"
"apskel-pos-be/internal/models"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
)
func TestCreateProductRequestToEntityPrinterTypes(t *testing.T) {
tests := []struct {
name string
printerTypes []string
want []string
}{
{
name: "nothing sent prints in the kitchen",
want: []string{"kitchen"},
},
{
name: "a meal-and-drink package prints in the kitchen and the bar",
printerTypes: []string{"kitchen", "bar"},
want: []string{"kitchen", "bar"},
},
{
name: "blanks and repeats are dropped",
printerTypes: []string{" kitchen ", "", "kitchen", "bar"},
want: []string{"kitchen", "bar"},
},
{
name: "a list of only blanks falls back to the kitchen",
printerTypes: []string{" ", ""},
want: []string{"kitchen"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
product := CreateProductRequestToEntity(&models.CreateProductRequest{
OrganizationID: uuid.New(),
CategoryID: uuid.New(),
Name: "Paket Makan Minum",
PrinterTypes: tt.printerTypes,
})
assert.Equal(t, entities.StringSlice(tt.want), product.PrinterTypes)
})
}
}
func TestUpdateProductEntityFromRequestPrinterTypes(t *testing.T) {
tests := []struct {
name string
printerTypes []string
want []string
}{
{
name: "a save that does not send printers keeps them",
want: []string{"kitchen", "bar"},
},
{
name: "printer_types replaces the whole list",
printerTypes: []string{"bar"},
want: []string{"bar"},
},
{
name: "an empty printer_types prints nowhere",
printerTypes: []string{},
want: []string{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
product := &entities.Product{}
product.SetPrinterTypes([]string{"kitchen", "bar"})
UpdateProductEntityFromRequest(product, &models.UpdateProductRequest{PrinterTypes: tt.printerTypes})
assert.Equal(t, entities.StringSlice(tt.want), product.PrinterTypes)
})
}
}
func TestOrderItemEntityToResponsePrinterTypes(t *testing.T) {
product := entities.Product{ID: uuid.New(), Name: "Paket Makan Minum"}
product.SetPrinterTypes([]string{"kitchen", "bar"})
response := OrderItemEntityToResponse(&entities.OrderItem{ProductID: product.ID, Product: product}, uuid.New())
assert.Equal(t, []string{"kitchen", "bar"}, response.PrinterTypes)
}
func TestProductEntityToResponseAlwaysHasAList(t *testing.T) {
// The POS reads printer_types as a list, so a product without one sends [] not null.
response := ProductEntityToResponse(&entities.Product{})
assert.Equal(t, []string{}, response.PrinterTypes)
}