Skip to content

Commit

Permalink
Revert PR #276
Browse files Browse the repository at this point in the history
In favour of a pseudo random urn generator that is not reliant of the
database.
This change will simplies the codebase
  • Loading branch information
fumimowdan committed Oct 9, 2023
1 parent a328e6e commit eab5db4
Show file tree
Hide file tree
Showing 16 changed files with 66 additions and 325 deletions.
63 changes: 30 additions & 33 deletions app/models/urn.rb
Original file line number Diff line number Diff line change
@@ -1,41 +1,38 @@
# == Schema Information
# frozen_string_literal: true

# Urn represents a Uniform Resource Name (URN) generator.
# It generates a URN with a fixed prefix and a random alphanumeric suffix.
#
# Table name: urns
#
# id :bigint not null, primary key
# code :string
# prefix :string
# suffix :integer
# created_at :datetime not null
# updated_at :datetime not null
# Example:
#
class Urn < ApplicationRecord
class NoUrnAvailableError < StandardError; end

PREFIX = "IRP".freeze
MAX_SUFFIX = 99_999
PADDING_SIZE = MAX_SUFFIX.to_s.size
VALID_CODES = {
"teacher" => "TE",
"salaried_trainee" => "ST",
}.freeze
# Urn.generate('teacher') # => "IRPTE12345"
# Urn.generate('teacher') # => "IRPTE12345"
# Urn.generate('salaried_trainee') # => "IRPST12345"
#
class Urn
attr_reader :value
attr_writer :suffix

def self.next(route)
code = VALID_CODES.fetch(route)
Urn.transaction do
urn = find_by!(code:)
urn.destroy!
urn.to_s
end
rescue KeyError => e
Sentry.capture_exception(e)
raise(ArgumentError, "Unknown route #{route}")
rescue ActiveRecord::RecordNotFound => e
Sentry.capture_exception(e)
raise(NoUrnAvailableError, "There no more unique URN available for #{route}")
def self.generate(applicant_type)
code = applicant_type_code(applicant_type)
PREFIX + code + Array.new(LENGTH) { CHARSET.sample }.join
end

def to_s
[prefix, code, sprintf("%0#{PADDING_SIZE}d", suffix)].join
CHARSET = %w[0 1 2 3 4 5 6 7 8 9].freeze
PREFIX = "IRP"
LENGTH = 5
private_constant :CHARSET, :PREFIX, :LENGTH

def self.applicant_type_code(applicant_type)
case applicant_type
when "teacher"
"TE"
when "salaried_trainee"
"ST"
else
raise(ArgumentError, "Invalid applicant type: #{applicant_type}")
end
end
private_methods :applicant_type_code
end
62 changes: 0 additions & 62 deletions app/services/generate_urns.rb

This file was deleted.

2 changes: 1 addition & 1 deletion app/services/submit_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def create_application
date_of_entry: form.date_of_entry,
start_date: form.start_date,
subject: SubjectStep.new(form).answer.formatted_value,
urn: Urn.next(form.application_route),
urn: Urn.generate(form.application_route),
visa_type: form.visa_type,
)
end
Expand Down
2 changes: 0 additions & 2 deletions bin/app-startup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ set -e

# run migrations
bundle exec rails db:migrate
# Front load urn generation
bundle exec rake urn:generate

# add seed data in review environment
if [[ "$RAILS_ENV" = "review" || "$RAILS_ENV" = "development" ]]; then
Expand Down
12 changes: 0 additions & 12 deletions db/migrate/20230927092305_create_urns.rb

This file was deleted.

7 changes: 7 additions & 0 deletions db/migrate/20231009110217_add_application_indexes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class AddApplicationIndexes < ActiveRecord::Migration[7.0]
def change
drop_table :urns
add_index :applications, :urn, unique: true
add_index :applications, :application_route
end
end
13 changes: 3 additions & 10 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 0 additions & 10 deletions lib/tasks/urn.rake

This file was deleted.

2 changes: 1 addition & 1 deletion spec/factories/applications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
visa_type { VisaStep::VALID_ANSWERS_OPTIONS.reject { _1 == "Other" }.sample }
date_of_entry { Time.zone.today }
start_date { 1.month.from_now.to_date }
urn { Urn.next(application_route) }
urn { Urn.generate(application_route) }

factory :teacher_application do
application_route { "teacher" }
Expand Down
18 changes: 0 additions & 18 deletions spec/factories/urns.rb

This file was deleted.

6 changes: 0 additions & 6 deletions spec/features/admin_console/applications_list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@

def given_there_are_few_applications
# Create 2 specific applications for search tests
create_list(:urn, 5, code: "TE")
create_list(:urn, 5, code: "ST")
unique_applicant = create(:applicant, given_name: "Unique Given Name", middle_name: "Unique Middle Name", family_name: "Unique Family Name", email_address: "[email protected]")
create(:application, applicant: unique_applicant, urn: "Unique Urn 1")

Expand All @@ -72,16 +70,12 @@ def given_there_are_few_applications
end

def given_there_is_an_application_that_breached_sla
create_list(:urn, 5, code: "TE")
create_list(:urn, 5, code: "ST")
applicant = create(:applicant)
application = create(:application, applicant:)
application.application_progress.update(initial_checks_completed_at: 4.days.ago)
end

def given_there_are_applications_with_different_dates
create_list(:urn, 5, code: "TE")
create_list(:urn, 5, code: "ST")
create(:application, application_progress: build(:application_progress, :initial_checks_completed, status: :initial_checks))
create(:application, application_progress: build(:application_progress, :home_office_checks_completed, status: :home_office_checks))
end
Expand Down
101 changes: 0 additions & 101 deletions spec/fixtures/urns.yml

This file was deleted.

Loading

0 comments on commit eab5db4

Please sign in to comment.