prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Speakers are queried by role in after event mail cron job but no such role exists
  Currently, speakers are being queried under the assumption that there is a role called speaker, no such role exists.
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `app/api/helpers/scheduled_jobs.py`
  Content:
  ```
  1 import datetime
  2 
  3 import pytz
  4 from dateutil.relativedelta import relativedelta
  5 
  6 from app.api.helpers.db import safe_query, save_to_db
  7 from app.api.helpers.mail import send_email_after_event, send_email_for_monthly_fee_payment, \
  8     send_followup_email_for_monthly_fee_payment
  9 from app.api.helpers.notification import send_notif_monthly_fee_payment, send_followup_notif_monthly_fee_payment, \
  10     send_notif_after_event
  11 from app.api.helpers.query import get_upcoming_events, get_user_event_roles_by_role_name
  12 from app.api.helpers.utilities import monthdelta
  13 from app.models import db
  14 from app.models.event import Event
  15 from app.models.event_invoice import EventInvoice
  16 from app.models.order import Order
  17 from app.models.session import Session
  18 from app.models.ticket import Ticket
  19 from app.models.ticket_fee import get_fee
  20 from app.settings import get_settings
  21 
  22 
  23 def send_after_event_mail():
  24     from app import current_app as app
  25     with app.app_context():
  26         events = Event.query.all()
  27         upcoming_events = get_upcoming_events()
  28         upcoming_event_links = "<ul>"
  29         for upcoming_event in upcoming_events:
  30             frontend_url = get_settings()['frontend_url']
  31             upcoming_event_links += "<li><a href='{}/events/{}'>{}</a></li>" \
  32                 .format(frontend_url, upcoming_event.id, upcoming_event.name)
  33         upcoming_event_links += "</ul>"
  34         for event in events:
  35             organizers = get_user_event_roles_by_role_name(event.id, 'organizer')
  36             speakers = get_user_event_roles_by_role_name(event.id, 'speaker')
  37             current_time = datetime.datetime.now(pytz.timezone(event.timezone))
  38             time_difference = current_time - event.ends_at
  39             time_difference_minutes = (time_difference.days * 24 * 60) + \
  40                 (time_difference.seconds / 60)
  41             if current_time > event.ends_at and time_difference_minutes < 1440:
  42                 for speaker in speakers:
  43                     send_email_after_event(speaker.user.email, event.name, upcoming_event_links)
  44                     send_notif_after_event(speaker.user, event.name)
  45                 for organizer in organizers:
  46                     send_email_after_event(organizer.user.email, event.name, upcoming_event_links)
  47                     send_notif_after_event(organizer.user, event.name)
  48 
  49 
  50 def change_session_state_on_event_completion():
  51     from app import current_app as app
  52     with app.app_context():
  53         sessions_to_be_changed = Session.query.join(Event).filter(Session.state == 'pending')\
  54                                  .filter(Event.ends_at < datetime.datetime.now())
  55         for session in sessions_to_be_changed:
  56             session.state = 'rejected'
  57             save_to_db(session, 'Changed {} session state to rejected'.format(session.title))
  58 
  59 
  60 def send_event_fee_notification():
  61     from app import current_app as app
  62     with app.app_context():
  63         events = Event.query.all()
  64         for event in events:
  65             latest_invoice = EventInvoice.query.filter_by(
  66                 event_id=event.id).order_by(EventInvoice.created_at.desc()).first()
  67 
  68             if latest_invoice:
  69                 orders = Order.query \
  70                     .filter_by(event_id=event.id) \
  71                     .filter_by(status='completed') \
  72                     .filter(Order.completed_at > latest_invoice.created_at).all()
  73             else:
  74                 orders = Order.query.filter_by(
  75                     event_id=event.id).filter_by(status='completed').all()
  76 
  77             fee_total = 0
  78             for order in orders:
  79                 for order_ticket in order.tickets:
  80                     ticket = safe_query(db, Ticket, 'id', order_ticket.ticket_id, 'ticket_id')
  81                     if order.paid_via != 'free' and order.amount > 0 and ticket.price > 0:
  82                         fee = ticket.price * (get_fee(order.event.payment_currency) / 100.0)
  83                         fee_total += fee
  84 
  85             if fee_total > 0:
  86                 organizer = get_user_event_roles_by_role_name(event.id, 'organizer').first()
  87                 new_invoice = EventInvoice(
  88                     amount=fee_total, event_id=event.id, user_id=organizer.user.id)
  89 
  90                 if event.discount_code_id and event.discount_code:
  91                     r = relativedelta(datetime.utcnow(), event.created_at)
  92                     if r <= event.discount_code.valid_till:
  93                         new_invoice.amount = fee_total - \
  94                             (fee_total * (event.discount_code.value / 100.0))
  95                         new_invoice.discount_code_id = event.discount_code_id
  96 
  97                 save_to_db(new_invoice)
  98                 prev_month = monthdelta(new_invoice.created_at, 1).strftime(
  99                     "%b %Y")  # Displayed as Aug 2016
  100                 app_name = get_settings()['app_name']
  101                 frontend_url = get_settings()['frontend_url']
  102                 link = '{}/invoices/{}'.format(frontend_url, new_invoice.identifier)
  103                 send_email_for_monthly_fee_payment(new_invoice.user.email,
  104                                                    event.name,
  105                                                    prev_month,
  106                                                    new_invoice.amount,
  107                                                    app_name,
  108                                                    link)
  109                 send_notif_monthly_fee_payment(new_invoice.user,
  110                                                event.name,
  111                                                prev_month,
  112                                                new_invoice.amount,
  113                                                app_name,
  114                                                link,
  115                                                new_invoice.event_id)
  116 
  117 
  118 def send_event_fee_notification_followup():
  119     from app import current_app as app
  120     with app.app_context():
  121         incomplete_invoices = EventInvoice.query.filter(EventInvoice.status != 'completed').all()
  122         for incomplete_invoice in incomplete_invoices:
  123             if incomplete_invoice.amount > 0:
  124                 prev_month = monthdelta(incomplete_invoice.created_at, 1).strftime(
  125                     "%b %Y")  # Displayed as Aug 2016
  126                 app_name = get_settings()['app_name']
  127                 frontend_url = get_settings()['frontend_url']
  128                 link = '{}/invoices/{}'.format(frontend_url,
  129                                                incomplete_invoice.identifier)
  130                 send_followup_email_for_monthly_fee_payment(incomplete_invoice.user.email,
  131                                                             incomplete_invoice.event.name,
  132                                                             prev_month,
  133                                                             incomplete_invoice.amount,
  134                                                             app_name,
  135                                                             link)
  136                 send_followup_notif_monthly_fee_payment(incomplete_invoice.user,
  137                                                         incomplete_invoice.event.name,
  138                                                         prev_month,
  139                                                         incomplete_invoice.amount,
  140                                                         app_name,
  141                                                         link,
  142                                                         incomplete_invoice.event.id)
  143 
  144 
  145 def expire_pending_tickets():
  146     from app import current_app as app
  147     with app.app_context():
  148         db.session.query(Order).filter(Order.status == 'pending',
  149                                        (Order.created_at + datetime.timedelta(minutes=30)) <= datetime.datetime.now()).\
  150                                        update({'status': 'expired'})
  151         db.session.commit()
  152
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/app/api/helpers/scheduled_jobs.py b/app/api/helpers/scheduled_jobs.py
  --- a/app/api/helpers/scheduled_jobs.py
  +++ b/app/api/helpers/scheduled_jobs.py
  @@ -14,6 +14,7 @@
   from app.models.event import Event
   from app.models.event_invoice import EventInvoice
   from app.models.order import Order
  +from app.models.speaker import Speaker
   from app.models.session import Session
   from app.models.ticket import Ticket
   from app.models.ticket_fee import get_fee
  @@ -33,7 +34,7 @@
           upcoming_event_links += "</ul>"
           for event in events:
               organizers = get_user_event_roles_by_role_name(event.id, 'organizer')
  -            speakers = get_user_event_roles_by_role_name(event.id, 'speaker')
  +            speakers = Speaker.query.filter_by(event_id=event.id, deleted_at=None).all()
               current_time = datetime.datetime.now(pytz.timezone(event.timezone))
               time_difference = current_time - event.ends_at
               time_difference_minutes = (time_difference.days * 24 * 60) + \
  
verification_info: {"golden_diff": "diff --git a/app/api/helpers/scheduled_jobs.py b/app/api/helpers/scheduled_jobs.py\n--- a/app/api/helpers/scheduled_jobs.py\n+++ b/app/api/helpers/scheduled_jobs.py\n@@ -14,6 +14,7 @@\n from app.models.event import Event\n from app.models.event_invoice import EventInvoice\n from app.models.order import Order\n+from app.models.speaker import Speaker\n from app.models.session import Session\n from app.models.ticket import Ticket\n from app.models.ticket_fee import get_fee\n@@ -33,7 +34,7 @@\n         upcoming_event_links += \"</ul>\"\n         for event in events:\n             organizers = get_user_event_roles_by_role_name(event.id, 'organizer')\n-            speakers = get_user_event_roles_by_role_name(event.id, 'speaker')\n+            speakers = Speaker.query.filter_by(event_id=event.id, deleted_at=None).all()\n             current_time = datetime.datetime.now(pytz.timezone(event.timezone))\n             time_difference = current_time - event.ends_at\n             time_difference_minutes = (time_difference.days * 24 * 60) + \\\n", "issue": "Speakers are queried by role in after event mail cron job but no such role exists\nCurrently, speakers are being queried under the assumption that there is a role called speaker, no such role exists.\n", "before_files": [{"content": "import datetime\n\nimport pytz\nfrom dateutil.relativedelta import relativedelta\n\nfrom app.api.helpers.db import safe_query, save_to_db\nfrom app.api.helpers.mail import send_email_after_event, send_email_for_monthly_fee_payment, \\\n    send_followup_email_for_monthly_fee_payment\nfrom app.api.helpers.notification import send_notif_monthly_fee_payment, send_followup_notif_monthly_fee_payment, \\\n    send_notif_after_event\nfrom app.api.helpers.query import get_upcoming_events, get_user_event_roles_by_role_name\nfrom app.api.helpers.utilities import monthdelta\nfrom app.models import db\nfrom app.models.event import Event\nfrom app.models.event_invoice import EventInvoice\nfrom app.models.order import Order\nfrom app.models.session import Session\nfrom app.models.ticket import Ticket\nfrom app.models.ticket_fee import get_fee\nfrom app.settings import get_settings\n\n\ndef send_after_event_mail():\n    from app import current_app as app\n    with app.app_context():\n        events = Event.query.all()\n        upcoming_events = get_upcoming_events()\n        upcoming_event_links = \"<ul>\"\n        for upcoming_event in upcoming_events:\n            frontend_url = get_settings()['frontend_url']\n            upcoming_event_links += \"<li><a href='{}/events/{}'>{}</a></li>\" \\\n                .format(frontend_url, upcoming_event.id, upcoming_event.name)\n        upcoming_event_links += \"</ul>\"\n        for event in events:\n            organizers = get_user_event_roles_by_role_name(event.id, 'organizer')\n            speakers = get_user_event_roles_by_role_name(event.id, 'speaker')\n            current_time = datetime.datetime.now(pytz.timezone(event.timezone))\n            time_difference = current_time - event.ends_at\n            time_difference_minutes = (time_difference.days * 24 * 60) + \\\n                (time_difference.seconds / 60)\n            if current_time > event.ends_at and time_difference_minutes < 1440:\n                for speaker in speakers:\n                    send_email_after_event(speaker.user.email, event.name, upcoming_event_links)\n                    send_notif_after_event(speaker.user, event.name)\n                for organizer in organizers:\n                    send_email_after_event(organizer.user.email, event.name, upcoming_event_links)\n                    send_notif_after_event(organizer.user, event.name)\n\n\ndef change_session_state_on_event_completion():\n    from app import current_app as app\n    with app.app_context():\n        sessions_to_be_changed = Session.query.join(Event).filter(Session.state == 'pending')\\\n                                 .filter(Event.ends_at < datetime.datetime.now())\n        for session in sessions_to_be_changed:\n            session.state = 'rejected'\n            save_to_db(session, 'Changed {} session state to rejected'.format(session.title))\n\n\ndef send_event_fee_notification():\n    from app import current_app as app\n    with app.app_context():\n        events = Event.query.all()\n        for event in events:\n            latest_invoice = EventInvoice.query.filter_by(\n                event_id=event.id).order_by(EventInvoice.created_at.desc()).first()\n\n            if latest_invoice:\n                orders = Order.query \\\n                    .filter_by(event_id=event.id) \\\n                    .filter_by(status='completed') \\\n                    .filter(Order.completed_at > latest_invoice.created_at).all()\n            else:\n                orders = Order.query.filter_by(\n                    event_id=event.id).filter_by(status='completed').all()\n\n            fee_total = 0\n            for order in orders:\n                for order_ticket in order.tickets:\n                    ticket = safe_query(db, Ticket, 'id', order_ticket.ticket_id, 'ticket_id')\n                    if order.paid_via != 'free' and order.amount > 0 and ticket.price > 0:\n                        fee = ticket.price * (get_fee(order.event.payment_currency) / 100.0)\n                        fee_total += fee\n\n            if fee_total > 0:\n                organizer = get_user_event_roles_by_role_name(event.id, 'organizer').first()\n                new_invoice = EventInvoice(\n                    amount=fee_total, event_id=event.id, user_id=organizer.user.id)\n\n                if event.discount_code_id and event.discount_code:\n                    r = relativedelta(datetime.utcnow(), event.created_at)\n                    if r <= event.discount_code.valid_till:\n                        new_invoice.amount = fee_total - \\\n                            (fee_total * (event.discount_code.value / 100.0))\n                        new_invoice.discount_code_id = event.discount_code_id\n\n                save_to_db(new_invoice)\n                prev_month = monthdelta(new_invoice.created_at, 1).strftime(\n                    \"%b %Y\")  # Displayed as Aug 2016\n                app_name = get_settings()['app_name']\n                frontend_url = get_settings()['frontend_url']\n                link = '{}/invoices/{}'.format(frontend_url, new_invoice.identifier)\n                send_email_for_monthly_fee_payment(new_invoice.user.email,\n                                                   event.name,\n                                                   prev_month,\n                                                   new_invoice.amount,\n                                                   app_name,\n                                                   link)\n                send_notif_monthly_fee_payment(new_invoice.user,\n                                               event.name,\n                                               prev_month,\n                                               new_invoice.amount,\n                                               app_name,\n                                               link,\n                                               new_invoice.event_id)\n\n\ndef send_event_fee_notification_followup():\n    from app import current_app as app\n    with app.app_context():\n        incomplete_invoices = EventInvoice.query.filter(EventInvoice.status != 'completed').all()\n        for incomplete_invoice in incomplete_invoices:\n            if incomplete_invoice.amount > 0:\n                prev_month = monthdelta(incomplete_invoice.created_at, 1).strftime(\n                    \"%b %Y\")  # Displayed as Aug 2016\n                app_name = get_settings()['app_name']\n                frontend_url = get_settings()['frontend_url']\n                link = '{}/invoices/{}'.format(frontend_url,\n                                               incomplete_invoice.identifier)\n                send_followup_email_for_monthly_fee_payment(incomplete_invoice.user.email,\n                                                            incomplete_invoice.event.name,\n                                                            prev_month,\n                                                            incomplete_invoice.amount,\n                                                            app_name,\n                                                            link)\n                send_followup_notif_monthly_fee_payment(incomplete_invoice.user,\n                                                        incomplete_invoice.event.name,\n                                                        prev_month,\n                                                        incomplete_invoice.amount,\n                                                        app_name,\n                                                        link,\n                                                        incomplete_invoice.event.id)\n\n\ndef expire_pending_tickets():\n    from app import current_app as app\n    with app.app_context():\n        db.session.query(Order).filter(Order.status == 'pending',\n                                       (Order.created_at + datetime.timedelta(minutes=30)) <= datetime.datetime.now()).\\\n                                       update({'status': 'expired'})\n        db.session.commit()\n", "path": "app/api/helpers/scheduled_jobs.py"}], "after_files": [{"content": "import datetime\n\nimport pytz\nfrom dateutil.relativedelta import relativedelta\n\nfrom app.api.helpers.db import safe_query, save_to_db\nfrom app.api.helpers.mail import send_email_after_event, send_email_for_monthly_fee_payment, \\\n    send_followup_email_for_monthly_fee_payment\nfrom app.api.helpers.notification import send_notif_monthly_fee_payment, send_followup_notif_monthly_fee_payment, \\\n    send_notif_after_event\nfrom app.api.helpers.query import get_upcoming_events, get_user_event_roles_by_role_name\nfrom app.api.helpers.utilities import monthdelta\nfrom app.models import db\nfrom app.models.event import Event\nfrom app.models.event_invoice import EventInvoice\nfrom app.models.order import Order\nfrom app.models.speaker import Speaker\nfrom app.models.session import Session\nfrom app.models.ticket import Ticket\nfrom app.models.ticket_fee import get_fee\nfrom app.settings import get_settings\n\n\ndef send_after_event_mail():\n    from app import current_app as app\n    with app.app_context():\n        events = Event.query.all()\n        upcoming_events = get_upcoming_events()\n        upcoming_event_links = \"<ul>\"\n        for upcoming_event in upcoming_events:\n            frontend_url = get_settings()['frontend_url']\n            upcoming_event_links += \"<li><a href='{}/events/{}'>{}</a></li>\" \\\n                .format(frontend_url, upcoming_event.id, upcoming_event.name)\n        upcoming_event_links += \"</ul>\"\n        for event in events:\n            organizers = get_user_event_roles_by_role_name(event.id, 'organizer')\n            speakers = Speaker.query.filter_by(event_id=event.id, deleted_at=None).all()\n            current_time = datetime.datetime.now(pytz.timezone(event.timezone))\n            time_difference = current_time - event.ends_at\n            time_difference_minutes = (time_difference.days * 24 * 60) + \\\n                (time_difference.seconds / 60)\n            if current_time > event.ends_at and time_difference_minutes < 1440:\n                for speaker in speakers:\n                    send_email_after_event(speaker.user.email, event.name, upcoming_event_links)\n                    send_notif_after_event(speaker.user, event.name)\n                for organizer in organizers:\n                    send_email_after_event(organizer.user.email, event.name, upcoming_event_links)\n                    send_notif_after_event(organizer.user, event.name)\n\n\ndef change_session_state_on_event_completion():\n    from app import current_app as app\n    with app.app_context():\n        sessions_to_be_changed = Session.query.join(Event).filter(Session.state == 'pending')\\\n                                 .filter(Event.ends_at < datetime.datetime.now())\n        for session in sessions_to_be_changed:\n            session.state = 'rejected'\n            save_to_db(session, 'Changed {} session state to rejected'.format(session.title))\n\n\ndef send_event_fee_notification():\n    from app import current_app as app\n    with app.app_context():\n        events = Event.query.all()\n        for event in events:\n            latest_invoice = EventInvoice.query.filter_by(\n                event_id=event.id).order_by(EventInvoice.created_at.desc()).first()\n\n            if latest_invoice:\n                orders = Order.query \\\n                    .filter_by(event_id=event.id) \\\n                    .filter_by(status='completed') \\\n                    .filter(Order.completed_at > latest_invoice.created_at).all()\n            else:\n                orders = Order.query.filter_by(\n                    event_id=event.id).filter_by(status='completed').all()\n\n            fee_total = 0\n            for order in orders:\n                for order_ticket in order.tickets:\n                    ticket = safe_query(db, Ticket, 'id', order_ticket.ticket_id, 'ticket_id')\n                    if order.paid_via != 'free' and order.amount > 0 and ticket.price > 0:\n                        fee = ticket.price * (get_fee(order.event.payment_currency) / 100.0)\n                        fee_total += fee\n\n            if fee_total > 0:\n                organizer = get_user_event_roles_by_role_name(event.id, 'organizer').first()\n                new_invoice = EventInvoice(\n                    amount=fee_total, event_id=event.id, user_id=organizer.user.id)\n\n                if event.discount_code_id and event.discount_code:\n                    r = relativedelta(datetime.utcnow(), event.created_at)\n                    if r <= event.discount_code.valid_till:\n                        new_invoice.amount = fee_total - \\\n                            (fee_total * (event.discount_code.value / 100.0))\n                        new_invoice.discount_code_id = event.discount_code_id\n\n                save_to_db(new_invoice)\n                prev_month = monthdelta(new_invoice.created_at, 1).strftime(\n                    \"%b %Y\")  # Displayed as Aug 2016\n                app_name = get_settings()['app_name']\n                frontend_url = get_settings()['frontend_url']\n                link = '{}/invoices/{}'.format(frontend_url, new_invoice.identifier)\n                send_email_for_monthly_fee_payment(new_invoice.user.email,\n                                                   event.name,\n                                                   prev_month,\n                                                   new_invoice.amount,\n                                                   app_name,\n                                                   link)\n                send_notif_monthly_fee_payment(new_invoice.user,\n                                               event.name,\n                                               prev_month,\n                                               new_invoice.amount,\n                                               app_name,\n                                               link,\n                                               new_invoice.event_id)\n\n\ndef send_event_fee_notification_followup():\n    from app import current_app as app\n    with app.app_context():\n        incomplete_invoices = EventInvoice.query.filter(EventInvoice.status != 'completed').all()\n        for incomplete_invoice in incomplete_invoices:\n            if incomplete_invoice.amount > 0:\n                prev_month = monthdelta(incomplete_invoice.created_at, 1).strftime(\n                    \"%b %Y\")  # Displayed as Aug 2016\n                app_name = get_settings()['app_name']\n                frontend_url = get_settings()['frontend_url']\n                link = '{}/invoices/{}'.format(frontend_url,\n                                               incomplete_invoice.identifier)\n                send_followup_email_for_monthly_fee_payment(incomplete_invoice.user.email,\n                                                            incomplete_invoice.event.name,\n                                                            prev_month,\n                                                            incomplete_invoice.amount,\n                                                            app_name,\n                                                            link)\n                send_followup_notif_monthly_fee_payment(incomplete_invoice.user,\n                                                        incomplete_invoice.event.name,\n                                                        prev_month,\n                                                        incomplete_invoice.amount,\n                                                        app_name,\n                                                        link,\n                                                        incomplete_invoice.event.id)\n\n\ndef expire_pending_tickets():\n    from app import current_app as app\n    with app.app_context():\n        db.session.query(Order).filter(Order.status == 'pending',\n                                       (Order.created_at + datetime.timedelta(minutes=30)) <= datetime.datetime.now()).\\\n                                       update({'status': 'expired'})\n        db.session.commit()\n", "path": "app/api/helpers/scheduled_jobs.py"}]}