From 3b625047987c2a2dbdd2355f3c960f0c92a6fbc4 Mon Sep 17 00:00:00 2001 From: ryan Date: Wed, 13 May 2026 22:30:55 +0700 Subject: [PATCH] fix --- .../product_outlet_price_processor.go | 25 ++++++++++++++++--- .../service/product_outlet_price_service.go | 8 +++++- .../product_outlet_price_validator.go | 4 +-- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/internal/processor/product_outlet_price_processor.go b/internal/processor/product_outlet_price_processor.go index 973eb20..97e00e0 100644 --- a/internal/processor/product_outlet_price_processor.go +++ b/internal/processor/product_outlet_price_processor.go @@ -23,16 +23,28 @@ type ProductOutletPriceProcessor interface { } 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{ - repo: repo, + repo: repo, + productRepo: productRepo, + outletRepo: outletRepo, } } 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{ ProductID: req.ProductID, 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 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) { diff --git a/internal/service/product_outlet_price_service.go b/internal/service/product_outlet_price_service.go index 89fbed8..dc35f68 100644 --- a/internal/service/product_outlet_price_service.go +++ b/internal/service/product_outlet_price_service.go @@ -2,6 +2,7 @@ package service import ( "context" + "errors" "apskel-pos-be/internal/constants" "apskel-pos-be/internal/contract" @@ -10,6 +11,7 @@ import ( "apskel-pos-be/internal/transformer" "github.com/google/uuid" + "gorm.io/gorm" ) 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 { result, err := s.processor.GetByProductAndOutlet(ctx, productID, outletID) 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}) } diff --git a/internal/validator/product_outlet_price_validator.go b/internal/validator/product_outlet_price_validator.go index d476919..7f78964 100644 --- a/internal/validator/product_outlet_price_validator.go +++ b/internal/validator/product_outlet_price_validator.go @@ -2,6 +2,7 @@ package validator import ( "errors" + "fmt" "apskel-pos-be/internal/constants" "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 } if p.Price < 0 { - _ = i - return errors.New("price must be non-negative for each price entry"), constants.MalformedFieldErrorCode + return fmt.Errorf("price at index %d must be non-negative", i), constants.MalformedFieldErrorCode } }