diff --git a/app/interactors/create_claim.rb b/app/interactors/create_claim.rb new file mode 100644 index 00000000..7dff7db2 --- /dev/null +++ b/app/interactors/create_claim.rb @@ -0,0 +1,19 @@ +class CreateClaim < BaseInteractor + def call + claim = Claim.new(claim_params) + if claim.save + context.claim = claim + else + context.fail!(message: claim.errors.full_messages) + end + end + + private + + def claim_params + { + sub: context.identity.sub, + hubee_folder_id: context.folder.id, + } + end +end diff --git a/app/models/claim.rb b/app/models/claim.rb new file mode 100644 index 00000000..ad9e58a2 --- /dev/null +++ b/app/models/claim.rb @@ -0,0 +1,2 @@ +class Claim < ApplicationRecord +end diff --git a/app/models/pivot_identity.rb b/app/models/pivot_identity.rb index 2d236a45..f40b7141 100755 --- a/app/models/pivot_identity.rb +++ b/app/models/pivot_identity.rb @@ -10,6 +10,7 @@ class PivotIdentity map from("info/last_name"), to("last_name") map from("extra/raw_info/birthcountry"), to("birth_country") map from("extra/raw_info/birthplace"), to("birthplace") + map from("extra/raw_info/sub"), to("sub") map from("credentials/token"), to("token") @@ -34,6 +35,10 @@ def authenticated? @auth.present? end + def sub + self[:sub] + end + def token self[:token] end diff --git a/app/organizers/store_quotient_familial.rb b/app/organizers/store_quotient_familial.rb index 616bfcc5..7bf45ee6 100644 --- a/app/organizers/store_quotient_familial.rb +++ b/app/organizers/store_quotient_familial.rb @@ -1,3 +1,3 @@ class StoreQuotientFamilial < BaseOrganizer - organize UploadQuotientFamilialToHubEE + organize UploadQuotientFamilialToHubEE, CreateClaim end diff --git a/db/migrate/20240603084727_create_claims.rb b/db/migrate/20240603084727_create_claims.rb new file mode 100644 index 00000000..8690eac6 --- /dev/null +++ b/db/migrate/20240603084727_create_claims.rb @@ -0,0 +1,10 @@ +class CreateClaims < ActiveRecord::Migration[7.1] + def change + create_table :claims do |t| + t.string :sub + t.string :hubee_folder_id + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index f9139984..863729d2 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,8 +10,15 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 0) do +ActiveRecord::Schema[7.1].define(version: 2024_06_03_084727) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" + create_table "claims", force: :cascade do |t| + t.string "sub" + t.string "hubee_folder_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + end diff --git a/spec/factories/claims.rb b/spec/factories/claims.rb new file mode 100644 index 00000000..b7007ceb --- /dev/null +++ b/spec/factories/claims.rb @@ -0,0 +1,6 @@ +FactoryBot.define do + factory :claim do + sub { "uuid" } + hubee_folder_id { "folder_uuid" } + end +end diff --git a/spec/interactors/create_claim_spec.rb b/spec/interactors/create_claim_spec.rb new file mode 100644 index 00000000..19e5fa73 --- /dev/null +++ b/spec/interactors/create_claim_spec.rb @@ -0,0 +1,24 @@ +require "rails_helper" + +RSpec.describe CreateClaim, type: :interactor do + describe ".call" do + subject(:interactor) { described_class.call(**params) } + + let(:folder) { instance_double("HubEE::Folder", id: "folder_uuid") } + let(:identity) { instance_double("PivotIdentity", sub: "real_uuid") } + let(:params) do + { + identity:, + folder:, + } + end + + context "when the claim is created" do + it { is_expected.to be_a_success } + + it "creates a claim" do + expect { interactor }.to change(Claim, :count).by(1) + end + end + end +end diff --git a/spec/organizers/store_quotient_familial_spec.rb b/spec/organizers/store_quotient_familial_spec.rb index f0fc5482..55b8adb6 100644 --- a/spec/organizers/store_quotient_familial_spec.rb +++ b/spec/organizers/store_quotient_familial_spec.rb @@ -4,10 +4,41 @@ let(:interactors) do [ UploadQuotientFamilialToHubEE, + CreateClaim, ] end it "setups the folder, sends it to HubEE and cleans afterwards" do expect(described_class).to organize interactors end + + describe ".call" do + subject(:organizer) { described_class.call(**params) } + + let(:folder) { build(:hubee_folder) } + let(:identity) { PivotIdentity.new(recipient:, auth: {info: {first_name: "David", last_name: "Heinemeier Hansson"}}) } + let(:recipient) { build(:hubee_recipient) } + let(:params) do + { + folder:, + identity:, + recipient:, + } + end + + before do + allow(SecureRandom).to receive(:hex).and_return("abcdef1234567thiswontbeused") + + stub_hubee_token + stub_hubee_create_folder + stub_hubee_upload_attachment + stub_hubee_mark_folder_complete + end + + it { is_expected.to be_a_success } + + it "creates a claim" do + expect { organizer }.to change(Claim, :count).by(1) + end + end end