Skip to content

Commit

Permalink
updated files for framework register changes. git clean commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
edwin-jebaraj committed Nov 22, 2024
1 parent 1424465 commit 4f2c008
Show file tree
Hide file tree
Showing 21 changed files with 333 additions and 181 deletions.
2 changes: 1 addition & 1 deletion app/components/activity_log/history_item_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class ActivityLog::HistoryItemComponent < ViewComponent::Base

def initialize(by:, date:, title: nil)
@title = title
@by = by
@by = by || "FaF Admin tool"
@date = date
end
end
7 changes: 4 additions & 3 deletions app/controllers/frameworks/frameworks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ def create

def update
@framework = Frameworks::Framework.find(params[:id])
@framework.assign_attributes(framework_params)

if @framework.update(framework_params)
if @framework.save(context: :updation_form)
redirect_to frameworks_framework_path(@framework)
else
render :edit
Expand All @@ -46,8 +47,8 @@ def update
def load_form_options
@procops_agents = Support::Agent.caseworkers
@e_and_o_agents = Support::Agent.e_and_o_staff
@providers = Frameworks::Provider.all
@provider_contacts = Frameworks::ProviderContact.all
@providers = Frameworks::Provider.all.order(:short_name)
@provider_contacts = Frameworks::ProviderContact.all.order(:name)
end

def filter_form_params
Expand Down
9 changes: 8 additions & 1 deletion app/models/frameworks/framework.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@ class Frameworks::Framework < ApplicationRecord
after_add: :log_framework_category_added,
after_remove: :log_framework_category_removed

validates :provider_id, presence: { message: "Please select a provider" }, on: :creation_form
validates :name, :url, :provider_id, :proc_ops_lead_id, :provider_end_date, presence: true, on: %i[creation_form updation_form]
validate :unique_name_and_provider, if: -> { name.present? }, on: :creation_form

def unique_name_and_provider
if name && provider_id && Frameworks::Framework.where(name:, provider_id:).exists?
errors.add(:name, :unique_name_and_provider)
end
end

enum :lot, {
single: 0,
Expand Down
2 changes: 1 addition & 1 deletion app/models/frameworks/framework/filtering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Frameworks::Framework::Filtering

initial_scope -> { Frameworks::Framework }

filter_by :status, options: -> { Frameworks::Framework.statuses.map { |label, _id| [label.humanize, label] } }
filter_by :status, options: -> { Frameworks::Framework.ordered_statuses_by_approval.map { |label, _id| [I18n.t(label, scope: "framework.framework_statuses"), label] } }
filter_by :provider, options: -> { Frameworks::Provider.order("short_name ASC").pluck(:short_name, :id) }
filter_by :e_and_o_lead, options: -> { get_agents[:e_and_o_lead_id].map { |agent| [agent.full_name, agent.id] } }
filter_by :proc_ops_lead, options: -> { get_agents[:proc_ops_lead_id].map { |agent| [agent.full_name, agent.id] } }
Expand Down
13 changes: 5 additions & 8 deletions app/models/frameworks/framework/sortable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ module Frameworks::Framework::Sortable

included do
scope :sort_by_updated, ->(direction = "descending") { order("frameworks_frameworks.updated_at #{safe_direction(direction)}") }
scope :sort_by_dfe_start_date, ->(direction = "descending") { order("dfe_start_date #{safe_direction(direction)}") }
scope :sort_by_dfe_review_date, ->(direction = "descending") { order("dfe_review_date #{safe_direction(direction)}") }
scope :sort_by_faf_end_date, ->(direction = "descending") { order("faf_end_date #{safe_direction(direction)}") }
scope :sort_by_provider_start_date, ->(direction = "descending") { order("provider_start_date #{safe_direction(direction)}") }
scope :sort_by_provider_end_date, ->(direction = "descending") { order("provider_end_date #{safe_direction(direction)}") }
scope :sort_by_reference, ->(direction = "descending") { order(Arel.sql("regexp_replace(frameworks_frameworks.reference, 'F', '')::int #{safe_direction(direction)}")) }
scope :sort_by_reference, lambda { |direction = "descending"|
sanitized_direction = safe_direction(direction)
order(Arel.sql("regexp_replace(frameworks_frameworks.reference, '[^0-9]', '', 'g')::int") => sanitized_direction)
}
scope :sort_by_provider_reference, ->(direction = "descending") { order("provider_reference #{safe_direction(direction)}") }
end

Expand All @@ -23,16 +23,13 @@ def available_sort_options
[
["Updated", "updated"],
["Reference", "reference"],
["DfE start date", "dfe_start_date"],
["DfE review date", "dfe_review_date"],
["Provider start date", "provider_start_date"],
["Provider end date", "provider_end_date"],
["FaF end date", "faf_end_date"],
]
end

def safe_direction(direction)
direction == "descending" ? "DESC" : "ASC"
direction.to_s.match?(/\Adesc/i) ? "desc" : "asc"
end
end
end
1 change: 1 addition & 0 deletions app/models/frameworks/framework/sourceable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Frameworks::Framework::Sourceable
enum :source, {
spreadsheet_import: 0,
register: 1,
faf_import: 2,
}
end
end
9 changes: 8 additions & 1 deletion app/models/frameworks/framework/status_changeable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@ module Frameworks::Framework::StatusChangeable
evaluating: 2,
dfe_approved: 3,
cab_approved: 4,
expired: 5,
archived: 6,
}

