Skip to content

Commit

Permalink
Merge pull request #1344 from SplitTime/oveson/record-file-downloads
Browse files Browse the repository at this point in the history
Record file downloads
  • Loading branch information
moveson authored Dec 19, 2024
2 parents cdacc82 + 7d11113 commit aaf31de
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 21 deletions.
14 changes: 14 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"
Expand Down
5 changes: 4 additions & 1 deletion app/controllers/lotteries_controller.rb
Original file line number Diff line number Diff line change
@@ -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]
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Madmin
class Analytics::SendgridEventsController < Madmin::ResourceController
end
end
4 changes: 0 additions & 4 deletions app/controllers/madmin/sendgrid_events_controller.rb

This file was deleted.

4 changes: 2 additions & 2 deletions app/controllers/webhooks/sendgrid_events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class SendgridEventResource < Madmin::Resource
class Analytics::SendgridEventResource < Madmin::Resource
# Attributes
attribute :id, form: false
attribute :email
Expand Down
8 changes: 8 additions & 0 deletions app/models/analytics/file_download.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

class SendgridEvent < ApplicationRecord
class ::Analytics::SendgridEvent < ApplicationRecord
validates_presence_of :email, :event, :timestamp

def timestamp=(timestamp)
Expand Down
12 changes: 5 additions & 7 deletions config/routes/madmin.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 3 additions & 3 deletions spec/controllers/webhooks/sendgrid_events_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions spec/models/sendgrid_event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down

0 comments on commit aaf31de

Please sign in to comment.