# Buggy code
def send_event_fee_notification():
    from app.instance import current_app as app

    with app.app_context():
        events = Event.query.filter_by(deleted_at=None, state="published").all()
        for event in events:
            latest_invoice = (
                EventInvoice.query.filter_by(event_id=event.id)
                .order_by(EventInvoice.created_at.desc())
                .first()
            )

            if latest_invoice:
                orders = (
                    Order.query.filter_by(event_id=event.id)
                    .filter_by(status="completed")
                    .filter(Order.completed_at > latest_invoice.created_at)
                    .all()
                )
            else:
                orders = (
                    Order.query.filter_by(event_id=event.id)
                    .filter_by(status="completed")
                    .all()
                )

            fee_total = 0
            for order in orders:
                for order_ticket in order.tickets:
                    ticket = safe_query(
                        db, Ticket, "id", order_ticket.ticket_id, "ticket_id"
                    )
                    if (
                        order.paid_via != "free"
                        and order.amount > 0
                        and ticket.price > 0
                    ):
                        fee = ticket.price * (
                            get_fee(event.payment_country, order.event.payment_currency)
                            / 100.0
                        )
                        fee_total += fee

            if fee_total > 0:
                owner = get_user_event_roles_by_role_name(event.id, "owner").first()
                new_invoice = EventInvoice(
                    amount=fee_total, event_id=event.id, user_id=owner.user.id
                )

                if event.discount_code_id and event.discount_code:
                    r = relativedelta(datetime.datetime.utcnow(), event.created_at)
                    if r <= event.discount_code.valid_till:
                        new_invoice.amount = fee_total - (
                            fee_total * (event.discount_code.value / 100.0)
                        )
                        new_invoice.discount_code_id = event.discount_code_id

                save_to_db(new_invoice)
                prev_month = monthdelta(new_invoice.created_at, 1).strftime(
                    "%b %Y"
                )  # Displayed as Aug 2016
                app_name = get_settings()["app_name"]
                frontend_url = get_settings()["frontend_url"]
                link = "{}/invoices/{}".format(frontend_url, new_invoice.identifier)
                send_email_for_monthly_fee_payment(
                    new_invoice.user.email,
                    event.name,
                    prev_month,
                    new_invoice.amount,
                    app_name,
                    link,
                )
                send_notif_monthly_fee_payment(
                    new_invoice.user,
                    event.name,
                    prev_month,
                    new_invoice.amount,
                    app_name,
                    link,
                    new_invoice.event_id,
                )


# Traceback (AttributeError)
raised unexpected: AttributeError("'Ticket' object has no attribute 'ticket_id'")
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/celery/app/trace.py", line 385, in trace_task
R = retval = fun(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/flask_celeryext/app.py", line 101, in __call__
res = Task.__call__(self, *args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/celery/app/trace.py", line 650, in __protected_call__
return self.run(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/sentry_sdk/integrations/celery.py", line 161, in _inner
reraise(*exc_info)
File "/usr/local/lib/python3.7/site-packages/sentry_sdk/_compat.py", line 57, in reraise
raise value
File "/usr/local/lib/python3.7/site-packages/sentry_sdk/integrations/celery.py", line 156, in _inner
return f(*args, **kwargs)
File "/data/app/app/api/helpers/scheduled_jobs.py", line 90, in send_event_fee_notification
ticket = safe_query(db, Ticket, 'id', order_ticket.ticket_id, 'ticket_id')
AttributeError: 'Ticket' object has no attribute 'ticket_id'

# Fixed code
def send_event_fee_notification():
    from app.instance import current_app as app

    with app.app_context():
        events = Event.query.filter_by(deleted_at=None, state="published").all()
        for event in events:
            latest_invoice = (
                EventInvoice.query.filter_by(event_id=event.id)
                .order_by(EventInvoice.created_at.desc())
                .first()
            )

            if latest_invoice:
                orders = (
                    Order.query.filter_by(event_id=event.id)
                    .filter_by(status="completed")
                    .filter(Order.completed_at > latest_invoice.created_at)
                    .all()
                )
            else:
                orders = (
                    Order.query.filter_by(event_id=event.id)
                    .filter_by(status="completed")
                    .all()
                )

            fee_total = 0
            for order in orders:
                for ticket in order.tickets:
                    if (
                        order.paid_via != "free"
                        and order.amount > 0
                        and ticket.price > 0
                    ):
                        fee = ticket.price * (
                            get_fee(event.payment_country, order.event.payment_currency)
                            / 100.0
                        )
                        fee_total += fee

            if fee_total > 0:
                owner = get_user_event_roles_by_role_name(event.id, "owner").first()
                new_invoice = EventInvoice(
                    amount=fee_total, event_id=event.id, user_id=owner.user.id
                )

                if event.discount_code_id and event.discount_code:
                    r = relativedelta(datetime.datetime.utcnow(), event.created_at)
                    if r <= event.discount_code.valid_till:
                        new_invoice.amount = fee_total - (
                            fee_total * (event.discount_code.value / 100.0)
                        )
                        new_invoice.discount_code_id = event.discount_code_id

                save_to_db(new_invoice)
                prev_month = monthdelta(new_invoice.created_at, 1).strftime(
                    "%b %Y"
                )  # Displayed as Aug 2016
                app_name = get_settings()["app_name"]
                frontend_url = get_settings()["frontend_url"]
                link = "{}/invoices/{}".format(frontend_url, new_invoice.identifier)
                send_email_for_monthly_fee_payment(
                    new_invoice.user.email,
                    event.name,
                    prev_month,
                    new_invoice.amount,
                    app_name,
                    link,
                )
                send_notif_monthly_fee_payment(
                    new_invoice.user,
                    event.name,
                    prev_month,
                    new_invoice.amount,
                    app_name,
                    link,
                    new_invoice.event_id,
                )