(self, request: HttpRequest, *args: Any, **kwargs: Any)
| 141 | ] |
| 142 | |
| 143 | def list(self, request: HttpRequest, *args: Any, **kwargs: Any) -> Response: |
| 144 | license = License.objects.first_valid() |
| 145 | if license and not license.is_v2_license: |
| 146 | raise NotFound("Billing V2 is not supported for this license type") |
| 147 | |
| 148 | org = self._get_org() |
| 149 | |
| 150 | # If on Cloud and we have the property billing - return 404 as we always use legacy billing it it exists |
| 151 | if hasattr(org, "billing"): |
| 152 | if org.billing.stripe_subscription_id: # type: ignore |
| 153 | raise NotFound("Billing V1 is active for this organization") |
| 154 | |
| 155 | billing_service_response: Dict[str, Any] = {} |
| 156 | response: Dict[str, Any] = {"available_features": []} |
| 157 | |
| 158 | # Load Billing info if we have a V2 license |
| 159 | if org and license and license.is_v2_license: |
| 160 | response["license"] = {"plan": license.plan} |
| 161 | billing_service_response = self._get_billing(license, org) |
| 162 | |
| 163 | # Sync the License and Org if we have a valid response |
| 164 | if license and billing_service_response.get("license"): |
| 165 | self._update_license_details(license, billing_service_response["license"]) |
| 166 | |
| 167 | if org and billing_service_response.get("customer"): |
| 168 | response.update(billing_service_response["customer"]) |
| 169 | |
| 170 | # If we don't have products then get the default ones with our local usage calculation |
| 171 | if not response.get("products"): |
| 172 | products = self._get_products(license, org) |
| 173 | response["products"] = products["standard"] |
| 174 | response["products_enterprise"] = products["enterprise"] |
| 175 | |
| 176 | calculated_usage = get_cached_current_usage(org) if org else None |
| 177 | |
| 178 | for product in response["products"] + response["products_enterprise"]: |
| 179 | if calculated_usage and product["type"] in calculated_usage: |
| 180 | product["current_usage"] = calculated_usage[product["type"]] |
| 181 | else: |
| 182 | product["current_usage"] = 0 |
| 183 | |
| 184 | # Either way calculate the percentage_used for each product |
| 185 | for product in response["products"]: |
| 186 | usage_limit = product.get("usage_limit", product.get("free_allocation")) |
| 187 | product["percentage_usage"] = product["current_usage"] / usage_limit if usage_limit else 0 |
| 188 | |
| 189 | # Before responding ensure the org is updated with the latest info |
| 190 | if org: |
| 191 | self._update_org_details(org, response) |
| 192 | |
| 193 | return Response(response) |
| 194 | |
| 195 | @action(methods=["PATCH"], detail=False, url_path="/") |
| 196 | def patch(self, request: Request, *args: Any, **kwargs: Any) -> Response: |
no test coverage detected