Repository: fossasia/open-event-server

Issue:
Order relationship is missing from attendee
**I'm submitting a ...**  (check one with "x")
- [ ] bug report
- [x] 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-orga-server

**Current behavior:**
Currently, there is no order information related with attendee. An attendee can exist without an order which is undefined behaviour

**Expected behavior:**
As the original workflow was like this:
- Create Order
- Select ticket(s) for the order
- Add attendee(s) for each ticket type
- Set payment method
- Confirm order

Similar flow should be there in the API as well. We should only be able to add attendees with order. Where we reference each attendee with a ticket and 1 order object containing this information.

Furthermore it is impossible to check in through QR code if there is no order related to attendee as we check the order identifier and attendee ID in the QR code

For now, what should be the flow to create an attendee with order?


Discussion:
@iamareebjamal  Order flow has been changed a little bit now.

**Current Flow**
1. Create Attendee Records without any order assigned
2. Send them in relationship to Order creation
3. Server will add relationship between these attendees and the order created
4. Server will send the payment link based on your payment method
5. You complete the payment and hit the charge API


I will add the order relationship in the API for attendees.

Resolving patch:
diff --git a/app/api/__init__.py b/app/api/__init__.py
--- a/app/api/__init__.py
+++ b/app/api/__init__.py
@@ -352,6 +352,7 @@
 api.route(AttendeeDetail, 'attendee_detail', '/attendees/<int:id>')
 api.route(AttendeeRelationshipOptional, 'attendee_ticket', '/attendees/<int:id>/relationships/ticket')
 api.route(AttendeeRelationshipRequired, 'attendee_event', '/attendees/<int:id>/relationships/event')
+api.route(AttendeeRelationshipRequired, 'attendee_order', '/attendees/<int:id>/relationships/order')
 
 # event types
 api.route(EventTypeList, 'event_type_list', '/event-types')
@@ -425,7 +426,8 @@
 api.route(OrdersListPost, 'order_list_post', '/orders')
 api.route(OrdersList, 'orders_list', '/orders', '/events/<int:event_id>/orders',
           '/events/<event_identifier>/orders')
-api.route(OrderDetail, 'order_detail', '/orders/<order_identifier>')
+api.route(OrderDetail, 'order_detail', '/orders/<order_identifier>',
+          '/attendees/<int:attendee_id>/order')
 
 # Charges API
 api.route(ChargeList, 'charge_list', '/orders/<identifier>/charge', '/orders/<order_identifier>/charge')
diff --git a/app/api/orders.py b/app/api/orders.py
--- a/app/api/orders.py
+++ b/app/api/orders.py
@@ -20,6 +20,7 @@
 from app.models import db
 from app.models.discount_code import DiscountCode, TICKET
 from app.models.order import Order, OrderTicket
+from app.models.ticket_holder import TicketHolder
 
 
 class OrdersListPost(ResourceList):
@@ -104,6 +105,11 @@ def query(self, view_kwargs):
 
 
 class OrderDetail(ResourceDetail):
+    def before_get_object(self, view_kwargs):
+        if view_kwargs.get('attendee_id'):
+            attendee = safe_query(self, TicketHolder, 'id', view_kwargs['attendee_id'], 'attendee_id')
+            view_kwargs['order_identifier'] = attendee.order.identifier
+
     def before_update_object(self, order, data, view_kwargs):
         if data.get('status'):
             if has_access('is_coorganizer', event_id=order.event.id):
diff --git a/app/api/schema/attendees.py b/app/api/schema/attendees.py
--- a/app/api/schema/attendees.py
+++ b/app/api/schema/attendees.py
@@ -43,3 +43,10 @@ class Meta:
                          related_view_kwargs={'attendee_id': '<id>'},
                          schema='EventSchemaPublic',
                          type_='event')
+
+    order = Relationship(self_view='v1.attendee_order',
+                         self_view_kwargs={'id': '<id>'},
+                         related_view='v1.order_detail',
+                         related_view_kwargs={'attendee_id': '<id>'},
+                         schema='OrderSchema',
+                         type_='order')


Test patch: