Skip to content

Commit

Permalink
feat(cms): ability to sync frameworks with FaF
Browse files Browse the repository at this point in the history
Jira: PWNN-1529
  • Loading branch information
EdwinKruglov committed Jul 11, 2023
1 parent e339e2a commit 1058aa9
Show file tree
Hide file tree
Showing 18 changed files with 232 additions and 98 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,6 @@ AWS_SECRET_ACCESS_KEY=
AWS_REGION=
BUCKET_NAME=

# FaF
FAF_WEBHOOK_SECRET=
FAF_FRAMEWORK_ENDPOINT=
3 changes: 3 additions & 0 deletions .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,6 @@ MS_GRAPH_CLIENT_SECRET="z"
MS_GRAPH_SHARED_MAILBOX_USER_ID="c"
MS_GRAPH_SHARED_MAILBOX_NAME="mailbox"
MS_GRAPH_SHARED_MAILBOX_ADDRESS="[email protected]"

# FaF
FAF_FRAMEWORK_ENDPOINT=http://faf.test
1 change: 1 addition & 0 deletions .github/workflows/ci-full-pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ jobs:
-e PROC_OPS_TEAM="DSI Caseworkers" \
-e QUALTRICS_SURVEY_URL=https://dferesearch.fra1.qualtrics.com \
-e [email protected] \
-e FAF_FRAMEWORK_ENDPOINT=http://faf.test \
-e CI_NODE_TOTAL=${{ matrix.ci_node_total }} \
-e CI_NODE_INDEX=${{ matrix.ci_node_index }} \
-e CI=1 \
Expand Down
29 changes: 0 additions & 29 deletions app/controllers/api/find_a_framework/frameworks_controller.rb

This file was deleted.

15 changes: 15 additions & 0 deletions app/controllers/support/management/sync_frameworks_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Support
module Management
class SyncFrameworksController < BaseController
after_action -> { flash.discard }, only: :create

def index; end

def create
Support::SyncFrameworksJob.perform_later
flash[:notice] = "Task triggered"
render :index
end
end
end
end
9 changes: 9 additions & 0 deletions app/jobs/support/sync_frameworks_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Support
class SyncFrameworksJob < ApplicationJob
queue_as :support

def perform
Support::SyncFrameworks.new.call
end
end
end
49 changes: 49 additions & 0 deletions app/services/support/sync_frameworks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module Support
class SyncFrameworks
def initialize(endpoint: ENV["FAF_FRAMEWORK_ENDPOINT"])
@endpoint = endpoint
end

def call
fetch_frameworks
upsert_frameworks if @frameworks.present?
end

private

def fetch_frameworks
uri = URI.parse(@endpoint)
response =
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https") do |http|
request = Net::HTTP::Get.new(uri)
http.request(request)
end

if response.code == "200"
@frameworks = JSON.parse(response.body)
else
Rollbar.error("Could not fetch frameworks", uri: @endpoint, status: response.code)
end
end

def upsert_frameworks
prepared_frameworks = prepare_frameworks(@frameworks)
Support::Framework.upsert_all(
prepared_frameworks,
unique_by: %i[ref],
)
end

def prepare_frameworks(frameworks)
frameworks.select { |framework| framework["expiry"].present? }.map do |framework|
{
name: framework["title"],
ref: framework["ref"],
supplier: framework["provider"].try(:[], "initials"),
category: framework["cat"].try(:[], "title"),
expires_at: Date.parse(framework["expiry"]),
}
end
end
end
end
1 change: 1 addition & 0 deletions app/views/support/management/base/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<h2 class="govuk-heading-m"><%= I18n.t("support.management.base.tasks") %></h2>

<ul>
<li><%= link_to I18n.t("support.management.sync_frameworks.header"), support_management_sync_frameworks_path, class: "govuk-link" %></li>
<li><%= link_to I18n.t("support.management.all_cases_surveys.header"), support_management_all_cases_surveys_path, class: "govuk-link" %></li>
</ul>

Expand Down
20 changes: 20 additions & 0 deletions app/views/support/management/sync_frameworks/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<%= content_for :title, "#{I18n.t("support.management.base.header")} | #{I18n.t("support.management.sync_frameworks.header")}" %>

