Skip to content

Commit 726cc32

Browse files
authored
Merge pull request #4 from ArtyomDevNow/PH-573
PH-573/PH-609 [BUG] sample-provider-python käitub delegate type=LEGAL_PERSON puhul valesti / Viia sample-provider-python üle API versioon 0.8.0 pealt 0.9.x peale
2 parents 900fd6a + c45e225 commit 726cc32

8 files changed

Lines changed: 121 additions & 119 deletions

File tree

api/app.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
from flask import Flask, jsonify, request
55
from flask_sqlalchemy import SQLAlchemy
66

7-
from api.exceptions import (CompanyCodeInvalid, DelegateNotFound,
7+
from api.exceptions import (CompanyCodeInvalid,
88
ErrorConfigBase, MandateDataInvalid,
99
MandateNotFound, MandateSubdelegateDataInvalid,
10-
RepresenteeNotFound, UnprocessableRequestError)
10+
UnprocessableRequestError, ActionInvalid)
1111
from api.serializers import (serialize_delegate_mandates,
1212
serialize_representee_mandates)
1313
from api.services import (create_mandate_pg, delete_mandate_pg,
@@ -46,11 +46,10 @@ def create_app():
4646
db.init_app(app)
4747
app.config['SETTINGS'] = parse_settings(app.config['SETTINGS_PATH'])
4848

49+
app.errorhandler(ActionInvalid)(create_error_handler(501))
4950
app.errorhandler(CompanyCodeInvalid)(create_error_handler(400))
5051
app.errorhandler(MandateDataInvalid)(create_error_handler(400))
5152
app.errorhandler(MandateSubdelegateDataInvalid)(create_error_handler(400))
52-
app.errorhandler(RepresenteeNotFound)(create_error_handler(404))
53-
app.errorhandler(DelegateNotFound)(create_error_handler(404))
5453
app.errorhandler(MandateNotFound)(create_error_handler(404))
5554
app.errorhandler(UnprocessableRequestError)(create_error_handler(422))
5655

@@ -99,10 +98,8 @@ def get_representees_delegates_mandates(representee_id):
9998
delegate_identifier=delegate_identifier,
10099
subdelegated_by_identifier=subdelegated_by_identifier
101100
)
102-
# does representee really uknown ?
103101
if not data_rows:
104-
error_config = app.config['SETTINGS']['errors']['representee_not_found']
105-
raise RepresenteeNotFound('Representee not found', error_config)
102+
return make_success_response(data_rows, 200)
106103

107104
representee, delegates = extract_representee_mandates(data_rows)
108105
response_data = serialize_representee_mandates(representee, delegates, app.config['SETTINGS'])
@@ -139,16 +136,20 @@ def post_representee_delegate_mandate(representee_id, delegate_id):
139136
return make_success_response([], 201)
140137

141138
@app.route(
142-
'/nss/<string:ns>/representees/<string:representee_id>/delegates/<string:delegate_id>/mandates/<string:mandate_id>',
143-
methods=['DELETE']
139+
'/representees/<string:representee_id>/delegates/<string:delegate_id>/mandates/<string:mandate_id>',
140+
methods=['PUT']
144141
)
145-
def delete_mandate(ns, representee_id, delegate_id, mandate_id):
142+
def delete_mandate(representee_id, delegate_id, mandate_id):
146143
xroad_user_id = request.headers.get('X-Road-UserId')
147144
app.logger.info(f'X-Road-UserId: {xroad_user_id} Deleting mandate')
145+
data = request.json
146+
if data['action'] != 'DELETE':
147+
error_config = app.config['SETTINGS']['errors']['action_invalid']
148+
raise ActionInvalid("Action invalid", error_config)
148149

149150
db_uri = app.config['SQLALCHEMY_DATABASE_URI']
150151
try:
151-
deleted = delete_mandate_pg(db_uri, ns, representee_id, delegate_id, mandate_id)
152+
deleted = delete_mandate_pg(db_uri, representee_id, delegate_id, mandate_id)
152153
except psycopg2.errors.RaiseException as e:
153154
app.logger.exception(str(e))
154155
error_config = app.config['SETTINGS']['errors']['unprocessable_request']
@@ -160,10 +161,10 @@ def delete_mandate(ns, representee_id, delegate_id, mandate_id):
160161
raise MandateNotFound('Mandate to delete was not found', error_config)
161162