# Method to return statuses in approval order
def self.ordered_statuses_by_approval
%w[dfe_approved cab_approved evaluating expired not_approved pending_evaluation archived]
end

aasm column: :status, enum: true do
Frameworks::Framework.statuses.each_key { |status| state status.to_sym }
Frameworks::Framework.ordered_statuses_by_approval.each { |status| state status.to_sym }

event :draft_evaluation do
transitions from: %i[not_approved], to: :pending_evaluation, after: :after_drafting_evaluation
Expand Down
126 changes: 117 additions & 9 deletions app/services/support/sync_frameworks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ module Support
class SyncFrameworks
include InsightsTrackable

delegate :statuses, to: ::Frameworks::Framework

def initialize(endpoint: ENV["FAF_FRAMEWORK_ENDPOINT"])
@endpoint = endpoint
end

def call
fetch_frameworks
upsert_frameworks if @frameworks.present?
update_frameworks if @frameworks.present?
end

private
Expand All @@ -28,24 +30,130 @@ def fetch_frameworks
end
end

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

prepared_frameworks.each do |faf|
records = ::Frameworks::Framework.where("LOWER(name) = LOWER(?) AND provider_id = ?", faf[:name].strip, faf[:provider_id])

if records.exists?
records.each do |record|
new_status = determine_new_status(record, faf, records.count)
update_record(record, faf, new_status)
end
else
record = ::Frameworks::Framework.new(name: faf[:name].strip, provider_id: faf[:provider_id])
update_record(record, faf, statuses["dfe_approved"])
record.save!
end
end

# check expired framework
update_expired_status
# check archived framework
update_archive_status(prepared_frameworks)
end

def determine_new_status(record, faf, record_count)
case record.status
when "dfe_approved"
get_framework_status(record.status, faf[:provider_end_date])
when "not_approved"
record_count > 1 ? statuses["archived"] : statuses["dfe_approved"]
else
record.status
end
end

def update_record(record, faf, new_status)
record.update(
faf_slug_ref: faf[:faf_slug_ref],
faf_category: faf[:faf_category],
provider_end_date: faf[:provider_end_date],
url: faf[:url],
description: faf[:description],
source: 2,
status: new_status,
)
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"]),
faf_slug_ref: framework["ref"],
provider_id: existing_provider(framework["provider"].try(:[], "initials"), framework["provider"].try(:[], "title")),
faf_category: framework["cat"].try(:[], "title"),
provider_end_date: Date.parse(framework["expiry"]),
url: framework["url"],
description: framework["descr"],
}
end
end

def expired?(provider_end_date)
provider_end_date && provider_end_date < Time.zone.today
end

def get_expired_status(provider_end_date)
provider_end_date && provider_end_date < Time.zone.today ? statuses["expired"] : statuses["dfe_approved"]
end

def existing_provider(provider_short_name, provider_name)
# check provider name exist
provider_detail = ::Frameworks::Provider.find_by("LOWER(short_name) = LOWER(?)", provider_short_name)
if provider_detail
if provider_detail.name != provider_name
provider_detail.update!(name: provider_name) # to update provider name when title is diff
end
else
provider_detail = ::Frameworks::Provider.new(short_name: provider_short_name, name: provider_name)
provider_detail.save!
end
provider_detail.id
end

def get_framework_status(status, provider_end_date)
if status == statuses["expired"] && !expired?(provider_end_date)
statuses["dfe_approved"]
elsif status == statuses["archived"]
statuses["archived"]
else
get_expired_status(provider_end_date)
end
end

def update_archive_status(prepared_frameworks)
cms_frameworks = ::Frameworks::Framework.where(source: 2)

cms_frameworks.each do |cms_framework|
exists = prepared_frameworks.any? do |faf_framework|
cms_framework.name.strip.casecmp?(faf_framework[:name].strip) && cms_framework.provider_id == faf_framework[:provider_id]
end

next if exists

::Frameworks::Framework.find_by!(
name: cms_framework.name,
provider_id: cms_framework.provider_id,
).update!(
status: statuses["archived"],
is_archived: true,
faf_archived_at: Time.zone.today,
)
end
end

def update_expired_status
cms_frameworks = ::Frameworks::Framework.all

cms_frameworks.each do |cms_framework|
if expired?(cms_framework.provider_end_date) && cms_framework.status != "archived"
::Frameworks::Framework.find_by!(id: cms_framework.id).update!(status: statuses["expired"])
elsif !expired?(cms_framework.provider_end_date) && cms_framework.status == "expired" && cms_framework.source != "faf_import"
::Frameworks::Framework.find_by!(id: cms_framework.id).update!(status: statuses["dfe_approved"])
end
end
end
end
end
Loading

0 comments on commit 4f2c008

Please sign in to comment.