Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ gem "administrate", "0.20.1"
# Reduces boot times through caching; required in config/boot.rb
gem "bootsnap", "1.18.3", require: false

# Rack middleware for blocking & throttling
gem 'rack-attack'
# Rack middleware for blocking & throttling
gem "rack-attack"

# Use Sass to process CSS
# gem "sassc-rails"
Expand Down Expand Up @@ -73,6 +73,8 @@ gem "rack-cors", "2.0.2"
gem "rails", "7.1.3.4"
# Use Redis adapter to run Action Cable in production
gem "redis", "5.2.0"
# User resend.com to send emails
gem "resend"
# Sentry SDK for Rails
gem "sentry-rails", "5.17.3"
# Sentry SDK for Ruby
Expand Down Expand Up @@ -109,6 +111,8 @@ group :development, :test do
end

group :development do
gem "brakeman", "6.1.2"
gem "bullet"
# To ensure code consistency [https://docs.rubocop.org]
gem "rubocop", "1.56.2"
gem "rubocop-factory_bot", "!= 2.26.0", require: false
Expand All @@ -118,8 +122,6 @@ group :development do
gem "rubocop-rspec_rails", "!= 2.29.0", require: false
# Use console on exceptions pages [https://github.com/rails/web-console]
gem "web-console", "4.2.1"
gem "bullet"
gem "brakeman", "6.1.2"

# Preview mail in the browser instead of sending.
gem "letter_opener", "1.10.0"
Expand Down
7 changes: 7 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ GEM
activesupport (>= 6.1)
hashery (2.1.2)
hashie (5.0.0)
httparty (0.23.2)
csv
mini_mime (>= 1.0.0)
multi_xml (>= 0.5.2)
i18n (1.14.5)
concurrent-ruby (~> 1.0)
importmap-rails (2.0.1)
Expand Down Expand Up @@ -454,6 +458,8 @@ GEM
io-console (~> 0.5)
request_store (1.7.0)
rack (>= 1.4)
resend (0.26.0)
httparty (>= 0.21.0)
responders (3.1.1)
actionpack (>= 5.2)
railties (>= 5.2)
Expand Down Expand Up @@ -645,6 +651,7 @@ DEPENDENCIES
rails (= 7.1.3.4)
redis (= 5.2.0)
reek (= 6.3.0)
resend
rspec-rails (= 6.0.3)
rspec-sidekiq (= 4.0.2)
rubocop (= 1.56.2)
Expand Down
44 changes: 22 additions & 22 deletions app/controllers/api/v1/event_procedures_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@ class EventProceduresController < ApiController
after_action :verify_authorized, except: :index
after_action :verify_policy_scoped, only: :index

def index
authorized_scope = policy_scope(EventProcedure)

listed_event_procedures = EventProcedures::List.result(
scope: authorized_scope,
params: event_procedure_permitted_query_params
)

event_procedures = listed_event_procedures.event_procedures
event_procedures_unpaginated = listed_event_procedures.event_procedures_unpaginated

total_amount_cents = EventProcedures::TotalAmountCents.call(
event_procedures: event_procedures_unpaginated
)

render json: {
total: total_amount_cents.total,
total_paid: total_amount_cents.paid,
total_unpaid: total_amount_cents.unpaid,
event_procedures: serialized_event_procedures(event_procedures)
}, status: :ok
end
def index
authorized_scope = policy_scope(EventProcedure)

listed_event_procedures = EventProcedures::List.result(
scope: authorized_scope,
params: event_procedure_permitted_query_params
)

event_procedures = listed_event_procedures.event_procedures
event_procedures_unpaginated = listed_event_procedures.event_procedures_unpaginated

total_amount_cents = EventProcedures::TotalAmountCents.call(
event_procedures: event_procedures_unpaginated
)

render json: {
total: total_amount_cents.total,
total_paid: total_amount_cents.paid,
total_unpaid: total_amount_cents.unpaid,
event_procedures: serialized_event_procedures(event_procedures)
}, status: :ok
end

def create
authorize(EventProcedure)
Expand Down
1 change: 0 additions & 1 deletion app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# frozen_string_literal: true

class ApplicationMailer < ActionMailer::Base
default from: "from@example.com"
layout "mailer"
end
8 changes: 5 additions & 3 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@

# The Bullet gem helps detect N+1 queries and other inefficiencies in ActiveRecord queries.
config.after_initialize do
Bullet.enable = true
Bullet.alert = true
Bullet.enable = true
Bullet.alert = true
Bullet.bullet_logger = true
Bullet.console = true
Bullet.console = true
end

