From 75d0d946c48f319bd24c357acd40112b1d3029ca Mon Sep 17 00:00:00 2001
From: Borja Gimeno <borja.gimeno@somconnexio.coop>
Date: Wed, 8 Mar 2023 18:29:08 +0100
Subject: [PATCH] feat: add paging in Contract & isolate FiberContractsToPack

---
 .../resources/contract.py                     | 71 ++++++++++++++-----
 .../resources/fiber_contracts.py              | 35 +++++++++
 .../resources/odoo_paging.py                  |  4 ++
 3 files changed, 93 insertions(+), 17 deletions(-)
 create mode 100644 odoo_somconnexio_python_client/resources/fiber_contracts.py
 create mode 100644 odoo_somconnexio_python_client/resources/odoo_paging.py

diff --git a/odoo_somconnexio_python_client/resources/contract.py b/odoo_somconnexio_python_client/resources/contract.py
index 4b74908..dfeced4 100644
--- a/odoo_somconnexio_python_client/resources/contract.py
+++ b/odoo_somconnexio_python_client/resources/contract.py
@@ -1,8 +1,22 @@
 from odoo_somconnexio_python_client.client import Client
+from odoo_somconnexio_python_client.resources.odoo_paging import Paging
 
 from ..exceptions import ResourceNotFound
 
 
+class PagingContracts:
+    def __init__(self, contracts, paging):
+        self.contracts = contracts
+        self.paging = paging
+
+
+class AddressContract:
+    def __init__(self, **kwargs):
+        self.street = kwargs.get("street")
+        self.zip = kwargs.get("zip")
+        self.city = kwargs.get("city")
+
+
 class Contract:
     _url_path = "/contract"
 
@@ -43,6 +57,37 @@ class Contract:
         self.date_end = date_end
         self.is_terminated = is_terminated
         self.fiber_signal = fiber_signal
+        self.description = kwargs.get("description")
+        self.email = kwargs.get("email")
+        self.subscription_type = kwargs.get("subscription_type")
+        self.subscription_technology = kwargs.get("subscription_technology")
+        self.available_operations = kwargs.get("available_operations")
+        self.address = AddressContract(**kwargs.get("address", {}))
+
+    @classmethod
+    def paginated_search_by_customer_ref(cls, customer_ref, limit, offset):
+        """
+        Search Contracts in Odoo by partner's ref and control paging params.
+
+        :return: Contracts object if exists and controls params to pagination
+        """
+        return cls._get(
+            params={"customer_ref": customer_ref, "limit": limit, "offset": offset},
+            is_paginated=True,
+        )
+
+    @classmethod
+    def search_by_customer_ref(cls, customer_ref):
+        """
+        Search Contracts in Odoo by partner's ref.
+
+        :return: Contracts object if exists
+        """
+        return cls._get(
+            params={
+                "customer_ref": customer_ref,
+            },
+        )
 
     @classmethod
     def search_by_customer_vat(cls, vat):
@@ -84,7 +129,7 @@ class Contract:
         )
 
     @classmethod
-    def _get(cls, id=None, params={}):
+    def _get(cls, id=None, params={}, is_paginated=False):
         if id:
             url = "{}/{}".format(cls._url_path, id)
         else:
@@ -97,22 +142,14 @@ class Contract:
         if not response_data:
             raise ResourceNotFound(resource=cls.__name__, filter=params)
 
-        return [cls(**contract_found) for contract_found in response_data]
-
-
-class FiberContractsToPack(Contract):
-    _url_path = Contract._url_path + "/available-fibers-to-link-with-mobile"
+        contracts = []
+        for contract_found in response_data.get("contracts", response_data):
+            contracts.append(cls(**contract_found))
 
-    @classmethod
-    def search_by_partner_ref(cls, partner_ref):
-        """
-        Search available fiber contracts to pack
-        with in in Odoo, by their partner reference.
+        if not is_paginated:
+            return contracts
 
-        :return: Contract object if exists
-        """
-        return cls._get(
-            params={
-                "partner_ref": partner_ref,
-            }
+        return PagingContracts(
+            contracts,
+            Paging(**response_data.get("paging", {})),
         )
diff --git a/odoo_somconnexio_python_client/resources/fiber_contracts.py b/odoo_somconnexio_python_client/resources/fiber_contracts.py
new file mode 100644
index 0000000..84c8ae8
--- /dev/null
+++ b/odoo_somconnexio_python_client/resources/fiber_contracts.py
@@ -0,0 +1,35 @@
+from odoo_somconnexio_python_client.client import Client
+from odoo_somconnexio_python_client.resources.contract import Contract
+
+from ..exceptions import ResourceNotFound
+
+
+class FiberContractsToPack:
+    _url_path = "/contract/available-fibers-to-link-with-mobile"
+
+    @classmethod
+    def search_by_partner_ref(cls, partner_ref):
+        """
+        Search available fiber contracts to pack
+        with in in Odoo, by their partner reference.
+
+        :return: Contract object if exists
+        """
+        return cls._get(
+            params={
+                "partner_ref": partner_ref,
+            }
+        )
+
+    @classmethod
+    def _get(cls, params={}):
+        url = cls._url_path
+
+        response_data = Client().get(
+            url,
+            params=params,
+        )
+        if not response_data:
+            raise ResourceNotFound(resource=cls.__name__, filter=params)
+
+        return [Contract(**contract_found) for contract_found in response_data]
diff --git a/odoo_somconnexio_python_client/resources/odoo_paging.py b/odoo_somconnexio_python_client/resources/odoo_paging.py
new file mode 100644
index 0000000..227a90d
--- /dev/null
+++ b/odoo_somconnexio_python_client/resources/odoo_paging.py
@@ -0,0 +1,4 @@
+class Paging:
+    def __init__(self, **kwargs):
+        self.limit = kwargs.get("limit")
+        self.total_number_records = kwargs.get("totalNumberOfRecords")
-- 
GitLab