Skip to content

Commit

Permalink
Changes to upgrade noticed
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleMacPherson committed Jun 11, 2024
1 parent 86d0619 commit ee6b78d
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 53 deletions.
4 changes: 2 additions & 2 deletions app/controllers/publishers/notifications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ def index

def notifications
@notifications ||= current_publisher.notifications
.created_within_data_access_period
.where("created_at >= ?", Time.current - DATA_ACCESS_PERIOD_FOR_PUBLISHERS)
.order(created_at: :desc)
end

def mark_notifications_as_read
notifications.mark_as_read!
notifications.mark_as_read
end
end
2 changes: 1 addition & 1 deletion app/models/job_application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class JobApplication < ApplicationRecord

has_many :feedbacks, dependent: :destroy, inverse_of: :job_application

has_noticed_notifications
has_many :noticed_events, as: :record, dependent: :destroy, class_name: "Noticed::Event"

scope :submitted_yesterday, -> { submitted.where("DATE(submitted_at) = ?", Date.yesterday) }
scope :after_submission, -> { where(status: %w[submitted reviewed shortlisted unsuccessful withdrawn]) }
Expand Down
9 changes: 2 additions & 7 deletions app/models/notification.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
class Notification < ApplicationRecord
include Noticed::Model
belongs_to :recipient, polymorphic: true

validates :type, presence: true

scope :created_within_data_access_period, -> { where("created_at >= ?", Time.current - DATA_ACCESS_PERIOD_FOR_PUBLISHERS) }
end
self.inheritance_column = nil
end
2 changes: 1 addition & 1 deletion app/models/vacancy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class Vacancy < ApplicationRecord
validates_with ExternalVacancyValidator, if: :external?
validates :organisations, :presence => true

has_noticed_notifications
has_many :noticed_events, as: :record, dependent: :destroy, class_name: "Noticed::Event"
has_paper_trail on: [:update],
only: ATTRIBUTES_TO_TRACK_IN_ACTIVITY_LOG,
if: proc { |vacancy| vacancy.listed? }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
class Publishers::JobApplicationDataExpiryNotification < Noticed::Base
class Publishers::JobApplicationDataExpiryNotification < Noticed::Event
include ActionView::Helpers::UrlHelper
include GovukLinkHelper
include GovukVisuallyHiddenHelper
include DatesHelper

deliver_by :database
deliver_by :email, mailer: "Publishers::JobApplicationDataExpiryMailer", method: :job_application_data_expiry
delegate :created_at, to: :record
param :vacancy, :publisher
required_params :vacancy, :publisher

def message
t("notifications.publishers/job_application_data_expiry_notification.message_html",
link: vacancy_applications_link, date: format_date(data_expiration_date))
end
notification_methods do
def message
t("notifications.publishers/job_application_data_expiry_notification.message_html",
link: vacancy_applications_link, date: format_date(data_expiration_date))
end

def timestamp
"#{day(created_at)} at #{format_time(created_at)}"
end
def timestamp
"#{day(created_at)} at #{format_time(created_at)}"
end

private
private

def data_expiration_date
(vacancy.expires_at + 1.year).to_date
end
def data_expiration_date
(vacancy.expires_at + 1.year).to_date
end

def vacancy
params[:vacancy]
end
def vacancy
params[:vacancy]
end

def vacancy_applications_link
govuk_link_to vacancy.job_title, organisation_job_job_applications_path(vacancy.id), class: "govuk-link--no-visited-state"
def vacancy_applications_link
govuk_link_to vacancy.job_title, organisation_job_job_applications_path(vacancy.id), class: "govuk-link--no-visited-state"
end
end
end
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
class Publishers::JobApplicationReceivedNotification < Noticed::Base
class Publishers::JobApplicationReceivedNotification < Noticed::Event
include ActionView::Helpers::UrlHelper
include GovukLinkHelper
include GovukVisuallyHiddenHelper
include DatesHelper

deliver_by :database
delegate :created_at, to: :record
param :vacancy, :job_application
required_params :vacancy, :job_application

def message
t("notifications.publishers/job_application_received_notification.message_html",
link: application_link, job_title: vacancy.job_title, organisation: vacancy.organisation_name)
end
notification_methods do
def message
t("notifications.publishers/job_application_received_notification.message_html",
link: application_link, job_title: vacancy.job_title, organisation: vacancy.organisation_name)
end

def timestamp
"#{day(created_at)} at #{format_time(created_at)}"
end
def timestamp
"#{day(created_at)} at #{format_time(created_at)}"
end

private
private

def application_link
govuk_link_to "an application", organisation_job_job_application_path(vacancy.id, job_application.id), class: "govuk-link--no-visited-state"
end
def application_link
govuk_link_to "an application", organisation_job_job_application_path(vacancy.id, job_application.id), class: "govuk-link--no-visited-state"
end

def job_application
params[:job_application]
end
def job_application
params[:job_application]
end

