feat: remove_gestionnaire
This commit is contained in:
parent
b100c8380e
commit
d5637ac7aa
55 changed files with 400 additions and 537 deletions
|
@ -1,30 +0,0 @@
|
|||
class AdminsGroup < ApplicationRecord
|
||||
belongs_to :admins_group, optional: true # parent
|
||||
has_many :children, class_name: "AdminsGroup", inverse_of: :admins_group
|
||||
has_many :administrateurs
|
||||
has_and_belongs_to_many :admins_group_managers
|
||||
|
||||
def add(admins_group_manager)
|
||||
admins_group_managers << admins_group_manager
|
||||
end
|
||||
|
||||
def add_admins_group_managers(ids: [], emails: [])
|
||||
admins_group_managers_to_add, valid_emails, invalid_emails = AdminsGroupManager.find_all_by_identifier_with_emails(ids:, emails:)
|
||||
not_found_emails = valid_emails - admins_group_managers_to_add.map(&:email)
|
||||
|
||||
# Send invitations to users without account
|
||||
if not_found_emails.present?
|
||||
admins_group_managers_to_add += not_found_emails.map do |email|
|
||||
user = User.create_or_promote_to_admins_group_manager(email, SecureRandom.hex)
|
||||
user.invite_admins_group_manager!(self)
|
||||
user.admins_group_manager
|
||||
end
|
||||
end
|
||||
|
||||
# We dont't want to assign a user to an admins_group if they are already assigned to it
|
||||
admins_group_managers_to_add -= admins_group_managers
|
||||
admins_group_managers_to_add.each { add(_1) }
|
||||
|
||||
[admins_group_managers_to_add, invalid_emails]
|
||||
end
|
||||
end
|
|
@ -1,41 +0,0 @@
|
|||
class AdminsGroupManager < ApplicationRecord
|
||||
has_and_belongs_to_many :admins_groups
|
||||
|
||||
belongs_to :user
|
||||
|
||||
delegate :email, to: :user
|
||||
|
||||
default_scope { eager_load(:user) }
|
||||
|
||||
def self.by_email(email)
|
||||
find_by(users: { email: email })
|
||||
end
|
||||
|
||||
def self.find_all_by_identifier(ids: [], emails: [])
|
||||
find_all_by_identifier_with_emails(ids:, emails:).first
|
||||
end
|
||||
|
||||
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 can_be_deleted?
|
||||
!(root_admins_group = admins_groups.where(admins_group: nil).first) || root_admins_group.admins_group_managers.size > 1
|
||||
end
|
||||
|
||||
def registration_state
|
||||
if user.active?
|
||||
'Actif'
|
||||
elsif user.reset_password_period_valid?
|
||||
'En attente'
|
||||
else
|
||||
'Expiré'
|
||||
end
|
||||
end
|
||||
end
|
|
@ -4,10 +4,24 @@ class GroupeGestionnaire < ApplicationRecord
|
|||
has_many :administrateurs
|
||||
has_and_belongs_to_many :gestionnaires
|
||||
|
||||
def root_groupe_gestionnaire?
|
||||
groupe_gestionnaire.nil?
|
||||
end
|
||||
|
||||
def add(gestionnaire)
|
||||
return if gestionnaire.nil?
|
||||
return if in?(gestionnaire.groupe_gestionnaires)
|
||||
|
||||
gestionnaires << gestionnaire
|
||||
end
|
||||
|
||||
def remove(gestionnaire)
|
||||
return if gestionnaire.nil?
|
||||
return if !in?(gestionnaire.groupe_gestionnaires)
|
||||
|
||||
gestionnaire.groupe_gestionnaires.destroy(self)
|
||||
end
|
||||
|
||||
def add_gestionnaires(ids: [], emails: [])
|
||||
gestionnaires_to_add, valid_emails, invalid_emails = Gestionnaire.find_all_by_identifier_with_emails(ids:, emails:)
|
||||
not_found_emails = valid_emails - gestionnaires_to_add.map(&:email)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue