[UPDT] OUTLOOK: Token refresh logic
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user