79 lines
2.2 KiB
Go
79 lines
2.2 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"apskel-pos-be/internal/models"
|
|
"apskel-pos-be/internal/processor"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type HPPService interface {
|
|
GetStandardHPP(ctx context.Context, req *models.StandardHPPRequest) (*models.StandardHPPResponse, error)
|
|
GetRealHPP(ctx context.Context, req *models.RealHPPRequest) (*models.RealHPPResponse, error)
|
|
}
|
|
|
|
type HPPServiceImpl struct {
|
|
hppProcessor processor.HPPProcessor
|
|
}
|
|
|
|
func NewHPPServiceImpl(hppProcessor processor.HPPProcessor) *HPPServiceImpl {
|
|
return &HPPServiceImpl{
|
|
hppProcessor: hppProcessor,
|
|
}
|
|
}
|
|
|
|
func (s *HPPServiceImpl) GetStandardHPP(ctx context.Context, req *models.StandardHPPRequest) (*models.StandardHPPResponse, error) {
|
|
if err := s.validateStandardHPPRequest(req); err != nil {
|
|
return nil, fmt.Errorf("validation error: %w", err)
|
|
}
|
|
|
|
response, err := s.hppProcessor.GetStandardHPP(ctx, req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to get standard HPP: %w", err)
|
|
}
|
|
|
|
return response, nil
|
|
}
|
|
|
|
func (s *HPPServiceImpl) GetRealHPP(ctx context.Context, req *models.RealHPPRequest) (*models.RealHPPResponse, error) {
|
|
if err := s.validateRealHPPRequest(req); err != nil {
|
|
return nil, fmt.Errorf("validation error: %w", err)
|
|
}
|
|
|
|
response, err := s.hppProcessor.GetRealHPP(ctx, req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to get real HPP: %w", err)
|
|
}
|
|
|
|
return response, nil
|
|
}
|
|
|
|
func (s *HPPServiceImpl) validateStandardHPPRequest(req *models.StandardHPPRequest) error {
|
|
if req.OrganizationID == uuid.Nil {
|
|
return fmt.Errorf("organization_id is required")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *HPPServiceImpl) validateRealHPPRequest(req *models.RealHPPRequest) error {
|
|
if req.OrganizationID == uuid.Nil {
|
|
return fmt.Errorf("organization_id is required")
|
|
}
|
|
if req.DateFrom.IsZero() {
|
|
return fmt.Errorf("date_from is required")
|
|
}
|
|
if req.DateTo.IsZero() {
|
|
return fmt.Errorf("date_to is required")
|
|
}
|
|
if req.DateFrom.After(req.DateTo) {
|
|
return fmt.Errorf("date_from cannot be after date_to")
|
|
}
|
|
if req.GroupBy != "" && req.GroupBy != "hour" && req.GroupBy != "day" && req.GroupBy != "week" && req.GroupBy != "month" {
|
|
return fmt.Errorf("invalid group_by value, must be one of: hour, day, week, month")
|
|
}
|
|
return nil
|
|
}
|