This commit is contained in:
ryan 2026-05-13 22:30:55 +07:00
parent 4130cb66df
commit 3b62504798
3 changed files with 30 additions and 7 deletions

View File

@ -23,16 +23,28 @@ type ProductOutletPriceProcessor interface {
} }
type ProductOutletPriceProcessorImpl struct { type ProductOutletPriceProcessorImpl struct {
repo repository.ProductOutletPriceRepository repo repository.ProductOutletPriceRepository
productRepo ProductRepository
outletRepo OutletRepository
} }
func NewProductOutletPriceProcessorImpl(repo repository.ProductOutletPriceRepository) *ProductOutletPriceProcessorImpl { func NewProductOutletPriceProcessorImpl(repo repository.ProductOutletPriceRepository, productRepo ProductRepository, outletRepo OutletRepository) *ProductOutletPriceProcessorImpl {
return &ProductOutletPriceProcessorImpl{ return &ProductOutletPriceProcessorImpl{
repo: repo, repo: repo,
productRepo: productRepo,
outletRepo: outletRepo,
} }
} }
func (p *ProductOutletPriceProcessorImpl) Upsert(ctx context.Context, req *models.CreateProductOutletPriceRequest) (*models.ProductOutletPrice, error) { func (p *ProductOutletPriceProcessorImpl) Upsert(ctx context.Context, req *models.CreateProductOutletPriceRequest) (*models.ProductOutletPrice, error) {
if _, err := p.productRepo.GetByID(ctx, req.ProductID); err != nil {
return nil, fmt.Errorf("product not found: %w", err)
}
if _, err := p.outletRepo.GetByID(ctx, req.OutletID); err != nil {
return nil, fmt.Errorf("outlet not found: %w", err)
}
entity := &entities.ProductOutletPrice{ entity := &entities.ProductOutletPrice{
ProductID: req.ProductID, ProductID: req.ProductID,
OutletID: req.OutletID, OutletID: req.OutletID,
@ -43,7 +55,12 @@ func (p *ProductOutletPriceProcessorImpl) Upsert(ctx context.Context, req *model
return nil, fmt.Errorf("failed to upsert product outlet price: %w", err) return nil, fmt.Errorf("failed to upsert product outlet price: %w", err)
} }
return mappers.ProductOutletPriceEntityToModel(entity), nil actual, err := p.repo.GetByProductAndOutlet(ctx, req.ProductID, req.OutletID)
if err != nil {
return nil, fmt.Errorf("failed to retrieve upserted product outlet price: %w", err)
}
return mappers.ProductOutletPriceEntityToModel(actual), nil
} }
func (p *ProductOutletPriceProcessorImpl) GetByProductAndOutlet(ctx context.Context, productID, outletID uuid.UUID) (*models.ProductOutletPrice, error) { func (p *ProductOutletPriceProcessorImpl) GetByProductAndOutlet(ctx context.Context, productID, outletID uuid.UUID) (*models.ProductOutletPrice, error) {

View File

@ -2,6 +2,7 @@ package service
import ( import (
"context" "context"
"errors"
"apskel-pos-be/internal/constants" "apskel-pos-be/internal/constants"
"apskel-pos-be/internal/contract" "apskel-pos-be/internal/contract"
@ -10,6 +11,7 @@ import (
"apskel-pos-be/internal/transformer" "apskel-pos-be/internal/transformer"
"github.com/google/uuid" "github.com/google/uuid"
"gorm.io/gorm"
) )
type ProductOutletPriceService interface { type ProductOutletPriceService interface {
@ -47,7 +49,11 @@ func (s *ProductOutletPriceServiceImpl) Upsert(ctx context.Context, req *contrac
func (s *ProductOutletPriceServiceImpl) GetByProductAndOutlet(ctx context.Context, productID, outletID uuid.UUID) *contract.Response { func (s *ProductOutletPriceServiceImpl) GetByProductAndOutlet(ctx context.Context, productID, outletID uuid.UUID) *contract.Response {
result, err := s.processor.GetByProductAndOutlet(ctx, productID, outletID) result, err := s.processor.GetByProductAndOutlet(ctx, productID, outletID)
if err != nil { if err != nil {
errorResp := contract.NewResponseError(constants.InternalServerErrorCode, constants.ProductOutletPriceServiceEntity, err.Error()) code := constants.InternalServerErrorCode
if errors.Is(err, gorm.ErrRecordNotFound) {
code = constants.NotFoundErrorCode
}
errorResp := contract.NewResponseError(code, constants.ProductOutletPriceServiceEntity, err.Error())
return contract.BuildErrorResponse([]*contract.ResponseError{errorResp}) return contract.BuildErrorResponse([]*contract.ResponseError{errorResp})
} }

View File

@ -2,6 +2,7 @@ package validator
import ( import (
"errors" "errors"
"fmt"
"apskel-pos-be/internal/constants" "apskel-pos-be/internal/constants"
"apskel-pos-be/internal/contract" "apskel-pos-be/internal/contract"
@ -71,8 +72,7 @@ func (v *ProductOutletPriceValidatorImpl) ValidateBulkCreateRequest(req *contrac
return errors.New("outlet_id is required for each price entry"), constants.MissingFieldErrorCode return errors.New("outlet_id is required for each price entry"), constants.MissingFieldErrorCode
} }
if p.Price < 0 { if p.Price < 0 {
_ = i return fmt.Errorf("price at index %d must be non-negative", i), constants.MalformedFieldErrorCode
return errors.New("price must be non-negative for each price entry"), constants.MalformedFieldErrorCode
} }
} }