Skip to content

Commit

Permalink
Merge pull request #819 from DFE-Digital/CPDNPQ-1188-background-job-t…
Browse files Browse the repository at this point in the history
…o-update-status-fields

CPDNPQ-1188 Create a task to update status fields in the NPQ
  • Loading branch information
javier-npq authored Jul 11, 2023
2 parents 21a80b3 + 451b3e6 commit 0502bc6
Show file tree
Hide file tree
Showing 44 changed files with 341 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/deploy_to_review_app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ jobs:
--command "cd /app && /usr/local/bundle/bin/bundle exec rails runner 'ImportGiasSchoolsJob.perform_later'" \
--process worker \
--name schedule_school_import
cf run-task "${{ env.APP_NAME }}"-"${{ env.ENV_STUB }}" \
--command "cd /app && /usr/local/bundle/bin/bundle exec rails runner 'ApplicationSynchronizationJob.perform_later'" \
--process worker \
--name application_synchronization_job
cf run-task "${{ env.APP_NAME }}"-"${{ env.ENV_STUB }}" \
--command "cd /app && /usr/local/bundle/bin/bundle exec rails \"private_childcare_providers:import[lib/private_childcare_providers/2021-08-31/childcare_providers.csv, childcare_providers]\"" \
Expand Down
82 changes: 82 additions & 0 deletions .github/workflows/update_application_statuses.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
name: Update Application Statuses
on:
schedule:
- cron: '0 0 * * *'

jobs:
sweep:
name: Update Application Statuses

runs-on: ubuntu-20.04
steps:
- name: Install CloudFoundry CLI
shell: bash
run: |
wget -q -O - https://packages.cloudfoundry.org/debian/cli.cloudfoundry.org.key | sudo apt-key add -
echo "deb https://packages.cloudfoundry.org/debian stable main" | sudo tee /etc/apt/sources.list.d/cloudfoundry-cli.list
sudo apt-get update
sudo apt-get install cf7-cli
- name: Update Application Statuses for dev
shell: bash
env:
PAAS_ORGANISATION: dfe
# This is a shared PAAS space
PAAS_SPACE: earlycareers-framework-dev
ENV_STUB: dev
APP_NAME: npq-registration
CF_USERNAME: ${{ secrets.GOVPAAS_DEV_USERNAME }}
CF_PASSWORD: ${{ secrets.GOVPAAS_DEV_PASSWORD }}
run: |
cf api https://api.london.cloud.service.gov.uk
cf auth
cf target -o "${{ env.PAAS_ORGANISATION }}" -s "${{ env.PAAS_SPACE }}"
cf run-task "${{ env.APP_NAME }}"-"${{ env.ENV_STUB }}" --command "cd /app && /usr/local/bundle/bin/bundle exec rails runner 'ApplicationSynchronizationJob.perform_later'" --process worker --name update_application_statuses
- name: Update Application Statuses for staging
shell: bash
env:
PAAS_ORGANISATION: dfe
# This is a shared PAAS space
PAAS_SPACE: early-careers-framework-staging
ENV_STUB: staging
APP_NAME: npq-registration
CF_USERNAME: ${{ secrets.GOVPAAS_STAG_USERNAME }}
CF_PASSWORD: ${{ secrets.GOVPAAS_STAG_PASSWORD }}
run: |
cf api https://api.london.cloud.service.gov.uk
cf auth
cf target -o "${{ env.PAAS_ORGANISATION }}" -s "${{ env.PAAS_SPACE }}"
cf run-task "${{ env.APP_NAME }}"-"${{ env.ENV_STUB }}" --command "cd /app && /usr/local/bundle/bin/bundle exec rails runner 'ApplicationSynchronizationJob.perform_later'" --process worker --name update_application_statuses
- name: Update Application Statuses for sandbox
shell: bash
env:
PAAS_ORGANISATION: dfe
# This is a shared PAAS space
PAAS_SPACE: early-careers-framework-sandbox
ENV_STUB: sandbox
APP_NAME: npq-registration
CF_USERNAME: ${{ secrets.GOVPAAS_STAG_USERNAME }}
CF_PASSWORD: ${{ secrets.GOVPAAS_STAG_PASSWORD }}
run: |
cf api https://api.london.cloud.service.gov.uk
cf auth
cf target -o "${{ env.PAAS_ORGANISATION }}" -s "${{ env.PAAS_SPACE }}"
cf run-task "${{ env.APP_NAME }}"-"${{ env.ENV_STUB }}" --command "cd /app && /usr/local/bundle/bin/bundle exec rails runner 'ApplicationSynchronizationJob.perform_later'" --process worker --name update_application_statuses
- name: Update Application Statuses for prod
shell: bash
env:
PAAS_ORGANISATION: dfe
# This is a shared PAAS space
PAAS_SPACE: early-careers-framework-prod
ENV_STUB: prod
APP_NAME: npq-registration
CF_USERNAME: ${{ secrets.GOVPAAS_PROD_USERNAME }}
CF_PASSWORD: ${{ secrets.GOVPAAS_PROD_PASSWORD }}
run: |
cf api https://api.london.cloud.service.gov.uk
cf auth
cf target -o "${{ env.PAAS_ORGANISATION }}" -s "${{ env.PAAS_SPACE }}"
cf run-task "${{ env.APP_NAME }}"-"${{ env.ENV_STUB }}" --command "cd /app && /usr/local/bundle/bin/bundle exec rails runner 'ApplicationSynchronizationJob.perform_later'" --process worker --name update_application_statuses
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,4 @@ ENV RAILS_ENV=production

