Skip to content

Commit

Permalink
fix: add prices to plan list response (#787)
Browse files Browse the repository at this point in the history
* fix: add prices to plan list repsonse

* get plan ids in product object
  • Loading branch information
anujk14 authored Sep 30, 2024
1 parent 5cd47b5 commit 69d32e4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
34 changes: 33 additions & 1 deletion billing/plan/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ type ProductService interface {
GetFeatureByProductID(ctx context.Context, id string) ([]product.Feature, error)
}

type PriceRepository interface {
List(ctx context.Context, flt product.Filter) ([]product.Price, error)
}

type FeatureRepository interface {
List(ctx context.Context, flt product.Filter) ([]product.Feature, error)
}
Expand All @@ -50,14 +54,16 @@ type Service struct {
stripeClient *client.API
productService ProductService
featureRepository FeatureRepository
priceRepository PriceRepository
}

func NewService(stripeClient *client.API, planRepository Repository, productService ProductService, featureRepository FeatureRepository) *Service {
func NewService(stripeClient *client.API, planRepository Repository, productService ProductService, featureRepository FeatureRepository, priceRepository PriceRepository) *Service {
return &Service{
stripeClient: stripeClient,
planRepository: planRepository,
productService: productService,
featureRepository: featureRepository,
priceRepository: priceRepository,
}
}

Expand Down Expand Up @@ -104,9 +110,18 @@ func (s Service) List(ctx context.Context, filter Filter) ([]Plan, error) {
// Populate a map initialized with features that belong to a product
productFeatureMapping := mapFeaturesToProducts(plans, features)

prices, err := s.priceRepository.List(ctx, product.Filter{})
if err != nil {
return nil, err
}

// Populate a map initialized with prices that belong to a product
productPriceMapping := mapPricesToProducts(plans, prices)

for _, plan := range plans {
for i, prod := range plan.Products {
plan.Products[i].Features = productFeatureMapping[prod.ID]
plan.Products[i].Prices = productPriceMapping[prod.ID]
}
}

Expand Down Expand Up @@ -357,3 +372,20 @@ func mapFeaturesToProducts(p []Plan, features []product.Feature) map[string][]pr

return productFeatures
}

func mapPricesToProducts(p []Plan, prices []product.Price) map[string][]product.Price {
productPrices := map[string][]product.Price{}
for _, pln := range p {
products := pln.Products
for _, prod := range products {
productPrices[prod.ID] = []product.Price{}
}
}

for _, price := range prices {
productID := price.ProductID
productPrices[productID] = append(productPrices[productID], price)
}

return productPrices
}
4 changes: 3 additions & 1 deletion cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,17 +421,19 @@ func buildAPIDependencies(
postgres.NewBillingCustomerRepository(dbc), cfg.Billing)

featureRepository := postgres.NewBillingFeatureRepository(dbc)
priceRepository := postgres.NewBillingPriceRepository(dbc)
productService := product.NewService(
stripeClient,
postgres.NewBillingProductRepository(dbc),
postgres.NewBillingPriceRepository(dbc),
priceRepository,
featureRepository,
)
planService := plan.NewService(
stripeClient,
postgres.NewBillingPlanRepository(dbc),
productService,
featureRepository,
priceRepository,
)
creditService := credit.NewService(postgres.NewBillingTransactionRepository(dbc))
subscriptionService := subscription.NewService(
Expand Down
1 change: 1 addition & 0 deletions internal/store/postgres/billing_plan_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ func (r BillingPlanRepository) ListWithProducts(ctx context.Context, filter plan
prd.Col("created_at").As("product_created_at"),
prd.Col("updated_at").As("product_updated_at"),
prd.Col("deleted_at").As("product_deleted_at"),
prd.Col("plan_ids").As("product_plan_ids"),
)

var ids []string
Expand Down

0 comments on commit 69d32e4

Please sign in to comment.