Skip to content

Commit

Permalink
Review improvements on hubee storing
Browse files Browse the repository at this point in the history
  • Loading branch information
jbfeldis committed May 31, 2024
1 parent fc6a96b commit ed74deb
Show file tree
Hide file tree
Showing 38 changed files with 327 additions and 258 deletions.
5 changes: 5 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ class ApplicationController < ActionController::Base

def identify_user
Current.user = PivotIdentity.new(auth: session_auth, recipient:)
Current.quotient_familial = quotient_familial
end

def quotient_familial
session.fetch("quotient_familial", {})
end

def recipient
Expand Down
8 changes: 4 additions & 4 deletions app/controllers/claims_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ def quotient_familial
result = GetFamilyQuotient.call(identity: Current.user)

if result.success?
session["qf"] = result.qf
Current.quotient_familial = result.qf
session["quotient_familial"] = result.quotient_familial
Current.quotient_familial = result.quotient_familial
else
raise
end
end

def send_qf
recipient = Hubee::Recipient.new(siren: "21040107100019", branch_code: "04107")
hubee_recipient = HubEE::Recipient.new(siren: "21040107100019", branch_code: "04107")

result = StoreQuotientFamilial.call(identity: Current.user, quotient_familial: Current.quotient_familial, recipient: recipient)
result = StoreQuotientFamilial.call(identity: Current.user, quotient_familial: Current.quotient_familial, recipient: hubee_recipient)

if result.success?
Rails.logger.debug "Noïce"
Expand Down
2 changes: 1 addition & 1 deletion app/interactors/hubee/clean_attachments.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Hubee::CleanAttachments < BaseInteractor
class HubEE::CleanAttachments < BaseInteractor
def call
context.folder.attachments.each do |attachment|
attachment.close_file
Expand Down
13 changes: 0 additions & 13 deletions app/interactors/hubee/create.rb

This file was deleted.

14 changes: 11 additions & 3 deletions app/interactors/hubee/create_folder.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
class Hubee
class CreateFolder < BaseOrganizer
organize PrepareFolder, PrepareAttachments, Create, UploadAttachments, MarkFolderComplete, CleanAttachments
class HubEE::CreateFolder < BaseInteractor
before do
context.session ||= HubEE::Api.session
end

def call
context.folder = context.session.create_folder(folder: context.folder)
end

def rollback
context.session.delete_folder(folder_id: context.folder.id)
end
end
6 changes: 5 additions & 1 deletion app/interactors/hubee/mark_folder_complete.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
class Hubee::MarkFolderComplete < BaseInteractor
class HubEE::MarkFolderComplete < BaseInteractor
before do
context.session ||= HubEE::Api.session
end

def call
context.session.mark_folder_complete(folder_id: context.folder.id)
end
Expand Down
2 changes: 1 addition & 1 deletion app/interactors/hubee/prepare_attachments.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Hubee::PrepareAttachments < BaseInteractor
class HubEE::PrepareAttachments < BaseInteractor
def call
context.folder = context.folder.with(attachments:)
end
Expand Down
6 changes: 5 additions & 1 deletion app/interactors/hubee/upload_attachments.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
class Hubee::UploadAttachments < BaseInteractor
class HubEE::UploadAttachments < BaseInteractor
before do
context.session ||= HubEE::Api.session
end

def call
context.folder.attachments.each do |attachment|
context.session.upload_attachment(folder_id: context.folder.id, attachment:)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Hubee::PrepareFolder < BaseInteractor
class PrepareQuotientFamilialHubEEFolder < BaseInteractor
def call
context.folder = ::Hubee::Folder.new(**folder_params)
context.folder = ::HubEE::Folder.new(**folder_params)
end

private
Expand All @@ -10,7 +10,8 @@ def case_external_id
end

def external_id
# HubEE's portal view greps 13 chars after two dashes
# This random string is formatted following HubEE's requirements
# so it can display nicely in their portal.
@external_id ||= "Formulaire-QF-#{SecureRandom.hex[0...13].upcase}"
end

