mirror of
https://github.com/i701/sarlink-portal-api.git
synced 2025-07-08 17:58:21 +00:00
feat(payment): update periodic task to run every minute and improve SMS notification handling for expired topups ✨
All checks were successful
Build and Push Docker Images / Build and Push Docker Images (push) Successful in 5m57s
All checks were successful
Build and Push Docker Images / Build and Push Docker Images (push) Successful in 5m57s
This commit is contained in:
@ -11,13 +11,18 @@ logger = logging.getLogger(__name__)
|
|||||||
|
|
||||||
|
|
||||||
@app.periodic(
|
@app.periodic(
|
||||||
cron="*/30 * * * * *", periodic_id="notify_expired_topups", queue="heavy_tasks"
|
cron="*/1 * * * * *", periodic_id="notify_expired_topups", queue="heavy_tasks"
|
||||||
) # every 30 seconds
|
) # every 1 minute
|
||||||
@app.task
|
@app.task
|
||||||
def update_expired_topups(timestamp: int):
|
def update_expired_topups(timestamp: int):
|
||||||
expired_topups_qs = Topup.objects.filter(
|
expired_topups_qs = Topup.objects.filter(
|
||||||
expires_at__lte=timezone.now(), expiry_notification_sent=False, paid=False
|
expires_at__lte=timezone.now(),
|
||||||
|
expiry_notification_sent=False,
|
||||||
|
paid=False,
|
||||||
).select_related("user")
|
).select_related("user")
|
||||||
|
if not expired_topups_qs.exists():
|
||||||
|
logger.info("No expired topups found.")
|
||||||
|
return {"total_expired_topups": 0}
|
||||||
|
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
count = expired_topups_qs.count()
|
count = expired_topups_qs.count()
|
||||||
@ -30,22 +35,29 @@ def update_expired_topups(timestamp: int):
|
|||||||
amount=topup.amount,
|
amount=topup.amount,
|
||||||
topup_id=str(topup.id),
|
topup_id=str(topup.id),
|
||||||
created_at=localtime(topup.created_at).isoformat(),
|
created_at=localtime(topup.created_at).isoformat(),
|
||||||
|
user=f"{topup.user.first_name + ' ' + topup.user.last_name}"
|
||||||
|
if topup.user.last_name and topup.user.first_name
|
||||||
|
else "User",
|
||||||
)
|
)
|
||||||
|
topup.expiry_notification_sent = True
|
||||||
|
topup.save()
|
||||||
else:
|
else:
|
||||||
# Mark as notified even if we can't send SMS (no mobile number)
|
# Mark as notified even if we can't send SMS (no mobile number)
|
||||||
topup.expiry_notification_sent = True
|
topup.expiry_notification_sent = True
|
||||||
topup.save()
|
topup.save()
|
||||||
|
return
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"total_expired_topups": count,
|
"total_expired_topups": count,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# Assuming you have a separate task for sending SMS if you go that route
|
|
||||||
@app.task
|
@app.task
|
||||||
def send_sms_task(mobile: str, amount: float, topup_id: str, created_at: str):
|
def send_sms_task(
|
||||||
|
user: str, mobile: str, amount: float, topup_id: str, created_at: str
|
||||||
|
):
|
||||||
message = (
|
message = (
|
||||||
f"Dear {mobile}, \n\nYour topup of {amount} MVR [created at {created_at}] has expired. "
|
f"Dear {user}, \n\nYour topup of {amount} MVR [created at {created_at}] has expired. "
|
||||||
"Please make a new topup to update your wallet. \n\n- SAR Link"
|
"Please make a new topup to update your wallet. \n\n- SAR Link"
|
||||||
)
|
)
|
||||||
send_sms(mobile, message)
|
send_sms(mobile, message)
|
||||||
|
Reference in New Issue
Block a user