162163
@app.route(
163-
'/nss/<string:ns>/representees/<string:representee_id>/delegates/<string:delegate_id>/mandates/<string:mandate_id>/subdelegates',
164+
'/representees/<string:representee_id>/delegates/<string:delegate_id>/mandates/<string:mandate_id>/subdelegates',
164165
methods=['POST']
165166
)
166-
def post_subdelegate_mandate(ns, representee_id, delegate_id, mandate_id):
167+
def post_subdelegate_mandate(representee_id, delegate_id, mandate_id):
167168
xroad_user_id = request.headers.get('X-Road-UserId')
168169
xroad_represented_party = request.headers.get('X-Road-Represented-Party')
169170
app.logger.info(f'X-Road-UserId: {xroad_user_id} Creating subdelegate')

api/exceptions.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,13 @@ def __init__(self, *args, **kwargs) -> None:
2323
self.status_code = status_code or CompanyCodeInvalid.status_code
2424

2525

26-
class RepresenteeNotFound(ErrorConfigBase):
27-
status_code = 404
28-
29-
def __init__(self, *args, **kwargs) -> None:
30-
super().__init__(*args, **kwargs)
31-
status_code = kwargs.get('status_code')
32-
self.status_code = status_code or RepresenteeNotFound.status_code
33-
34-
35-
class DelegateNotFound(ErrorConfigBase):
36-
status_code = 404
26+
class ActionInvalid(ErrorConfigBase):
27+
status_code = 501
3728

3829
def __init__(self, *args, **kwargs) -> None:
3930
super().__init__(*args, **kwargs)
4031
status_code = kwargs.get('status_code')
41-
self.status_code = status_code or DelegateNotFound.status_code
32+
self.status_code = status_code or ActionInvalid.status_code
4233

4334

4435
class MandateDataInvalid(ErrorConfigBase):

api/serializers.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,9 @@ def serialize_representee_mandates(representee, delegates, settings):
2626
response_data = []
2727
for delegate in delegates:
2828
item = {
29-
'delegate': {
30-
'firstName': delegate['delegate_first_name'],
31-
'identifier': f'{delegate["delegate_identifier"]}',
32-
'surname': delegate['delegate_surname'],
33-
'type': delegate['delegate_type']
34-
},
29+
'delegate': serialize_item_by_type(delegate, 'delegate'),
3530
'mandates': [],
36-
'representee': {
37-
'identifier': f'{representee["representee_identifier"]}',
38-
'legalName': representee['representee_legal_name'],
39-
'type': representee['representee_type']
40-
}
31+
'representee': serialize_item_by_type(representee, 'representee'),
4132
}
4233
for mandate in delegate['mandates']:
4334
mandate_data = serialize_mandate(representee, delegate, mandate, settings)
@@ -46,6 +37,25 @@ def serialize_representee_mandates(representee, delegates, settings):
4637
return response_data
4738

4839

