api_entreprise: add endpoint to get last document of given type (#35361)
This commit is contained in:
parent
5afa6b45f8
commit
ff87ebb7c2
|
@ -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}/',
|
||||
|
|
|
@ -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'],
|
||||
|
|
Loading…
Reference in New Issue