Skip to content

Commit

Permalink
hotfix for customer details
Browse files Browse the repository at this point in the history
  • Loading branch information
diego-escobedo committed Dec 18, 2022
1 parent fa8856e commit 3a4321d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
7 changes: 6 additions & 1 deletion backend/metering_billing/billable_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
)
from metering_billing.utils import (
convert_to_date,
convert_to_datetime,
date_as_min_dt,
now_utc,
periods_bwn_twodates,
Expand Down Expand Up @@ -799,7 +800,11 @@ def get_usage(
)
)
now = now_utc()
plan_periods = [x for x in plan_periods if x <= end and x <= now]
plan_periods = [
x
for x in plan_periods
if x <= convert_to_datetime(end, date_behavior="max") and x <= now
]
# for each period, get the events and calculate the usage
usage_dict = {}
for customer_name, cust_usages in period_usages.items():
Expand Down
17 changes: 15 additions & 2 deletions backend/metering_billing/serializers/model_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1557,19 +1557,32 @@ def validate(self, data):
data = super().validate(data)
# check that the customer ID matches an existing customer
try:
data["customer"] = Customer.objects.get(customer_id=data["customer_id"])
data["customer"] = Customer.objects.get(
customer_id=data["customer_id"],
organization=self.context["organization"],
)
except Customer.DoesNotExist:
raise serializers.ValidationError(
f"Customer with customer_id {data['customer_id']} does not exist"
)
except Customer.MultipleObjectsReturned:
raise ServerError(
"Something went wrong with the database state. Please allow us to investigate!"
)
# check that the plan ID matches an existing plan
if data.get("plan_id"):
try:
data["plan"] = Plan.objects.get(plan_id=data["plan_id"])
data["plan"] = Plan.objects.get(
plan_id=data["plan_id"], organization=self.context["organization"]
)
except Plan.DoesNotExist:
raise serializers.ValidationError(
f"Plan with plan_id {data['plan_id']} does not exist"
)
except Plan.MultipleObjectsReturned:
raise ServerError(
"Something went wrong with the database state. Please allow us to investigate!"
)
return data


Expand Down
8 changes: 6 additions & 2 deletions backend/metering_billing/views/model_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -774,9 +774,13 @@ def get_queryset(self):
parsed_params = json.loads(raw_filters)
dict_params["subscription_filters"] = parsed_params
if self.action == "update_plans":
serializer = SubscriptionRecordFilterSerializer(data=dict_params)
serializer = SubscriptionRecordFilterSerializer(
data=dict_params, context={"organization": organization}
)
elif self.action == "cancel_plans":
serializer = SubscriptionRecordFilterSerializerDelete(data=dict_params)
serializer = SubscriptionRecordFilterSerializerDelete(
data=dict_params, context={"organization": organization}
)
serializer.is_valid(raise_exception=True)
args = []
args.append(Q(status=SUBSCRIPTION_STATUS.ACTIVE))
Expand Down

0 comments on commit 3a4321d

Please sign in to comment.