# Buggy code
def send_after_event_mail():
    from app import current_app as app

    with app.app_context():
        events = Event.query.all()
        upcoming_events = get_upcoming_events()
        upcoming_event_links = "<ul>"
        for upcoming_event in upcoming_events:
            frontend_url = get_settings()["frontend_url"]
            upcoming_event_links += "<li><a href='{}/events/{}'>{}</a></li>".format(
                frontend_url, upcoming_event.id, upcoming_event.name
            )
        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")
            current_time = datetime.datetime.now(pytz.timezone(event.timezone))
            time_difference = current_time - event.ends_at
            time_difference_minutes = (time_difference.days * 24 * 60) + (
                time_difference.seconds / 60
            )
            if current_time > event.ends_at and time_difference_minutes < 1440:
                for speaker in speakers:
                    send_email_after_event(
                        speaker.user.email, event.name, upcoming_event_links
                    )
                    send_notif_after_event(speaker.user, event.name)
                for organizer in organizers:
                    send_email_after_event(
                        organizer.user.email, event.name, upcoming_event_links
                    )
                    send_notif_after_event(organizer.user.email, event.name)


# Traceback (AttributeError)
2019-06-22T05:30:05.422615216Z ERROR:apscheduler.executors.default:Job "send_after_event_mail (trigger: cron[hour='5', minute='30'], next run at: 2019-06-23 05:30:00 UTC)" raised an exception
2019-06-22T05:30:05.422679235Z Traceback (most recent call last):
2019-06-22T05:30:05.422686046Z   File "/usr/local/lib/python3.6/site-packages/apscheduler/executors/base.py", line 125, in run_job
2019-06-22T05:30:05.422691849Z     retval = job.func(*job.args, **job.kwargs)
2019-06-22T05:30:05.422696907Z   File "/data/app/app/api/helpers/scheduled_jobs.py", line 47, in send_after_event_mail
2019-06-22T05:30:05.422702403Z     send_notif_after_event(organizer.user.email, event.name)
2019-06-22T05:30:05.422707205Z   File "/data/app/app/api/helpers/notification.py", line 221, in send_notif_after_event
2019-06-22T05:30:05.422712346Z     send_notification(user, title, message)
2019-06-22T05:30:05.422717388Z   File "/data/app/app/api/helpers/notification.py", line 27, in send_notification
2019-06-22T05:30:05.422722453Z     notification = Notification(user_id=user.id,
2019-06-22T05:30:05.422727394Z AttributeError: 'str' object has no attribute 'id'
2019-06-22T06:15:52.785255345Z ERROR:flask_jwt:Bad Request. Invalid credentials

# Fixed code
def send_after_event_mail():
    from app import current_app as app

    with app.app_context():
        events = Event.query.all()
        upcoming_events = get_upcoming_events()
        upcoming_event_links = "<ul>"
        for upcoming_event in upcoming_events:
            frontend_url = get_settings()["frontend_url"]
            upcoming_event_links += "<li><a href='{}/events/{}'>{}</a></li>".format(
                frontend_url, upcoming_event.id, upcoming_event.name
            )
        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")
            current_time = datetime.datetime.now(pytz.timezone(event.timezone))
            time_difference = current_time - event.ends_at
            time_difference_minutes = (time_difference.days * 24 * 60) + (
                time_difference.seconds / 60
            )
            if current_time > event.ends_at and time_difference_minutes < 1440:
                for speaker in speakers:
                    send_email_after_event(
                        speaker.user.email, event.name, upcoming_event_links
                    )
                    send_notif_after_event(speaker.user, event.name)
                for organizer in organizers:
                    send_email_after_event(
                        organizer.user.email, event.name, upcoming_event_links
                    )
                    send_notif_after_event(organizer.user, event.name)