From 69d32e426f159a8c55d584656ec1eec4173fe0d1 Mon Sep 17 00:00:00 2001 From: Anuj Khandelwal Date: Mon, 30 Sep 2024 23:20:48 +0530 Subject: [PATCH] fix: add prices to plan list response (#787) * fix: add prices to plan list repsonse * get plan ids in product object --- billing/plan/service.go | 34 ++++++++++++++++++- cmd/serve.go | 4 ++- .../store/postgres/billing_plan_repository.go | 1 + 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/billing/plan/service.go b/billing/plan/service.go index 21c769674..fa2624dc8 100644 --- a/billing/plan/service.go +++ b/billing/plan/service.go @@ -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) } @@ -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, } } @@ -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] } } @@ -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 +} diff --git a/cmd/serve.go b/cmd/serve.go index e004f607a..8af132a01 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -421,10 +421,11 @@ 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( @@ -432,6 +433,7 @@ func buildAPIDependencies( postgres.NewBillingPlanRepository(dbc), productService, featureRepository, + priceRepository, ) creditService := credit.NewService(postgres.NewBillingTransactionRepository(dbc)) subscriptionService := subscription.NewService( diff --git a/internal/store/postgres/billing_plan_repository.go b/internal/store/postgres/billing_plan_repository.go index 2fb7e2f1b..31137720b 100644 --- a/internal/store/postgres/billing_plan_repository.go +++ b/internal/store/postgres/billing_plan_repository.go @@ -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