-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
86d0619
commit ee6b78d
Showing
10 changed files
with
146 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 20 additions & 19 deletions
39
app/notifications/publishers/job_application_data_expiry_notification.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
39 changes: 20 additions & 19 deletions
39
app/notifications/publishers/job_application_received_notification.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
37
db/migrate/20240611142852_create_noticed_tables.noticed.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
6 changes: 6 additions & 0 deletions
6
db/migrate/20240611142853_add_notifications_count_to_noticed_event.noticed.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |