diff --git a/backend/metering_billing/billable_metrics.py b/backend/metering_billing/billable_metrics.py index 05de47285..39239d8d5 100644 --- a/backend/metering_billing/billable_metrics.py +++ b/backend/metering_billing/billable_metrics.py @@ -28,6 +28,7 @@ ) from metering_billing.utils import ( convert_to_date, + convert_to_datetime, date_as_min_dt, now_utc, periods_bwn_twodates, @@ -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(): diff --git a/backend/metering_billing/serializers/model_serializers.py b/backend/metering_billing/serializers/model_serializers.py index 86a5998a4..e9ebebc0c 100644 --- a/backend/metering_billing/serializers/model_serializers.py +++ b/backend/metering_billing/serializers/model_serializers.py @@ -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 diff --git a/backend/metering_billing/views/model_views.py b/backend/metering_billing/views/model_views.py index ab4e742ee..3dcd45fdd 100644 --- a/backend/metering_billing/views/model_views.py +++ b/backend/metering_billing/views/model_views.py @@ -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))