Skip to content

Commit a6c2e2c

Browse files
feat: Schedule invoice notifications (#7338)
1 parent ceb0982 commit a6c2e2c

File tree

1 file changed

+59
-21
lines changed

1 file changed

+59
-21
lines changed

app/api/helpers/scheduled_jobs.py

Lines changed: 59 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,6 @@ def change_session_state_on_event_completion():
7777
save_to_db(session, f'Changed {session.title} session state to rejected')
7878

7979

80-
@celery.task(base=RequestContextTask, name='send.event.fee.notification.followup')
81-
def send_event_fee_notification_followup():
82-
from app.instance import current_app as app
83-
84-
with app.app_context():
85-
incomplete_invoices = EventInvoice.query.filter(
86-
EventInvoice.amount > 0, EventInvoice.status != 'paid'
87-
).all()
88-
for incomplete_invoice in incomplete_invoices:
89-
incomplete_invoice.send_notification(follow_up=True)
90-
91-
9280
@celery.task(base=RequestContextTask, name='expire.pending.tickets')
9381
def expire_pending_tickets():
9482
Order.query.filter(
@@ -140,6 +128,34 @@ def this_month_date() -> datetime.datetime:
140128
).replace(tzinfo=pytz.UTC)
141129

142130

131+
@celery.task(base=RequestContextTask, name='send.event.fee.notification.followup')
132+
def send_event_fee_notification_followup(follow_up=True):
133+
if not follow_up:
134+
logger.warning('Not valid follow up request: %s', follow_up)
135+
return
136+
query = EventInvoice.query.filter(
137+
EventInvoice.amount > 0, EventInvoice.status != 'paid'
138+
)
139+
this_month = this_month_date()
140+
if follow_up != 'post_due':
141+
query = query.filter(EventInvoice.issued_at >= this_month)
142+
else:
143+
# For post due invoices, we want invoices of previous month only
144+
# Because it gets executed on 3rd day of the next month from issued date
145+
last_month = monthdelta(this_month, -1)
146+
query = query.filter(
147+
EventInvoice.issued_at >= last_month, EventInvoice.issued_at < this_month
148+
)
149+
incomplete_invoices = query.all()
150+
logger.info(
151+
'Sending notification %s for %d event invoices',
152+
follow_up,
153+
len(incomplete_invoices),
154+
)
155+
for incomplete_invoice in incomplete_invoices:
156+
send_invoice_notification.delay(incomplete_invoice.id, follow_up=follow_up)
157+
158+
143159
@celery.task(base=RequestContextTask, name='send.monthly.event.invoice')
144160
def send_monthly_event_invoice(send_notification: bool = True):
145161
this_month = this_month_date()
@@ -212,28 +228,50 @@ def send_event_invoice(
212228
else:
213229
logger.warning('Failed to generate event invoice PDF %s', event)
214230
if saved and send_notification:
215-
logger.info('Sending Invoice Notification %s', event_invoice)
216-
event_invoice.send_notification()
231+
send_invoice_notification.delay(event_invoice.id)
217232
return pdf_url
218233
except LockError as e:
219234
logger.exception('Error while acquiring lock. Retrying')
220235
self.retry(exc=e)
221236

222237

238+
@celery.task(base=RequestContextTask)
239+
def send_invoice_notification(invoice_id: int, follow_up: bool = False):
240+
event_invoice = EventInvoice.query.get(invoice_id)
241+
logger.info(
242+
'Sending notification %s for event invoice %s of event %s',
243+
follow_up,
244+
event_invoice,
245+
event_invoice.event,
246+
)
247+
event_invoice.send_notification(follow_up=follow_up)
248+
249+
223250
@celery.on_after_configure.connect
224251
def setup_scheduled_task(sender, **kwargs):
225252
from celery.schedules import crontab
226253

227254
# Every day at 5:30
228255
sender.add_periodic_task(crontab(hour=5, minute=30), send_after_event_mail)
229256
# Every 1st day of month at 0:00
230-
# sender.add_periodic_task(
231-
# crontab(minute=0, hour=0, day_of_month=1), send_event_fee_notification_followup
232-
# )
233-
# Every 1st day of month at 0:00
234-
# sender.add_periodic_task(
235-
# crontab(minute=0, hour=0, day_of_month=1), send_monthly_event_invoice
236-
# )
257+
sender.add_periodic_task(
258+
crontab(minute=0, hour=0, day_of_month=1), send_monthly_event_invoice
259+
)
260+
# Every 14th day of month at 0:00
261+
sender.add_periodic_task(
262+
crontab(minute=0, hour=0, day_of_month=14),
263+
send_event_fee_notification_followup.s(follow_up=True),
264+
)
265+
# Every 27th day of month at 0:00
266+
sender.add_periodic_task(
267+
crontab(minute=0, hour=0, day_of_month=27),
268+
send_event_fee_notification_followup.s(follow_up='pre_due'),
269+
)
270+
# Every 3rd day of next month at 0:00
271+
sender.add_periodic_task(
272+
crontab(minute=0, hour=0, day_of_month=3),
273+
send_event_fee_notification_followup.s(follow_up='post_due'),
274+
)
237275
# Every day at 5:30
238276
sender.add_periodic_task(
239277
crontab(hour=5, minute=30), change_session_state_on_event_completion

0 commit comments

Comments
 (0)