Skip to content

Commit

Permalink
Merge pull request #1821 from DFE-Digital/remove-core-legacy-code
Browse files Browse the repository at this point in the history
Remove legacy CRM integration code from core classes
  • Loading branch information
ethax-ross authored Jul 23, 2021
2 parents 9cd80ce + 633c443 commit d7423d9
Show file tree
Hide file tree
Showing 46 changed files with 382 additions and 1,112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,13 @@ def create

unless registration_session.completed?
self.current_candidate = Bookings::Candidate.create_or_update_from_registration_session! \
gitis_crm,
registration_session,
current_contact

placement_request = current_candidate.placement_requests.create_from_registration_session! \
registration_session,
cookies[:analytics_tracking_uuid]

api_contact = current_contact.is_a?(GetIntoTeachingApiClient::SchoolsExperienceSignUp)

unless Bookings::Gitis::PrivacyPolicy.default.nil? || api_contact
AcceptPrivacyPolicyJob.perform_later \
current_candidate.gitis_uuid,
Bookings::Gitis::PrivacyPolicy.default
end

Bookings::Gitis::EventLogger.write_later \
current_candidate.gitis_uuid, :request, placement_request

Expand Down
17 changes: 3 additions & 14 deletions app/controllers/concerns/gitis_authentication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,19 @@ class InvalidContact < RuntimeError; end
def current_contact
return nil unless session[:gitis_contact]

@current_contact ||=
if Flipper.enabled?(:git_api)
GetIntoTeachingApiClient::SchoolsExperienceSignUp.new(session[:gitis_contact])
else
Bookings::Gitis::Contact.new(session[:gitis_contact])
end
GetIntoTeachingApiClient::SchoolsExperienceSignUp.new(session[:gitis_contact])
end

def current_contact=(contact)
valid_classes = [GetIntoTeachingApiClient::SchoolsExperienceSignUp, Bookings::Gitis::Contact]
raise InvalidContact unless valid_classes.any? { |klass| contact.is_a?(klass) }
raise InvalidContact unless contact.is_a?(GetIntoTeachingApiClient::SchoolsExperienceSignUp)

if current_contact && current_contact.candidate_id != contact.candidate_id
Rails.logger.warn "Signed in Candidate overwritten - #{current_contact.candidate_id} to #{contact.candidate_id}"
delete_registration_sessions!
end

contact.tap do |c|
session[:gitis_contact] =
if c.is_a?(GetIntoTeachingApiClient::SchoolsExperienceSignUp)
c.to_hash
else
c.attributes
end
session[:gitis_contact] = c.to_hash
end
end

Expand Down
8 changes: 8 additions & 0 deletions app/helpers/gitis_contact_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,12 @@ def gitis_contact_display_address(contact)
contact.address_postcode,
].compact.join(", ")
end

def gitis_contact_full_name(contact)
if contact.is_a?(Bookings::Gitis::MissingContact)
contact.full_name
else
"#{contact.first_name} #{contact.last_name}"
end
end
end
32 changes: 9 additions & 23 deletions app/models/bookings/candidate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,33 +51,23 @@ def find_by_gitis_contact!(contact)
end
end

def create_or_update_from_registration_session!(crm, registration, contact)
def create_or_update_from_registration_session!(registration, contact)
if contact
find_or_create_from_gitis_contact!(contact) \
.update_from_registration_session!(crm, registration)
.update_from_registration_session!(registration)
else
create_from_registration_session!(crm, registration)
create_from_registration_session!(registration)
end
end

def create_from_registration_session!(crm, registration)
gitis_contact =
if Flipper.enabled?(:git_api)
GetIntoTeachingApiClient::SchoolsExperienceSignUp.new
else
Bookings::Gitis::Contact.new
end
def create_from_registration_session!(registration)
gitis_contact = GetIntoTeachingApiClient::SchoolsExperienceSignUp.new

mapper = Bookings::RegistrationContactMapper.new \
registration, gitis_contact

contact = mapper.registration_to_contact

if Flipper.enabled?(:git_api)
contact = api_write_contact(contact)
else
crm.write! contact
end
mapped_contact = mapper.registration_to_contact
contact = api_write_contact(mapped_contact)

create!(gitis_uuid: contact.candidate_id, confirmed_at: Time.zone.now, created_in_gitis: true).tap do |candidate|
candidate.gitis_contact = contact
Expand Down Expand Up @@ -110,17 +100,13 @@ def last_signed_in_at
confirmed_at || session_tokens.confirmed.maximum(:confirmed_at)
end

def update_from_registration_session!(crm, registration)
def update_from_registration_session!(registration)
mapper = Bookings::RegistrationContactMapper.new \
registration, gitis_contact