40+
def serialize_item_by_type(item, key_type):
41+
switcher = {
42+
'LEGAL_PERSON': {
43+
'identifier': item[key_type + '_identifier'],
44+
'type': item[key_type + '_type'],
45+
'legalName': item[key_type + '_legal_name']
46+
},
47+
'NATURAL_PERSON': {
48+
'identifier': item[key_type + '_identifier'],
49+
'type': item[key_type + '_type'],
50+
'firstName': item[key_type + '_first_name'],
51+
'surname': item[key_type + '_surname']
52+
},
53+
}
54+
55+
default = {k: v for k, v in item.items() if v is not None and k != key_type + '_id'}
56+
return switcher.get(item[key_type + '_type'], default)
57+
58+
4959
def serialize_mandate(representee, delegate, mandate, settings):
5060
links = {
5161
'delete': mandate['link_delete'],
@@ -73,16 +83,14 @@ def serialize_mandate(representee, delegate, mandate, settings):
7383

7484

7585
def set_subdelegate_link(mandate, representee, delegate):
76-
ns = mandate['role'].split(':')[0]
7786
representee_id = representee['representee_id']
7887
delegate_id = delegate['delegate_id']
7988
mandate_id = mandate['mandate_id']
80-
return f'/v1/nss/{ns}/representees/{representee_id}/delegates/{delegate_id}/mandates/{mandate_id}/subdelegates'
89+
return f'/v1/representees/{representee_id}/delegates/{delegate_id}/mandates/{mandate_id}/subdelegates'
8190

8291

8392
def set_delete_link(mandate, representee, delegate):
84-
ns = mandate['role'].split(':')[0] if mandate.get('role') else ''
8593
representee_id = representee['representee_id']
8694
delegate_id = delegate['delegate_id']
8795
mandate_id = mandate['mandate_id']
88-
return f'/v1/nss/{ns}/representees/{representee_id}/delegates/{delegate_id}/mandates/{mandate_id}'
96+
return f'/v1/representees/{representee_id}/delegates/{delegate_id}/mandates/{mandate_id}'

api/services.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,15 +254,14 @@ def create_mandate_pg(uri, data):
254254
conn.close()
255255

256256

257-
def delete_mandate_pg(uri, namespace, representee_identifier,
257+
def delete_mandate_pg(uri, representee_identifier,
258258
delegate_identifier, mandate_identifier):
259259
conn = psycopg2.connect(uri)
260260
conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
261261

262262
cur = conn.cursor()
263263
cur.callproc(
264264
'function_delete_mandate', [
265-
namespace,
266265
representee_identifier,
267266
delegate_identifier,
268267
mandate_identifier,

settings.yaml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ errors:
88
et: Volituse kustutamine ebaõnnestus
99
ru: Volituse kustutamine ebaõnnestus (ru)
1010
type: test-type-for-legal-person-validation
11-
representee_not_found:
12-
reference: http://example-representee-not-found-guidence.com
13-
title: Representee was not found
11+
action_invalid:
12+
reference: http://example-delegate-not-found-guidence.com
13+
title: Action is invalid
1414
translation:
15-
en: Representee was not found
16-
et: Volituse kustutamine ebaõnnestus
17-
ru: Volituse kustutamine ebaõnnestus (ru)
18-
type: test-type-for-representee-not-found
15+
en: Action is invalid
16+
et: Action is invalid (et)
17+
ru: Action is invalid (ru)
18+
type: test-type-for-action-invalid
1919
mandate_data_invalid:
2020
reference: http://example-mandate-data-invalid-guidence.com
2121
title: Mandate data is invalid

tests/pg_data/02_init_views.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ SELECT
2222
mandate.original_mandate_id,
2323
mandate.document_uuid,
2424
mandate.can_display_document_to_delegate,
25-
CONCAT('/', 'v1/nss/', LEFT(mandate.role, POSITION(':' IN mandate.role) - 1), '/representees/', mandate.representee_id, '/delegates/', mandate.delegate_id, '/mandates/', mandate.id) AS link_delete,
25+
CONCAT('/v1/representees/', mandate.representee_id, '/delegates/', mandate.delegate_id, '/mandates/', mandate.id) AS link_delete,
2626
CASE
27-
WHEN mandate.can_sub_delegate IS TRUE THEN CONCAT('/', 'v1/nss/', LEFT(mandate.role, POSITION(':' IN mandate.role) - 1), '/representees/', mandate.representee_id, '/delegates/', mandate.delegate_id, '/mandates/', mandate.id, '/subdelegates')
27+
WHEN mandate.can_sub_delegate IS TRUE THEN CONCAT('/v1/representees/', mandate.representee_id, '/delegates/', mandate.delegate_id, '/mandates/', mandate.id, '/subdelegates')
2828
ELSE mandate.link_add_sub_delegate
2929
END AS link_add_sub_delegate
3030
FROM mandate

tests/pg_data/03_init_functions.sql

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ END;
6161
$$ LANGUAGE PLPGSQL;
6262

6363
CREATE OR REPLACE FUNCTION function_delete_mandate(
64-
p_namespace TEXT,
6564
p_representee_identifier TEXT,
6665
p_delegate_id TEXT, -- change to text type
6766
p_mandate_id TEXT -- change to text type
@@ -86,8 +85,7 @@ BEGIN
8685
SET deleted = TRUE
8786
WHERE id = CAST(p_mandate_id as INTEGER)
8887
AND representee_id = v_representee_id
89-
AND delegate_id = CAST(p_delegate_id AS INTEGER)
90-
AND (mandate.role LIKE (p_namespace || ':%') OR mandate.role = p_namespace);
88+
AND delegate_id = CAST(p_delegate_id AS INTEGER);
9189
IF FOUND THEN
9290
RETURN TRUE;
9391
ELSE

0 commit comments

Comments
 (0)