diff --git a/apps/product/models.py b/apps/product/models.py index 2bfd223..ce9d209 100644 --- a/apps/product/models.py +++ b/apps/product/models.py @@ -529,6 +529,24 @@ class Quota(BaseModel): "inventory_entry_balance": stat.first().inventory_entry_balance if stat.exists() else 0, } + def get_quota_stat(self, org: Organization): + """ + get stats of quota from org quota stat model + """ + + stat = OrganizationQuotaStats.objects.filter( + quota=self, + organization=org + ) + + if not stat.exists() and org.type.key == 'ADM': + # if org is admin just see the quota was created by registerer org + org = self.registerer_organization + stat = OrganizationQuotaStats.objects.filter( + quota=self, + ) + return stat.first() + def soft_delete(self): self.trash = True self.save(update_fields=['trash']) diff --git a/apps/product/web/api/v1/serializers/quota_serializers.py b/apps/product/web/api/v1/serializers/quota_serializers.py index 8e2b814..565b82c 100644 --- a/apps/product/web/api/v1/serializers/quota_serializers.py +++ b/apps/product/web/api/v1/serializers/quota_serializers.py @@ -89,13 +89,16 @@ class QuotaSerializer(serializers.ModelSerializer): representation['incentive_plan'] = list(incentive_plan_map.values()) + # get quota stat for filtering pricing features (every distribution of this quota has different attributes) + quota_stat = instance.get_quota_stat(org) if instance.edited_pricing_features else None # noqa + representation['attribute_values'] = product_serializers.AttributeValueSerializer( - instance.attribute_values.all(), + instance.attribute_values.filter(org_quota_stat=quota_stat), many=True ).data representation['brokers'] = QuotaBrokerValueSerializer( - instance.broker_values.all(), + instance.broker_values.filter(org_quota_stat=quota_stat), many=True ).data