<div class="govuk-breadcrumbs">
<ol class="govuk-breadcrumbs__list">
<li class="govuk-breadcrumbs__list-item">
<%= link_to I18n.t("support.management.base.header"), support_management_path, class: "govuk-breadcrumbs__link" %>
</li>
<li class="govuk-breadcrumbs__list-item">
<%= link_to I18n.t("support.management.sync_frameworks.header"), support_management_email_templates_path, class: "govuk-breadcrumbs__link" %>
</li>
</ol>
</div>

<h1 class="govuk-heading-l"><%= I18n.t("support.management.sync_frameworks.header") %></h1>

<% I18n.t("support.management.sync_frameworks.body").each do |p| %>
<p class="govuk-body"><%= p.html_safe %></p>
<% end %>
<%= button_to I18n.t("support.management.sync_frameworks.sync"), support_management_sync_frameworks_path, class: "govuk-button" %>
6 changes: 6 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1465,6 +1465,12 @@ en:
notice: Updates to your template have been saved
destroy:
notice: Your template has been deleted
sync_frameworks:
header: Synchronise frameworks
body:
- Frameworks are fetched from Find a Framework and saved automatically every midnight and midday.
- Click <strong>Synchronise</strong> to trigger the task manually.
sync: Synchronise
procurement_details:
edit:
ended_at:
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@
end
resource :category_detection, only: %i[new create]
resources :all_cases_surveys, only: %i[index create]
resources :sync_frameworks, only: %i[index create]
end
end

Expand Down
5 changes: 5 additions & 0 deletions config/schedule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,8 @@ refresh_establishment_groups:
cron: 0 4 1 * * # At 04:00 on day-of-month 1.
class: Support::RefreshEstablishmentGroupsJob
description: Update internal EstablishmentGroup records using public data from GIAS.

sync_frameworks:
cron: "0 0,12 * * *" # every midnight and midday
class: Support::SyncFrameworksJob
description: Fetch and persist frameworks from FaF
6 changes: 6 additions & 0 deletions db/migrate/20230707133414_add_ref_to_support_frameworks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddRefToSupportFrameworks < ActiveRecord::Migration[7.0]
def change
add_column :support_frameworks, :ref, :string
add_index :support_frameworks, :ref, unique: true
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[7.0].define(version: 2023_06_29_150515) do
ActiveRecord::Schema[7.0].define(version: 2023_07_07_133414) do
# These are extensions that must be enabled in order to support this database
enable_extension "citext"
enable_extension "pg_trgm"
Expand Down Expand Up @@ -559,7 +559,9 @@
t.date "expires_at"
t.datetime "created_at", default: -> { "CURRENT_TIMESTAMP" }, null: false
t.datetime "updated_at", default: -> { "CURRENT_TIMESTAMP" }, null: false
t.string "ref"
t.index ["name", "supplier"], name: "index_support_frameworks_on_name_and_supplier", unique: true
t.index ["ref"], name: "index_support_frameworks_on_ref", unique: true
end

create_table "support_group_types", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
Expand Down
26 changes: 26 additions & 0 deletions lib/tasks/data_migrations.rake
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,30 @@ namespace :data_migrations do
agent.save!
end
end

desc "Populate Support::Framework refs"
task populate_framework_refs: :environment do
uri = URI.parse(ENV["FAF_FRAMEWORK_ENDPOINT"])
response =
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https") do |http|
request = Net::HTTP::Get.new(uri)
http.request(request)
end

if response.code == "200"
frameworks = JSON.parse(response.body)
prepared_frameworks = frameworks.map do |framework|
{
name: framework["title"],
supplier: framework["provider"]["initials"],
ref: framework["ref"],
}
end

Support::Framework.all.each do |framework|
match = prepared_frameworks.select { |p| p[:name] == framework.name && p[:supplier] == framework.supplier }
framework.update!(ref: match.first[:ref]) if match.present?
end
end
end
end
1 change: 1 addition & 0 deletions spec/factories/support/framework.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
supplier { "Test supplier" }
category { "Test category" }
expires_at { "2022-12-02" }
sequence(:ref) { |n| "test-#{n}" }
end
end

This file was deleted.

Loading

0 comments on commit 1058aa9

Please sign in to comment.