Skip to content

Commit

Permalink
feat(fworks): sync frameworks from faf
Browse files Browse the repository at this point in the history
  • Loading branch information
ryantk committed Sep 12, 2023
1 parent 11d3871 commit 2988113
Show file tree
Hide file tree
Showing 13 changed files with 198 additions and 2 deletions.
5 changes: 5 additions & 0 deletions app/models/frameworks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Frameworks
def self.table_name_prefix
"frameworks_"
end
end
6 changes: 6 additions & 0 deletions app/models/frameworks/framework.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Frameworks::Framework < ApplicationRecord
include FafImportable
include StatusChangeable

belongs_to :provider
end
21 changes: 21 additions & 0 deletions app/models/frameworks/framework/faf_importable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Frameworks::Framework::FafImportable
extend ActiveSupport::Concern

class_methods do
def import_from_faf(framework_details)
framework = find_or_initialize_by(provider_reference: framework_details.provider_reference)

provider = Frameworks::Provider.find_or_create_by!(name: framework_details.provider_name)

framework.update!(
status: :dfe_approved,
name: framework_details.name,
provider_url: framework_details.provider_url,
ends_at: framework_details.ends_at,
description: framework_details.description,
published_on_faf: true,
provider:,
)
end
end
end
12 changes: 12 additions & 0 deletions app/models/frameworks/framework/status_changeable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Frameworks::Framework::StatusChangeable
extend ActiveSupport::Concern

included do
enum status: {
pending_evaluation: 0,
not_approved: 1,
dfe_approved: 2,
cab_approved: 3,
}
end
end
2 changes: 2 additions & 0 deletions app/models/frameworks/provider.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Frameworks::Provider < ApplicationRecord
end
20 changes: 19 additions & 1 deletion app/services/support/sync_frameworks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ def initialize(endpoint: ENV["FAF_FRAMEWORK_ENDPOINT"])

def call
fetch_frameworks
upsert_frameworks if @frameworks.present?

if @frameworks.present?
upsert_frameworks
import_to_framework_register
end
end

private
Expand Down Expand Up @@ -36,6 +40,20 @@ def upsert_frameworks
)
end

def import_to_framework_register
@frameworks.each do |raw_framework|
framework = OpenStruct.new(
name: raw_framework["title"],
provider_reference: raw_framework["ref"],
provider_name: raw_framework["provider"].try(:[], "title"),
provider_url: raw_framework["url"],
ends_at: raw_framework["expiry"],
description: raw_framework["descr"],
)
Frameworks::Framework.import_from_faf(framework)
end
end

