Skip to content

Commit

Permalink
Hotfix/bugs (uselotus#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
diego-escobedo authored Nov 7, 2022
1 parent b388336 commit 1f33785
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 30 deletions.
25 changes: 15 additions & 10 deletions backend/metering_billing/demos.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@
User,
)
from metering_billing.tasks import run_backtest
from metering_billing.utils import date_as_max_dt, date_as_min_dt, now_utc
from metering_billing.utils import (
date_as_max_dt,
date_as_min_dt,
now_utc,
plan_version_uuid,
)
from metering_billing.utils.enums import (
BACKTEST_KPI,
FLAT_FEE_BILLING_TYPE,
Expand Down Expand Up @@ -120,7 +125,7 @@ def setup_demo_3(company_name, username, email, password):
plan=plan,
status=PLAN_VERSION_STATUS.ACTIVE,
flat_rate=0,
version_id="free",
version_id=plan_version_uuid,
)
pc1 = PlanComponent.objects.create(
billable_metric=sum_words, max_metric_units=2_000
Expand All @@ -147,7 +152,7 @@ def setup_demo_3(company_name, username, email, password):
plan=plan,
status=PLAN_VERSION_STATUS.ACTIVE,
flat_rate=49,
version_id="10_og",
version_id=plan_version_uuid,
)
pc1 = PlanComponent.objects.create(
billable_metric=sum_words,
Expand Down Expand Up @@ -175,7 +180,7 @@ def setup_demo_3(company_name, username, email, password):
plan=plan,
status=PLAN_VERSION_STATUS.ACTIVE,
flat_rate=99,
version_id="25_og",
version_id=plan_version_uuid,
)
pc1 = PlanComponent.objects.create(
billable_metric=sum_words, max_metric_units=25_000
Expand All @@ -202,7 +207,7 @@ def setup_demo_3(company_name, username, email, password):
plan=plan,
status=PLAN_VERSION_STATUS.ACTIVE,
flat_rate=279,
version_id="50_og",
version_id=plan_version_uuid,
)
pc1 = PlanComponent.objects.create(
billable_metric=sum_words, max_metric_units=50_000
Expand All @@ -229,7 +234,7 @@ def setup_demo_3(company_name, username, email, password):
plan=plan,
status=PLAN_VERSION_STATUS.ACTIVE,
flat_rate=19,
version_id="10_compute_seats",
version_id=plan_version_uuid,
)
pc1 = PlanComponent.objects.create(
billable_metric=sum_words, max_metric_units=10_000
Expand Down Expand Up @@ -264,7 +269,7 @@ def setup_demo_3(company_name, username, email, password):
plan=plan,
status=PLAN_VERSION_STATUS.ACTIVE,
flat_rate=59,
version_id="25_compute_seats",
version_id=plan_version_uuid,
)
pc1 = PlanComponent.objects.create(
billable_metric=sum_words, max_metric_units=25_000
Expand Down Expand Up @@ -299,7 +304,7 @@ def setup_demo_3(company_name, username, email, password):
plan=plan,
status=PLAN_VERSION_STATUS.ACTIVE,
flat_rate=179,
version_id="50_compute_seats",
version_id=plan_version_uuid,
)
pc1 = PlanComponent.objects.create(
billable_metric=sum_words, max_metric_units=50_000
Expand Down Expand Up @@ -378,7 +383,7 @@ def setup_demo_3(company_name, username, email, password):
max_users = float(
plan.components.get(billable_metric=num_seats).max_metric_units
)
n = int(random.gauss(6, 1.5) // 1)
n = max(int(random.gauss(6, 1.5) // 1), 1)
baker.make(
Event,
organization=organization,
Expand Down Expand Up @@ -447,7 +452,7 @@ def setup_demo_3(company_name, username, email, password):
max_users = float(
plan.components.get(billable_metric=num_seats).max_metric_units
)
n = int(random.gauss(6, 1.5) // 1)
n = max(int(random.gauss(6, 1.5) // 1), 1)
baker.make(
Event,
organization=organization,
Expand Down
4 changes: 3 additions & 1 deletion backend/metering_billing/invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ def generate_invoice(subscription, draft=False, charge_next_plan=False):
k,
)
for amount, plan_version_id, start, end in date_range_costs:
cur_bp = PlanVersion.objects.get(version_id=plan_version_id)
cur_bp = PlanVersion.objects.get(
organization=organization, version_id=plan_version_id
)
billing_plan_name = cur_bp.plan.plan_name
billing_plan_version = cur_bp.version
summary_dict["line_items"].append(
Expand Down
2 changes: 1 addition & 1 deletion backend/metering_billing/management/commands/demo1_up.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def handle(self, *args, **options):
idempotency_id=uuid.uuid4,
_quantity=n,
)
n = int(random.gauss(6, 1.5) // 1)
n = max(int(random.gauss(6, 1.5) // 1), 1)
baker.make(
Event,
organization=organization,
Expand Down
4 changes: 2 additions & 2 deletions backend/metering_billing/management/commands/demo2_up.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ def handle(self, *args, **options):
max_users = float(
plan.components.get(billable_metric=num_seats).max_metric_units
)
n = int(random.gauss(6, 1.5) // 1)
n = max(int(random.gauss(6, 1.5) // 1), 1)
baker.make(
Event,
organization=organization,
Expand Down Expand Up @@ -537,7 +537,7 @@ def handle(self, *args, **options):
max_users = float(
plan.components.get(billable_metric=num_seats).max_metric_units
)
n = int(random.gauss(6, 1.5) // 1)
n = max(int(random.gauss(6, 1.5) // 1), 1)
baker.make(
Event,
organization=organization,
Expand Down
50 changes: 34 additions & 16 deletions backend/metering_billing/views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,30 +379,48 @@ class CustomerDetailView(APIView):
fields={"customer_id": serializers.CharField()},
),
],
responses={200: CustomerDetailSerializer},
responses={
200: CustomerDetailSerializer,
400: inline_serializer(
name="CustomerDetailErrorResponseSerializer",
fields={"error_detail": serializers.CharField()},
),
},
)
def get(self, request, format=None):
"""
Get the current settings for the organization.
"""
organization = parse_organization(request)
customer_id = request.query_params.get("customer_id")
customer = (
Customer.objects.filter(organization=organization, customer_id=customer_id)
.prefetch_related(
Prefetch(
"customer_subscriptions",
queryset=Subscription.objects.filter(organization=organization),
to_attr="subscriptions",
),
Prefetch(
"subscriptions__billing_plan",
queryset=PlanVersion.objects.filter(organization=organization),
to_attr="billing_plans",
),
try:
customer = (
Customer.objects.filter(
organization=organization, customer_id=customer_id
)
.prefetch_related(
Prefetch(
"customer_subscriptions",
queryset=Subscription.objects.filter(organization=organization),
to_attr="subscriptions",
),
Prefetch(
"subscriptions__billing_plan",
queryset=PlanVersion.objects.filter(organization=organization),
to_attr="billing_plans",
),
)
.get()
)
except Customer.DoesNotExist:
return Response(
{
"error_detail": "Customer with customer_id {} does not exist".format(
customer_id
)
},
status=status.HTTP_400_BAD_REQUEST,
)
.get()
)
sub_usg_summaries = customer.get_usage_and_revenue()
total_amount_due = sum(
x["total_amount_due"] for x in sub_usg_summaries["subscriptions"]
Expand Down

0 comments on commit 1f33785

Please sign in to comment.