EXPOSE ${PORT}

CMD RAILS_ENV=${RAILS_ENV} bundle exec rake db:migrate && bundle exec rails s -e ${RAILS_ENV} -p ${PORT} --binding=0.0.0.0
CMD RAILS_ENV=${RAILS_ENV} bundle exec rake db:migrate && bundle exec whenever --update-crontab && bundle exec rails s -e ${RAILS_ENV} -p ${PORT} --binding=0.0.0.0
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ ruby File.read(".ruby-version").chomp
gem "activerecord-session_store"
gem "bootsnap", ">= 1.1.0", require: false
gem "canonical-rails"
gem "daemons"
gem "delayed_job", "~> 4.1"
gem "delayed_job_active_record"
gem "devise", "~> 4.9"
Expand Down Expand Up @@ -37,6 +38,7 @@ gem "sentry-rails"
gem "sentry-ruby"
gem "stimulus-rails"
gem "webpacker"
gem "whenever"

gem "net-imap", require: false
gem "net-pop", require: false
Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,15 @@ GEM
capybara (>= 1.0, < 4)
launchy
childprocess (4.1.0)
chronic (0.10.2)
coderay (1.1.3)
coercible (1.0.0)
descendants_tracker (~> 0.0.1)
concurrent-ruby (1.2.2)
crack (0.4.5)
rexml
crass (1.0.6)
daemons (1.4.1)
date (3.3.3)
deep_merge (1.2.1)
delayed_job (4.1.11)
Expand Down Expand Up @@ -500,6 +502,8 @@ GEM
websocket-driver (0.7.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
whenever (1.0.0)
chronic (>= 0.6.3)
with_model (2.1.6)
activerecord (>= 5.2)
xpath (3.2.0)
Expand All @@ -521,6 +525,7 @@ DEPENDENCIES
canonical-rails
capybara
capybara-screenshot
daemons
delayed_job (~> 4.1)
delayed_job_active_record
devise (~> 4.9)
Expand Down Expand Up @@ -573,6 +578,7 @@ DEPENDENCIES
webdrivers
webmock
webpacker
whenever
with_model

RUBY VERSION
Expand Down
6 changes: 6 additions & 0 deletions app/jobs/application_synchronization_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class ApplicationSynchronizationJob < ApplicationJob
queue_as :default
def perform
Services::Ecf::EcfApplicationSynchronization.new.call
end
end
66 changes: 66 additions & 0 deletions app/lib/services/ecf/ecf_application_synchronization.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
module Services
module Ecf
class EcfApplicationSynchronization
def call
uri = build_uri
request = build_http_get_request(uri)
response = send_http_request(uri, request)
handle_response(response)
rescue StandardError => e
Rails.logger.error "An error occurred during application synchronization: #{e.message}"
end

private

def build_uri
URI.parse("#{ENV['ECF_APP_BASE_URL']}/api/v1/npq/application_synchronizations")
end

def build_http_get_request(uri)
request = Net::HTTP::Get.new(uri)
request["Authorization"] = "Bearer #{ENV['ECF_APP_BEARER_TOKEN']}"
request
end

def send_http_request(uri, request)
Net::HTTP.start(uri.hostname, uri.port, use_ssl: use_ssl?, read_timeout: 30) do |http|
http.request(request)
end
end

def handle_response(response)
if response.is_a?(Net::HTTPSuccess)
data = response_data(response)
data.each do |record|
id = record.id
lead_provider_approval_status = record.lead_provider_approval_status
participant_outcome_state = record.participant_outcome_state

application = Application.find_by!(ecf_id: id)
application.update!(lead_provider_approval_status:, participant_outcome_state:)
end
else
raise "Failed to update application: #{response.message}"
end
end

def response_data(response)
data = JSON.parse(response.body)["data"]
array = []
data.each do |record|
array << OpenStruct.new(
id: record["attributes"]["id"],
lead_provider_approval_status: record["attributes"]["lead_provider_approval_status"],
participant_outcome_state: record["attributes"]["participant_outcome_state"],
)
end

array
end

def use_ssl?
build_uri.scheme == "https"
end
end
end
end
2 changes: 1 addition & 1 deletion app/views/accounts/_application_table.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
{
key: { text: "Provider" },
value: { text: application.lead_provider.name },
}
},
])
end %>
<% end %>
13 changes: 13 additions & 0 deletions app/views/accounts/_provider_details.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<%= govuk_summary_card(title: "Provider details") do |card|

