From f0a67bee59296463a972c1dcba49cdffd161738d Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Fri, 21 Jan 2022 13:04:19 +0100
Subject: [PATCH] esirius_swi: compute status on response (#60958)

---
 passerelle/contrib/esirius_swi/models.py |  3 +++
 passerelle/contrib/esirius_swi/utils.py  | 27 ++++++++++++++++++++++++
 tests/test_esirius_swi.py                | 26 +++++++++++++++++++++++
 3 files changed, 56 insertions(+)
 create mode 100644 passerelle/contrib/esirius_swi/utils.py

diff --git a/passerelle/contrib/esirius_swi/models.py b/passerelle/contrib/esirius_swi/models.py
index 9c496e2a..7cde1c7a 100644
--- a/passerelle/contrib/esirius_swi/models.py
+++ b/passerelle/contrib/esirius_swi/models.py
@@ -19,16 +19,18 @@ from urllib.parse import urljoin
 import zeep
 from django.db import models
 from django.utils.translation import ugettext_lazy as _
 
 from passerelle.base.models import BaseResource
 from passerelle.utils.api import endpoint
 from passerelle.utils.jsonresponse import APIError
 
+from . import utils
+
 
 class ESiriusSwi(BaseResource):
     base_url = models.URLField(_('Base API URL'), help_text=_('with trailing slash'))
     verify_cert = models.BooleanField(default=True, verbose_name=_('Check HTTPS Certificate validity'))
 
     category = _('Business Process Connectors')
 
     class Meta:
@@ -74,10 +76,11 @@ class ESiriusSwi(BaseResource):
                 'realMaxWaitingTime',
                 'resourceNumber',
                 'serviceWaitingIndicatorWSList',
                 'siteCode',
                 'waitingVisitorNumber',
                 'weekHoursWS',
             ]:
                 item_dest[key] = item_src[key]
+            utils.compute_status(item_dest)
             data.append(item_dest)
         return {'err': 0, 'data': data}
diff --git a/passerelle/contrib/esirius_swi/utils.py b/passerelle/contrib/esirius_swi/utils.py
new file mode 100644
index 00000000..509caa19
--- /dev/null
+++ b/passerelle/contrib/esirius_swi/utils.py
@@ -0,0 +1,27 @@
+# -*- coding: utf-8 -*-
+# Copyright (C) 2022 Entr'ouvert
+#
+# This program is free software: you can redistribute it and/or modify it
+# under the terms of the GNU Affero General Public License as published
+# by the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+
+def compute_status(data):
+    data.update(
+        {
+            'closed': not data['resourceNumber'] or data['estimatedAvgWaitingTime'] == '-',
+            'estimatedAvgWaitingTimeInMinutes': None,
+        }
+    )
+    if not data['closed']:
+        values = [int(x) for x in data['estimatedAvgWaitingTime'].split(':')]
+        data['estimatedAvgWaitingTimeInMinutes'] = 60 * values[0] + values[1] + 1
diff --git a/tests/test_esirius_swi.py b/tests/test_esirius_swi.py
index 90ae2943..e2f10f7a 100644
--- a/tests/test_esirius_swi.py
+++ b/tests/test_esirius_swi.py
@@ -16,16 +16,17 @@
 import json
 import os
 
 import mock
 import pytest
 import utils
 
 from passerelle.contrib.esirius_swi.models import ESiriusSwi
+from passerelle.contrib.esirius_swi.utils import compute_status
 
 pytestmark = pytest.mark.django_db
 
 TEST_BASE_DIR = os.path.join(os.path.dirname(__file__), 'data', 'esirius_swi')
 
 
 def get_xml_file(filename):
     with open(os.path.join(TEST_BASE_DIR, filename), 'rb') as desc:
@@ -60,16 +61,39 @@ def test_soap_content_error(mocked_post, mocked_get, v1_service_wsdl, connector,
         status_code=200,
         headers={'Content-Type': 'text/xml'},
     )
     resp = app.get('/esirius-swi/test/get_all_indicators')
     assert resp.json['err'] == 1
     assert resp.json['err_desc'] == "'NoneType' object has no attribute 'getroottree'"
 
 
+@pytest.mark.parametrize(
+    'resource, time, closed, minutes',
+    [
+        (0, '00:01:39', True, None),
+        (1, '-', True, None),
+        (2, '00:01:39', False, 2),
+        (99, '01:59:00', False, 120),
+    ],
+)
+def test_status(resource, time, closed, minutes):
+    data = {
+        'resourceNumber': resource,
+        'estimatedAvgWaitingTime': time,
+    }
+    compute_status(data)
+    assert data == {
+        'resourceNumber': resource,
+        'estimatedAvgWaitingTime': time,
+        'closed': closed,
+        'estimatedAvgWaitingTimeInMinutes': minutes,
+    }
+
+
 @mock.patch('passerelle.utils.Request.get')
 @mock.patch('passerelle.utils.Request.post')
 def test_get_all_indicator(mocked_post, mocked_get, v1_service_wsdl, connector, app):
     mocked_get.return_value = mock.Mock(content=v1_service_wsdl)
     mocked_post.return_value = mock.Mock(
         content=get_xml_file('get_all_indicators.xml'),
         status_code=200,
         headers={'Content-Type': 'text/xml'},
@@ -85,10 +109,12 @@ def test_get_all_indicator(mocked_post, mocked_get, v1_service_wsdl, connector,
             'name': "Braine-l'Alleud",
             'realAvgWaitingTime': '00:03:51',
             'realMaxWaitingTime': '00:06:42',
             'resourceNumber': 9.000003,
             'serviceWaitingIndicatorWSList': None,
             'siteCode': 'site1',
             'waitingVisitorNumber': None,
             'weekHoursWS': None,
+            'closed': False,
+            'estimatedAvgWaitingTimeInMinutes': 4,
         }
     ]
-- 
2.30.2

