Repository: fossasia/open-event-server

Issue:
Discount Codes' time frame not being considered while applying it
**Describe the bug**
<!-- A clear and concise description of what the bug is. -->
ATM, any discount code's validity time frame is not being considered while applying it. In short, if my discount code is valid from 24 Feb to 2 March '19, I can still manage to apply it on 14 Feb. Moreover, the discount codes with Inactive condition are also being applied which should not be the case

**To Reproduce**
Steps to reproduce the behavior:
1. Setup a discount code with a time frame
2. Go to the tickets
3. Apply Promocode
4. See error

**Expected behavior**
<!-- A clear and concise description of what you expected to happen. -->
Any out of time-frame and inactive discount codes should not be valid.

**Screenshots**
<!-- If applicable, add screenshots to help explain your problem. -->
![give](https://user-images.githubusercontent.com/21087061/52749539-81b0c880-300f-11e9-8848-87127de59da7.png)
![validfrom](https://user-images.githubusercontent.com/21087061/52749540-81b0c880-300f-11e9-8f65-cd5b66c48701.png)


**Desktop (please complete the following information):**
 - OS: [e.g. iOS] Ubuntu
 - Browser [e.g. chrome, safari] Chrome

**Additional context**
<!-- Add any other context about the problem here. -->
Looking into it


Discussion:
@CosmicCoder96 @iamareebjamal @niranjan94  
I could actually use some assistance regarding which `schema` \ `before_get_object` to be modified whenever we use this request in the frontend : `http://localhost:4200/e/<event_id(or code)>?code=<discount_name>`
Please guide me on which file to modify in the server

Resolving patch:
diff --git a/app/api/discount_codes.py b/app/api/discount_codes.py
--- a/app/api/discount_codes.py
+++ b/app/api/discount_codes.py
@@ -3,7 +3,7 @@
 from sqlalchemy.orm.exc import NoResultFound
 
 from app.api.helpers.db import safe_query
-from app.api.helpers.exceptions import ConflictException, ForbiddenException, UnprocessableEntity
+from app.api.helpers.exceptions import ConflictException, ForbiddenException, UnprocessableEntity, MethodNotAllowed
 from app.api.helpers.permission_manager import has_access
 from app.api.helpers.permissions import jwt_required, current_identity
 from app.api.helpers.utilities import require_relationship
@@ -15,6 +15,8 @@
 from app.models.ticket import Ticket
 from app.models.user import User
 
+from datetime import datetime
+
 class DiscountCodeListPost(ResourceList):
     """
     Create Event and Ticket Discount code and Get Event Discount Codes
@@ -208,6 +210,13 @@ def before_get(self, args, kwargs):
             discount = db.session.query(DiscountCode).filter_by(code=kwargs.get('code'), deleted_at=None).first()
             if discount:
                 kwargs['id'] = discount.id
+                discount_tz = discount.valid_from.tzinfo
+                current_time = datetime.now().replace(tzinfo=discount_tz)
+                if not discount.is_active:
+                    raise MethodNotAllowed({'parameter': '{code}'}, "Discount Code is not active")
+                elif current_time < discount.valid_from or current_time > discount.valid_till:
+                    raise MethodNotAllowed({'parameter': '{code}'},
+                                           "Discount Code is not active in current time frame")
             else:
                 raise ObjectNotFound({'parameter': '{code}'}, "DiscountCode: not found")
 
diff --git a/app/factories/common.py b/app/factories/common.py
--- a/app/factories/common.py
+++ b/app/factories/common.py
@@ -8,7 +8,7 @@
 url_ = 'http://example.com'
 imageUrl_ = 'https://www.w3schools.com/html/pic_mountain.jpg'
 date_ = '2016-12-13T23:59:59.123456+00:00'
-dateEnd_ = '2016-12-14T23:59:59.123456+00:00'
+dateEnd_ = '2020-12-14T23:59:59.123456+00:00'
 country_ = 'US'
 currency_ = 'USD'
 int_ = '1'


Test patch: