From e71543793e5f94e9e3d373d6d6ef708d67b4a2c4 Mon Sep 17 00:00:00 2001
From: Benjami <benjami94@gmail.com>
Date: Tue, 20 Jun 2023 12:53:14 +0200
Subject: [PATCH] Add admin type depends on hierarchy level

---
 energy_communities/models/res_company.py      | 22 ++++++++++++-------
 .../views/res_company_views.xml               |  9 +-------
 .../wizards/assign_admin_wizard.py            |  2 +-
 3 files changed, 16 insertions(+), 17 deletions(-)

diff --git a/energy_communities/models/res_company.py b/energy_communities/models/res_company.py
index 798985d1d..0a9e2c10d 100644
--- a/energy_communities/models/res_company.py
+++ b/energy_communities/models/res_company.py
@@ -155,27 +155,33 @@ class ResCompany(models.Model):
                 admins_user_ids.append(role_line.user_id.id)
         return any([user in admins_user_ids for user in company_user_ids])
 
-    # TODO: Get admins depends on hierarcy level
+    def _get_admin_role_name(self):
+        if self.hierarchy_level == 'community':
+            return "role_ce_admin"
+        elif self.hierarchy_level == 'coordinator':
+            return "role_coordination"
+        elif self.hierarchy_level == 'instance':
+            return "role_platform_admin"
+
     def _get_admins(self):
+        role_name = self._get_admin_role_name()
         for rec in self:
             role_lines = self.env["res.users.role.line"].sudo().search([
                 ("company_id.id", "=", self.id),
                 ("active", "=", True),
-                ("role_id.code", "=", "role_ce_admin")
+                ("role_id.code", "=", role_name)
             ])
             rec.admins = role_lines.mapped("user_id")
 
     def add_ce_admin(self, user):
-        if self.hierarchy_level != 'community':
-            raise UserError(_("Only a CE can have CE admins"))
-
+        role_name = self._get_admin_role_name()
         role = self.env["res.users.role"].sudo().search([(
-            "code", "=", "role_ce_admin"
+            "code", "=", role_name
         )])
         current_role = self.env["res.users.role.line"].sudo().search([
             ("user_id", "=", user.id),
             ("active", "=", True),
-            ("company_id", "=", self.id)  # It's M2M, = is okey?
+            ("company_id", "=", self.id)
         ])
         if current_role and len(current_role) > 1:
             raise UserError(_("Error: This user have multiple roles for this company"))
@@ -201,7 +207,7 @@ class ResCompany(models.Model):
     def get_ce_members(self, domain_key="in_kc_and_active"):
         domains_dict = {
             "in_kc_and_active": [
-                ("company_id", "=", self.id),  # TODO: company_ids ??
+                ("company_id", "=", self.id),
                 ("oauth_uid", "!=", None),
                 ("active", "=", True),
             ]
diff --git a/energy_communities/views/res_company_views.xml b/energy_communities/views/res_company_views.xml
index e8d876d50..c9d821258 100644
--- a/energy_communities/views/res_company_views.xml
+++ b/energy_communities/views/res_company_views.xml
@@ -15,7 +15,7 @@
                 <header>
                     <field name="landing_page_id" invisible="1"/>
                     <button name="create_landing" type="object" string="Create landing page" attrs="{'invisible': [('landing_page_id','!=',False)]}" />
-                    <button name="action_open_assign_admin_wizard" type="object" string="Assing administrator" />
+                    <button name="action_open_assign_admin_wizard" type="object" string="Assing administrator" groups="['group_admin','group_platform_manager','group_coordinator']"/>
                 </header>
             </xpath>
             <xpath expr="//field[@name='logo']" position="before">
@@ -23,13 +23,6 @@
                     <button name="get_landing_page_form" type="object" string="My landing page" class="oe_stat_button" icon="fa-globe" attrs="{'invisible': [('landing_page_id','=',False)]}" />
                 </div>
                 <field name="admins" invisible="True"></field>
-                <div class='oe_button_box'>
-                    <button name="action_open_assign_admin_wizard" type="object" string="Administrators" class="oe_stat_button" icon="fa-globe" attrs="{'invisible': [('admins','=',[])]}" />
-                </div>
-                <!-- <field name="admin_mids" invisible="True"></field>
-                <div class='oe_button_box'>
-                    <button name="get_admins" type="object" string="Administrators" class="oe_stat_button" icon="fa-globe" attrs="{'invisible': [('admin_mids','=',[])]}" />
-                </div> -->
             </xpath>
             <xpath expr="//notebook" position="inside">
                 <page string="Energy Communities">
diff --git a/energy_communities/wizards/assign_admin_wizard.py b/energy_communities/wizards/assign_admin_wizard.py
index 63bdcbecd..2bf9ff89a 100644
--- a/energy_communities/wizards/assign_admin_wizard.py
+++ b/energy_communities/wizards/assign_admin_wizard.py
@@ -47,7 +47,7 @@ class AssignAdminWizard(models.TransientModel):
             "company_id": company_id,
             "company_ids": [company_id],
             "lang": self.lang.code,
-            "email": self.email,  # It works?
+            "email": self.email,
         }
         user = self.env["res.users"].create(vals)
 
-- 
GitLab