mapper.registration_to_contact

if gitis_contact.is_a?(GetIntoTeachingApiClient::SchoolsExperienceSignUp)
self.class.api_write_contact(gitis_contact)
else
crm.write gitis_contact
end
self.class.api_write_contact(gitis_contact)

self
end
Expand Down
129 changes: 21 additions & 108 deletions app/models/bookings/registration_contact_mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,78 +8,9 @@ class RegistrationContactMapper
def initialize(registration_session, gitis_contact)
@registration_session = registration_session
@gitis_contact = gitis_contact
@is_api_contact = gitis_contact.is_a?(GetIntoTeachingApiClient::SchoolsExperienceSignUp)
end

def registration_to_contact
if @is_api_contact
api_registration_to_contact
else
direct_registration_to_contact
end
end

def contact_to_contact_information
if @is_api_contact
api_contact_to_contact_information
else
direct_contact_to_contact_information
end
end

def contact_to_personal_information
{
'first_name' => gitis_contact.first_name,
'last_name' => gitis_contact.last_name,
'email' => gitis_contact.email&.strip,
'date_of_birth' => gitis_contact.date_of_birth
}
end

def contact_to_background_check
if @is_api_contact
{
'has_dbs_check' => gitis_contact.has_dbs_certificate
}
else
{
'has_dbs_check' => gitis_contact.has_dbs_check
}
end
end

def contact_to_teaching_preference
if @is_api_contact
api_contact_to_teaching_preference
else
direct_contact_to_teaching_preference
end
end

private

def direct_registration_to_contact
gitis_contact.first_name = personal_information.first_name
gitis_contact.last_name = personal_information.last_name
gitis_contact.email = personal_information.email
gitis_contact.date_of_birth = personal_information.date_of_birth

gitis_contact.phone = contact_information.phone
gitis_contact.building = contact_information.building
gitis_contact.street = contact_information.street
gitis_contact.town_or_city = contact_information.town_or_city
gitis_contact.county = contact_information.county
gitis_contact.postcode = contact_information.postcode

gitis_contact.has_dbs_check = background_check.has_dbs_check

gitis_contact.dfe_PreferredTeachingSubject01 = find_teaching_subjects['dfe_teachingsubject01']
gitis_contact.dfe_PreferredTeachingSubject02 = find_teaching_subjects['dfe_teachingsubject02']

gitis_contact
end

def api_registration_to_contact
gitis_contact.first_name = personal_information.first_name
gitis_contact.last_name = personal_information.last_name
gitis_contact.email ||= personal_information.email
Expand Down Expand Up @@ -110,46 +41,43 @@ def api_registration_to_contact
gitis_contact
end

def api_contact_to_teaching_preference
def contact_to_contact_information
{
'subject_first_choice' =>
api_subject_from_gitis_uuid(gitis_contact.preferred_teaching_subject_id),
'subject_second_choice' =>
api_subject_from_gitis_uuid(gitis_contact.secondary_preferred_teaching_subject_id)
'phone' => gitis_contact.secondary_telephone.presence || gitis_contact.telephone.presence || gitis_contact.mobile_telephone,
'building' => gitis_contact.address_line1,
'street' => gitis_contact.address_line2,
'town_or_city' => gitis_contact.address_city,
'county' => gitis_contact.address_state_or_province,
'postcode' => gitis_contact.address_postcode
}
end

def direct_contact_to_teaching_preference
def contact_to_personal_information
{
'subject_first_choice' =>
direct_subject_from_gitis_uuid(gitis_contact._dfe_preferredteachingsubject01_value),
'subject_second_choice' =>
direct_subject_from_gitis_uuid(gitis_contact._dfe_preferredteachingsubject02_value)
'first_name' => gitis_contact.first_name,
'last_name' => gitis_contact.last_name,
'email' => gitis_contact.email&.strip,
'date_of_birth' => gitis_contact.date_of_birth
}
end

def api_contact_to_contact_information
def contact_to_background_check
{
'phone' => gitis_contact.secondary_telephone.presence || gitis_contact.telephone.presence || gitis_contact.mobile_telephone,
'building' => gitis_contact.address_line1,
'street' => gitis_contact.address_line2,
'town_or_city' => gitis_contact.address_city,
'county' => gitis_contact.address_state_or_province,
'postcode' => gitis_contact.address_postcode
'has_dbs_check' => gitis_contact.has_dbs_certificate
}
end

def direct_contact_to_contact_information
def contact_to_teaching_preference
{
'phone' => gitis_contact.phone,
'building' => gitis_contact.building,
'street' => gitis_contact.street,
'town_or_city' => gitis_contact.town_or_city,
'county' => gitis_contact.county,
'postcode' => gitis_contact.postcode
'subject_first_choice' =>
api_subject_from_gitis_uuid(gitis_contact.preferred_teaching_subject_id),
'subject_second_choice' =>
api_subject_from_gitis_uuid(gitis_contact.secondary_preferred_teaching_subject_id)
}
end

private

def current_privacy_policy
policy_id = Rails.configuration.x.gitis.privacy_policy_id

Expand All @@ -175,21 +103,6 @@ def find_teaching_subjects
end
end

def direct_subject_from_gitis_uuid(uuid)
return nil if uuid.blank?

@subjects_from_gitis_uuids ||= begin
uuids = [
gitis_contact._dfe_preferredteachingsubject01_value,
gitis_contact._dfe_preferredteachingsubject02_value
].map(&:presence).compact

Bookings::Subject.where(gitis_uuid: uuids).index_by(&:gitis_uuid)
end

@subjects_from_gitis_uuids[uuid]&.name
end

def api_subject_id_from_gitis_value(value)
return nil if value.blank?

Expand Down
2 changes: 1 addition & 1 deletion app/models/schools/csv_export_row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def row
private

def name
request.candidate.gitis_contact&.full_name || "Unavailable"
request.candidate.full_name || "Unavailable"
end

def email
Expand Down
12 changes: 0 additions & 12 deletions app/services/bookings/gitis/contact_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,6 @@ def missing_contact(candidate)
end

def fetch_contact(id_or_ids)
if Flipper.enabled?(:git_api)
api_fetch(id_or_ids)
else
direct_fetch(id_or_ids)
end
end

def direct_fetch(id_or_ids)
crm.find(id_or_ids)
end

def api_fetch(id_or_ids)
api = GetIntoTeachingApiClient::SchoolsExperienceApi.new

if id_or_ids.is_a?(Array)
Expand Down
12 changes: 0 additions & 12 deletions app/services/bookings/gitis/store/dynamics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ def initialize(token, service_url: nil, endpoint: nil)
end

def find(entity_type, id_or_ids, **options)
if Flipper.enabled?(:git_api)
Sentry.capture_message("Call to Dynamics#find with git_api enabled!")
end

params = parse_find_options(**options)

if !id_or_ids.is_a?(Array)
Expand All @@ -27,10 +23,6 @@ def find(entity_type, id_or_ids, **options)
end

def fetch(entity_type, filter: nil, limit: 10, order: nil)
if Flipper.enabled?(:git_api)
Sentry.capture_message("Call to Dynamics#fetch with git_api enabled!")
end

params = {
'$select' => entity_type.attributes_to_select,
'$top' => limit
Expand All @@ -47,10 +39,6 @@ def fetch(entity_type, filter: nil, limit: 10, order: nil)
end

def write(entity)
if Flipper.enabled?(:git_api)
Sentry.capture_message("Call to Dynamics#write with git_api enabled!")
end

raise ArgumentError, "entity must include Entity" unless entity.class < Entity
return false unless entity.valid?

Expand Down
2 changes: 1 addition & 1 deletion app/views/schools/confirmed_bookings/date/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<%= GovukElementsErrorsHelper.error_summary @booking, 'There is a problem', '' %>

<h1>Change the booking date for <%= @booking.gitis_contact.full_name %></h1>
<h1>Change the booking date for <%= gitis_contact_full_name(@booking.gitis_contact) %></h1>

<p>
Before you change the date contact the candidate and make sure they can
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<tr class="govuk-table__row placement-request" data-placement-request="<%= placement_request.id %>" data-urn="<%= placement_request.urn %>">
<td class="govuk-table__cell govuk-!-font-weight-bold">
<%= placement_request.gitis_contact.full_name %>
<%= gitis_contact_full_name(placement_request.gitis_contact) %>
</td>
<td class="govuk-table__cell">
<%= placement_request_status(placement_request) %>
Expand All @@ -13,6 +13,6 @@
</td>
<td class="govuk-table__cell">
<%= link_to "View", schools_placement_request_path(placement_request),
'aria-label': "View placement request for #{placement_request.gitis_contact.full_name} #{placement_request.dates_requested}" %>
'aria-label': "View placement request for #{gitis_contact_full_name(placement_request.gitis_contact)} #{placement_request.dates_requested}" %>
</td>
</tr>
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<p>
We’ve sent your confirmation to
<%= @placement_request.gitis_contact.full_name %>
<%= gitis_contact_full_name(@placement_request.gitis_contact) %>
</p>
<p>
They’ve been told to arrive at your school on the date and time you
Expand Down
Loading

0 comments on commit d7423d9

Please sign in to comment.