card.with_summary_list(rows: [
{
key: { text: "Provider" },
value: { text: application.lead_provider.name }
},
{
key: { text: "Provider Application" },
value: { text: application.participant_outcome_state ? application.participant_outcome_state&.capitalize : application.lead_provider_approval_status&.capitalize },
},
])
end %>
1 change: 1 addition & 0 deletions app/views/accounts/user_registrations/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@
<h1 class="govuk-heading-xl"><%= title_embedded_course_name(@application.course) %> registration details</h1>
<%= render "accounts/work_details", application: @application %>
<%= render "accounts/course_details", application: @application %>
<%= render "accounts/provider_details", application: @application %>
5 changes: 5 additions & 0 deletions config/schedule.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require File.expand_path(File.join(File.dirname(__FILE__), "..", "config", "environment"))

every 1.day, at: "00:00" do
runner "ApplicationSynchronizationJob.perform_later", environment: Rails.env
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddLeadProviderApprovalStatusToApplications < ActiveRecord::Migration[6.1]
def change
add_column :applications, :lead_provider_approval_status, :text
add_column :applications, :participant_outcome_state, :text
end
end
4 changes: 3 additions & 1 deletion 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.define(version: 2023_04_14_121939) do
ActiveRecord::Schema.define(version: 2023_06_19_162920) do

# These are extensions that must be enabled in order to support this database
enable_extension "btree_gin"
Expand Down Expand Up @@ -51,6 +51,8 @@
t.integer "number_of_pupils", default: 0
t.boolean "tsf_primary_eligibility", default: false
t.boolean "tsf_primary_plus_eligibility", default: false
t.text "lead_provider_approval_status"
t.text "participant_outcome_state"
t.index ["course_id"], name: "index_applications_on_course_id"
t.index ["lead_provider_id"], name: "index_applications_on_lead_provider_id"
t.index ["user_id"], name: "index_applications_on_user_id"
Expand Down
4 changes: 4 additions & 0 deletions docs/github_actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ Runs once a day to delete stale sessions from the database.

