Skip to content

Commit

Permalink
API updates (#86)
Browse files Browse the repository at this point in the history
* RFC - Part 5

* no message

* no message

* no message

* no message

* no message
  • Loading branch information
kris-daxiom authored Aug 9, 2024
1 parent 681ef37 commit 0e745b9
Show file tree
Hide file tree
Showing 6 changed files with 497 additions and 120 deletions.
2 changes: 1 addition & 1 deletion jobs/auto-approval/.devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ ENV PYTHONUNBUFFERED 1

# [Optional] Uncomment this section to install additional OS packages.
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
&& apt-get -y install --no-install-recommends postgresql-client libpq-dev
&& apt-get -y install --no-install-recommends postgresql-client libpq-dev libpango1.0-0
1 change: 1 addition & 0 deletions strr-api/src/strr_api/enums/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,4 @@ class ErrorMessage(Enum):
INVALID_APPLICATION_STATUS = "Invalid application status."
APPLICATION_TERMINAL_STATE = "Application has reached the final state."
PROCESSING_ERROR = "An error occurred while processing the request."
DOCUMENT_NOT_FOUND = "Document not found."
6 changes: 4 additions & 2 deletions strr-api/src/strr_api/requests/RegistrationRequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ def __init__(self, sbc_account_id):
class Registration:
"""Registration payload object."""

def __init__(
def __init__( # pylint: disable=W0102
self,
primaryContact,
unitAddress,
unitDetails,
listingDetails,
principalResidence,
secondaryContact=None,
documents=[],
documents=[], # pylint: disable=W0102
):
self.primaryContact = Contact(**primaryContact)
self.secondaryContact = None
Expand Down Expand Up @@ -132,6 +132,8 @@ def __init__(self, name, dateOfBirth, details, mailingAddress, socialInsuranceNu


class Document:
"""Document object."""

def __init__(self, fileName: str, fileType: str, fileKey: str):
self.fileName = fileName
self.fileKey = fileKey
Expand Down
61 changes: 60 additions & 1 deletion strr-api/src/strr_api/resources/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@

import logging
from http import HTTPStatus
from io import BytesIO

from flasgger import swag_from
from flask import Blueprint, g, jsonify, request
from flask import Blueprint, g, jsonify, request, send_file
from flask_cors import cross_origin
from werkzeug.utils import secure_filename

Expand Down Expand Up @@ -448,6 +449,64 @@ def upload_registration_supporting_document(application_id):
return exception_response(service_exception)


@bp.route("/<application_id>/documents/<document_key>", methods=("GET",))
@swag_from({"security": [{"Bearer": []}]})
@cross_origin(origin="*")
@jwt.requires_auth
def get_document(application_id, document_key):
"""
Get document.
---
tags:
- application
parameters:
- in: path
name: application_id
type: integer
required: true
description: Application Id
- in: path
name: file_key
type: string
required: true
description: File key from the upload document response
responses:
204:
description:
401:
description:
403:
description:
502:
description:
"""

try:
# only allow fetch for applications that belong to the user
application = ApplicationService.get_application(application_id=application_id)
if not application:
raise AuthException()
application_documents = [
doc
for doc in application.application_json.get("registration").get("documents", [])
if doc.get("fileKey") == document_key
]
if not application_documents:
return error_response(ErrorMessage.DOCUMENT_NOT_FOUND.value, HTTPStatus.BAD_REQUEST)
document = application_documents[0]
file_content = DocumentService.get_document_by_key(document_key)
return send_file(
BytesIO(file_content),
as_attachment=True,
download_name=document.get("fileName"),
mimetype=document.get("fileType"),
)
except AuthException as auth_exception:
return exception_response(auth_exception)
except ExternalServiceException as external_exception:
return exception_response(external_exception)


@bp.route("/<application_id>/documents/<document_key>", methods=("DELETE",))
@swag_from({"security": [{"Bearer": []}]})
@cross_origin(origin="*")
Expand Down
5 changes: 5 additions & 0 deletions strr-api/src/strr_api/services/document_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,8 @@ def get_registration_document(cls, registration_id, document_id):
.filter(Document.id == document_id)
.one_or_none()
)

@classmethod
def get_document_by_key(cls, file_key: str):
"""Get registration document by file_key."""
return GCPStorageService.fetch_registration_document(blob_name=file_key)
Loading

0 comments on commit 0e745b9

Please sign in to comment.