Skip to content

Commit

Permalink
feat: add rake task to generate a csv file with users with pending vo…
Browse files Browse the repository at this point in the history
…te (#557)

* feat: add rake task to generate a csv file with  users with pending vote

* SMS Gateway call in rake task

* i18n-tasks normalize

---------

Co-authored-by: moustachu <[email protected]>
  • Loading branch information
Stef-Rousset and moustachu authored Jun 13, 2024
1 parent 5e85db6 commit e63279e
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 3 deletions.
1 change: 1 addition & 0 deletions .env-example
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ RAILS_LOG_LEVEL=warn
## SMS Gateway Service (eg: decidim-half_signup)
# SMS_GATEWAY_SERVICE="Decidim::SmsGatewayService"
# SMS_GATEWAY_URL="https://sms.gateway.service/api"
# SMS_GATEWAY_BULK_URL="https://sms.gateway.service/api/bulk"
# SMS_GATEWAY_USERNAME=
# SMS_GATEWAY_PASSWORD=
## Set to replace the organization name
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ gem "faker", "~> 2.14"
gem "fog-aws"
gem "foundation_rails_helper", git: "https://github.com/sgruhier/foundation_rails_helper.git"
gem "letter_opener_web", "~> 1.3"
gem "multipart-post"
gem "nokogiri", "1.13.4"
gem "omniauth-rails_csrf_protection", "~> 1.0"
gem "puma", ">= 5.5.1"
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,7 @@ GEM
msgpack (1.7.2)
multi_json (1.15.0)
multi_xml (0.6.0)
multipart-post (2.4.1)
mustache (1.1.1)
net-http (0.4.1)
uri
Expand Down Expand Up @@ -1168,6 +1169,7 @@ DEPENDENCIES
letter_opener_web (~> 1.3)
listen (~> 3.1)
lograge
multipart-post
nokogiri (= 1.13.4)
omniauth-france_connect!
omniauth-publik!
Expand Down
5 changes: 2 additions & 3 deletions app/views/decidim/account/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@
->(locale) {locale_name(locale) },
{},
{ disabled: true }
)
%>
) %>
<% end %>

<p class="help-text"><%= t(".available_locales_helper") %></p>
Expand All @@ -67,4 +66,4 @@
<%= f.submit t(".update_account") %>
</div>
<% end %>
</div>
</div>
2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ en:
osp_authorization_workflow:
name: Authorization procedure
budgets:
order_reminder:
text_message: Your vote is still pending for the participatory budget. Please reconnect to %{organization_host} to confirm it
projects:
count:
projects_count:
Expand Down
2 changes: 2 additions & 0 deletions config/locales/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ fr:
osp_authorization_workflow:
name: Procédure d'autorisation
budgets:
order_reminder:
text_message: Votre vote au budget participatif n'est pas terminé. Pour le terminer, reconnectez-vous sur %{organization_host}
projects:
count:
projects_count:
Expand Down
1 change: 1 addition & 0 deletions config/secrets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ default: &default
sms_gateway:
service: <%= ENV.fetch("SMS_GATEWAY_SERVICE", "Decidim::Verifications::Sms::ExampleGateway") %>
url: <%= ENV["SMS_GATEWAY_URL"] %>
bulk_url: <%= ENV["SMS_GATEWAY_BULK_URL"] %>
username: <%= ENV["SMS_GATEWAY_USERNAME"] %>
password: <%= ENV["SMS_GATEWAY_PASSWORD"] %>
platform: <%= ENV["SMS_GATEWAY_PLATFORM"] %>
Expand Down
68 changes: 68 additions & 0 deletions lib/tasks/decidim_app.rake
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require "net/http/post/multipart"
require "decidim_app/k8s/configuration_exporter"
require "decidim_app/k8s/organization_exporter"
require "decidim_app/k8s/manager"
Expand Down Expand Up @@ -76,4 +77,71 @@ namespace :decidim_app do
DecidimApp::K8s::Manager.run(ENV.fetch("path", nil))
end
end

namespace :budgets do
# This task is used to send a reminder by sms to voters who haven't finished their
# vote for a budget. When you launch the task, you have to pass the id of the budget
# in a variable in terminal
desc "send a reminder to vote for budget"
task send_sms_reminder: :environment do
if (budget_id = ENV.fetch("BUDGET_ID")).nil?
p "You need to provide a budget ID"
next
end

if (budget = Decidim::Budgets::Budget.find_by(id: budget_id)).nil?
p "Budget with id #{budget_id} not found"
next
end

organization = budget.organization

users_ids = Decidim::Budgets::Order.where(budget: budget)
.pending
&.pluck(:decidim_user_id)
if users_ids.empty?
p "no pending votes"
next
end
users = Decidim::User.where(id: users_ids).where.not(phone_number: nil, phone_country: nil)

if users.blank?
p "no pending votes from users with phone number"
next
end

filename = "send_sms_reminder_#{Time.current.year}_#{Time.current.month}_#{Time.current.day}_#{Time.current.min}.csv"
message = I18n.t("decidim.budgets.order_reminder.text_message", locale: organization.default_locale, organization_host: organization.host)

file = Tempfile.new(filename)
CSV.open(file, "w", col_sep: ";") do |csv|
users.each do |user|
csv << ["#{user.phone_country}#{user.phone_number}", message]
end
end
file.rewind

p "users ids: #{users.ids}"
p "csv done at #{filename}"

api_url = Rails.application.secrets.dig(:decidim, :sms_gateway, :bulk_url)
username = Rails.application.secrets.dig(:decidim, :sms_gateway, :username)
password = Rails.application.secrets.dig(:decidim, :sms_gateway, :password)

url = URI(api_url)
request = Net::HTTP::Post::Multipart.new(url, {
u: username,
p: password,
f: "sms",
c: "Reminder",
file: UploadIO.new(file, "text/csv", filename)
})

response = Net::HTTP.start(url.hostname, url.port, use_ssl: true) do |https| # pay attention to use_ssl if you need it
https.request(request)
end

p "API response is #{response.body}"
end
end
end

0 comments on commit e63279e

Please sign in to comment.