[UPDT] OUTLOOK: Token refresh logic

This commit is contained in:
Horilla
2025-03-06 12:10:01 +05:30
parent 88a4cdd2ac
commit fbd556a9b9
3 changed files with 14 additions and 3 deletions

View File

@@ -71,3 +71,14 @@ class AzureApi(models.Model):
if duration_seconds > expires_in_seconds:
return _("Expired⚠")
return f"{display}/{sec_to_hm(expires_in_seconds)}"
def is_token_expired(self):
"""
is token expired
"""
if self.last_refreshed:
duration_seconds = (timezone.now() - self.last_refreshed).seconds
expires_in_seconds = self.token.get("expires_in")
if duration_seconds > expires_in_seconds:
return True
return False

View File

@@ -36,7 +36,7 @@ if not any(
scheduler.add_job(
refresh_outlook_auth_token,
"interval",
minutes=30,
minutes=50,
id="refresh_outlook_auth_token",
)
scheduler.start()

View File

@@ -132,8 +132,8 @@ def send_outlook_email(request, email_data=None):
else:
api = models.AzureApi.objects.filter(company=selected_company).first()
token = api.token
refresh_outlook_token(api)
if not token and not api.is_token_expired():
refresh_outlook_token(api)
if not token and request:
messages.info(request, "Mail not sent")
return redirect("outlook_login")