def vacancy
params[:vacancy]
def vacancy
params[:vacancy]
end
end
end
37 changes: 37 additions & 0 deletions db/migrate/20240611142852_create_noticed_tables.noticed.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# This migration comes from noticed (originally 20231215190233)
class CreateNoticedTables < ActiveRecord::Migration[6.1]
def change
primary_key_type, foreign_key_type = primary_and_foreign_key_types
create_table :noticed_events, id: primary_key_type do |t|
t.string :type
t.belongs_to :record, polymorphic: true, type: foreign_key_type
if t.respond_to?(:jsonb)
t.jsonb :params
else
t.json :params
end

t.timestamps
end

create_table :noticed_notifications, id: primary_key_type do |t|
t.string :type
t.belongs_to :event, null: false, type: foreign_key_type
t.belongs_to :recipient, polymorphic: true, null: false, type: foreign_key_type
t.datetime :read_at
t.datetime :seen_at

t.timestamps
end
end

private

def primary_and_foreign_key_types
config = Rails.configuration.generators
setting = config.options[config.orm][:primary_key_type]
primary_key_type = setting || :primary_key
foreign_key_type = setting || :bigint
[primary_key_type, foreign_key_type]
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# This migration comes from noticed (originally 20240129184740)
class AddNotificationsCountToNoticedEvent < ActiveRecord::Migration[6.1]
def change
add_column :noticed_events, :notifications_count, :integer
end
end
33 changes: 29 additions & 4 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2024_05_28_084036) do
ActiveRecord::Schema[7.1].define(version: 2024_06_11_142853) do
# These are extensions that must be enabled in order to support this database
enable_extension "btree_gist"
enable_extension "citext"
Expand Down Expand Up @@ -76,9 +76,9 @@
t.uuid "job_application_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.text "organisation_ciphertext"
t.integer "employment_type", default: 0
t.text "reason_for_break", default: ""
t.text "organisation_ciphertext"
t.text "job_title_ciphertext"
t.text "main_duties_ciphertext"
t.uuid "jobseeker_profile_id"
Expand Down Expand Up @@ -243,6 +243,7 @@
t.string "safeguarding_issue"
t.text "safeguarding_issue_details"
t.boolean "training_and_cpds_section_completed"
t.jsonb "imported_steps", default: {}, null: false
t.index ["jobseeker_id"], name: "index_job_applications_jobseeker_id"
t.index ["vacancy_id"], name: "index_job_applications_on_vacancy_id"
end
Expand Down Expand Up @@ -358,6 +359,30 @@
t.index ["publisher_id"], name: "index_notes_on_publisher_id"
end

create_table "noticed_events", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.string "type"
t.string "record_type"
t.uuid "record_id"
t.jsonb "params"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "notifications_count"
t.index ["record_type", "record_id"], name: "index_noticed_events_on_record"
end

create_table "noticed_notifications", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.string "type"
t.uuid "event_id", null: false
t.string "recipient_type", null: false
t.uuid "recipient_id", null: false
t.datetime "read_at", precision: nil
t.datetime "seen_at", precision: nil
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["event_id"], name: "index_noticed_notifications_on_event_id"
t.index ["recipient_type", "recipient_id"], name: "index_noticed_notifications_on_recipient"
end

create_table "notifications", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.string "recipient_type", null: false
t.uuid "recipient_id", null: false
Expand Down Expand Up @@ -627,11 +652,11 @@
t.string "external_reference"
t.string "external_advert_url"
t.integer "ect_status"
t.string "pay_scale"
t.boolean "benefits"
t.text "full_time_details"
t.text "part_time_details"
t.integer "phases", array: true
t.string "pay_scale"
t.boolean "benefits"
t.integer "start_date_type"
t.date "earliest_start_date"
t.date "latest_start_date"
Expand Down
28 changes: 28 additions & 0 deletions lib/tasks/migrate_notifications.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace :notifications do
desc "Migrate notifications to allow upgrade to noticed 2.0"
task :migrate => :environment do
# Migrate each record to the new tables
Notification.find_each do |notification|
puts "#{notification.id}, #{notification.type}"
attributes = notification.attributes.slice("type", "created_at", "updated_at").with_indifferent_access
# attributes[:type] = attributes[:type].sub("Notification", "Notifier")
attributes[:params] = Noticed::Coder.load(notification.params)
attributes[:params] = {} if attributes[:params].try(:has_key?, "noticed_error") # Skip invalid records

# Extract related record to `belongs_to :record` association
# This allows ActiveRecord associations instead of querying the JSON data
# attributes[:record] = attributes[:params].delete(:user) || attributes[:params].delete(:account)

attributes[:notifications_attributes] = [{
type: "#{attributes[:type]}::Notification",
recipient_type: notification.recipient_type,
recipient_id: notification.recipient_id,
read_at: notification.read_at,
seen_at: notification.read_at,
created_at: notification.created_at,
updated_at: notification.updated_at
}]
Noticed::Event.create!(attributes)
end
end
end

0 comments on commit ee6b78d

Please sign in to comment.