mirror of
https://github.com/andwati/telecom-platform.git
synced 2026-11-26 05:48:47 +00:00
refactor: Update service layer to use repository package types and align imports
- Update Service struct to use repository.Repository interface - Update all method signatures to use repository package types (RatePlan, RatePlanSubscription, RatePlanUsage, etc.) - Update filter and request types to use repository package (RatePlanFilter, SubscriptionFilter, CalculateCostRequest, etc.) - Update status constants to use repository package (SubscriptionStatusActive, PlanStatusActive, DiscountTypePercentage, DiscountTypeFixed)
This commit is contained in:
@@ -6,24 +6,25 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
"github.com/nutcas3/telecom-platform/apps/carrier-connector/internal/repository"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Service provides business logic for rate plan operations
|
// Service provides business logic for rate plan operations
|
||||||
type Service struct {
|
type Service struct {
|
||||||
repo Repository
|
repo repository.Repository
|
||||||
logger *logrus.Logger
|
logger *logrus.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewService creates a new rate plan service
|
// NewService creates a new rate plan service
|
||||||
func NewService(repo Repository, logger *logrus.Logger) *Service {
|
func NewService(repo repository.Repository, logger *logrus.Logger) *Service {
|
||||||
return &Service{
|
return &Service{
|
||||||
repo: repo,
|
repo: repo,
|
||||||
logger: logger,
|
logger: logger,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) CreateRatePlan(ctx context.Context, plan *RatePlan) (*RatePlan, error) {
|
func (s *Service) CreateRatePlan(ctx context.Context, plan *repository.RatePlan) (*repository.RatePlan, error) {
|
||||||
// Generate ID if not provided
|
// Generate ID if not provided
|
||||||
if plan.ID == "" {
|
if plan.ID == "" {
|
||||||
plan.ID = uuid.New().String()
|
plan.ID = uuid.New().String()
|
||||||
@@ -45,7 +46,7 @@ func (s *Service) CreateRatePlan(ctx context.Context, plan *RatePlan) (*RatePlan
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetRatePlan retrieves a rate plan by ID
|
// GetRatePlan retrieves a rate plan by ID
|
||||||
func (s *Service) GetRatePlan(ctx context.Context, id string) (*RatePlan, error) {
|
func (s *Service) GetRatePlan(ctx context.Context, id string) (*repository.RatePlan, error) {
|
||||||
plan, err := s.repo.GetRatePlan(ctx, id)
|
plan, err := s.repo.GetRatePlan(ctx, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.logger.WithError(err).WithField("plan_id", id).Error("Failed to get rate plan")
|
s.logger.WithError(err).WithField("plan_id", id).Error("Failed to get rate plan")
|
||||||
@@ -56,7 +57,7 @@ func (s *Service) GetRatePlan(ctx context.Context, id string) (*RatePlan, error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateRatePlan updates an existing rate plan
|
// UpdateRatePlan updates an existing rate plan
|
||||||
func (s *Service) UpdateRatePlan(ctx context.Context, plan *RatePlan) (*RatePlan, error) {
|
func (s *Service) UpdateRatePlan(ctx context.Context, plan *repository.RatePlan) (*repository.RatePlan, error) {
|
||||||
// Validate rate plan
|
// Validate rate plan
|
||||||
if err := s.validateRatePlan(plan); err != nil {
|
if err := s.validateRatePlan(plan); err != nil {
|
||||||
return nil, fmt.Errorf("validation failed: %w", err)
|
return nil, fmt.Errorf("validation failed: %w", err)
|
||||||
@@ -81,9 +82,9 @@ func (s *Service) UpdateRatePlan(ctx context.Context, plan *RatePlan) (*RatePlan
|
|||||||
// DeleteRatePlan deletes a rate plan
|
// DeleteRatePlan deletes a rate plan
|
||||||
func (s *Service) DeleteRatePlan(ctx context.Context, id string) error {
|
func (s *Service) DeleteRatePlan(ctx context.Context, id string) error {
|
||||||
// Check if plan has active subscriptions
|
// Check if plan has active subscriptions
|
||||||
subscriptions, err := s.repo.ListSubscriptions(ctx, "", &SubscriptionFilter{
|
subscriptions, err := s.repo.ListSubscriptions(ctx, "", &repository.SubscriptionFilter{
|
||||||
RatePlanID: id,
|
RatePlanID: id,
|
||||||
Status: SubscriptionStatusActive,
|
Status: repository.SubscriptionStatusActive,
|
||||||
Limit: 1,
|
Limit: 1,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -105,7 +106,7 @@ func (s *Service) DeleteRatePlan(ctx context.Context, id string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListRatePlans retrieves rate plans with filtering
|
// ListRatePlans retrieves rate plans with filtering
|
||||||
func (s *Service) ListRatePlans(ctx context.Context, filter *RatePlanFilter) ([]*RatePlan, error) {
|
func (s *Service) ListRatePlans(ctx context.Context, filter *repository.RatePlanFilter) ([]*repository.RatePlan, error) {
|
||||||
plans, err := s.repo.ListRatePlans(ctx, filter)
|
plans, err := s.repo.ListRatePlans(ctx, filter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.logger.WithError(err).Error("Failed to list rate plans")
|
s.logger.WithError(err).Error("Failed to list rate plans")
|
||||||
@@ -116,12 +117,12 @@ func (s *Service) ListRatePlans(ctx context.Context, filter *RatePlanFilter) ([]
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SearchRatePlans searches for rate plans based on criteria
|
// SearchRatePlans searches for rate plans based on criteria
|
||||||
func (s *Service) SearchRatePlans(ctx context.Context, criteria SearchCriteria) ([]*RatePlan, error) {
|
func (s *Service) SearchRatePlans(ctx context.Context, criteria repository.SearchCriteria) ([]*repository.RatePlan, error) {
|
||||||
filter := &RatePlanFilter{
|
filter := &repository.RatePlanFilter{
|
||||||
CarrierID: criteria.CarrierID,
|
CarrierID: criteria.CarrierID,
|
||||||
Region: criteria.Region,
|
Region: criteria.Region,
|
||||||
PlanType: criteria.PlanType,
|
PlanType: criteria.PlanType,
|
||||||
Status: PlanStatusActive,
|
Status: repository.PlanStatusActive,
|
||||||
IsActive: &[]bool{true}[0],
|
IsActive: &[]bool{true}[0],
|
||||||
MinPrice: criteria.MinPrice,
|
MinPrice: criteria.MinPrice,
|
||||||
MaxPrice: criteria.MaxPrice,
|
MaxPrice: criteria.MaxPrice,
|
||||||
@@ -133,8 +134,9 @@ func (s *Service) SearchRatePlans(ctx context.Context, criteria SearchCriteria)
|
|||||||
|
|
||||||
return s.repo.ListRatePlans(ctx, filter)
|
return s.repo.ListRatePlans(ctx, filter)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CalculateCost calculates the cost for a rate plan based on usage
|
// CalculateCost calculates the cost for a rate plan based on usage
|
||||||
func (s *Service) CalculateCost(ctx context.Context, req *CalculateCostRequest) (*RatePlanCostCalculation, error) {
|
func (s *Service) CalculateCost(ctx context.Context, req *repository.CalculateCostRequest) (*repository.RatePlanCostCalculation, error) {
|
||||||
// Get the rate plan
|
// Get the rate plan
|
||||||
plan, err := s.repo.GetRatePlan(ctx, req.RatePlanID)
|
plan, err := s.repo.GetRatePlan(ctx, req.RatePlanID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -166,9 +168,9 @@ func (s *Service) CalculateCost(ctx context.Context, req *CalculateCostRequest)
|
|||||||
for _, discount := range plan.Discounts {
|
for _, discount := range plan.Discounts {
|
||||||
if discount.ID == discountID && discount.IsActive {
|
if discount.ID == discountID && discount.IsActive {
|
||||||
switch discount.Type {
|
switch discount.Type {
|
||||||
case DiscountTypePercentage:
|
case repository.DiscountTypePercentage:
|
||||||
discountCost += baseCost * discount.Value / 100
|
discountCost += baseCost * discount.Value / 100
|
||||||
case DiscountTypeFixed:
|
case repository.DiscountTypeFixed:
|
||||||
discountCost += discount.Value
|
discountCost += discount.Value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -178,7 +180,7 @@ func (s *Service) CalculateCost(ctx context.Context, req *CalculateCostRequest)
|
|||||||
|
|
||||||
totalCost := baseCost + overageCost - discountCost
|
totalCost := baseCost + overageCost - discountCost
|
||||||
|
|
||||||
calculation := &RatePlanCostCalculation{
|
calculation := &repository.RatePlanCostCalculation{
|
||||||
RatePlanID: req.RatePlanID,
|
RatePlanID: req.RatePlanID,
|
||||||
BaseCost: baseCost,
|
BaseCost: baseCost,
|
||||||
OverageCost: overageCost,
|
OverageCost: overageCost,
|
||||||
|
|||||||
@@ -6,10 +6,11 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
"github.com/nutcas3/telecom-platform/apps/carrier-connector/internal/repository"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *Service) SubscribeToPlan(ctx context.Context, req *SubscribeRequest) (*RatePlanSubscription, error) {
|
func (s *Service) SubscribeToPlan(ctx context.Context, req *repository.SubscribeRequest) (*repository.RatePlanSubscription, error) {
|
||||||
if err := s.validateSubscribeRequest(req); err != nil {
|
if err := s.validateSubscribeRequest(req); err != nil {
|
||||||
return nil, fmt.Errorf("validation failed: %w", err)
|
return nil, fmt.Errorf("validation failed: %w", err)
|
||||||
}
|
}
|
||||||
@@ -19,7 +20,7 @@ func (s *Service) SubscribeToPlan(ctx context.Context, req *SubscribeRequest) (*
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if !plan.IsActive || plan.Status != PlanStatusActive {
|
if !plan.IsActive || plan.Status != repository.PlanStatusActive {
|
||||||
return nil, fmt.Errorf("rate plan is not available for subscription")
|
return nil, fmt.Errorf("rate plan is not available for subscription")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,11 +34,11 @@ func (s *Service) SubscribeToPlan(ctx context.Context, req *SubscribeRequest) (*
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create subscription
|
// Create subscription
|
||||||
subscription := &RatePlanSubscription{
|
subscription := &repository.RatePlanSubscription{
|
||||||
ID: uuid.New().String(),
|
ID: uuid.New().String(),
|
||||||
ProfileID: req.ProfileID,
|
ProfileID: req.ProfileID,
|
||||||
RatePlanID: req.RatePlanID,
|
RatePlanID: req.RatePlanID,
|
||||||
Status: SubscriptionStatusActive,
|
Status: repository.SubscriptionStatusActive,
|
||||||
StartedAt: time.Now(),
|
StartedAt: time.Now(),
|
||||||
BillingCycle: plan.BillingCycle,
|
BillingCycle: plan.BillingCycle,
|
||||||
NextBillingDate: s.calculateNextBillingDate(plan.BillingCycle, time.Now()),
|
NextBillingDate: s.calculateNextBillingDate(plan.BillingCycle, time.Now()),
|
||||||
@@ -62,7 +63,7 @@ func (s *Service) SubscribeToPlan(ctx context.Context, req *SubscribeRequest) (*
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetSubscription retrieves a subscription by ID
|
// GetSubscription retrieves a subscription by ID
|
||||||
func (s *Service) GetSubscription(ctx context.Context, id string) (*RatePlanSubscription, error) {
|
func (s *Service) GetSubscription(ctx context.Context, id string) (*repository.RatePlanSubscription, error) {
|
||||||
subscription, err := s.repo.GetSubscription(ctx, id)
|
subscription, err := s.repo.GetSubscription(ctx, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.logger.WithError(err).WithField("subscription_id", id).Error("Failed to get subscription")
|
s.logger.WithError(err).WithField("subscription_id", id).Error("Failed to get subscription")
|
||||||
@@ -73,7 +74,7 @@ func (s *Service) GetSubscription(ctx context.Context, id string) (*RatePlanSubs
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateSubscription updates an existing subscription
|
// UpdateSubscription updates an existing subscription
|
||||||
func (s *Service) UpdateSubscription(ctx context.Context, subscription *RatePlanSubscription) (*RatePlanSubscription, error) {
|
func (s *Service) UpdateSubscription(ctx context.Context, subscription *repository.RatePlanSubscription) (*repository.RatePlanSubscription, error) {
|
||||||
if err := s.repo.UpdateSubscription(ctx, subscription); err != nil {
|
if err := s.repo.UpdateSubscription(ctx, subscription); err != nil {
|
||||||
s.logger.WithError(err).Error("Failed to update subscription")
|
s.logger.WithError(err).Error("Failed to update subscription")
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -90,12 +91,12 @@ func (s *Service) CancelSubscription(ctx context.Context, subscriptionID string,
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if subscription.Status != SubscriptionStatusActive {
|
if subscription.Status != repository.SubscriptionStatusActive {
|
||||||
return fmt.Errorf("subscription is not active")
|
return fmt.Errorf("subscription is not active")
|
||||||
}
|
}
|
||||||
|
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
subscription.Status = SubscriptionStatusCancelled
|
subscription.Status = repository.SubscriptionStatusCancelled
|
||||||
subscription.EndedAt = &now
|
subscription.EndedAt = &now
|
||||||
subscription.UpdatedAt = now
|
subscription.UpdatedAt = now
|
||||||
|
|
||||||
@@ -114,7 +115,7 @@ func (s *Service) CancelSubscription(ctx context.Context, subscriptionID string,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetActiveSubscription retrieves the active subscription for a profile
|
// GetActiveSubscription retrieves the active subscription for a profile
|
||||||
func (s *Service) GetActiveSubscription(ctx context.Context, profileID string) (*RatePlanSubscription, error) {
|
func (s *Service) GetActiveSubscription(ctx context.Context, profileID string) (*repository.RatePlanSubscription, error) {
|
||||||
subscription, err := s.repo.GetActiveSubscription(ctx, profileID)
|
subscription, err := s.repo.GetActiveSubscription(ctx, profileID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.logger.WithError(err).WithField("profile_id", profileID).Error("Failed to get active subscription")
|
s.logger.WithError(err).WithField("profile_id", profileID).Error("Failed to get active subscription")
|
||||||
@@ -125,7 +126,7 @@ func (s *Service) GetActiveSubscription(ctx context.Context, profileID string) (
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListSubscriptions retrieves subscriptions for a profile
|
// ListSubscriptions retrieves subscriptions for a profile
|
||||||
func (s *Service) ListSubscriptions(ctx context.Context, profileID string, filter *SubscriptionFilter) ([]*RatePlanSubscription, error) {
|
func (s *Service) ListSubscriptions(ctx context.Context, profileID string, filter *repository.SubscriptionFilter) ([]*repository.RatePlanSubscription, error) {
|
||||||
subscriptions, err := s.repo.ListSubscriptions(ctx, profileID, filter)
|
subscriptions, err := s.repo.ListSubscriptions(ctx, profileID, filter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.logger.WithError(err).Error("Failed to list subscriptions")
|
s.logger.WithError(err).Error("Failed to list subscriptions")
|
||||||
@@ -135,7 +136,7 @@ func (s *Service) ListSubscriptions(ctx context.Context, profileID string, filte
|
|||||||
return subscriptions, nil
|
return subscriptions, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) RecordUsage(ctx context.Context, req *RecordUsageRequest) (*RatePlanUsage, error) {
|
func (s *Service) RecordUsage(ctx context.Context, req *repository.RecordUsageRequest) (*repository.RatePlanUsage, error) {
|
||||||
// Get active subscription
|
// Get active subscription
|
||||||
subscription, err := s.repo.GetActiveSubscription(ctx, req.ProfileID)
|
subscription, err := s.repo.GetActiveSubscription(ctx, req.ProfileID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -153,10 +154,10 @@ func (s *Service) RecordUsage(ctx context.Context, req *RecordUsageRequest) (*Ra
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create or update usage record
|
// Create or update usage record
|
||||||
var usage *RatePlanUsage
|
var usage *repository.RatePlanUsage
|
||||||
if currentUsage == nil {
|
if currentUsage == nil {
|
||||||
// Create new usage record
|
// Create new usage record
|
||||||
usage = &RatePlanUsage{
|
usage = &repository.RatePlanUsage{
|
||||||
ID: uuid.New().String(),
|
ID: uuid.New().String(),
|
||||||
RatePlanID: subscription.RatePlanID,
|
RatePlanID: subscription.RatePlanID,
|
||||||
ProfileID: req.ProfileID,
|
ProfileID: req.ProfileID,
|
||||||
|
|||||||
Reference in New Issue
Block a user