Skip to content

Commit

Permalink
handle keyword search in memory
Browse files Browse the repository at this point in the history
  • Loading branch information
starswan committed Dec 2, 2024
1 parent 21303b1 commit d2b64a2
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 34 deletions.
10 changes: 1 addition & 9 deletions app/jobs/alert_email/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class AlertEmail::Base < ApplicationJob
phases: ->(vacancy, value) { (vacancy.phases & value).any? },
working_patterns: ->(vacancy, value) { (vacancy.working_patterns & value).any? },
organisation_slug: ->(vacancy, value) { vacancy.organisations.map(&:slug).include?(value) },
keyword: ->(vacancy, value) { vacancy.searchable_content.include? value.downcase.strip },
}.freeze

def perform # rubocop:disable Metrics/AbcSize
Expand All @@ -24,7 +25,6 @@ def perform # rubocop:disable Metrics/AbcSize
subscriptions.find_each.reject { |sub| already_run_ids.include?(sub.id) }.each do |subscription|
scope = default_scope
criteria = subscription.search_criteria.symbolize_keys
scope, criteria = handle_keywords(scope, criteria)
scope, criteria = handle_location(scope, criteria)

vacancies = scope.select do |vacancy|
Expand All @@ -38,14 +38,6 @@ def perform # rubocop:disable Metrics/AbcSize

private

def handle_keywords(scope, criteria)
if criteria.key?(:keyword)
[scope.search_by_full_text(criteria[:keyword]), criteria.except(:keyword)]
else
[scope, criteria]
end
end

def handle_location(scope, criteria)
if criteria.key?(:location)
[scope.search_by_location(criteria[:location], criteria[:radius]), criteria.except(:location, :radius)]
Expand Down
84 changes: 59 additions & 25 deletions spec/jobs/send_daily_alert_email_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,21 @@
RSpec.describe SendDailyAlertEmailJob do
subject(:job) { described_class.perform_later }

let(:mail) { double(:mail, deliver_later: nil) }

describe "#perform" do
let(:mail) { double(:mail, deliver_later: nil) }

context "with vacancies" do
before do
create(:vacancy, :published_slugged, contact_number: "1", ect_status: :ect_unsuitable, job_roles: %w[teacher], subjects: %w[English], phases: %w[secondary], working_patterns: %w[full_time])
create(:vacancy, :published_slugged, contact_number: "2", job_roles: %w[it_support], subjects: %w[English], phases: %w[secondary], working_patterns: %w[full_time])
create(:vacancy, :published_slugged, contact_number: "3", visa_sponsorship_available: true, job_roles: %w[headteacher], subjects: %w[English], phases: %w[secondary], working_patterns: %w[full_time])
create(:vacancy, :published_slugged, contact_number: "4", ect_status: :ect_suitable, job_roles: %w[headteacher teacher], subjects: %w[English], phases: %w[secondary], working_patterns: %w[full_time], publish_on: 1.day.ago)
create(:vacancy, :published_slugged, contact_number: "5", job_roles: %w[headteacher], subjects: %w[French], phases: %w[secondary], working_patterns: %w[full_time])
create(:vacancy, :published_slugged, contact_number: "6", job_roles: %w[headteacher], phases: %w[primary], working_patterns: %w[full_time])
create(:vacancy, :published_slugged, contact_number: "7", job_roles: %w[headteacher], phases: %w[secondary], working_patterns: %w[part_time])
create(:vacancy, :published_slugged, contact_number: "8", organisations: [new_org], job_roles: %w[headteacher], phases: %w[secondary], working_patterns: %w[full_time])
create(:vacancy, :published_slugged, contact_number: "9", job_title: "This is a Really Nice job", job_roles: %w[headteacher], phases: %w[secondary], working_patterns: %w[full_time])
create(:vacancy, :published_slugged, job_roles: %w[headteacher], visa_sponsorship_available: false, ect_status: :ect_unsuitable, subjects: %w[German], phases: %w[secondary], working_patterns: %w[full_time])
end

let(:new_org) { create(:school) }
let(:teacher_vacancy) { Vacancy.find_by!(contact_number: "1") }
let(:support_vacancy) { Vacancy.find_by!(contact_number: "2") }
let(:visa_job) { Vacancy.find_by!(contact_number: "3") }
let(:ect_job) { Vacancy.find_by!(contact_number: "4") }
let(:french_job) { Vacancy.find_by!(contact_number: "5") }
let(:primary_job) { Vacancy.find_by!(contact_number: "6") }
let(:part_time_job) { Vacancy.find_by!(contact_number: "7") }
let(:new_org_job) { Vacancy.find_by!(contact_number: "8") }
let(:nice_job) { Vacancy.find_by!(contact_number: "9") }

context "with keyword" do
let(:subscription) { create(:subscription, keyword: keyword, frequency: :daily) }
let(:subscription) { create(:daily_subscription, keyword: keyword) }
let(:nice_job) { Vacancy.find_by!(contact_number: "9") }

before do
create(:vacancy, :published_slugged, contact_number: "9", job_title: "This is a Really Nice job", job_roles: %w[headteacher], phases: %w[secondary], working_patterns: %w[full_time])
end

