diff --git a/infoenergia_api/api/cch/cch.py b/infoenergia_api/api/cch/cch.py index 32a7a0f..e93669c 100644 --- a/infoenergia_api/api/cch/cch.py +++ b/infoenergia_api/api/cch/cch.py @@ -31,7 +31,7 @@ async def get(self, request, contractId, user): if collection in ('P1', 'P2'): collection = 'tg_p1' cch_measure_json = [ - await (await Cch.create(cch_id, collection)).cch_measures(user, request) for cch_id in cch_ids + await (await Cch.create(cch_id, collection)).cch_measures(user, request, contractId) for cch_id in cch_ids ] response = { diff --git a/infoenergia_api/contrib/cch.py b/infoenergia_api/contrib/cch.py index 5bcaca5..c6acdb5 100644 --- a/infoenergia_api/contrib/cch.py +++ b/infoenergia_api/contrib/cch.py @@ -107,17 +107,28 @@ def measurements(self): 'dateUpdate': (self.update_at).strftime("%Y-%m-%d %H:%M:%S"), } - async def cch_measures(self, user, request): - contractId = await request.app.loop.run_in_executor( - self._executor, get_contract_id, self._erp, self.name, user - ) + async def cch_measures(self, user, request, contractId=None): if contractId: return { 'contractId': contractId, 'meteringPointId': make_uuid('giscedata.cups.ps', self.name), 'measurements': self.measurements } - return {} + else: + contractId = await request.app.loop.run_in_executor( + self._executor, + get_contract_id, + self._erp, + self.name, + user, + ) + if contractId: + return { + 'contractId': contractId, + 'meteringPointId': make_uuid('giscedata.cups.ps', self.name), + 'measurements': self.measurements + } + return {} async def async_get_cch(request, contractId=None): diff --git a/infoenergia_api/contrib/f1.py b/infoenergia_api/contrib/f1.py index d2688ff..710c5cd 100644 --- a/infoenergia_api/contrib/f1.py +++ b/infoenergia_api/contrib/f1.py @@ -135,8 +135,13 @@ def f1_maximeter(self): 'maxPower': 4.95, 'period': '2.0TD (P2)' """ + maximeter_obj=self._erp.model('giscedata.f1.maximetre.consumidor') - maximeters = maximeter_obj.read(self.maximetre_consumidor_ids) or [] + maximeters = maximeter_obj.read([ + ('polissa_id', '=', self.polissa_id[0]), + ('data_final', '<=', self.data_final), + ('data_final', '>=', self.data_inici) + ]) or [] return[{ 'dateStart':maximeter['data_inici'], diff --git a/infoenergia_api/utils.py b/infoenergia_api/utils.py index 6fc7bf0..3d216c2 100644 --- a/infoenergia_api/utils.py +++ b/infoenergia_api/utils.py @@ -185,10 +185,12 @@ def get_contract_id(erp_client, cups, user): ('active', '=', True), ('state', '=', 'activa'), ('emp_allow_send_data', '=', True), - ('cups', 'ilike', cups[:20]) + ('cups.name', '=', cups) ] filters = get_contract_user_filters(erp_client, user, filters) contract = contract_obj.search(filters) - + if not contract: + filters += [('cups.name', 'ilike', cups[:20])] + contract = contract_obj.search(filters) if contract: return contract_obj.read(contract, ['name'])[0]['name'] diff --git a/tests/test_f1.py b/tests/test_f1.py index 35c97a4..7743f20 100644 --- a/tests/test_f1.py +++ b/tests/test_f1.py @@ -24,7 +24,7 @@ def test__get_f1_measures_by_contract_id(self, get_invoices_mock): token = self.get_auth_token(user.username, "123412345") request, response = self.client.get( - '/f1/{}?limit=1'.format(self.json4test['invoices_f1_by_contract_id']['contractId']), + '/f1/{}'.format(self.json4test['invoices_f1_by_contract_id']['contractId']), headers={ 'Authorization': 'Bearer {}'.format(token) }, @@ -387,15 +387,15 @@ def test__f1_maximeter(self): self.assertListEqual(f1_maximeter, [ { 'dateStart':'2021-06-01', - 'dateEnd':'2021-09-10', - 'maxPower':4.956, - 'period':'2.0TD (P2)' + 'dateEnd':'2021-09-09', + 'maxPower':4.352, + 'period':'2.0TD (P1)' }, { 'dateStart':'2021-06-01', - 'dateEnd':'2021-09-11', - 'maxPower':2.632, - 'period':'2.0TD (P1)' + 'dateEnd':'2021-09-09', + 'maxPower':2.5, + 'period':'2.0TD (P2)' }, ] )