From 206fed617d6703861a738e1997289a92413cc882 Mon Sep 17 00:00:00 2001
From: Benjami <benjami94@gmail.com>
Date: Fri, 9 Jun 2023 07:50:15 +0200
Subject: [PATCH] WIP: Added methods into community and user to get admins

---
 energy_communities/models/res_company.py | 13 ++++++++++++-
 energy_communities/models/res_users.py   | 12 ++++++++++++
 2 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/energy_communities/models/res_company.py b/energy_communities/models/res_company.py
index fcffb71d5..0c73aae4e 100644
--- a/energy_communities/models/res_company.py
+++ b/energy_communities/models/res_company.py
@@ -155,6 +155,17 @@ 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])
 
+    def get_ce_admins(self):
+        admins = []
+        role_lines = self.env["res.users.role.line"].sudo().search([
+            ("allowed_company_ids.id", "=", self.id),  # It's M2M, = is okey?
+            ("active", "=", True),
+            ("role_id.code", "=", "role_ce_admin")
+        ])
+        for role_line in role_lines:
+            admins.append(role_line.user_id)
+        return admins
+
     def add_ce_admin(self, user):
         if self.hierarchy_level != COMMUNITY_HIERARCHY:
             raise UserError(_("Only a CE can have CE admins"))
@@ -191,7 +202,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),
+                ("company_id", "=", self.id),  # TODO: company_ids ??
                 ("oauth_uid", "!=", None),
                 ("active", "=", True),
             ]
diff --git a/energy_communities/models/res_users.py b/energy_communities/models/res_users.py
index fa5759a19..0d2fc9226 100644
--- a/energy_communities/models/res_users.py
+++ b/energy_communities/models/res_users.py
@@ -195,3 +195,15 @@ class ResUsers(models.Model):
     def get_role_codes(self):
         # TODO Map all code to company and enable (We should update the API schema too)
         return self.role_line_ids[0].role_id.code
+
+    def get_administrared_ce(self):
+        communities = []
+        role_lines = self.env["res.users.role.line"].sudo().search([
+            ("user_id.id", "=", self.id),
+            ("active", "=", True),
+            ("role_id.code", "=", "role_ce_admin")
+        ])
+        for role_line in role_lines:
+            for company in role_line.allowed_company_ids:
+                communities.append(company.id)
+        return communities
-- 
GitLab