-
Notifications
You must be signed in to change notification settings - Fork 5
create and read ownership transfer #1017
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DNR500
wants to merge
19
commits into
main
Choose a base branch
from
create-and-get-ownership-transfer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
3b9af1b
add nominee, requester and status columns to ownership transfers
cocomarine 768f133
Revert "add nominee, requester and status columns to ownership transf…
cocomarine e8eceae
add string enum status, nominee and requester to ownership_transfers …
cocomarine 3aa9ec6
update owner transfer model, factory and test
cocomarine 08ace96
add has many association to school
cocomarine 521a896
redo migration with updated status default
cocomarine 9649cac
update models and test with status default pending and simplify nomin…
cocomarine be71d4b
fix: prevent concurrent pending ownership transfers for a school
DNR500 3d7abd2
feat: add endpoint to create a school ownership transfer
DNR500 dc71508
feat: add endpoint to view a school's ownership transfer status
DNR500 b611bf5
fix: use a nominee with a real role in the ownership mailer spec
DNR500 69a8eed
feat: show the transfer's actual status instead of hiding non-pending…
DNR500 93d088a
Merge branch 'main' into create-and-get-ownership-transfer
DNR500 f9361f2
fix: guard against an uninitialized response in OwnershipTransfer::Cr…
DNR500 f186480
fix: return the friendly duplicate-transfer message on a concurrent race
DNR500 bd2893f
refactor: extract the nominee role-eligibility check onto School
DNR500 e9ecd30
test: assert the create endpoint's 422 error response body
DNR500 3ae080a
fix: stop leaking exception details in the create error response
DNR500 2472932
fix: stop masking the nominee-role error behind an email format error
DNR500 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,53 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Api | ||
| class OwnershipTransfersController < ApiController | ||
| before_action :authorize_user | ||
| load_and_authorize_resource :school | ||
| authorize_resource :ownership_transfer, class: false | ||
|
|
||
| def show | ||
| @ownership_transfer = most_recent_ownership_transfer | ||
|
|
||
| if @ownership_transfer.blank? || cannot?(:read, @ownership_transfer) | ||
| head :not_found | ||
| elsif current_user_is_requester? | ||
| render json: { status: @ownership_transfer.status, you_are: 'owner', nominee_name: nominee_name }, status: :ok | ||
| else | ||
| render json: { status: @ownership_transfer.status, you_are: 'nominee' }, status: :ok | ||
| end | ||
| end | ||
|
|
||
| def create | ||
| result = OwnershipTransfer::Create.call(school: @school, nominated_user_id:, requested_by_user_id: current_user.id) | ||
|
|
||
| if result.success? | ||
| head :created | ||
| else | ||
| render json: { error: result[:error] }, status: :unprocessable_content | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def ownership_transfer_params | ||
| params.expect(ownership_transfer: [:nominated_user_id]) | ||
| end | ||
|
|
||
| def nominated_user_id | ||
| ownership_transfer_params[:nominated_user_id] | ||
| end | ||
|
|
||
| def most_recent_ownership_transfer | ||
| @school.ownership_transfers.order(created_at: :desc).first | ||
| end | ||
|
|
||
| def current_user_is_requester? | ||
| @ownership_transfer.requested_by_user_id == current_user.id | ||
| end | ||
|
|
||
| def nominee_name | ||
| User.from_userinfo(ids: @ownership_transfer.nominated_user_id).first&.name | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -13,6 +13,9 @@ class OwnershipTransfer < ApplicationRecord | |
| validates :requested_by_user_id, presence: true | ||
| validates :email_address, | ||
| format: { with: EmailValidator.regexp, message: I18n.t('validations.invitation.email_address') } | ||
| validates :school_id, | ||
| uniqueness: { conditions: -> { where(status: :pending) }, message: I18n.t('validations.ownership_transfer.school_pending') }, | ||
| on: :create | ||
| validate :nominee_has_the_school_owner_or_school_teacher_role_for_the_school | ||
|
Comment on lines
14
to
19
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed |
||
|
|
||
| after_create_commit :send_ownership_transfer_request_email | ||
|
|
@@ -21,9 +24,9 @@ class OwnershipTransfer < ApplicationRecord | |
| private | ||
|
|
||
| def nominee_has_the_school_owner_or_school_teacher_role_for_the_school | ||
| return unless nominated_user_id_changed? && errors.blank? && school | ||
| return unless nominated_user_id_changed? && school | ||
|
|
||
| return if school.roles.exists?(user_id: nominated_user_id, role: %i[owner teacher]) | ||
| return if school.owner_or_teacher?(nominated_user_id) | ||
|
|
||
| msg = "'#{nominated_user_id}' does not have the 'owner' or 'teacher' role for school '#{school.id}'" | ||
| errors.add(:nominated_user_id, msg) | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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
13 changes: 13 additions & 0 deletions
13
db/migrate/20260915100000_add_unique_pending_index_to_ownership_transfers.rb
This file contains hidden or 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,13 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class AddUniquePendingIndexToOwnershipTransfers < ActiveRecord::Migration[8.1] | ||
| disable_ddl_transaction! | ||
|
|
||
| def change | ||
| add_index :ownership_transfers, :school_id, | ||
| unique: true, | ||
| where: "status = 'pending'", | ||
| name: 'index_ownership_transfers_on_school_id_when_pending', | ||
| algorithm: :concurrently | ||
| 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 contains hidden or 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,43 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class OwnershipTransfer | ||
| class Create | ||
| class << self | ||
| def call(school:, nominated_user_id:, requested_by_user_id:) | ||
| response = OperationResponse.new | ||
| ownership_transfer = build_ownership_transfer(school:, nominated_user_id:, requested_by_user_id:) | ||
|
|
||
| if ownership_transfer.save | ||
| response[:ownership_transfer] = ownership_transfer | ||
| else | ||
| response[:error] = ownership_transfer.errors | ||
| end | ||
|
|
||
| response | ||
| rescue ActiveRecord::RecordNotUnique | ||
| response ||= OperationResponse.new | ||
| ownership_transfer.errors.add(:school_id, I18n.t('validations.ownership_transfer.school_pending')) | ||
|
DNR500 marked this conversation as resolved.
Dismissed
|
||
| response[:error] = ownership_transfer.errors | ||
|
DNR500 marked this conversation as resolved.
Dismissed
|
||
| response | ||
| rescue StandardError => e | ||
| response ||= OperationResponse.new | ||
| Sentry.capture_exception(e) | ||
| response[:error] = 'Error creating ownership transfer' | ||
| response | ||
|
Copilot marked this conversation as resolved.
|
||
| end | ||
|
|
||
| private | ||
|
|
||
| def build_ownership_transfer(school:, nominated_user_id:, requested_by_user_id:) | ||
| email_address = nominee_email(school:, nominated_user_id:) | ||
| OwnershipTransfer.new(school:, nominated_user_id:, requested_by_user_id:, email_address:) | ||
| end | ||
|
|
||
| def nominee_email(school:, nominated_user_id:) | ||
| return unless school.owner_or_teacher?(nominated_user_id) | ||
|
|
||
| User.from_userinfo(ids: nominated_user_id).first&.email | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or 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,70 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe OwnershipTransfer::Create, type: :unit do | ||
| let(:school) { create(:verified_school) } | ||
| let(:owner) { create(:owner, school:) } | ||
| let(:nominee) { create(:teacher, school:) } | ||
|
|
||
| before { stub_user_info_api_for(nominee) } | ||
|
|
||
| it 'returns a successful response and creates the transfer' do | ||
| response = described_class.call(school:, nominated_user_id: nominee.id, requested_by_user_id: owner.id) | ||
|
|
||
| expect(response.success?).to be(true) | ||
| expect(response[:ownership_transfer]).to have_attributes( | ||
| school:, | ||
| nominated_user_id: nominee.id, | ||
| requested_by_user_id: owner.id, | ||
| email_address: nominee.email | ||
| ) | ||
| end | ||
|
|
||
| it 'returns a failure response naming the ineligible nominee when the nominee has no role at the school' do | ||
| response = described_class.call(school:, nominated_user_id: SecureRandom.uuid, requested_by_user_id: owner.id) | ||
|
|
||
| expect(response.failure?).to be(true) | ||
| expect(response[:error][:nominated_user_id]).to be_present | ||
| end | ||
|
|
||
| context 'when a duplicate pending transfer is created concurrently' do | ||
| before do | ||
| allow(OwnershipTransfer).to receive(:new).and_wrap_original do |method, *args| | ||
| method.call(*args).tap do |ownership_transfer| | ||
| allow(ownership_transfer).to receive(:save).and_raise(ActiveRecord::RecordNotUnique) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| it 'returns the same friendly error a non-concurrent duplicate would get, not the raw exception' do | ||
| response = described_class.call(school:, nominated_user_id: nominee.id, requested_by_user_id: owner.id) | ||
|
|
||
| expect(response.failure?).to be(true) | ||
| expect(response[:error][:school_id]).to include('already has a pending ownership transfer') | ||
| end | ||
|
|
||
| it 'does not report the race to Sentry, since it is an expected, handled outcome' do | ||
| allow(Sentry).to receive(:capture_exception) | ||
|
|
||
| described_class.call(school:, nominated_user_id: nominee.id, requested_by_user_id: owner.id) | ||
|
|
||
| expect(Sentry).not_to have_received(:capture_exception) | ||
| end | ||
| end | ||
|
|
||
| context 'when an unexpected error occurs' do | ||
| before do | ||
| allow(OwnershipTransfer).to receive(:new).and_raise(StandardError, 'boom') | ||
| allow(Sentry).to receive(:capture_exception) | ||
| end | ||
|
|
||
| it 'reports it to Sentry and returns a generic error' do | ||
| response = described_class.call(school:, nominated_user_id: nominee.id, requested_by_user_id: owner.id) | ||
|
|
||
| expect(Sentry).to have_received(:capture_exception) | ||
| expect(response.failure?).to be(true) | ||
| expect(response[:error]).to include('Error creating ownership transfer') | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.