# Raises error for missing translations.
Expand All @@ -86,6 +86,8 @@

config.action_mailer.default_url_options = { host: "localhost", port: 3000 }

# Use letter_opener to local development or resend to test real emails
config.action_mailer.delivery_method = :letter_opener
# config.action_mailer.delivery_method = :resend
config.action_mailer.perform_deliveries = true
end
13 changes: 1 addition & 12 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,5 @@

config.action_mailer.default_url_options = { host: "api.meusprocedimentos.com.br", protocol: "https" }

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: "smtp.sendgrid.net",
port: 587,
domain: "meusprocedimentos.com.br",
user_name: Rails.application.credentials.dig(:smtp, :username),
password: Rails.application.credentials.dig(:smtp, :password),
authentication: "plain",
enable_starttls: true,
open_timeout: 5,
read_timeout: 5
}
config.action_mailer.delivery_method = :resend
end
3 changes: 3 additions & 0 deletions config/initializers/mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# frozen_string_literal: true

Resend.api_key = ENV.fetch("RESEND_API_KEY", nil)
18 changes: 7 additions & 11 deletions config/initializers/rack_attack.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

class Rack::Attack

### Configure Cache ###

# If you don't want to use Rails.cache (Rack::Attack's default), then
Expand All @@ -11,7 +10,7 @@ class Rack::Attack
# safelisting). It must implement .increment and .write like
# ActiveSupport::Cache::Store

Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new
Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new

### Throttle Spammy Clients ###

Expand All @@ -26,7 +25,7 @@ class Rack::Attack
# Throttle all requests by IP (60rpm)
#
# Key: "rack::attack:#{Time.now.to_i/:period}:req/ip:#{req.ip}"
throttle('req/ip', limit: 300, period: 5.minutes) do |req|
throttle("req/ip", limit: 300, period: 5.minutes) do |req|
req.ip # unless req.path.start_with?('/assets')
end

Expand All @@ -42,10 +41,8 @@ class Rack::Attack
# Throttle POST requests to /login by IP address
#
# Key: "rack::attack:#{Time.now.to_i/:period}:logins/ip:#{req.ip}"
throttle('logins/ip', limit: 5, period: 20.seconds) do |req|
if req.path == '/login' && req.post?
req.ip
end
throttle("logins/ip", limit: 5, period: 20.seconds) do |req|
req.ip if req.path == "/login" && req.post?
end

# Throttle POST requests to /login by email param
Expand All @@ -56,11 +53,11 @@ class Rack::Attack
# throttle logins for another user and force their login requests to be
# denied, but that's not very common and shouldn't happen to you. (Knock
# on wood!)
throttle('logins/email', limit: 5, period: 20.seconds) do |req|
if req.path == '/login' && req.post?
throttle("logins/email", limit: 5, period: 20.seconds) do |req|
if req.path == "/login" && req.post?
# Normalize the email, using the same logic as your authentication process, to
# protect against rate limit bypasses. Return the normalized email if present, nil otherwise.
req.params['email'].to_s.downcase.gsub(/\s+/, "").presence
req.params["email"].to_s.downcase.gsub(/\s+/, "").presence
end
end

Expand All @@ -78,4 +75,3 @@ class Rack::Attack
# ['']] # body
# end
end

1 change: 0 additions & 1 deletion spec/operations/event_procedures/list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
require "rails_helper"

RSpec.describe EventProcedures::List, type: :operation do

describe ".result" do
it "is successful" do
result = described_class.result(scope: EventProcedure.all, params: {})
Expand Down
2 changes: 1 addition & 1 deletion spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
Rails.root.glob("spec/support/**/*.rb").each { |f| require f }

# Clears Rack:Attack cache between specs
config.before(:each) do
config.before do
Rack::Attack.cache.store.clear if Rack::Attack.cache.store.respond_to?(:clear)
end
end
8 changes: 4 additions & 4 deletions spec/support/query_helper.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# frozen_string_literal: true

module QueryHelper
def count_queries(&block)
def count_queries(&)
queries = []
callback = ->(_name, _start, _finish, _id, payload) do
queries << payload[:sql] unless payload[:name] =~ /SCHEMA|TRANSACTION/
callback = lambda do |_name, _start, _finish, _id, payload|
queries << payload[:sql] unless /SCHEMA|TRANSACTION/.match?(payload[:name])
end

ActiveSupport::Notifications.subscribed(callback, "sql.active_record", &block)
ActiveSupport::Notifications.subscribed(callback, "sql.active_record", &)
queries.count
end
end