Files
nairobi/app/models/concerns/verification.rb
Javi Martín 2abe9f27b5 Use ranges instead of comparisons in SQL queries
These cases aren't covered by the `Rails/WhereRange` rubocop rule, but
IMHO using ranges makes them more consistent. Besides, they generate SQL
which is more consistent with what Rails usually generates. For example,
`Poll.where("starts_at <= :time and ends_at >= :time", time:
Time.current)` generates:

```
SELECT \"polls\".\"id\", (...) WHERE \"polls\".\"hidden_at\" IS NULL AND
(starts_at <= '2024-07-(...)' and ends_at >= '2024-07-(...)')
```

And `Poll.where(starts_at: ..Time.current, ends_at: Time.current..)`
generates:

```
SELECT \"polls\".\"id\", (...) WHERE \"polls\".\"hidden_at\" IS NULL AND
\"polls\".\"starts_at\" <= '2024-07-(...)' AND \"polls\".\"ends_at\" >=
'2024-07-(...)'"
```

Note that the `not_archived` scope in proposals slightly changes, since
we were using `>` and now we use the equivalent of `>=`. However, since
the `created_at` field is a time, this will only mean that a proposal
will be archived about one microsecond later.

For consistency, we're also changing the `archived` scope, so a proposal
is never archived and not archived at the same time (not even for a
microsecond).
2024-07-05 17:24:56 +02:00

101 lines
2.6 KiB
Ruby

module Verification
extend ActiveSupport::Concern
included do
scope :residence_verified, -> { where.not(residence_verified_at: nil) }
scope :residence_unverified, -> { where(residence_verified_at: nil) }
scope :residence_and_phone_verified, -> { residence_verified.where.not(confirmed_phone: nil) }
scope :residence_or_phone_unverified, -> { residence_unverified.or(where(confirmed_phone: nil)) }
scope :phone_not_fully_confirmed, -> { where(unconfirmed_phone: nil).or(where(confirmed_phone: nil)) }
scope :level_three_verified, -> { where.not(verified_at: nil) }
scope :level_two_verified, -> do
where.not(level_two_verified_at: nil).or(residence_and_phone_verified.where(verified_at: nil))
end
scope :level_two_or_three_verified, -> { level_two_verified.or(level_three_verified) }
scope :unverified, -> do
residence_or_phone_unverified.where(verified_at: nil, level_two_verified_at: nil)
end
scope :incomplete_verification, -> do
residence_unverified.where(failed_census_calls_count: 1..)
.or(residence_verified.phone_not_fully_confirmed)
end
end
def skip_verification?
Setting["feature.user.skip_verification"].present?
end
def verification_email_sent?
return true if skip_verification?
email_verification_token.present?
end
def verification_sms_sent?
return true if skip_verification?
unconfirmed_phone.present? && sms_confirmation_code.present?
end
def verification_letter_sent?
return true if skip_verification?
letter_requested_at.present? && letter_verification_code.present?
end
def residence_verified?
return true if skip_verification?
residence_verified_at.present?
end
def sms_verified?
return true if skip_verification?
confirmed_phone.present?
end
def level_two_verified?
return true if skip_verification?
level_two_verified_at.present? || (residence_verified? && sms_verified?)
end
def level_three_verified?
return true if skip_verification?
verified_at.present?
end
def level_two_or_three_verified?
level_two_verified? || level_three_verified?
end
def unverified?
!level_two_or_three_verified?
end
def failed_residence_verification?
!residence_verified? && !failed_census_calls.empty?
end
def no_phone_available?
!verification_sms_sent?
end
def user_type
if level_three_verified?
:level_3_user
elsif level_two_verified?
:level_2_user
else
:level_1_user
end
end
def sms_code_not_confirmed?
!sms_verified?
end
end