feat(graphql): implement add/remove instructeurs via API
This commit is contained in:
parent
0557b84eed
commit
afc1f12028
17 changed files with 480 additions and 41 deletions
|
@ -52,42 +52,18 @@ class Instructeur < ApplicationRecord
|
|||
find_by(users: { email: email })
|
||||
end
|
||||
|
||||
def self.find_or_invite(emails: [], ids: [], groupe_instructeur:)
|
||||
valid_emails, invalid_emails = emails
|
||||
.map(&:strip)
|
||||
.map(&:downcase)
|
||||
.partition { URI::MailTo::EMAIL_REGEXP.match?(_1) }
|
||||
valid_ids = ids.map { _1.is_a?(String) ? id_from_typed_id(_1) : _1 }
|
||||
|
||||
instructeurs_by_ids = where(id: valid_ids)
|
||||
instructeurs_by_emails = where(users: { email: valid_emails })
|
||||
|
||||
instructeurs = if valid_ids.present? && valid_emails.present?
|
||||
instructeurs_by_ids.or(instructeurs_by_emails).distinct(:id)
|
||||
elsif valid_emails.present?
|
||||
instructeurs_by_emails
|
||||
else
|
||||
instructeurs_by_ids
|
||||
end
|
||||
|
||||
not_found_emails = valid_emails - instructeurs.map(&:email)
|
||||
|
||||
# Send invitations to users without account
|
||||
if not_found_emails.present?
|
||||
administrateurs = groupe_instructeur.procedure.administrateurs
|
||||
instructeurs += not_found_emails.map { invite(_1, administrateurs) }
|
||||
end
|
||||
|
||||
# We dont't want to assign a user to a groupe_instructeur if they are already assigned to it
|
||||
instructeurs -= groupe_instructeur.instructeurs
|
||||
|
||||
[instructeurs, invalid_emails]
|
||||
def self.find_all_by_identifier(ids: [], emails: [])
|
||||
find_all_by_identifier_with_emails(ids:, emails:).first
|
||||
end
|
||||
|
||||
def self.invite(email, administrateurs)
|
||||
user = User.create_or_promote_to_instructeur(email, SecureRandom.hex, administrateurs:)
|
||||
user.invite!
|
||||
user.instructeur
|
||||
def self.find_all_by_identifier_with_emails(ids: [], emails: [])
|
||||
valid_emails, invalid_emails = emails.partition { URI::MailTo::EMAIL_REGEXP.match?(_1) }
|
||||
|
||||
[
|
||||
where(id: ids).or(where(users: { email: valid_emails })).distinct(:id),
|
||||
valid_emails,
|
||||
invalid_emails
|
||||
]
|
||||
end
|
||||
|
||||
def email
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue