first commit
This commit is contained in:
commit
efa5080897
127 changed files with 18610 additions and 0 deletions
57
backend/app/dlt_service.py
Normal file
57
backend/app/dlt_service.py
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
from datetime import datetime, timezone
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from .dlt_constants import DLT_DOCUMENT_TYPES, PE_TM_CHAIN_BINDING_ID
|
||||
from .models import DltApplication, DltDocument, UserAccount
|
||||
|
||||
|
||||
def ensure_dlt_application(db: Session, user: UserAccount) -> DltApplication:
|
||||
if user.dlt_application:
|
||||
return user.dlt_application
|
||||
application = DltApplication(user_id=user.id, status="not_started")
|
||||
db.add(application)
|
||||
db.flush()
|
||||
for document_type in DLT_DOCUMENT_TYPES:
|
||||
db.add(DltDocument(application_id=application.id, document_type=document_type, status="missing"))
|
||||
db.commit()
|
||||
db.refresh(application)
|
||||
return application
|
||||
|
||||
|
||||
def serialize_dlt(application: DltApplication) -> dict:
|
||||
documents = sorted(application.documents, key=lambda doc: doc.document_type)
|
||||
all_approved = bool(documents) and all(doc.status == "approved" for doc in documents)
|
||||
return {
|
||||
"id": str(application.id),
|
||||
"status": "approved" if all_approved and application.status != "not_started" else application.status,
|
||||
"completed": all_approved,
|
||||
"submitted_at": application.submitted_at,
|
||||
"approved_at": application.approved_at,
|
||||
"pe_tm_chain_binding_id": PE_TM_CHAIN_BINDING_ID,
|
||||
"documents": [
|
||||
{
|
||||
"id": str(doc.id),
|
||||
"document_type": doc.document_type,
|
||||
"original_filename": doc.original_filename,
|
||||
"status": doc.status,
|
||||
"review_note": doc.review_note,
|
||||
"uploaded_at": doc.uploaded_at,
|
||||
"reviewed_at": doc.reviewed_at,
|
||||
}
|
||||
for doc in documents
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
def refresh_dlt_application_status(application: DltApplication) -> None:
|
||||
documents = application.documents
|
||||
if not documents:
|
||||
return
|
||||
if all(doc.status == "approved" for doc in documents):
|
||||
application.status = "approved"
|
||||
application.approved_at = datetime.now(timezone.utc)
|
||||
elif any(doc.status == "rejected" for doc in documents):
|
||||
application.status = "rejected"
|
||||
elif application.submitted_at:
|
||||
application.status = "submitted"
|
||||
Loading…
Add table
Add a link
Reference in a new issue