From d2b64a29cd2b0cab10aa79b211ba7a618dcfe827 Mon Sep 17 00:00:00 2001 From: starswan Date: Thu, 28 Nov 2024 16:03:07 +0000 Subject: [PATCH] handle keyword search in memory --- app/jobs/alert_email/base.rb | 10 +-- spec/jobs/send_daily_alert_email_job_spec.rb | 84 ++++++++++++++------ 2 files changed, 60 insertions(+), 34 deletions(-) diff --git a/app/jobs/alert_email/base.rb b/app/jobs/alert_email/base.rb index c7ce3acb74..4452196797 100644 --- a/app/jobs/alert_email/base.rb +++ b/app/jobs/alert_email/base.rb @@ -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 @@ -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| @@ -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)] diff --git a/spec/jobs/send_daily_alert_email_job_spec.rb b/spec/jobs/send_daily_alert_email_job_spec.rb index a960a3d1dd..f747f07a6a 100644 --- a/spec/jobs/send_daily_alert_email_job_spec.rb +++ b/spec/jobs/send_daily_alert_email_job_spec.rb @@ -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" } @@ -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 @@ -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 @@ -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 } @@ -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 } @@ -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 } @@ -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