diff --git a/easy_my_coop/demo/coop.xml b/easy_my_coop/demo/coop.xml index 6538115..c8cce44 100644 --- a/easy_my_coop/demo/coop.xml +++ b/easy_my_coop/demo/coop.xml @@ -76,8 +76,16 @@ + + 100910 + Variable Capital + + + + - + @@ -127,6 +135,38 @@ + + + Catherine des Champs + catherine@demo.net + Chemin des bois fleuris + 1000 + Brussels + + + manual + 4 + + en_US + + waiting + + + + Catherine des Champs + + + catherine@demo.net + Chemin des bois fleuris, 2 + Brussels + 1000 + + + + + + diff --git a/easy_my_coop_api/demo/demo.xml b/easy_my_coop_api/demo/demo.xml index a88981a..4caccc9 100644 --- a/easy_my_coop_api/demo/demo.xml +++ b/easy_my_coop_api/demo/demo.xml @@ -4,7 +4,15 @@ License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). --> + + + cbd07f57-c903-43b4-b668-436b3bec5f15 + + 1 + + 2 + diff --git a/easy_my_coop_api/services/account_invoice_service.py b/easy_my_coop_api/services/account_invoice_service.py new file mode 100644 index 0000000..e69de29 diff --git a/easy_my_coop_api/services/schemas.py b/easy_my_coop_api/services/schemas.py index f20dab7..2b374f4 100644 --- a/easy_my_coop_api/services/schemas.py +++ b/easy_my_coop_api/services/schemas.py @@ -41,6 +41,12 @@ S_SUBSCRIPTION_REQUEST_RETURN_GET = { }, }, "lang": {"type": "string", "required": True, "empty": False}, + "capital_release_request": { + "type": "list", + "schema": {"type": "integer"}, + "required": True, + "empty": True, + }, } S_SUBSCRIPTION_REQUEST_SEARCH = { @@ -93,3 +99,5 @@ S_SUBSCRIPTION_REQUEST_UPDATE = { "lang": {"type": "string"}, "share_product": {"type": "integer"}, } + +S_SUBSCRIPTION_REQUEST_VALIDATE = {"_id": {"type": "integer"}} diff --git a/easy_my_coop_api/services/subscription_request_service.py b/easy_my_coop_api/services/subscription_request_service.py index b120a82..c962690 100644 --- a/easy_my_coop_api/services/subscription_request_service.py +++ b/easy_my_coop_api/services/subscription_request_service.py @@ -75,9 +75,34 @@ class SubscriptionRequestService(Component): sr.write(params) return self._to_dict(sr) + def validate(self, _id, **params): + sr = self.env["subscription.request"].search( + [("external_id", "=", _id)] + ) + if not sr: + raise wrapJsonException( + NotFound(_("No subscription request for id %s") % _id) + ) + if sr.state != "draft": + raise wrapJsonException( + BadRequest( + _("Subscription request %s is not in draft state") % _id + ) + ) + sr.validate_subscription_request() + return self._to_dict(sr) + def _to_dict(self, sr): sr.ensure_one() + if sr.capital_release_request: + invoice_ids = [ + invoice.get_external_id() + for invoice in sr.capital_release_request + ] + else: + invoice_ids = [] + return { "id": sr.get_external_id(), "name": sr.name, @@ -96,6 +121,7 @@ class SubscriptionRequestService(Component): "country": sr.country_id.code, }, "lang": sr.lang, + "capital_release_request": invoice_ids, } def _get_country(self, code): @@ -191,3 +217,9 @@ class SubscriptionRequestService(Component): def _validator_return_update(self): return schemas.S_SUBSCRIPTION_REQUEST_RETURN_GET + + def _validator_validate(self): + return schemas.S_SUBSCRIPTION_REQUEST_VALIDATE + + def _validator_return_validate(self): + return schemas.S_SUBSCRIPTION_REQUEST_RETURN_GET diff --git a/easy_my_coop_api/tests/common.py b/easy_my_coop_api/tests/common.py index b44d54d..5c91962 100644 --- a/easy_my_coop_api/tests/common.py +++ b/easy_my_coop_api/tests/common.py @@ -17,31 +17,32 @@ HOST = "127.0.0.1" PORT = odoo.tools.config["http_port"] -def _add_api_key(headers): - key_dict = {"API-KEY": "api-key"} - if headers: - headers.update(key_dict) - else: - headers = key_dict - return headers - - class BaseEMCRestCase(BaseRestCase): @classmethod def setUpClass(cls, *args, **kwargs): super().setUpClass(*args, **kwargs) cls.AuthApiKey = cls.env["auth.api.key"] - emc_manager = cls.env.ref("easy_my_coop.res_users_manager_emc_demo") - cls.api_key_test = cls.AuthApiKey.create( - {"name": "test-key", "key": "api-key", "user_id": emc_manager.id} + cls.api_key_test = cls.env.ref( + "easy_my_coop_api.auth_api_key_manager_emc_demo" ) + def _add_api_key(self, headers): + key_dict = {"API-KEY": self.api_key_test.key} + if headers: + headers.update(key_dict) + else: + headers = key_dict + return headers + def setUp(self): super().setUp() self.session = requests.Session() self.demo_request_1 = self.browse_ref( "easy_my_coop.subscription_request_1_demo" ) + self.demo_request_2 = self.browse_ref( + "easy_my_coop.subscription_request_2_demo" + ) self.demo_share_product = ( self.demo_request_1.share_product_id.product_tmpl_id ) @@ -65,10 +66,11 @@ class BaseEMCRestCase(BaseRestCase): "country": "BE", }, "lang": "en_US", + "capital_release_request": [], } def http_get(self, url, headers=None): - headers = _add_api_key(headers) + headers = self._add_api_key(headers) if url.startswith("/"): url = "http://{}:{}{}".format(HOST, PORT, url) @@ -81,7 +83,7 @@ class BaseEMCRestCase(BaseRestCase): return json.loads(content) def http_post(self, url, data, headers=None): - headers = _add_api_key(headers) + headers = self._add_api_key(headers) if url.startswith("/"): url = "http://{}:{}{}".format(HOST, PORT, url) diff --git a/easy_my_coop_api/tests/test_subscription_requests.py b/easy_my_coop_api/tests/test_subscription_requests.py index 405205b..36764da 100644 --- a/easy_my_coop_api/tests/test_subscription_requests.py +++ b/easy_my_coop_api/tests/test_subscription_requests.py @@ -5,6 +5,8 @@ import json from datetime import timedelta +from werkzeug.exceptions import BadRequest + import odoo from odoo.fields import Date @@ -22,27 +24,29 @@ class TestSRController(BaseEMCRestCase): model_name="rest.service.registration", collection=collection ) - self.service = emc_services_env.component(usage="subscription-request") + self.sr_service = emc_services_env.component( + usage="subscription-request" + ) def test_service(self): # kept as example # useful if you need to change data in database and check db type - result = self.service.get(self.demo_request_1.id) + result = self.sr_service.get(self.demo_request_1.external_id) self.assertEquals(self.demo_request_1_dict, result) - all_sr = self.service.search() + all_sr = self.sr_service.search() self.assertTrue(all_sr) sr_date = self.demo_request_1.date date_from = Date.to_string(sr_date - timedelta(days=1)) date_to = Date.to_string(sr_date + timedelta(days=1)) - date_sr = self.service.search(date_from=date_from, date_to=date_to) + date_sr = self.sr_service.search(date_from=date_from, date_to=date_to) self.assertTrue(date_sr) def test_route_get(self): - id_ = self.demo_request_1.id + id_ = self.demo_request_1.external_id route = "/api/subscription-request/%s" % id_ content = self.http_get_content(route) self.assertEquals(self.demo_request_1_dict, content) @@ -126,12 +130,13 @@ class TestSRController(BaseEMCRestCase): "id": self.demo_share_product.id, "name": self.demo_share_product.name, }, + "capital_release_request": [], }, } self.assertEquals(expected, content) def test_route_update(self): - url = "/api/subscription-request/%s" % self.demo_request_1.id + url = "/api/subscription-request/%s" % self.demo_request_1.external_id data = {"state": "done"} response = self.http_post(url, data=data) @@ -141,3 +146,24 @@ class TestSRController(BaseEMCRestCase): expected = self.demo_request_1_dict expected["state"] = "done" self.assertEquals(expected, content) + + def test_route_validate(self): + url = ( + "/api/subscription-request/%s/validate" + % self.demo_request_1.external_id + ) + response = self.http_post(url, data={}) + self.assertEquals(response.status_code, 200) + content = json.loads(response.content.decode("utf-8")) + + state = content.get("state") + self.assertEquals(state, "done") + + def test_service_validate_draft_request(self): + self.sr_service.validate(self.demo_request_1.external_id) + self.assertEquals(self.demo_request_1.state, "done") + self.assertTrue(len(self.demo_request_1.capital_release_request) > 0) + + def test_service_validate_done_request(self): + with self.assertRaises(BadRequest): + self.sr_service.validate(self.demo_request_2.external_id)