Runs on demand to update course definitions in the database. Syncing them to match `Courses::DEFINITIONS`.

### Update Application Statuses ([update_application_statuses.yml](../.github/workflows/update_application_statuses.yml))

Runs once a day to update applications lead_provider_approval_status and participant_outcome_state retrieved from the ecf.

### Updating Schools

See [Importing schools](importing_data.md#importing-schools) for more information.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@
"headteacher_status" => nil,
"itt_provider" => nil,
"lead_mentor" => false,
"lead_provider_approval_status" => nil,
"participant_outcome_state" => nil,
"lead_provider_id" => LeadProvider.find_by(name: "Teach First").id,
"private_childcare_provider_urn" => nil,
"school_urn" => "100000",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@
"kind_of_nursery" => nil,
"itt_provider" => nil,
"lead_mentor" => false,
"lead_provider_approval_status" => nil,
"participant_outcome_state" => nil,
"private_childcare_provider_urn" => nil,
"school_urn" => nil,
"targeted_delivery_funding_eligibility" => false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@
"tsf_primary_plus_eligibility" => false,
"itt_provider" => nil,
"lead_mentor" => false,
"lead_provider_approval_status" => nil,
"participant_outcome_state" => nil,
"works_in_childcare" => true,
"works_in_nursery" => nil,
"works_in_school" => false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@
"headteacher_status" => nil,
"itt_provider" => nil,
"lead_mentor" => false,
"lead_provider_approval_status" => nil,
"participant_outcome_state" => nil,
"kind_of_nursery" => "private_nursery",
"lead_provider_id" => LeadProvider.find_by(name: "Teach First").id,
"private_childcare_provider_urn" => "EY123456",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@
"headteacher_status" => nil,
"itt_provider" => nil,
"lead_mentor" => false,
"lead_provider_approval_status" => nil,
"participant_outcome_state" => nil,
"kind_of_nursery" => public_kind_of_nursery_key,
"lead_provider_id" => LeadProvider.find_by(name: "Teach First").id,
"private_childcare_provider_urn" => nil,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@
"kind_of_nursery" => nil,
"itt_provider" => nil,
"lead_mentor" => false,
"lead_provider_approval_status" => nil,
"participant_outcome_state" => nil,
"headteacher_status" => nil,
"lead_provider_id" => LeadProvider.find_by(name: "Teach First").id,
"private_childcare_provider_urn" => nil,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@
"kind_of_nursery" => nil,
"itt_provider" => nil,
"lead_mentor" => false,
"lead_provider_approval_status" => nil,
"participant_outcome_state" => nil,
"lead_provider_id" => LeadProvider.find_by(name: "Teach First").id,
"private_childcare_provider_urn" => nil,
"school_urn" => "100000",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@
"school_urn" => "100000",
"itt_provider" => nil,
"lead_mentor" => false,
"lead_provider_approval_status" => nil,
"participant_outcome_state" => nil,
"targeted_delivery_funding_eligibility" => false,
"teacher_catchment" => "england",
"teacher_catchment_country" => nil,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@
"funding_choice" => "school",
"itt_provider" => nil,
"lead_mentor" => false,
"lead_provider_approval_status" => nil,
"participant_outcome_state" => nil,
"funding_eligiblity_status_code" => "not_in_england",
"headteacher_status" => nil,
"kind_of_nursery" => nil,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@
"school_urn" => "100000",
"itt_provider" => nil,
"lead_mentor" => false,
"lead_provider_approval_status" => nil,
"participant_outcome_state" => nil,
"targeted_delivery_funding_eligibility" => false,
"teacher_catchment" => "england",
"teacher_catchment_country" => nil,
Expand Down
Loading

0 comments on commit 0502bc6

Please sign in to comment.