| 1263 | ) |
| 1264 | @action(detail=True, methods=["post"], url_path="delete") |
| 1265 | def delete(self, request, *args, **kwargs): |
| 1266 | organization = self.request.organization |
| 1267 | now = now_utc() |
| 1268 | plan = self.get_object() |
| 1269 | num_active_subscriptions = ( |
| 1270 | SubscriptionRecord.objects.active() |
| 1271 | .filter( |
| 1272 | organization=organization, |
| 1273 | billing_plan__plan=plan, |
| 1274 | ) |
| 1275 | .count() |
| 1276 | ) |
| 1277 | if num_active_subscriptions > 0: |
| 1278 | raise InvalidOperation("Cannot delete plan with active subscriptions") |
| 1279 | versions = plan.versions.filter(deleted__isnull=True) |
| 1280 | num_versions = versions.count() |
| 1281 | versions.update(deleted=now) |
| 1282 | plan.deleted = now |
| 1283 | plan.save() |
| 1284 | return Response( |
| 1285 | { |
| 1286 | "success": True, |
| 1287 | "message": f"Deleted {num_versions} versions of plan {plan.plan_name}", |
| 1288 | }, |
| 1289 | status=status.HTTP_200_OK, |
| 1290 | ) |
| 1291 | |
| 1292 | @extend_schema( |
| 1293 | parameters=None, |