From 99b2a694ba8b1232303b284b44462ad64b817c80 Mon Sep 17 00:00:00 2001 From: moveson Date: Wed, 18 Dec 2024 18:11:59 -0700 Subject: [PATCH 1/4] Move SendgridEvent into a new Analytics module --- .../madmin/analytics/sendgrid_events_controller.rb | 4 ++++ app/controllers/madmin/sendgrid_events_controller.rb | 4 ---- .../webhooks/sendgrid_events_controller.rb | 4 ++-- .../{ => analytics}/sendgrid_event_resource.rb | 2 +- app/models/{ => analytics}/sendgrid_event.rb | 2 +- config/routes/madmin.rb | 12 +++++------- .../webhooks/sendgrid_events_controller_spec.rb | 6 +++--- spec/models/sendgrid_event_spec.rb | 4 ++-- 8 files changed, 18 insertions(+), 20 deletions(-) create mode 100644 app/controllers/madmin/analytics/sendgrid_events_controller.rb delete mode 100644 app/controllers/madmin/sendgrid_events_controller.rb rename app/madmin/resources/{ => analytics}/sendgrid_event_resource.rb (92%) rename app/models/{ => analytics}/sendgrid_event.rb (85%) diff --git a/app/controllers/madmin/analytics/sendgrid_events_controller.rb b/app/controllers/madmin/analytics/sendgrid_events_controller.rb new file mode 100644 index 000000000..82a25b627 --- /dev/null +++ b/app/controllers/madmin/analytics/sendgrid_events_controller.rb @@ -0,0 +1,4 @@ +module Madmin + class Analytics::SendgridEventsController < Madmin::ResourceController + end +end diff --git a/app/controllers/madmin/sendgrid_events_controller.rb b/app/controllers/madmin/sendgrid_events_controller.rb deleted file mode 100644 index d50c2277f..000000000 --- a/app/controllers/madmin/sendgrid_events_controller.rb +++ /dev/null @@ -1,4 +0,0 @@ -module Madmin - class SendgridEventsController < Madmin::ResourceController - end -end diff --git a/app/controllers/webhooks/sendgrid_events_controller.rb b/app/controllers/webhooks/sendgrid_events_controller.rb index 963fb1694..1aff884ad 100644 --- a/app/controllers/webhooks/sendgrid_events_controller.rb +++ b/app/controllers/webhooks/sendgrid_events_controller.rb @@ -8,11 +8,11 @@ def create rows = params.require(:_json) rows.each do |row| - sendgrid_event = SendgridEvent.new(row.permit(*sendgrid_event_params)) + sendgrid_event = Analytics::SendgridEvent.new(row.permit(*sendgrid_event_params)) sendgrid_event.event_type = row[:type] unless sendgrid_event.save - Sentry.capture_message("Failed to save SendgridEvent", extra: { sendgrid_event: sendgrid_event, errors: sendgrid_event.errors.full_messages }) + Sentry.capture_message("Failed to save Analytics::SendgridEvent", extra: { sendgrid_event: sendgrid_event, errors: sendgrid_event.errors.full_messages }) status = :unprocessable_entity break end diff --git a/app/madmin/resources/sendgrid_event_resource.rb b/app/madmin/resources/analytics/sendgrid_event_resource.rb similarity index 92% rename from app/madmin/resources/sendgrid_event_resource.rb rename to app/madmin/resources/analytics/sendgrid_event_resource.rb index 1c5d84995..a4bdb1719 100644 --- a/app/madmin/resources/sendgrid_event_resource.rb +++ b/app/madmin/resources/analytics/sendgrid_event_resource.rb @@ -1,4 +1,4 @@ -class SendgridEventResource < Madmin::Resource +class Analytics::SendgridEventResource < Madmin::Resource # Attributes attribute :id, form: false attribute :email diff --git a/app/models/sendgrid_event.rb b/app/models/analytics/sendgrid_event.rb similarity index 85% rename from app/models/sendgrid_event.rb rename to app/models/analytics/sendgrid_event.rb index ec6e22c82..5d86ae2f9 100644 --- a/app/models/sendgrid_event.rb +++ b/app/models/analytics/sendgrid_event.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -class SendgridEvent < ApplicationRecord +class ::Analytics::SendgridEvent < ApplicationRecord validates_presence_of :email, :event, :timestamp def timestamp=(timestamp) diff --git a/config/routes/madmin.rb b/config/routes/madmin.rb index ab79b7035..39f52ac1b 100644 --- a/config/routes/madmin.rb +++ b/config/routes/madmin.rb @@ -1,16 +1,14 @@ # Below are the routes for madmin namespace :madmin do resources :historical_facts - resources :sendgrid_events - resources :connections - namespace :active_storage do - resources :variant_records - end - namespace :active_storage do - resources :blobs + namespace :analytics do + resources :sendgrid_events end + resources :connections namespace :active_storage do resources :attachments + resources :blobs + resources :variant_records end namespace :shortener do resources :shortened_urls diff --git a/spec/controllers/webhooks/sendgrid_events_controller_spec.rb b/spec/controllers/webhooks/sendgrid_events_controller_spec.rb index 61cf83cd8..c92c03ec8 100644 --- a/spec/controllers/webhooks/sendgrid_events_controller_spec.rb +++ b/spec/controllers/webhooks/sendgrid_events_controller_spec.rb @@ -33,12 +33,12 @@ end it "creates a new event for each row" do - expect { make_request }.to change { SendgridEvent.count }.by(11) + expect { make_request }.to change { Analytics::SendgridEvent.count }.by(11) end it "saves type as the event type" do make_request - sendgrid_event = SendgridEvent.find_by(event: "bounce") + sendgrid_event = Analytics::SendgridEvent.find_by(event: "bounce") expect(sendgrid_event.event_type).to eq("bounced") end end @@ -58,7 +58,7 @@ end it "does not create a new record" do - expect { make_request }.to_not change { SendgridEvent.count } + expect { make_request }.to_not change { Analytics::SendgridEvent.count } end end diff --git a/spec/models/sendgrid_event_spec.rb b/spec/models/sendgrid_event_spec.rb index 126278b86..5edfd057e 100644 --- a/spec/models/sendgrid_event_spec.rb +++ b/spec/models/sendgrid_event_spec.rb @@ -2,9 +2,9 @@ require "rails_helper" -RSpec.describe SendgridEvent do +RSpec.describe ::Analytics::SendgridEvent do describe "#timestamp=" do - let(:sendgrid_event) { SendgridEvent.new(timestamp: timestamp) } + let(:sendgrid_event) { described_class.new(timestamp: timestamp) } context "when provided an integer" do let(:timestamp) { 1683409840 } From 1b7e6c59ec6af92f5b2c437e82456c8c661da9eb Mon Sep 17 00:00:00 2001 From: moveson Date: Wed, 18 Dec 2024 20:14:11 -0700 Subject: [PATCH 2/4] Get local file downloads working --- app/controllers/application_controller.rb | 4 ++++ .../lotteries/entrant_service_details_controller.rb | 3 +++ app/controllers/lotteries_controller.rb | 3 +++ 3 files changed, 10 insertions(+) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index c5904ff14..9c8e6c7d9 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -106,6 +106,10 @@ def user_not_authorized redirect_to(request.referrer || root_path) end + def set_current_url_options + ::ActiveStorage::Current.url_options = { host: OstConfig.full_uri } if Rails.env.development? + end + def set_current_user User.current = current_user end diff --git a/app/controllers/lotteries/entrant_service_details_controller.rb b/app/controllers/lotteries/entrant_service_details_controller.rb index a3438202d..3f206c72b 100644 --- a/app/controllers/lotteries/entrant_service_details_controller.rb +++ b/app/controllers/lotteries/entrant_service_details_controller.rb @@ -1,6 +1,9 @@ # frozen_string_literal: true class Lotteries::EntrantServiceDetailsController < ApplicationController + # Rails 7.0 is unable to generate a local URL for ActiveStorage using Disk service + # unless ActiveStorage::Current.url_options is set + before_action :set_current_url_options, only: [:download_completed_form] before_action :authenticate_user! before_action :set_organization before_action :set_lottery diff --git a/app/controllers/lotteries_controller.rb b/app/controllers/lotteries_controller.rb index 7598893df..de743ee9e 100644 --- a/app/controllers/lotteries_controller.rb +++ b/app/controllers/lotteries_controller.rb @@ -1,6 +1,9 @@ # frozen_string_literal: true class LotteriesController < ApplicationController + # Rails 7.0 is unable to generate a local URL for ActiveStorage using Disk service + # unless ActiveStorage::Current.url_options is set + before_action :set_current_url_options, only: [:download_service_form] before_action :authenticate_user!, except: [:index, :show, :download_service_form] before_action :set_organization before_action :authorize_organization, except: [:index, :show, :download_service_form] From caacb6ec8651c5f5af0097a37616fd190b61773d Mon Sep 17 00:00:00 2001 From: moveson Date: Wed, 18 Dec 2024 20:14:52 -0700 Subject: [PATCH 3/4] Record file downloads for completed forms and service forms --- app/controllers/application_controller.rb | 10 ++++++++++ .../lotteries/entrant_service_details_controller.rb | 1 + app/controllers/lotteries_controller.rb | 2 +- app/models/analytics/file_download.rb | 6 ++++++ 4 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 app/models/analytics/file_download.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 9c8e6c7d9..cc9b37261 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -24,6 +24,16 @@ def process_action(*args) head :not_acceptable end + def record_file_download(attachment) + ::Analytics::FileDownload.create( + user: current_user, + record: attachment.record, + name: attachment.name, + filename: attachment.filename, + byte_size: attachment.byte_size, + ) + end + def route_not_found raise ::ActionController::RoutingError, "Route does not exist" end diff --git a/app/controllers/lotteries/entrant_service_details_controller.rb b/app/controllers/lotteries/entrant_service_details_controller.rb index 3f206c72b..794e546f6 100644 --- a/app/controllers/lotteries/entrant_service_details_controller.rb +++ b/app/controllers/lotteries/entrant_service_details_controller.rb @@ -38,6 +38,7 @@ def attach_completed_form def download_completed_form if @service_detail.completed_form.attached? redirect_to @service_detail.completed_form.url(disposition: :attachment), allow_other_host: true + record_file_download(@service_detail.completed_form) else redirect_to organization_lottery_entrant_service_detail_path(@organization, @lottery, @service_detail), notice: "No completed service form is attached" diff --git a/app/controllers/lotteries_controller.rb b/app/controllers/lotteries_controller.rb index de743ee9e..9f2285f52 100644 --- a/app/controllers/lotteries_controller.rb +++ b/app/controllers/lotteries_controller.rb @@ -190,8 +190,8 @@ def attach_service_form # GET /organizations/:organization_id/lotteries/:id/download_service_form def download_service_form if @lottery.service_form.attached? - @lottery.increment!(:service_form_download_count) redirect_to @lottery.service_form.url(disposition: :attachment), allow_other_host: true + record_file_download(@lottery.service_form) else redirect_to setup_organization_lottery_path(@organization, @lottery), notice: "No service form is attached" end diff --git a/app/models/analytics/file_download.rb b/app/models/analytics/file_download.rb new file mode 100644 index 000000000..b008219d7 --- /dev/null +++ b/app/models/analytics/file_download.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +class Analytics::FileDownload < ApplicationRecord + belongs_to :user + belongs_to :record, polymorphic: true +end From 7d11113ddd1be2601365e0ccf98b65cbfd7f5002 Mon Sep 17 00:00:00 2001 From: moveson Date: Wed, 18 Dec 2024 20:30:27 -0700 Subject: [PATCH 4/4] Set table_name for Analytics::FileDownload model --- app/models/analytics/file_download.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/models/analytics/file_download.rb b/app/models/analytics/file_download.rb index b008219d7..86ff2ea14 100644 --- a/app/models/analytics/file_download.rb +++ b/app/models/analytics/file_download.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true class Analytics::FileDownload < ApplicationRecord + self.table_name = "analytics_file_downloads" + belongs_to :user belongs_to :record, polymorphic: true end