Expand All @@ -19,7 +20,8 @@ def folder_params
applicant: context.identity,
attachments: [
json_file,
text_file,
# can't setup this file until we can upload a proper PDF
# text_file,
],
cases: [
external_id: case_external_id,
Expand All @@ -31,12 +33,12 @@ def folder_params
end

def json_file
::Hubee::Attachment.new(
::HubEE::Attachment.new(
file_name: "FormulaireQF.json",
mime_type: "application/json",
recipients: [case_external_id],
type: process_code,
file_content: '{"identite": "pivot"}'
file_content: '{"first_name":"David"}'
)
end

Expand All @@ -45,7 +47,7 @@ def process_code
end

def text_file
::Hubee::Attachment.new(
::HubEE::Attachment.new(
file_name: "FormulaireQF.pdf",
mime_type: "application/pdf",
recipients: [case_external_id],
Expand Down
2 changes: 0 additions & 2 deletions app/models/hubee.rb

This file was deleted.

2 changes: 1 addition & 1 deletion app/models/hubee/attachment.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Hubee
module HubEE
class Attachment < Data.define(:id, :file, :file_content, :file_name, :file_size, :mime_type, :recipients, :type)
def initialize(file_content:, file_name:, mime_type:, recipients:, type:, id: nil, file: nil, file_size: nil)
super
Expand Down
2 changes: 1 addition & 1 deletion app/models/hubee/folder.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Hubee
module HubEE
class Folder < Data.define(:id, :applicant, :attachments, :cases, :external_id, :process_code)
def initialize(applicant:, attachments:, cases:, external_id:, process_code:, id: nil)
super
Expand Down
2 changes: 1 addition & 1 deletion app/models/hubee/recipient.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Hubee
module HubEE
class Recipient < Data.define(:siren, :branch_code, :type)
def initialize(siren:, branch_code:, type: "SI")
super
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class GetFamilyQuotient < BaseInteractor
def call
context.qf = {
context.quotient_familial = {
regime: "CNAF",
enfants: [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class StoreQuotientFamilial < BaseOrganizer
organize Hubee::CreateFolder
organize UploadQuotientFamilialToHubEE
end
8 changes: 8 additions & 0 deletions app/organizers/upload_quotient_familial_to_hubee.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class UploadQuotientFamilialToHubEE < BaseOrganizer
organize PrepareQuotientFamilialHubEEFolder,
HubEE::PrepareAttachments,
HubEE::CreateFolder,
HubEE::UploadAttachments,
HubEE::MarkFolderComplete,
HubEE::CleanAttachments
end
3 changes: 3 additions & 0 deletions config/initializers/inflections.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@
# ActiveSupport::Inflector.inflections(:en) do |inflect|
# inflect.acronym "RESTful"
# end
ActiveSupport::Inflector.inflections do |inflect|
inflect.acronym "HubEE"
end
2 changes: 1 addition & 1 deletion lib/hubee/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require "net/http"
require "json"

class Hubee::Api
class HubEE::Api
def self.session
new
end
Expand Down
6 changes: 3 additions & 3 deletions spec/factories/hubee_attachment.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
FactoryBot.define do
factory :hubee_attachment, class: Hubee::Attachment do
factory :hubee_attachment, class: HubEE::Attachment do
initialize_with { new(**attributes) }

file_content { "{\"first_name\":\"David\"}" }
file_name { "FormulaireQF.json" }
mime_type { "application/json" }
recipients { ["external_id-01"] }
recipients { ["Formulaire-QF-ABCDEF1234567-01"] }
type { "FormulaireQF" }

trait :with_file do
Expand All @@ -16,7 +16,7 @@
end
end

file_size { file.size }
file_size { 22 }
end
end
end
6 changes: 3 additions & 3 deletions spec/factories/hubee_folder.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
FactoryBot.define do
factory :hubee_folder, class: Hubee::Folder do
factory :hubee_folder, class: HubEE::Folder do
initialize_with { new(**attributes) }

applicant { {first_name: "David", last_name: "Heinemeier Hansson"} }
attachments { [build(:hubee_attachment, :with_file)] }
cases { [recipient: build(:hubee_recipient), external_id: "case_id"] }
external_id { "external_id" }
cases { [recipient: build(:hubee_recipient), external_id: "Formulaire-QF-ABCDEF1234567-01"] }
external_id { "Formulaire-QF-ABCDEF1234567" }
process_code { "FormulaireQF" }
end
end
2 changes: 1 addition & 1 deletion spec/factories/hubee_recipient.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FactoryBot.define do
factory :hubee_recipient, class: Hubee::Recipient do
factory :hubee_recipient, class: HubEE::Recipient do
initialize_with { new(**attributes) }

siren { "123456789" }
Expand Down
28 changes: 14 additions & 14 deletions spec/interactors/hubee/clean_attachments_spec.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
require "rails_helper"

RSpec.describe Hubee::CleanAttachments, type: :interactor do
subject(:interactor) { described_class.call(**params) }
RSpec.describe HubEE::CleanAttachments, type: :interactor do
describe ".call" do
subject(:interactor) { described_class.call(**params) }

let(:attachment) { double(Hubee::Attachment) }
let(:attachments) { [attachment] }
let(:folder) { double(Hubee::Folder, attachments: attachments) }
let(:params) do
{
folder: folder,
}
end
let(:attachment) { double(HubEE::Attachment) }
let(:attachments) { [attachment] }
let(:folder) { double(HubEE::Folder, attachments: attachments) }
let(:params) do
{
folder: folder,
}
end

before do
allow(attachment).to receive(:close_file)
end
before do
allow(attachment).to receive(:close_file)
end

describe ".call" do
it "closes the attachments" do
expect(attachment).to receive(:close_file)
interactor
Expand Down
50 changes: 38 additions & 12 deletions spec/interactors/hubee/create_folder_spec.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,44 @@
require "rails_helper"

RSpec.describe Hubee::CreateFolder, type: :organizer do
let(:interactors) do
[
Hubee::PrepareFolder,
Hubee::PrepareAttachments,
Hubee::Create,
Hubee::UploadAttachments,
Hubee::MarkFolderComplete,
Hubee::CleanAttachments,
]
RSpec.describe HubEE::CreateFolder, type: :interactor do
subject(:interactor) { described_class.call(**params) }

let(:params) do
{
folder:,
}
end

let(:session) { double(HubEE::Api) }

before do
allow(HubEE::Api).to receive(:session).and_return(session)
end

it "creates the folder, uploads the attachments and marks the folder complete" do
expect(described_class).to organize interactors
describe ".call" do
let(:folder) { double(HubEE::Folder) }
it "creates the folder" do
expect(session).to receive(:create_folder).with(folder:)
interactor
end
end

describe "#rollback" do
subject(:rollback) { interactor.rollback }

let(:folder) { double(HubEE::Folder, id: folder_id) }
let(:folder_id) { "folder_id" }
let(:interactor) { described_class.new(**params) }
let(:params) do
{
folder:,
session:,
}
end

it "deletes the folder" do
expect(session).to receive(:delete_folder).with(folder_id:)
rollback
end
end
end
44 changes: 0 additions & 44 deletions spec/interactors/hubee/create_spec.rb

This file was deleted.

Loading

0 comments on commit ed74deb

Please sign in to comment.