def prepare_frameworks(frameworks)
frameworks.select { |framework| framework["expiry"].present? }.map do |framework|
{
Expand Down
17 changes: 17 additions & 0 deletions db/migrate/20230823132039_create_frameworks_frameworks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class CreateFrameworksFrameworks < ActiveRecord::Migration[7.0]
def change
create_table :frameworks_frameworks, id: :uuid do |t|
t.integer :status, default: 0
t.string :provider_url
t.string :provider_reference
t.string :name
t.string :short_name
t.string :description
t.datetime :starts_at
t.datetime :ends_at
t.boolean :published_on_faf
t.uuid :provider_id, null: false
t.timestamps
end
end
end
8 changes: 8 additions & 0 deletions db/migrate/20230823132322_create_frameworks_providers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class CreateFrameworksProviders < ActiveRecord::Migration[7.0]
def change
create_table :frameworks_providers, id: :uuid do |t|
t.string :name
t.timestamps
end
end
end
23 changes: 22 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,27 @@
t.index ["user_id"], name: "index_framework_requests_on_user_id"
end

create_table "frameworks_frameworks", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.integer "status", default: 0
t.string "provider_url"
t.string "provider_reference"
t.string "name"
t.string "short_name"
t.string "description"
t.datetime "starts_at"
t.datetime "ends_at"
t.boolean "published_on_faf"
t.uuid "provider_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "frameworks_providers", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "journeys", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
Expand Down Expand Up @@ -408,8 +429,8 @@
t.boolean "exit_survey_sent", default: false
t.uuid "detected_category_id"
t.integer "creation_source"
t.uuid "created_by_id"
t.text "user_selected_category"
t.uuid "created_by_id"
t.uuid "procurement_stage_id"
t.string "initial_request_text"
t.boolean "with_school", default: false, null: false
Expand Down
5 changes: 5 additions & 0 deletions spec/factories/frameworks/frameworks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FactoryBot.define do
factory :frameworks_framework, class: "Frameworks::Framework" do
association :provider, factory: :frameworks_provider
end
end
4 changes: 4 additions & 0 deletions spec/factories/frameworks/providers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FactoryBot.define do
factory :frameworks_provider, class: "Frameworks::Provider" do
end
end
73 changes: 73 additions & 0 deletions spec/models/frameworks/framework/faf_importable_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
require "rails_helper"

describe Frameworks::Framework::FafImportable do
describe ".import_from_faf" do
subject(:frameworks) { Frameworks::Framework }

let(:framework_data) do
OpenStruct.new(
name: "Books, stationery and education supplies",
provider_reference: "books",
provider_name: "Super Books LTD",
provider_url: "https://my.framework.com",
ends_at: 2.weeks.from_now,
description: "Description of books"
)
end

it "creates frameworks with a status of dfe_approved already set" do
frameworks.import_from_faf(framework_data)
expect(Frameworks::Framework.all.all?(&:dfe_approved?)).to eq(true)
expect(Frameworks::Framework.all.all?(&:published_on_faf?)).to eq(true)
end

context "when the Framework already exists for the given provider_reference" do
let!(:existing_framework) { create(:frameworks_framework, provider_reference: "books") }

it "updates the Framework details only" do
frameworks.import_from_faf(framework_data)

existing_framework.reload
expect(existing_framework.provider_url).to eq("https://my.framework.com")
expect(existing_framework.name).to eq("Books, stationery and education supplies")
expect(existing_framework.ends_at).to eq(framework_data.ends_at)
expect(existing_framework.description).to eq("Description of books")
end
end

context "when the Framework does not already exist for the given provider_reference" do
it "creates a new Framework with the given details" do
expect { frameworks.import_from_faf(framework_data) }.to \
change { Frameworks::Framework.where(provider_reference: "books").count }.from(0).to(1)

new_framework = Frameworks::Framework.where(provider_reference: "books").first
expect(new_framework.provider_url).to eq("https://my.framework.com")
expect(new_framework.name).to eq("Books, stationery and education supplies")
expect(new_framework.ends_at).to eq(framework_data.ends_at)
expect(new_framework.description).to eq("Description of books")
end
end

context "when the Provider with the given name does not already exist" do
it "creates a new Provider linked to the Framework" do
expect { frameworks.import_from_faf(framework_data) }.to \
change { Frameworks::Provider.where(name: "Super Books LTD").count }.from(0).to(1)

framework = Frameworks::Framework.where(provider_reference: "books").first
expect(framework.provider.name).to eq("Super Books LTD")
end
end

context "when the Provider with the given name already exists" do
let!(:existing_provider) { create(:frameworks_provider, name: "Super Books LTD") }

it "links the existing Provider to the Framework" do
expect { frameworks.import_from_faf(framework_data) }.not_to \
change { Frameworks::Provider.where(name: "Super Books LTD").count }.from(1)

framework = Frameworks::Framework.where(provider_reference: "books").first
expect(framework.provider).to eq(existing_provider)
end
end
end
end
4 changes: 4 additions & 0 deletions spec/services/support/sync_frameworks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@
.and(not_change { existing_framework2.reload.expires_at })
end
end

it "converts the support frameworks into the framework register records" do
expect { service.call }.to change(Frameworks::Framework, :count).from(0).to(2)
end
end
end
end

0 comments on commit 2988113

Please sign in to comment.