Skip to content

Commit

Permalink
PH-1067 update requests related to api-specs changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Artjom Aralov committed Feb 21, 2024
1 parent 0635848 commit 0af58e1
Show file tree
Hide file tree
Showing 11 changed files with 365 additions and 228 deletions.
56 changes: 31 additions & 25 deletions api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,27 +84,37 @@ def handle_unhandled_error(e):
return jsonify(base.to_dict()), 500

@app.route('/v1/delegates/<string:delegate_identifier>/representees/mandates', methods=['GET'])
def get_delegates_representees_mandates(delegate_identifier):
def get_mandates_by_delegate(delegate_identifier):
xroad_user_id = request.headers.get('X-Road-UserId')
app.logger.info(f'X-Road-UserId: {xroad_user_id} Getting delegate mandates')
error_config = app.config['SETTINGS']['errors']['legal_person_format_validation_failed']
validate_person_company_code(delegate_identifier, error_config)

data_rows = get_mandates(db, delegate_identifier=delegate_identifier)
args = request.args
role_ns = args.get('ns')
subdelegated_by_identifier = args.get('subDelegatedBy')

data_rows = get_mandates(
db,
delegate_identifier=delegate_identifier,
role_ns=role_ns,
subdelegated_by_identifier=subdelegated_by_identifier
)
if not data_rows:
return make_success_response(data_rows, 200)
delegate, representees = extract_delegates_mandates(data_rows)
response_data = serialize_delegate_mandates(delegate, representees, app.config['SETTINGS'])
return make_success_response(response_data, 200)

@app.route('/v1/representees/<string:representee_identifier>/delegates/mandates', methods=['GET'])
def get_representees_delegates_mandates(representee_identifier):
def get_mandates_by_representee(representee_identifier):
xroad_user_id = request.headers.get('X-Road-UserId')
app.logger.info(f'X-Road-UserId: {xroad_user_id} Getting representee mandates')

args = request.args
subdelegated_by_identifier = args.get('subDelegatedBy')
delegate_identifier = args.get('delegate')
role_ns = args.get('ns')
subdelegated_by_identifier = args.get('subDelegatedBy')

