Repository: fossasia/open-event-server

Issue:
Use event identifier instead of event id, in the URLs to fetch discount codes
@iamareebjamal Regarding the recent refactors in discount codes, we have to provide the event id, can we instead switch it to using an identifier instead of the id? As that is what all public event related routes use to refer to it, and also identifiers are the primary key for the FE while dealing with the event.


Discussion:
Issue-Label Bot is automatically applying the label `feature` to this issue, with a confidence of 0.81. Please mark this comment with :thumbsup: or :thumbsdown: to give our bot feedback! 

 Links: [app homepage](https://github.com/marketplace/issue-label-bot), [dashboard](https://mlbot.net/data/fossasia/open-event-server) and [code](https://github.com/hamelsmu/MLapp) for this bot.
Support both. Event IDs are the primary keys in both DB and mobile

Resolving patch:
diff --git a/app/api/__init__.py b/app/api/__init__.py
--- a/app/api/__init__.py
+++ b/app/api/__init__.py
@@ -474,7 +474,8 @@
           '/tickets/<int:ticket_id>/discount-codes')
 api.route(DiscountCodeDetail, 'discount_code_detail', '/discount-codes/<int:id>',
           '/events/<int:event_id>/discount-code', '/event-invoices/<int:event_invoice_id>/discount-code',
-          '/events/<int:discount_event_id>/discount-codes/<code>')
+          '/events/<int:discount_event_id>/discount-codes/<code>',
+          '/events/<discount_event_identifier>/discount-codes/<code>')
 api.route(DiscountCodeRelationshipRequired, 'discount_code_event',
           '/discount-codes/<int:id>/relationships/event')
 api.route(DiscountCodeRelationshipOptional, 'discount_code_events',
@@ -542,7 +543,8 @@
           '/events/<event_identifier>/access-codes', '/users/<int:user_id>/access-codes',
           '/tickets/<int:ticket_id>/access-codes')
 api.route(AccessCodeDetail, 'access_code_detail', '/access-codes/<int:id>',
-          '/events/<int:access_event_id>/access-codes/<code>')
+          '/events/<int:access_event_id>/access-codes/<code>',
+          '/events/<int:access_event_identifier>/access-codes/<code>',)
 api.route(AccessCodeRelationshipRequired, 'access_code_event',
           '/access-codes/<int:id>/relationships/event')
 api.route(AccessCodeRelationshipOptional, 'access_code_user',
diff --git a/app/api/access_codes.py b/app/api/access_codes.py
--- a/app/api/access_codes.py
+++ b/app/api/access_codes.py
@@ -15,6 +15,7 @@
 from app.models.access_code import AccessCode
 from app.models.ticket import Ticket
 from app.models.user import User
+from app.models.event import Event
 
 
 class AccessCodeListPost(ResourceList):
@@ -110,7 +111,13 @@ def before_get(self, args, kwargs):
         :param kwargs:
         :return:
         """
-        # Any registered user can fetch access code details using the code.
+        # Any user can fetch access code details using the code.
+
+        if kwargs.get('access_event_identifier'):
+            event = safe_query(
+                db, Event, 'identifier', kwargs['discount_event_identifier'],
+                'event_identifier')
+            kwargs['access_event_id'] = event.id
         if kwargs.get('code') and kwargs.get('access_event_id'):
             access = db.session.query(AccessCode).filter_by(code=kwargs.get('code'),
                                                             event_id=kwargs.get('access_event_id')).first()
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
@@ -198,6 +198,12 @@ def before_get(self, args, kwargs):
                 'event_identifier')
             kwargs['event_id'] = event.id
 
+        if kwargs.get('discount_event_identifier'):
+            event = safe_query(
+                db, Event, 'identifier', kwargs['discount_event_identifier'],
+                'event_identifier')
+            kwargs['discount_event_id'] = event.id
+
         if kwargs.get('event_id') and has_access('is_admin'):
             event = safe_query(db, Event, 'id', kwargs['event_id'], 'event_id')
             if event.discount_code_id:


Test patch: