apskel-pos-backend/internal/processor/product_recipe_processor.go

256 lines
9.6 KiB
Go
Raw Normal View History

2025-08-10 21:46:44 +07:00
package processor
import (
2025-09-13 02:17:51 +07:00
"apskel-pos-be/internal/contract"
2025-08-10 21:46:44 +07:00
"context"
"fmt"
"time"
"apskel-pos-be/internal/entities"
"apskel-pos-be/internal/repository"
"github.com/google/uuid"
)
type ProductRecipeProcessor interface {
2025-09-13 02:17:51 +07:00
Create(ctx context.Context, req *contract.CreateProductRecipeRequest, organizationID uuid.UUID) (*contract.ProductRecipeResponse, error)
GetByID(ctx context.Context, id uuid.UUID, organizationID uuid.UUID) (*contract.ProductRecipeResponse, error)
GetByProductID(ctx context.Context, productID uuid.UUID, organizationID uuid.UUID) ([]*contract.ProductRecipeResponse, error)
GetByProductAndVariantID(ctx context.Context, productID uuid.UUID, variantID *uuid.UUID, organizationID uuid.UUID) ([]*contract.ProductRecipeResponse, error)
GetByIngredientID(ctx context.Context, ingredientID uuid.UUID, organizationID uuid.UUID) ([]*contract.ProductRecipeResponse, error)
Update(ctx context.Context, id uuid.UUID, req *contract.UpdateProductRecipeRequest, organizationID uuid.UUID) (*contract.ProductRecipeResponse, error)
2025-08-10 21:46:44 +07:00
Delete(ctx context.Context, id uuid.UUID, organizationID uuid.UUID) error
2025-09-13 02:17:51 +07:00
BulkCreate(ctx context.Context, recipes []contract.CreateProductRecipeRequest, organizationID uuid.UUID) ([]*contract.ProductRecipeResponse, error)
2025-08-10 21:46:44 +07:00
}
type ProductRecipeProcessorImpl struct {
productRecipeRepo *repository.ProductRecipeRepository
productRepo ProductRepository
ingredientRepo IngredientRepository
}
func NewProductRecipeProcessor(productRecipeRepo *repository.ProductRecipeRepository, productRepo ProductRepository, ingredientRepo IngredientRepository) *ProductRecipeProcessorImpl {
return &ProductRecipeProcessorImpl{
productRecipeRepo: productRecipeRepo,
productRepo: productRepo,
ingredientRepo: ingredientRepo,
}
}
2025-09-13 02:17:51 +07:00
func (p *ProductRecipeProcessorImpl) Create(ctx context.Context, req *contract.CreateProductRecipeRequest, organizationID uuid.UUID) (*contract.ProductRecipeResponse, error) {
2025-08-10 21:46:44 +07:00
_, err := p.productRepo.GetByID(ctx, req.ProductID)
if err != nil {
return nil, fmt.Errorf("invalid product: %w", err)
}
_, err = p.ingredientRepo.GetByID(ctx, req.IngredientID, organizationID)
if err != nil {
return nil, fmt.Errorf("invalid ingredient: %w", err)
}
entity := &entities.ProductRecipe{
2025-09-13 02:17:51 +07:00
ID: uuid.New(),
OrganizationID: organizationID,
OutletID: req.OutletID,
ProductID: req.ProductID,
VariantID: req.VariantID,
IngredientID: req.IngredientID,
Quantity: req.Quantity,
WastePercentage: req.WastePercentage,
CreatedAt: time.Now().UTC(),
UpdatedAt: time.Now().UTC(),
2025-08-10 21:46:44 +07:00
}
if err := p.productRecipeRepo.Create(ctx, entity); err != nil {
return nil, fmt.Errorf("failed to create product recipe: %w", err)
}
createdEntity, err := p.productRecipeRepo.GetByID(ctx, entity.ID, organizationID)
if err != nil {
return nil, fmt.Errorf("failed to get created product recipe: %w", err)
}
return p.entityToResponse(createdEntity), nil
}
2025-09-13 02:17:51 +07:00
func (p *ProductRecipeProcessorImpl) GetByID(ctx context.Context, id uuid.UUID, organizationID uuid.UUID) (*contract.ProductRecipeResponse, error) {
2025-08-10 21:46:44 +07:00
entity, err := p.productRecipeRepo.GetByID(ctx, id, organizationID)
if err != nil {
return nil, fmt.Errorf("failed to get product recipe: %w", err)
}
return p.entityToResponse(entity), nil
}
2025-09-13 02:17:51 +07:00
func (p *ProductRecipeProcessorImpl) GetByProductID(ctx context.Context, productID uuid.UUID, organizationID uuid.UUID) ([]*contract.ProductRecipeResponse, error) {
2025-08-10 21:46:44 +07:00
entities, err := p.productRecipeRepo.GetByProductID(ctx, productID, organizationID)
if err != nil {
return nil, fmt.Errorf("failed to get product recipes by product ID: %w", err)
}
2025-09-13 02:17:51 +07:00
responses := make([]*contract.ProductRecipeResponse, len(entities))
2025-08-10 21:46:44 +07:00
for i, entity := range entities {
responses[i] = p.entityToResponse(entity)
}
return responses, nil
}
2025-09-13 02:17:51 +07:00
func (p *ProductRecipeProcessorImpl) GetByProductAndVariantID(ctx context.Context, productID uuid.UUID, variantID *uuid.UUID, organizationID uuid.UUID) ([]*contract.ProductRecipeResponse, error) {
2025-08-10 21:46:44 +07:00
entities, err := p.productRecipeRepo.GetByProductAndVariantID(ctx, productID, variantID, organizationID)
if err != nil {
return nil, fmt.Errorf("failed to get product recipes by product and variant ID: %w", err)
}
2025-09-13 02:17:51 +07:00
responses := make([]*contract.ProductRecipeResponse, len(entities))
2025-08-10 21:46:44 +07:00
for i, entity := range entities {
responses[i] = p.entityToResponse(entity)
}
return responses, nil
}
2025-09-13 02:17:51 +07:00
func (p *ProductRecipeProcessorImpl) GetByIngredientID(ctx context.Context, ingredientID uuid.UUID, organizationID uuid.UUID) ([]*contract.ProductRecipeResponse, error) {
2025-08-10 21:46:44 +07:00
entities, err := p.productRecipeRepo.GetByIngredientID(ctx, ingredientID, organizationID)
if err != nil {
return nil, fmt.Errorf("failed to get product recipes by ingredient ID: %w", err)
}
2025-09-13 02:17:51 +07:00
responses := make([]*contract.ProductRecipeResponse, len(entities))
2025-08-10 21:46:44 +07:00
for i, entity := range entities {
responses[i] = p.entityToResponse(entity)
}
return responses, nil
}
2025-09-13 02:17:51 +07:00
func (p *ProductRecipeProcessorImpl) Update(ctx context.Context, id uuid.UUID, req *contract.UpdateProductRecipeRequest, organizationID uuid.UUID) (*contract.ProductRecipeResponse, error) {
2025-08-10 21:46:44 +07:00
// Get existing entity
existingEntity, err := p.productRecipeRepo.GetByID(ctx, id, organizationID)
if err != nil {
return nil, fmt.Errorf("product recipe not found: %w", err)
}
// Update fields
existingEntity.OutletID = req.OutletID
existingEntity.VariantID = req.VariantID
existingEntity.Quantity = req.Quantity
2025-09-13 02:17:51 +07:00
existingEntity.WastePercentage = req.WastePercentage
2025-08-10 21:46:44 +07:00
existingEntity.UpdatedAt = time.Now().UTC()
if err := p.productRecipeRepo.Update(ctx, existingEntity); err != nil {
return nil, fmt.Errorf("failed to update product recipe: %w", err)
}
// Get the updated entity with relations
updatedEntity, err := p.productRecipeRepo.GetByID(ctx, id, organizationID)
if err != nil {
return nil, fmt.Errorf("failed to get updated product recipe: %w", err)
}
return p.entityToResponse(updatedEntity), nil
}
func (p *ProductRecipeProcessorImpl) Delete(ctx context.Context, id uuid.UUID, organizationID uuid.UUID) error {
if err := p.productRecipeRepo.Delete(ctx, id, organizationID); err != nil {
return fmt.Errorf("failed to delete product recipe: %w", err)
}
return nil
}
2025-09-13 02:17:51 +07:00
func (p *ProductRecipeProcessorImpl) BulkCreate(ctx context.Context, recipes []contract.CreateProductRecipeRequest, organizationID uuid.UUID) ([]*contract.ProductRecipeResponse, error) {
responses := make([]*contract.ProductRecipeResponse, 0, len(recipes))
2025-08-10 21:46:44 +07:00
for _, recipe := range recipes {
response, err := p.Create(ctx, &recipe, organizationID)
if err != nil {
return nil, fmt.Errorf("failed to create recipe for product %s and ingredient %s: %w", recipe.ProductID, recipe.IngredientID, err)
}
responses = append(responses, response)
}
return responses, nil
}
2025-09-13 02:17:51 +07:00
func (p *ProductRecipeProcessorImpl) entityToResponse(entity *entities.ProductRecipe) *contract.ProductRecipeResponse {
response := &contract.ProductRecipeResponse{
ID: entity.ID,
OrganizationID: entity.OrganizationID,
OutletID: entity.OutletID,
ProductID: entity.ProductID,
VariantID: entity.VariantID,
IngredientID: entity.IngredientID,
Quantity: entity.Quantity,
WastePercentage: entity.WastePercentage,
CreatedAt: entity.CreatedAt,
UpdatedAt: entity.UpdatedAt,
2025-08-10 21:46:44 +07:00
}
if entity.Product != nil {
2025-09-13 02:17:51 +07:00
response.Product = &contract.ProductResponse{
2025-08-10 21:46:44 +07:00
ID: entity.Product.ID,
OrganizationID: entity.Product.OrganizationID,
CategoryID: entity.Product.CategoryID,
SKU: entity.Product.SKU,
Name: entity.Product.Name,
Description: entity.Product.Description,
Price: entity.Product.Price,
Cost: entity.Product.Cost,
2025-09-13 02:17:51 +07:00
BusinessType: string(entity.Product.BusinessType),
2025-08-10 21:46:44 +07:00
ImageURL: entity.Product.ImageURL,
PrinterType: entity.Product.PrinterType,
Metadata: entity.Product.Metadata,
IsActive: entity.Product.IsActive,
CreatedAt: entity.Product.CreatedAt,
UpdatedAt: entity.Product.UpdatedAt,
}
}
if entity.ProductVariant != nil {
2025-09-13 02:17:51 +07:00
response.ProductVariant = &contract.ProductVariantResponse{
2025-08-10 21:46:44 +07:00
ID: entity.ProductVariant.ID,
ProductID: entity.ProductVariant.ProductID,
Name: entity.ProductVariant.Name,
PriceModifier: entity.ProductVariant.PriceModifier,
Cost: entity.ProductVariant.Cost,
Metadata: entity.ProductVariant.Metadata,
CreatedAt: entity.ProductVariant.CreatedAt,
UpdatedAt: entity.ProductVariant.UpdatedAt,
}
}
if entity.Ingredient != nil {
2025-09-13 02:17:51 +07:00
response.Ingredient = &contract.ProductRecipeIngredientResponse{
2025-08-10 21:46:44 +07:00
ID: entity.Ingredient.ID,
OrganizationID: entity.Ingredient.OrganizationID,
OutletID: entity.Ingredient.OutletID,
Name: entity.Ingredient.Name,
UnitID: entity.Ingredient.UnitID,
Cost: entity.Ingredient.Cost,
Stock: entity.Ingredient.Stock,
IsSemiFinished: entity.Ingredient.IsSemiFinished,
IsActive: entity.Ingredient.IsActive,
Metadata: entity.Ingredient.Metadata,
CreatedAt: entity.Ingredient.CreatedAt,
UpdatedAt: entity.Ingredient.UpdatedAt,
}
2025-09-13 02:17:51 +07:00
// Add unit if available
if entity.Ingredient.Unit != nil {
symbol := ""
if entity.Ingredient.Unit.Abbreviation != nil {
symbol = *entity.Ingredient.Unit.Abbreviation
}
response.Ingredient.Unit = &contract.ProductRecipeUnitResponse{
ID: entity.Ingredient.Unit.ID,
Name: entity.Ingredient.Unit.Name,
Symbol: symbol,
CreatedAt: entity.Ingredient.Unit.CreatedAt,
UpdatedAt: entity.Ingredient.Unit.UpdatedAt,
}
}
2025-08-10 21:46:44 +07:00
}
return response
2025-09-13 02:17:51 +07:00
}