refactor(tasks): replace Enum with string literals for notification types in SMS tasks 🔨

This commit is contained in:
2025-07-09 20:32:04 +05:00
parent 911d01b8e3
commit 6418b1469d

View File

@ -7,16 +7,10 @@ from api.notifications import send_sms
from billing.models import Topup, Payment from billing.models import Topup, Payment
from django.utils.timezone import localtime from django.utils.timezone import localtime
from datetime import datetime from datetime import datetime
from enum import Enum
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class NotificationType(Enum):
TOPUP = "TOPUP"
PAYMENT = "PAYMENT"
@app.periodic( @app.periodic(
cron="*/1 * * * * *", periodic_id="notify_expired_topups", queue="heavy_tasks" cron="*/1 * * * * *", periodic_id="notify_expired_topups", queue="heavy_tasks"
) )
@ -39,7 +33,7 @@ def update_expired_topups(timestamp: int):
if topup.user and topup.user.mobile and not topup.expiry_notification_sent: if topup.user and topup.user.mobile and not topup.expiry_notification_sent:
send_sms_task.defer( send_sms_task.defer(
mobile=topup.user.mobile, mobile=topup.user.mobile,
type=NotificationType.TOPUP, type="TOPUP",
amount=topup.amount, amount=topup.amount,
model_id=str(topup.id), model_id=str(topup.id),
created_at=localtime(topup.created_at).isoformat(), created_at=localtime(topup.created_at).isoformat(),
@ -85,7 +79,7 @@ def update_expired_payments(timestamp: int):
): ):
send_sms_task.defer( send_sms_task.defer(
mobile=payment.user.mobile, mobile=payment.user.mobile,
type=NotificationType.PAYMENT, type="PAYMENT",
amount=payment.amount, amount=payment.amount,
model_id=str(payment.id), model_id=str(payment.id),
created_at=localtime(payment.created_at).isoformat(), created_at=localtime(payment.created_at).isoformat(),
@ -112,7 +106,7 @@ def send_sms_task(
amount: float, amount: float,
model_id: str, model_id: str,
created_at: str, created_at: str,
type: NotificationType = NotificationType.TOPUP, type: str = "TOPUP", # Default to TOPUP if not provided,
): ):
try: try:
dt = datetime.fromisoformat(created_at) dt = datetime.fromisoformat(created_at)
@ -120,17 +114,17 @@ def send_sms_task(
except Exception: except Exception:
formatted_date = created_at formatted_date = created_at
message: str = "" message: str = ""
if type == NotificationType.TOPUP: if type == "TOPUP":
message = ( message = (
f"Dear {user}, \n\nYour topup of {amount} MVR [created at {formatted_date}] has expired. " f"Dear {user}, \n\nYour topup of {amount} MVR [created at {formatted_date}] 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"
) )
elif type == NotificationType.PAYMENT: elif type == "PAYMENT":
message = f"Dear {user}, \n\nYour payment of {amount} MVR [created at {formatted_date}] has expired. \n\n- SAR Link" message = f"Dear {user}, \n\nYour payment of {amount} MVR [created at {formatted_date}] has expired. \n\n- SAR Link"
send_sms(mobile, message) send_sms(mobile, message)
logger.info(f"SMS sent to {mobile} for expired {type.value} of {amount} MVR.") logger.info(f"SMS sent to {mobile} for expired {type} of {amount} MVR.")
if type == NotificationType.TOPUP: if type == "TOPUP":
try: try:
topup = Topup.objects.get(id=model_id) topup = Topup.objects.get(id=model_id)
topup.expiry_notification_sent = True topup.expiry_notification_sent = True