first
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
package pricing
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
repo Repository
|
||||
}
|
||||
|
||||
func NewService(repo Repository) *Service {
|
||||
return &Service{repo: repo}
|
||||
}
|
||||
|
||||
func (s *Service) ListByProduct(ctx context.Context, productID uuid.UUID) ([]*PriceTier, error) {
|
||||
return s.repo.ListByProduct(ctx, productID)
|
||||
}
|
||||
|
||||
func (s *Service) Get(ctx context.Context, id uuid.UUID) (*PriceTier, error) {
|
||||
return s.repo.FindByID(ctx, id)
|
||||
}
|
||||
|
||||
func (s *Service) Create(ctx context.Context, productID, unitID uuid.UUID, quantity float64, priceCents int64, position int) (*PriceTier, error) {
|
||||
t := &PriceTier{
|
||||
ID: uuid.New(),
|
||||
ProductID: productID,
|
||||
UnitID: unitID,
|
||||
Quantity: quantity,
|
||||
PriceCents: priceCents,
|
||||
Position: position,
|
||||
}
|
||||
if err := s.repo.Create(ctx, t); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return t, nil
|
||||
}
|
||||
|
||||
func (s *Service) Update(ctx context.Context, id uuid.UUID, unitID uuid.UUID, quantity float64, priceCents int64, position int) (*PriceTier, error) {
|
||||
t, err := s.repo.FindByID(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
t.UnitID = unitID
|
||||
t.Quantity = quantity
|
||||
t.PriceCents = priceCents
|
||||
t.Position = position
|
||||
if err := s.repo.Update(ctx, t); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return t, nil
|
||||
}
|
||||
|
||||
func (s *Service) Delete(ctx context.Context, id uuid.UUID) error {
|
||||
return s.repo.Delete(ctx, id)
|
||||
}
|
||||
|
||||
// PriceForQuantity is the centralized pricing calculation used by the
|
||||
// orders module: given a chosen tier and how many of that tier package the
|
||||
// customer wants, it returns the authoritative total in cents. The
|
||||
// frontend never gets to supply a price directly.
|
||||
func (s *Service) PriceForQuantity(ctx context.Context, tierID uuid.UUID, multiplier int64) (totalCents int64, tier *PriceTier, err error) {
|
||||
t, err := s.repo.FindByID(ctx, tierID)
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
if multiplier < 1 {
|
||||
multiplier = 1
|
||||
}
|
||||
return t.PriceCents * multiplier, t, nil
|
||||
}
|
||||
Reference in New Issue
Block a user