diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index c5904ff14..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 @@ -106,6 +116,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..794e546f6 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 @@ -35,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 7598893df..9f2285f52 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] @@ -187,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/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/analytics/file_download.rb b/app/models/analytics/file_download.rb new file mode 100644 index 000000000..86ff2ea14 --- /dev/null +++ b/app/models/analytics/file_download.rb @@ -0,0 +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 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 }