# --- 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)