Skip to content

Commit

Permalink
Extract validation helper method and adjust things
Browse files Browse the repository at this point in the history
  • Loading branch information
sauloperez committed Apr 1, 2021
1 parent 0514e51 commit 3d4c321
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions lib/decidim/direct_verifications/authorize_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,42 @@ def initialize(email, data, session, organization, instrumenter)
def call
user = find_user

if user
auth = authorization(user)
auth.metadata = data

return unless !auth.granted? || auth.expired?
unless user
instrumenter.add_error :authorized, email
else
@authorization = find_or_create_authorization(user)
return unless valid_authorization?

Verification::ConfirmUserAuthorization.call(auth, authorize_form(user), session) do
Verification::ConfirmUserAuthorization.call(authorization, authorize_form(user), session) do
on(:ok) do
instrumenter.add_processed :authorized, email
end
on(:invalid) do
instrumenter.add_error :authorized, email
end
end
else
instrumenter.add_error :authorized, email
end
end

private

attr_reader :email, :data, :session, :organization, :instrumenter
attr_reader :email, :data, :session, :organization, :instrumenter, :authorization

def valid_authorization?
!authorization.granted? || authorization.expired?
end

def find_user
User.find_by(email: email, decidim_organization_id: organization.id)
end

def authorization(user)
Authorization.find_or_initialize_by(
def find_or_create_authorization(user)
auth = Authorization.find_or_initialize_by(
user: user,
name: :direct_verifications
)
auth.metadata = data
auth
end

def authorize_form(user)
Expand Down

0 comments on commit 3d4c321

Please sign in to comment.