From f640381b49a06fd1117b4e433ea3292890a1487f Mon Sep 17 00:00:00 2001
From: Serghei Mihai <smihai@entrouvert.com>
Date: Mon, 12 Aug 2019 12:21:59 +0200
Subject: [PATCH 2/2] api_entreprise: add endpoint to get last document of
 given type (#35361)

---
 passerelle/apps/api_entreprise/models.py | 35 ++++++++++++++++++++++++
 tests/test_api_entreprise.py             | 24 ++++++++++++++--
 2 files changed, 57 insertions(+), 2 deletions(-)

diff --git a/passerelle/apps/api_entreprise/models.py b/passerelle/apps/api_entreprise/models.py
index 36ba6c61..3cab3eea 100644
--- a/passerelle/apps/api_entreprise/models.py
+++ b/passerelle/apps/api_entreprise/models.py
@@ -208,6 +208,41 @@ class APIEntreprise(BaseResource):
             return HttpResponse(response, content_type='application/pdf')
         raise Http404('document not found')
 
+    @endpoint(name='document_association',
+              pattern=r'(?P<association_id>\w+)/get-last/$',
+              example_pattern='{association_id}/get-last/',
+              description=_('Get association\'s last document of type'),
+              parameters={
+                  'association_id': {
+                      'description': _('association SIREN or RNA/WALDEC number'),
+                      'example_value': '44317013900036',
+                  },
+                  'document_type': {
+                      'description': _('document type'),
+                      'example_value': 'Statuts',
+                  },
+                  'object': {
+                      'Description': _('request object: form number, file identifier...'),
+                      'example_value': '42'
+                  },
+                  'context': {
+                      'description': _('request context: MPS, APS...'),
+                      'example_value': 'APS'
+                  },
+                  'recipient': {
+                      'description': _('request recipient: usually customer number'),
+                      'example_value': '44317013900036'
+                  }
+              })
+    def get_last_document_of_type(self, request, association_id, document_type, **kwargs):
+        document = None
+        resp = self.get('documents_associations/%s/' % association_id, **kwargs)
+        documents = [item for item in resp['data'].get('documents', []) if item.get('type') == document_type]
+        if documents:
+            documents.sort(key=lambda doc: doc['timestamp'])
+            document = documents[-1]
+        return {'data': document}
+
     @endpoint(perm='can_access',
               pattern=r'(?P<siren>\w+)/$',
               example_pattern='{siren}/',
diff --git a/tests/test_api_entreprise.py b/tests/test_api_entreprise.py
index aa3ced33..a1cc280b 100644
--- a/tests/test_api_entreprise.py
+++ b/tests/test_api_entreprise.py
@@ -214,6 +214,11 @@ DOCUMENTS_ASSOCIATION_RESPONSE = {
       "url": "https://apientreprise.fr/attestations/40ab0b07d434d0417e8997ce7c5afbef/recepisse_association.pdf",
       "timestamp": "1500667325"
     },
+    {
+      "timestamp": "1337158058",
+      "url": "https://apientreprise.fr/attestations/40ab0b07d434d0417e8997ce7c5afbef/attestation_document_association.pdf",
+      "type": "Statuts"
+    },
   ]
 }
 
@@ -374,7 +379,7 @@ def test_documents_associations_endpoint(app, resource, mock_api_entreprise):
                        params=REQUEST_PARAMS)
     assert 'data' in response.json
     data = response.json['data']
-    assert len(data) == 2
+    assert len(data) == 3
     for document in data:
         assert 'id' in document
         assert 'text' in document
@@ -383,6 +388,21 @@ def test_documents_associations_endpoint(app, resource, mock_api_entreprise):
         assert 'datetime' in document
 
 
+def test_associations_last_document_of_type_endpoint(app, resource, mock_api_entreprise):
+    params = REQUEST_PARAMS
+    params['document_type'] = 'Statuts'
+    response = app.get('/api-entreprise/test/document_association/443170139/get-last/',
+                       params=params)
+    assert 'data' in response.json
+    data = response.json['data']
+    assert data['timestamp'] == '1500660325'
+
+    params['document_type'] = 'Liste des dirigeants'
+    response = app.get('/api-entreprise/test/document_association/443170139/get-last/',
+                       params=params)
+    assert not response.json['data']
+
+
 def test_extraits_rcs(app, resource, mock_api_entreprise, freezer):
     response = app.get('/api-entreprise/test/extraits_rcs/443170139/',
                        params=REQUEST_PARAMS)
@@ -401,7 +421,7 @@ def test_document_association(app, resource, mock_api_entreprise, freezer):
                        params=REQUEST_PARAMS)
     assert 'data' in response.json
     data = response.json['data']
-    assert len(data) == 2
+    assert len(data) == 3
     document = data[0]
     assert 'url' in document
     resp = app.get(document['url'],
-- 
2.23.0

