Skip to content

Commit

Permalink
Fix: SMS Gateway Service (#535)
Browse files Browse the repository at this point in the history
* bump gem web-console (annoying error message)

* Tidy / Clean Up SMS Gateway Service configuration vars

* fix: bump decidim-module-half_sign_up for not overriding local SMS content
  • Loading branch information
moustachu committed Jun 14, 2024
1 parent 6cd8421 commit ca24c8e
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 17 deletions.
12 changes: 7 additions & 5 deletions .env-example
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,13 @@ RAILS_LOG_LEVEL=warn
# Default notifications sending frequency : (daily, weekly, none, real_time)
# NOTIFICATIONS_SENDING_FREQUENCY=daily

#SMS_GATEWAY_SERVICE=
#SMS_GATEWAY_USERNAME=
#SMS_GATEWAY_PASSWORD=
#SMS_GATEWAY_URL=
#SMS_GATEWAY_PLATFORM=
## SMS Gateway Service (eg: decidim-half_signup)
# SMS_GATEWAY_SERVICE="Decidim::SmsGatewayService"
# SMS_GATEWAY_URL="https://sms.gateway.service/api"
# SMS_GATEWAY_USERNAME=
# SMS_GATEWAY_PASSWORD=
## Set to replace the organization name
# SMS_GATEWAY_PLATFORM="hashimoto.local"

#Timeout for the unsubscribe link of the newsletter
#NEWSLETTERS_UNSUBSCRIBE_TIMEOUT=
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ group :development do
gem "rubocop-faker"
gem "spring", "~> 2.0"
gem "spring-watcher-listen", "~> 2.0"
gem "web-console", "4.0.4"
gem "web-console", "~> 4.1"
end

group :development, :test do
Expand Down
6 changes: 3 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ GIT

GIT
remote: https://github.com/OpenSourcePolitics/decidim-module-half_sign_up.git
revision: cec7c1f329777a1a011214346c7a957475fa7679
revision: 5f24e2cd183767e48c390b0fc2064c5544f60c49
branch: feature/half_signup_and_budgets_booth
specs:
decidim-half_signup (0.27.0)
Expand Down Expand Up @@ -1075,7 +1075,7 @@ GEM
rexml (~> 3.2)
warden (1.2.9)
rack (>= 2.0.9)
web-console (4.0.4)
web-console (4.2.1)
actionview (>= 6.0.0)
activemodel (>= 6.0.0)
bindex (>= 0.4.0)
Expand Down Expand Up @@ -1175,7 +1175,7 @@ DEPENDENCIES
spring (~> 2.0)
spring-watcher-listen (~> 2.0)
sys-filesystem
web-console (= 4.0.4)
web-console (~> 4.1)
wicked_pdf (= 2.6.3)

RUBY VERSION
Expand Down
21 changes: 16 additions & 5 deletions app/services/decidim/sms_gateway_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ module Decidim
class SmsGatewayService
attr_reader :mobile_phone_number, :code

def initialize(mobile_phone_number, code, sms_gateway_context)
def initialize(mobile_phone_number, code, sms_gateway_context = {})
@mobile_phone_number = mobile_phone_number
@code = code
@organization_name = sms_gateway_context[:organization]&.name
@url = ENV.fetch("SMS_GATEWAY_URL", nil)
@username = ENV.fetch("SMS_GATEWAY_USERNAME", nil)
@password = ENV.fetch("SMS_GATEWAY_PASSWORD", nil)
@url = fetch_configuration(:url)
@username = fetch_configuration(:username)
@password = fetch_configuration(:password)
@message = sms_message
@type = "sms"
end
Expand All @@ -32,7 +32,18 @@ def deliver_code
def sms_message
return code if code.to_s.length > Decidim::HalfSignup.auth_code_length

I18n.t("sms_verification_workflow.message", code: code, platform: ENV.fetch("SMS_GATEWAY_PLATFORM", @organization_name))
platform = fetch_configuration(:platform, required: false).presence || @organization_name
I18n.t("sms_verification_workflow.message", code: code, platform: platform)
end

def fetch_configuration(key, required: true)
value = Rails.application.secrets.dig(:decidim, :sms_gateway, key.to_sym)
if required && value.blank?
Rails.logger.error "Decidim::SmsGatewayService is missing a configuration value for :#{key}, " \
"please check Rails.application.secrets.dig(:decidim, :sms_gateway, :#{key}) " \
"or environment variable SMS_GATEWAY_#{key.to_s.upcase}"
end
value
end
end
end
3 changes: 1 addition & 2 deletions config/initializers/decidim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
# take over user accounts.
#
config.enable_html_header_snippets = true
config.sms_gateway_service = "Decidim::Verifications::Sms::ExampleGateway"

# SMS gateway configuration
#
Expand All @@ -92,7 +91,7 @@
# end
# end
#
config.sms_gateway_service = Rails.application.secrets.dig(:decidim, :sms_gateway_service) if Rails.application.secrets.dig(:decidim, :sms_gateway_service).present?
config.sms_gateway_service = Rails.application.secrets.dig(:decidim, :sms_gateway, :service)

# Etherpad configuration
#
Expand Down
7 changes: 6 additions & 1 deletion config/secrets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ default: &default
throttle:
max_requests: <%= ENV["THROTTLING_MAX_REQUESTS"]&.to_i || 100 %>
period: <%= ENV["THROTTLING_PERIOD"]&.to_i || 60 %>
sms_gateway_service: <%= ENV["SMS_GATEWAY_SERVICE"] %>
sms_gateway:
service: <%= ENV.fetch("SMS_GATEWAY_SERVICE", "Decidim::Verifications::Sms::ExampleGateway") %>
url: <%= ENV["SMS_GATEWAY_URL"] %>
username: <%= ENV["SMS_GATEWAY_USERNAME"] %>
password: <%= ENV["SMS_GATEWAY_PASSWORD"] %>
platform: <%= ENV["SMS_GATEWAY_PLATFORM"] %>
newsletters_unsubscribe_timeout: <%= ENV.fetch("NEWSLETTERS_UNSUBSCRIBE_TIMEOUT", 365).to_i %>
modules:
gallery:
Expand Down

0 comments on commit ca24c8e

Please sign in to comment.