-
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.
Merge branch 'main' into feature/rename-rejection-details-to-comments
- Loading branch information
Showing
30 changed files
with
436 additions
and
190 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module Reports | ||
class Base | ||
def initialize(**kwargs) | ||
@kwargs = kwargs | ||
@name = self.class.name.titleize.tr(" /", "-").downcase | ||
end | ||
|
||
attr_reader :name, :kwargs | ||
|
||
def filename | ||
current_time = Time.zone.now.strftime("%Y%m%d-%H%M%S") | ||
|
||
"#{name}-#{current_time}.csv" | ||
end | ||
|
||
def csv; end | ||
|
||
def post_generation_hook; end | ||
end | ||
end |
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 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 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,38 +1,41 @@ | ||
# 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. | ||
# | ||
# == Schema Information | ||
# | ||
# Example: | ||
# Table name: urns | ||
# | ||
# Urn.generate('teacher') # => "IRPTE12345" | ||
# Urn.generate('teacher') # => "IRPTE12345" | ||
# Urn.generate('salaried_trainee') # => "IRPST12345" | ||
# id :bigint not null, primary key | ||
# code :string | ||
# prefix :string | ||
# suffix :integer | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
class Urn | ||
attr_reader :value | ||
attr_writer :suffix | ||
|
||
def self.generate(applicant_type) | ||
code = applicant_type_code(applicant_type) | ||
PREFIX + code + Array.new(LENGTH) { CHARSET.sample }.join | ||
end | ||
class Urn < ApplicationRecord | ||
class NoUrnAvailableError < StandardError; end | ||
|
||
CHARSET = %w[0 1 2 3 4 5 6 7 8 9].freeze | ||
PREFIX = "IRP" | ||
LENGTH = 5 | ||
private_constant :CHARSET, :PREFIX, :LENGTH | ||
PREFIX = "IRP".freeze | ||
MAX_SUFFIX = 99_999 | ||
PADDING_SIZE = MAX_SUFFIX.to_s.size | ||
VALID_CODES = { | ||
"teacher" => "TE", | ||
"salaried_trainee" => "ST", | ||
}.freeze | ||
|
||
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}") | ||
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}") | ||
end | ||
|
||
def to_s | ||
[prefix, code, sprintf("%0#{PADDING_SIZE}d", suffix)].join | ||
end | ||
private_methods :applicant_type_code | ||
end |
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,62 @@ | ||
# Service responsible for the generation of all urns | ||
# It will save the set of available urns based the current URN format | ||
# and store it in the database URNs table. | ||
# | ||
# The Urn model will then be able to fetch the next available unique and | ||
# random urn for application submition | ||
# | ||
# Example: | ||
# | ||
# Urn.next("teacher") # => "IRPTE12345" | ||
# Urn.next("teacher") # => "IRPTE12345" | ||
# Urn.next("salaried_trainee") # => "IRPST12345" | ||
# | ||
class GenerateUrns | ||
def self.call | ||
return if Urn.count.positive? # Do not override the current urn state | ||
|
||
Urn.transaction do | ||
Urn::VALID_CODES.each_value do |code| | ||
new(code:).generate | ||
end | ||
end | ||
end | ||
|
||
def initialize(code:) | ||
@code = code | ||
end | ||
|
||
attr_reader :code | ||
|
||
def generate | ||
data = unused_urns.map do |suffix| | ||
{ prefix: Urn::PREFIX, code: code, suffix: suffix } | ||
end | ||
Urn.insert_all(data) # rubocop:disable Rails/SkipsModelValidations | ||
end | ||
|
||
private | ||
|
||
def unused_urns | ||
generate_suffixes - existing_suffixes | ||
end | ||
|
||
def generate_suffixes | ||
Array | ||
.new(Urn::MAX_SUFFIX) { _1 } | ||
.drop(1) | ||
.shuffle! | ||
end | ||
|
||
def existing_suffixes | ||
route = Urn::VALID_CODES.key(code) | ||
Application | ||
.where(application_route: route) | ||
.pluck(:urn) | ||
.map { extract_suffix(_1) } | ||
end | ||
|
||
def extract_suffix(urn) | ||
urn.match(/\d+/)[0].to_i | ||
end | ||
end |
Oops, something went wrong.