context "with plain keyword" do
let(:keyword) { "nice" }
Expand All @@ -52,16 +38,32 @@
end
end

# context "with location" do
# context "with nationwide location" do
# let(:subscription) { create(:daily_subscription, location: "england")}
# end
# end

context "with teaching job roles" do
before do
create(:vacancy, :published_slugged, contact_number: "1", job_roles: %w[teacher], subjects: %w[English], phases: %w[secondary], working_patterns: %w[full_time])
end

let(:teacher_vacancy) { Vacancy.find_by!(contact_number: "1") }
let(:subscription) { create(:subscription, teaching_job_roles: %w[teacher], frequency: :daily) }

it "only finds the teaching job" do
expect(Jobseekers::AlertMailer).to receive(:alert).with(subscription.id, [teacher_vacancy, ect_job].pluck(:id)) { mail }
expect(Jobseekers::AlertMailer).to receive(:alert).with(subscription.id, [teacher_vacancy].pluck(:id)) { mail }
perform_enqueued_jobs { job }
end
end

context "with support job roles" do
before do
create(:vacancy, :published_slugged, contact_number: "2", job_roles: %w[it_support], subjects: %w[English], phases: %w[secondary], working_patterns: %w[full_time])
end

let(:support_vacancy) { Vacancy.find_by!(contact_number: "2") }
let(:subscription) { create(:subscription, support_job_roles: %w[it_support], frequency: :daily) }

it "only finds the support job" do
Expand All @@ -71,6 +73,11 @@
end

context "with visa sponsorship" do
before do
create(:vacancy, :published_slugged, contact_number: "3", visa_sponsorship_available: true, job_roles: %w[headteacher], subjects: %w[English], phases: %w[secondary], working_patterns: %w[full_time])
end

let(:visa_job) { Vacancy.find_by!(contact_number: "3") }
let(:subscription) { create(:subscription, :visa_sponsorship_required, frequency: :daily) }

it "only finds the visa job" do
Expand All @@ -80,7 +87,13 @@
end

context "with ECT" do
before do
create(:vacancy, :published_slugged, contact_number: "4", ect_status: :ect_suitable, job_roles: %w[headteacher teacher], subjects: %w[English], phases: %w[secondary], working_patterns: %w[full_time])
create(:vacancy, :published_slugged, ect_status: nil, job_roles: %w[headteacher teacher], subjects: %w[English], phases: %w[secondary], working_patterns: %w[full_time])
end

let(:subscription) { create(:subscription, :ect_suitable, frequency: :daily) }
let(:ect_job) { Vacancy.find_by!(contact_number: "4") }

it "only finds the ECT job" do
expect(Jobseekers::AlertMailer).to receive(:alert).with(subscription.id, [ect_job].pluck(:id)) { mail }
Expand All @@ -89,16 +102,26 @@
end

context "with subjects filter" do
before do
create(:vacancy, :published_slugged, contact_number: "5", job_roles: %w[headteacher], subjects: %w[French], phases: %w[secondary], working_patterns: %w[full_time])
end

let(:french_job) { Vacancy.find_by!(contact_number: "5") }
let(:subscription) { create(:subscription, subjects: %w[French], frequency: :daily) }

it "only finds the Maths job" do
it "only finds the French job" do
expect(Jobseekers::AlertMailer).to receive(:alert).with(subscription.id, [french_job].pluck(:id)) { mail }
perform_enqueued_jobs { job }
end
end

context "with phases filter" do
before do
create(:vacancy, :published_slugged, contact_number: "6", job_roles: %w[headteacher], phases: %w[primary], working_patterns: %w[full_time])
end

let(:subscription) { create(:subscription, phases: %w[primary], frequency: :daily) }
let(:primary_job) { Vacancy.find_by!(contact_number: "6") }

it "only finds the primary school job" do
expect(Jobseekers::AlertMailer).to receive(:alert).with(subscription.id, [primary_job].pluck(:id)) { mail }
Expand All @@ -107,7 +130,12 @@
end

context "with working patterns filter" do
before do
create(:vacancy, :published_slugged, contact_number: "7", job_roles: %w[headteacher], phases: %w[secondary], working_patterns: %w[part_time])
end

let(:subscription) { create(:subscription, working_patterns: %w[part_time], frequency: :daily) }
let(:part_time_job) { Vacancy.find_by!(contact_number: "7") }

it "only finds the part_time job" do
expect(Jobseekers::AlertMailer).to receive(:alert).with(subscription.id, [part_time_job].pluck(:id)) { mail }
Expand All @@ -116,6 +144,12 @@
end

context "with organisation filter" do
before do
create(:vacancy, :published_slugged, contact_number: "8", organisations: [new_org], job_roles: %w[headteacher], phases: %w[secondary], working_patterns: %w[full_time])
end

let(:new_org) { create(:school) }
let(:new_org_job) { Vacancy.find_by!(contact_number: "8") }
let(:subscription) { create(:subscription, organisation_slug: new_org.slug, frequency: :daily) }

it "only finds the new_publisher job" do
Expand Down

0 comments on commit d2b64a2

Please sign in to comment.