-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In favour of a pseudo random urn generator that is not reliant of the database. This change will simplies the codebase
- Loading branch information
1 parent
042c6d7
commit 2e1e59c
Showing
16 changed files
with
66 additions
and
326 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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") | ||
|
||
|
@@ -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 | ||
|
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.