Repository: fossasia/open-event-server

Issue:
Add missing min max validation to access codes api
**I'm submitting a ...**  (check one with "x")
- [x] bug report
- [ ] feature request
- [ ] support request => Please do not submit support requests here, instead ask your query in out Gitter channel at https://gitter.im/fossasia/open-event-server

**Current behavior:**
<!-- Describe how the bug manifests. -->
The min and max validation is missing on access code api for tickets.

**Expected behavior:**
<!-- Describe what the behavior would be without the bug. -->
The min and max validation should be there similar to the discount coder api instead of relying on the client for the validations.











Discussion:

Resolving patch:
diff --git a/app/api/schema/access_codes.py b/app/api/schema/access_codes.py
--- a/app/api/schema/access_codes.py
+++ b/app/api/schema/access_codes.py
@@ -36,19 +36,31 @@ def validate_date(self, data, original_data):
             raise UnprocessableEntity({'pointer': '/data/attributes/valid-till'},
                                       "valid_till should be after valid_from")
 
-    @validates_schema
-    def validate_order_quantity(self, data):
-        if 'max_order' in data and 'min_order' in data:
-            if data['max_order'] < data['min_order']:
-                raise UnprocessableEntity({'pointer': '/data/attributes/max-order'},
-                                          "max-order should be greater than min-order")
-
-        if 'quantity' in data and 'min_order' in data:
-            if data['quantity'] < data['min_order']:
-                raise UnprocessableEntity({'pointer': '/data/attributes/quantity'},
-                                          "quantity should be greater than min-order")
-
-    id = fields.Integer(dump_only=True)
+    @validates_schema(pass_original=True)
+    def validate_order_quantity(self, data, original_data):
+        if 'id' in original_data['data']:
+            access_code = AccessCode.query.filter_by(id=original_data['data']['id']).one()
+
+            if 'min_quantity' not in data:
+                data['min_quantity'] = access_code.min_quantity
+
+            if 'max_quantity' not in data:
+                data['max_quantity'] = access_code.max_quantity
+
+            if 'tickets_number' not in data:
+                data['tickets_number'] = access_code.tickets_number
+
+        if 'min_quantity' in data and 'max_quantity' in data:
+            if data['min_quantity'] >= data['max_quantity']:
+                raise UnprocessableEntity({'pointer': '/data/attributes/min-quantity'},
+                                          "min-quantity should be less than max-quantity")
+
+        if 'tickets_number' in data and 'max_quantity' in data:
+            if data['tickets_number'] < data['max_quantity']:
+                raise UnprocessableEntity({'pointer': '/data/attributes/tickets-number'},
+                                          "tickets-number should be greater than max-quantity")
+
+    id = fields.Integer(dump_ony=True)
     code = fields.Str(allow_none=True)
     access_url = fields.Url(allow_none=True)
     is_active = fields.Boolean(default=False)


Test patch: