From 20cd71ea5bbd9acf210e623e2d7f06e9068c38ec Mon Sep 17 00:00:00 2001
From: pabois <pierreandre.boissinot@noesya.coop>
Date: Mon, 18 Sep 2023 12:06:19 +0200
Subject: [PATCH] add sms credits warning

---
 app/jobs/sendinblue/sms_credits_warning_job.rb  | 17 +++++++++++++++++
 app/mailers/notification_mailer.rb              |  8 ++++++++
 .../notifications/low_sms_credits.html.erb      |  3 +++
 config/locales/fr.yml                           |  4 ++++
 .../previews/notification_mailer_preview.rb     | 17 +++++++++++++++--
 5 files changed, 47 insertions(+), 2 deletions(-)
 create mode 100644 app/jobs/sendinblue/sms_credits_warning_job.rb
 create mode 100644 app/views/mailers/notifications/low_sms_credits.html.erb

diff --git a/app/jobs/sendinblue/sms_credits_warning_job.rb b/app/jobs/sendinblue/sms_credits_warning_job.rb
new file mode 100644
index 000000000..a460aee09
--- /dev/null
+++ b/app/jobs/sendinblue/sms_credits_warning_job.rb
@@ -0,0 +1,17 @@
+class Sendinblue::SmsCreditsWarningJob < ApplicationJob
+  queue_as :default
+
+  SMS_CREDITS_LIMIT = 500
+
+  def perform
+    return unless ENV['APPLICATION_ENV'] == 'production'
+    api_instance = SibApiV3Sdk::AccountApi.new
+    result = api_instance.get_account
+    sms_credits = result.plan.detect { |plan| plan.type == 'sms' }&.credits
+    if sms_credits.present? && sms_credits < SMS_CREDITS_LIMIT
+      # this message is sent to server_admins only, and server_admins are duplicated between all universities.
+      # so we take the first university
+      NotificationMailer.low_sms_credits(University.first, sms_credits).deliver_later
+    end
+  end
+end
\ No newline at end of file
diff --git a/app/mailers/notification_mailer.rb b/app/mailers/notification_mailer.rb
index 0f4326c20..c4c9aa3b1 100644
--- a/app/mailers/notification_mailer.rb
+++ b/app/mailers/notification_mailer.rb
@@ -29,5 +29,13 @@ class NotificationMailer < ApplicationMailer
     subject = t('mailers.notifications.website_invalid_access_token.subject', website: website)
     mail(from: user.university.mail_from[:full], to: user.email, subject: subject)
   end
+  
+  def low_sms_credits(university, credits)
+    merge_with_university_infos(university, {})
+    @credits = credits.to_i
+    mails = university.users.server_admin.pluck(:email)
+    subject = t('mailers.notifications.low_sms_credits.subject', credits: @credits)
+    mail(from: university.mail_from[:full], to: mails, subject: subject)
+  end
 
 end
diff --git a/app/views/mailers/notifications/low_sms_credits.html.erb b/app/views/mailers/notifications/low_sms_credits.html.erb
new file mode 100644
index 000000000..1407e0440
--- /dev/null
+++ b/app/views/mailers/notifications/low_sms_credits.html.erb
@@ -0,0 +1,3 @@
+<%= t('mailers.notifications.low_sms_credits.body_1_html', credits: @credits) %>
+<br>
+<%= t('mailers.notifications.low_sms_credits.body_2_html', link: 'https://app.sendinblue.com/billing/addon/customize/sms') %>
\ No newline at end of file
diff --git a/config/locales/fr.yml b/config/locales/fr.yml
index aa61ec47d..0b58be5c5 100644
--- a/config/locales/fr.yml
+++ b/config/locales/fr.yml
@@ -270,6 +270,10 @@ fr:
         text_line_3_html: "Nombre de lignes traitées : %{number}."
         text_error_msg: "Ligne %{line} : %{error}"
         text_errors_title: "Des erreurs sont survenues :"
+      low_sms_credits: 
+        body_1_html: "Attention, vos crédits SMS sont bas : %{credits} crédits restants !"
+        body_2_html: "Cliquez <a href=\"%{link}\" target=\"_blank\" style=\"color: #c72b43;\">ici</a> pour recharger le compte."
+        subject: "Osuny - Credits SMS bas (%{credits})"
       website_invalid_access_token:
         subject: Jeton d'accès expiré pour « %{website} »
         text_line_1_html: Le jeton d'accès utilisé pour le site « %{website} » a expiré et ne permet plus la mise à jour du site.
diff --git a/test/mailers/previews/notification_mailer_preview.rb b/test/mailers/previews/notification_mailer_preview.rb
index 332563510..d5557f836 100644
--- a/test/mailers/previews/notification_mailer_preview.rb
+++ b/test/mailers/previews/notification_mailer_preview.rb
@@ -1,14 +1,27 @@
+# Preview all emails at http://localhost:3000/rails/mailers/notification_mailer
+
 class NotificationMailerPreview < BaseMailerPreview
+
+  # Preview this email at http://localhost:3000/rails/mailers/notification_mailer/import
   def import
     NotificationMailer.import(organizations_import)
   end
-
+  
+  # Preview this email at http://localhost:3000/rails/mailers/notification_mailer/emergency_message
   def emergency_message
     NotificationMailer.emergency_message(sample_emergency_message, user, 'fr')
   end
-
+  
+  # Preview this email at http://localhost:3000/rails/mailers/notification_mailer/website_invalid_access_token
   def website_invalid_access_token
     NotificationMailer.website_invalid_access_token(website, user)
   end
 
+  # Preview this email at http://localhost:3000/rails/mailers/notification_mailer/low_sms_credits
+  def low_sms_credits
+    credits = 22.0
+    NotificationMailer.low_sms_credits(university, credits)
+  end
+
+
 end
-- 
GitLab