error_config = app.config['SETTINGS']['errors']['legal_person_format_validation_failed']
[
Expand All @@ -115,6 +125,7 @@ def get_representees_delegates_mandates(representee_identifier):
db,
representee_identifier=representee_identifier,
delegate_identifier=delegate_identifier,
role_ns=role_ns,
subdelegated_by_identifier=subdelegated_by_identifier
)
if not data_rows:
Expand Down Expand Up @@ -144,13 +155,10 @@ def post_representee_delegate_mandate(representee_identifier, delegate_identifie
db_uri = app.config['SQLALCHEMY_DATABASE_URI']
try:
create_mandate_pg(db_uri, data_to_insert)
except psycopg2.errors.RaiseException as e:
app.logger.exception(str(e))
except Exception as custom_exception:
app.logger.exception(str(custom_exception))
error_config = app.config['SETTINGS']['errors']['unprocessable_request']
raise UnprocessableRequestError(
'Unprocessable request while creating mandate. Something went wrong.',
error_config
)
raise UnprocessableRequestError(str(custom_exception), error_config)
return make_success_response([], 201)

@app.route(
Expand Down Expand Up @@ -206,7 +214,6 @@ def delete_mandate(representee_id, delegate_id, mandate_id):
)
def post_subdelegate_mandate(representee_id, delegate_id, mandate_id):
xroad_user_id = request.headers.get('X-Road-UserId')
xroad_represented_party = request.headers.get('X-Road-Represented-Party')
app.logger.info(f'X-Road-UserId: {xroad_user_id} Creating subdelegate')

data = request.json
Expand All @@ -224,13 +231,10 @@ def post_subdelegate_mandate(representee_id, delegate_id, mandate_id):
data_to_insert['data_created_by'] = xroad_user_id
try:
result = subdelegate_mandate_pg(app.config['SQLALCHEMY_DATABASE_URI'], data_to_insert)
except psycopg2.errors.RaiseException as e:
app.logger.exception(str(e))
except Exception as custom_exception:
app.logger.exception(str(custom_exception))
error_config = app.config['SETTINGS']['errors']['unprocessable_request']
raise UnprocessableRequestError(
'Unprocessable request while subdelegating mandate. Something went wrong.',
error_config
)
raise UnprocessableRequestError(str(custom_exception), error_config)
if not result:
error_config = app.config['SETTINGS']['errors']['mandate_not_found']
raise MandateNotFound('Mandate to delete was not found', error_config)
Expand All @@ -242,24 +246,26 @@ def get_roles():
roles_data = get_roles_pg(db)
mapped = {
'code': 'code',
'delegate_can_equal_to_representee': 'delegateCanEqualToRepresentee',
'modified': 'modified',
'validity_period_from_not_in_future': 'validityPeriodFromNotInFuture',
'validity_period_through_must_be_undefined': 'validityPeriodThroughMustBeUndefined',
'assignable_only_if_representee_has_role_in': 'assignableOnlyIfRepresenteeHasRoleIn',
'delegate_must_equal_to_representee': 'delegateMustEqualToRepresenteeOnAdd',
'addable_only_if_representee_has_role_in': 'addableOnlyIfRepresenteeHasRoleIn',
'delegate_type': 'delegateType',
'can_sub_delegate': 'canSubDelegate',
'sub_delegable': 'subDelegable',
'sub_delegate_type': 'subDelegateType',
'sub_delegable_by': 'subDelegableBy',
'sub_delegating_must_be_signed': 'subDelegatingMustBeSigned',
'addable_by': 'addableBy',
'adding_must_be_signed': 'addingMustBeSigned',
'assignable_by': 'assignableBy',
'waivable_by': 'waivableBy',
'waiving_must_be_signed': 'waivingMustBeSigned',
'withdrawable_by': 'withdrawableBy',
'withdrawal_must_be_signed': 'withdrawalMustBeSigned',
'deletable_by': 'deletableBy',
'deletable_by_delegate': 'deletableByDelegate',
'representee_type': 'representeeType',
'visible': 'visible',
'hidden': 'hidden',
'representee_identifier_in': 'representeeIdentifierIn',
'validity_period_from_not_in_future': 'validityPeriodFromNotInFuture',
'validity_period_through_must_be_undefined': 'validityPeriodThroughMustBeUndefined'
}
for role in roles_data:
role_item = {
Expand Down
1 change: 0 additions & 1 deletion api/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
class Action(Enum):
DELETE_WITHDRAW = 'DELETE_WITHDRAW'
DELETE_WAIVE = 'DELETE_WAIVE'
DELETE = 'DELETE'


def is_valid_action(value):
Expand Down
1 change: 1 addition & 0 deletions api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def serialize_mandate(representee, delegate, mandate, settings):
mandate_data = {
'links': links,
'role': mandate['role'],
'subDelegable': mandate['can_sub_delegate'],
**({'subDelegatorIdentifier': mandate['subdelegated_by_identifier']}
if mandate['subdelegated_by_identifier'] else {}),
**({'validityPeriod': validity_period}
Expand Down
12 changes: 11 additions & 1 deletion api/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
from sqlalchemy import text


def get_mandates(db, representee_identifier=None, delegate_identifier=None, subdelegated_by_identifier=None):
def get_mandates(
db,
representee_identifier=None,
delegate_identifier=None,
role_ns=None,
subdelegated_by_identifier=None
):
params = {
'date_now': datetime.today()
}
Expand All @@ -20,6 +26,10 @@ def get_mandates(db, representee_identifier=None, delegate_identifier=None, subd
where_conditions.append('delegate_identifier=:delegate_identifier')
params['delegate_identifier'] = delegate_identifier

if role_ns:
where_conditions.append("role_ns=:role_ns")
params['role_ns'] = role_ns

if subdelegated_by_identifier:
where_conditions.append('subdelegated_by_identifier=:subdelegated_by_identifier')
params['subdelegated_by_identifier'] = subdelegated_by_identifier
Expand Down
105 changes: 84 additions & 21 deletions api/static/x-road-services-consumed-by-paasuke.json

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ errors:
reference: http://example-legal-person-naming-guidence.com
title: Legal person validation failed
translation:
en: Legal person does not match format (et)
et: Legal person does not match format
en: Legal person does not match format
et: Legal person does not match format (et)
ru: Legal person does not match format (ru)
type: test-type-for-legal-person-validation
action_invalid:
Expand All @@ -19,39 +19,39 @@ errors:
reference: http://example-mandate-data-invalid-guidence.com
title: Mandate data is invalid
translation:
en: Mandate data is invalid (et)
et: Mandate data is invalid
en: Mandate data is invalid
et: Mandate data is invalid (et)
ru: Mandate data is invalid (ru)
type: test-type-for-mandate-data-invalid
mandate_subdelegate_data_invalid:
reference: http://example-mandate-subdelegate-data-invalid-guidence.com
title: Mandate subdelegate data is invalid
translation:
en: Mandate subdelegare data is invalid (et)
et: Mandate subdelegare data is invalid
en: Mandate subdelegare data is invalid
et: Mandate subdelegare data is invalid (et)
ru: Mandate subdelegare data is invalid (ru)
type: test-type-for-mandate-subdelegate-data-invalid
mandate_not_found:
reference: http://example-mandate-not-found-guidence.com
title: Mandate not found
translation:
en: Mandate not found (et)
et: Mandate not found
en: Mandate not found
et: Mandate not found (et)
ru: Mandate not found (ru)
type: test-type-for-mandate-not-found
internal_server_error:
reference: http://example-internal-server-error-guidence.com
title: Internal server error
translation:
en: Internal server error (et)
et: Internal server error
en: Internal server error
et: Internal server error (et)
ru: Internal server error (ru)
type: test-type-for-internal-server-error-found
unprocessable_request:
reference: http://example-unprocessable-request-error-guidence.com
title: Unprocessable request error
translation:
en: Unprocessable request error (et)
et: Unprocessable request error
en: Unprocessable request error
et: Unprocessable request error (et)
ru: Unprocessable request error (ru)
type: test-type-for-internal-server-error-found
3 changes: 3 additions & 0 deletions tests/pg_data/02a_view_mandates_view.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ CREATE OR REPLACE VIEW paasuke_mandates_view AS
SELECT
mandate.id,
mandate.role,
split_part(mandate.role, ':', 1) AS role_ns,
mandate.validity_period_from::DATE,
mandate.validity_period_through::DATE,
mandate.can_sub_delegate,
Expand Down Expand Up @@ -35,4 +36,6 @@ SELECT
FROM mandate
JOIN person AS delegate ON delegate.id = mandate.delegate_id
JOIN person AS representee ON representee.id = mandate.representee_id
LEFT JOIN mandate original_mandate ON original_mandate.id=mandate.original_mandate_id
LEFT JOIN person subdelegated_by_person ON subdelegated_by_person.id = original_mandate.delegate_id
WHERE mandate.deleted is NOT TRUE;
Loading

0 comments on commit 0af58e1

Please sign in to comment.