File size: 7,979 Bytes
ef1ad9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# --- Library Imports ---
from fastapi import APIRouter, BackgroundTasks, Depends, Form, Query, Security, UploadFile
from sqlalchemy.orm import Session

from app.schema.examples.document import *
from app.schema.examples.error_response import *
from app.schema.response_schema import ResponseSchema
from app.services.document_service import DocumentService
from app.services.dropdown_service import DocumentDropdownService

# ---
# --- User Imports ---
from app.utils.database import get_db
from app.utils.jwt import VerifyToken
from app.utils.utility import create_swagger_response

# ---


router = APIRouter()
auth = VerifyToken()


@router.post(

    "/upload_file",

    dependencies=[Depends(auth.verify)],

    response_model=ResponseSchema,

    summary="Upload File",

    responses=create_swagger_response(upload_file_response, [unauthorized_response]),

    description="Upload a file to blob storage for a specified application.",

)
async def upload_file(

    background_tasks: BackgroundTasks,

    applicationId: int = Form(),

    userProfileId: int = Form(),

    documentTypeId: int = Form(default=None, Optional=True),

    userId: int = Form(default=None, Optional=True),

    documentSubTypeId: int = Form(default=None, Optional=True),

    documentTitle: str = Form(default=None, Optional=True),

    file: UploadFile = UploadFile(...),

    user=Security(auth.verify, scopes=[]),

    db: Session = Depends(get_db),

):
    """

    Upload a file to blob storage for a specified application.



    - **background_tasks**: The background task manager.

    - **applicationId**: The ID of the application.

    - **userProfileId**: The ID of the user profile.

    - **documentTypeId**: The ID of the document type.

    - **userId**: The ID of the user.

    - **documentSubTypeId**: The ID of the document subtype.

    - **documentTitle**: The title of the document.

    - **file**: The file to be uploaded.

    - **user**: The authenticated user.

    - **db**: The database session.

    """
    payload = {
        "user_id": userId,
        "user_profile_id": userProfileId,
        "application_id": applicationId,
        "document_subtype_id": documentSubTypeId,
        "document_type_id": documentTypeId,
        "document_title": documentTitle,
    }
    result = await DocumentService.upload_file_to_blob_storage(file, user, payload, db, background_tasks)
    return result


@router.put(

    "/update_file/{document_id}",

    dependencies=[Depends(auth.verify)],

    response_model=ResponseSchema,

    summary="Update File",

    responses=create_swagger_response(update_file_response, [unauthorized_response, document_not_found]),

    description="Update an existing file in blob storage for a specified application.",

)
async def update_file(

    document_id: int,

    applicationId: int = Form(),

    documentTitle: str = Form(default=None, Optional=True),

    file: UploadFile = UploadFile(...),

    userId: int = Form(default=None, Optional=True),

    user=Security(auth.verify, scopes=[]),

    db: Session = Depends(get_db),

):
    """

    Update an existing file in blob storage for a specified application.



    - **document_id**: The ID of the document to update.

    - **applicationId**: The ID of the application.

    - **documentTitle**: The title of the document.

    - **file**: The file to be updated.

    - **userId**: The ID of the user.

    - **user**: The authenticated user.

    - **db**: The database session.

    """
    payload = {
        "document_id": document_id,
        "application_id": applicationId,
        "document_title": documentTitle,
        "user_id": userId,
    }
    result = await DocumentService.update_file(file, user, payload, db)
    return result


@router.get(

    "/read_file/{document_id}",

    dependencies=[Depends(auth.verify)],

    response_model=ResponseSchema,

    summary="Read File",

    description="Read a file from blob storage.",

)
async def read_file(

    document_id: int,

    user=Security(auth.verify, scopes=[]),

    db: Session = Depends(get_db),

):
    """

    Read a file from blob storage.



    - **document_id**: The ID of the document to read.

    - **user**: The authenticated user.

    - **db**: The database session.

    """
    result = await DocumentService.read_file_from_blob_storage(document_id, user, None, db)
    return result


@router.get(

    "/documents/{application_id}",

    dependencies=[Depends(auth.verify)],

    response_model=ResponseSchema,

    summary="List Documents",

    responses=create_swagger_response(list_documents_response, [unauthorized_response]),

    description="Fetch a list of document types for the specified application.",

)
async def list_documents(

    application_id: int,

    user=Security(auth.verify, scopes=[]),

    db: Session = Depends(get_db),

):
    """

    Fetch a list of document types for the specified application.



    - **application_id**: The ID of the application.

    - **user**: The authenticated user.

    - **db**: The database session.

    """
    return await DocumentService.get_document_types(application_id, user, db)


@router.get(

    "/documents/internal/{application_id}",

    response_model=ResponseSchema,

    summary="List Internal Documents",

    responses=create_swagger_response(list_documents_response),

    description="Fetch a list of internal document types for the specified application.",

)
async def list_documents_internal(

    application_id: int,

    user_id: str = Query(),

    db: Session = Depends(get_db),

):
    """

    Fetch a list of internal document types for the specified application.



    - **application_id**: The ID of the application.

    - **user_id**: The ID of the user.

    - **db**: The database session.

    """
    return await DocumentService.get_document_types(application_id, user_id, db)


@router.delete(

    "/document/{document_id}",

    dependencies=[Depends(auth.verify)],

    response_model=ResponseSchema,

    summary="Delete Document",

    responses=create_swagger_response(delete_document_response, [unauthorized_response, document_not_found]),

    description="Delete a document from blob storage.",

)
async def delete_document(

    document_id: int,

    db: Session = Depends(get_db),

    user=Security(auth.verify, scopes=[]),

    user_id: int = Form(default=None, Optional=True),

):
    """

    Delete a document from blob storage.



    - **document_id**: The ID of the document to delete.

    - **db**: The database session.

    - **user**: The authenticated user.

    - **user_id**: The ID of the user.

    """
    return await DocumentService.delete_document(db, document_id, user, user_id)


@router.get(

    "/user_info",

    dependencies=[Depends(auth.verify)],

    response_model=ResponseSchema,

    summary="Get User Info",

    responses=create_swagger_response(user_info_response, [unauthorized_response, application_not_found]),

    description="Fetch information about the authenticated user.",

)
async def get_user_info(

    user=Security(auth.verify, scopes=[]),

    db: Session = Depends(get_db),

):
    """

    Fetch information about the authenticated user.



    - **user**: The authenticated user.

    - **db**: The database session.

    """
    return await DocumentService.get_user_info(db, user)


@router.get(

    "/document_status",

    dependencies=[Depends(auth.verify)],

    response_model=ResponseSchema,

    summary="Get Document Status",

    responses=create_swagger_response(document_status_response, [unauthorized_response]),

    description="Fetch the status of all loan documents.",

)
async def document_status(db: Session = Depends(get_db)):
    """

    Fetch the status of all loan documents.



    - **db**: The database session.

    """
    return await DocumentDropdownService.get_loanTypes(db)