Repository: fossasia/open-event-server

Issue:
Delete attendees on order expiry
**Is your feature request related to a problem? Please describe.**
Currently attendees locked to a ticket are not released on expiring of an order and failed attempt to create an order.

**Describe the solution you'd like**
Delete attendees and release the ticket reservation when order expires or POST request does not succeed,



Discussion:
I think deleting attendees on expiry makes sense but on failed POST doesn't. Let's say there was a 422 when creating an order, we don't want to start all over again. I'll do it for expiry though.
:+1: 

Resolving patch:
diff --git a/app/api/helpers/order.py b/app/api/helpers/order.py
--- a/app/api/helpers/order.py
+++ b/app/api/helpers/order.py
@@ -1,24 +1,24 @@
 import logging
 from datetime import timedelta, datetime, timezone
 
-from app.api.helpers.db import save_to_db
 from app.api.helpers import ticketing
+from app.api.helpers.db import save_to_db
+from app.models import db
 
 
-def delete_related_attendees_for_order(self, order):
+def delete_related_attendees_for_order(order):
     """
     Delete the associated attendees of an order when it is cancelled/deleted/expired
-    :param self:
     :param order: Order whose attendees have to be deleted.
     :return:
     """
     for ticket_holder in order.ticket_holders:
-        self.session.delete(ticket_holder)
+        db.session.delete(ticket_holder)
         try:
-            self.session.commit()
+            db.session.commit()
         except Exception as e:
             logging.error('DB Exception! %s' % e)
-            self.session.rollback()
+            db.session.rollback()
 
 
 def set_expiry_for_order(order, override=False):
@@ -33,5 +33,6 @@ def set_expiry_for_order(order, override=False):
                 order.created_at +
                 timedelta(minutes=ticketing.TicketingManager.get_order_expiry())) < datetime.now(timezone.utc))):
             order.status = 'expired'
+            delete_related_attendees_for_order(order)
             save_to_db(order)
     return order
diff --git a/app/api/helpers/ticketing.py b/app/api/helpers/ticketing.py
--- a/app/api/helpers/ticketing.py
+++ b/app/api/helpers/ticketing.py
@@ -119,7 +119,7 @@ def charge_stripe_order_payment(order, token_id):
             save_to_db(order)
 
             # delete related attendees to unlock the tickets
-            delete_related_attendees_for_order(db, order)
+            delete_related_attendees_for_order(order)
 
             # return the failure message from stripe.
             return False, charge.failure_message
@@ -164,7 +164,7 @@ def charge_paypal_order_payment(order, token_id):
                 save_to_db(order)
 
                 # delete related attendees to unlock the tickets
-                delete_related_attendees_for_order(db, order)
+                delete_related_attendees_for_order(order)
 
                 # return the error message from Paypal
                 return False, capture_result['L_SHORTMESSAGE0']
diff --git a/app/api/orders.py b/app/api/orders.py
--- a/app/api/orders.py
+++ b/app/api/orders.py
@@ -286,7 +286,7 @@ def after_update_object(self, order, data, view_kwargs):
             send_notif_ticket_cancel(order)
 
             # delete the attendees so that the tickets are unlocked.
-            delete_related_attendees_for_order(self, order)
+            delete_related_attendees_for_order(order)
 
     def before_delete_object(self, order, view_kwargs):
         """


Test patch: