Implement workflow improvements: admin document review, user email notifications, and engagement letter download

This commit is contained in:
Atharv1705
2026-09-30 16:37:33 +05:30
parent cb4d939c0b
commit 268c3d402b
22 changed files with 1193 additions and 330 deletions
+39 -6
View File
@@ -47,6 +47,7 @@ def create_access_token(
payload = {
"sub": subject,
"role": role.lower(),
"type": "access",
"exp": expire,
}
return jwt.encode(payload, SECRET_KEY, algorithm=ALGORITHM)
@@ -169,8 +170,12 @@ def get_current_user(
try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
email: str | None = payload.get("sub")
if not email:
# Old access tokens omitted 'type', so we default to 'access'.
# Reset and verification tokens explicitly set their type, so they will fail this check.
token_type = payload.get("type", "access")
# We must explicitly check type claim to prevent reset tokens from acting as access tokens
if not email or token_type != "access":
raise credentials_exception
except JWTError:
@@ -207,8 +212,29 @@ def require_super_admin(current_user=Depends(get_current_user)):
return current_user
def assert_admin_can_access(current_user, target_user):
"""
Enforces that:
1. The current user is actually an admin/super_admin.
2. The target user exists (throws 404).
3. The target user has a 'user' role (throws 403).
"""
if not target_user:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="User not found")
if current_user.role not in [UserRole.admin, UserRole.super_admin]:
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Admin access required")
if target_user.role != UserRole.user:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Admins can only interact with regular user accounts"
)
# =========================
# Auth Routes
# =========================
@router.post("/signup", response_model=TokenResponse)
@@ -469,6 +495,7 @@ def update_me(
def list_users_for_admin(
skip: int = Query(0, ge=0),
limit: int = Query(100, ge=1, le=500),
tax_year: int = Query(None),
db: Session = Depends(get_db),
current_user=Depends(get_current_user),
_=Depends(require_admin),
@@ -482,16 +509,22 @@ def list_users_for_admin(
from sqlalchemy import func as sqlfunc
def _pending_count(uid: int) -> dict:
p = db.query(sqlfunc.count(PersonalDocument.id)).filter(
p_q = db.query(sqlfunc.count(PersonalDocument.id)).filter(
PersonalDocument.user_id == uid,
PersonalDocument.review_status == "pending",
PersonalDocument.deleted_at == None,
).scalar() or 0
b = db.query(sqlfunc.count(BusinessDocument.id)).filter(
)
b_q = db.query(sqlfunc.count(BusinessDocument.id)).filter(
BusinessDocument.user_id == uid,
BusinessDocument.review_status == "pending",
BusinessDocument.deleted_at == None,
).scalar() or 0
)
if tax_year is not None:
p_q = p_q.filter(PersonalDocument.tax_year == tax_year)
b_q = b_q.filter(BusinessDocument.tax_year == tax_year)
p = p_q.scalar() or 0
b = b_q.scalar() or 0
return {"pending_personal": p, "pending_business": b, "pending_docs": p + b}
if current_user.role == UserRole.super_admin:
+1
View File
@@ -200,3 +200,4 @@ def spa_fallback(full_path: str):
status_code=404,
detail="Frontend not built. Run 'npm run build' in /frontend.",
)
# reload
+51 -2
View File
@@ -61,7 +61,7 @@ class PersonalDocument(Base):
content_type = Column(String(100))
uploaded_at = Column(DateTime(timezone=True), server_default=func.now())
deleted_at = Column(DateTime(timezone=True), nullable=True, default=None)
review_status = Column(String(20), default="pending", nullable=False)
review_status = Column(String(20), default="draft", nullable=False)
review_note = Column(String(500), nullable=True)
tax_year = Column(Integer, default=2025, nullable=False)
extracted_data = Column(JSON, nullable=True)
@@ -89,7 +89,7 @@ class BusinessDocument(Base):
content_type = Column(String(100))
uploaded_at = Column(DateTime(timezone=True), server_default=func.now())
deleted_at = Column(DateTime(timezone=True), nullable=True, default=None)
review_status = Column(String(20), default="pending", nullable=False)
review_status = Column(String(20), default="draft", nullable=False)
review_note = Column(String(500), nullable=True)
tax_year = Column(Integer, default=2025, nullable=False)
extracted_data = Column(JSON, nullable=True)
@@ -127,6 +127,12 @@ class AdminDocument(Base):
# None = still processing, non-null = ready to display
ai_summary = Column(Text, nullable=True)
# Workflow additions
tax_year = Column(Integer, nullable=True)
review_status = Column(String(20), default="pending", nullable=False)
review_note = Column(String(500), nullable=True)
reviewed_at = Column(DateTime(timezone=True), nullable=True)
admin = relationship("User", foreign_keys=[uploaded_by])
user = relationship("User", foreign_keys=[user_id])
@@ -248,3 +254,46 @@ class FilingDeadline(Base):
Index("ix_filing_deadlines_user_id", "user_id"),
Index("ix_filing_deadlines_deadline_date", "deadline_date"),
)
# ─────────────────────────────────────────────
# Workflow & Review System
# ─────────────────────────────────────────────
class DocumentReviewEvent(Base):
__tablename__ = "document_review_events"
id = Column(Integer, primary_key=True, index=True)
doc_kind = Column(String(50), nullable=False) # 'admin' | 'personal' | 'business'
doc_id = Column(Integer, nullable=False)
owner_user_id = Column(Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False)
actor_id = Column(Integer, ForeignKey("users.id", ondelete="SET NULL"), nullable=True)
actor_role = Column(String(20), nullable=False) # 'user' | 'admin' | 'super_admin'
action = Column(String(50), nullable=False) # 'uploaded', 'returned_for_review', 'approved', 'rejected', 'resubmitted', 'admin_comment'
from_status = Column(String(20), nullable=True)
to_status = Column(String(20), nullable=True)
tax_year = Column(Integer, nullable=True)
comment = Column(Text, nullable=True)
created_at = Column(DateTime(timezone=True), server_default=func.now(), nullable=False)
owner = relationship("User", foreign_keys=[owner_user_id])
actor = relationship("User", foreign_keys=[actor_id])
__table_args__ = (
Index("ix_document_review_events_composite", "doc_kind", "doc_id", "created_at"),
Index("ix_document_review_events_owner", "owner_user_id"),
)
class AdminDocumentBookmark(Base):
__tablename__ = "admin_document_bookmarks"
id = Column(Integer, primary_key=True, index=True)
admin_id = Column(Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False)
doc_kind = Column(String(50), nullable=False)
doc_id = Column(Integer, nullable=False)
created_at = Column(DateTime(timezone=True), server_default=func.now(), nullable=False)
admin = relationship("User")
__table_args__ = (
UniqueConstraint("admin_id", "doc_kind", "doc_id", name="uq_admin_bookmark"),
)
+105
View File
@@ -145,6 +145,26 @@ async def admin_doc_response(
f"belonging to user {doc.user_id}"
)
raise HTTPException(status_code=403, detail="Not authorized")
doc.review_status = payload.status
if payload.reason:
doc.review_note = payload.reason
db.commit()
from app.models import DocumentReviewEvent
event = DocumentReviewEvent(
doc_kind="admin",
doc_id=doc.id,
owner_user_id=doc.user_id,
actor_id=current_user.id,
actor_role="user",
action=payload.status,
to_status=payload.status,
note=payload.reason,
tax_year=doc.tax_year
)
db.add(event)
db.commit()
admin = db.query(User).filter_by(id=doc.uploaded_by).first()
if not admin:
@@ -168,7 +188,92 @@ async def admin_doc_response(
logger.info(f"User {current_user.id} {payload.status} admin doc {payload.doc_id}")
return {"status": "notified"}
# ─────────────────────────────────────────────
# Update Status and History
# ─────────────────────────────────────────────
@router.get("/history/{doc_kind}/{doc_id}")
def get_document_review_history(
doc_kind: str,
doc_id: int,
db: Session = Depends(get_db),
current_user=Depends(get_current_user),
):
from app.models import DocumentReviewEvent
events = db.query(DocumentReviewEvent).filter_by(
doc_kind=doc_kind,
doc_id=doc_id
).order_by(DocumentReviewEvent.created_at.asc()).all()
# If standard user, verify ownership
if current_user.role == UserRole.user:
if not events or events[0].owner_user_id != current_user.id:
raise HTTPException(status_code=403, detail="Unauthorized")
return {
"history": [
{
"id": e.id,
"actor_role": e.actor_role,
"action": e.action,
"to_status": e.to_status,
"note": e.note,
"created_at": e.created_at.isoformat() + "Z" if e.created_at else None
} for e in events
]
}
from pydantic import BaseModel
class StatusUpdateRequest(BaseModel):
status: str
note: str | None = None
@router.put("/status/{doc_kind}/{doc_id}")
def update_document_status(
doc_kind: str,
doc_id: int,
payload: StatusUpdateRequest,
db: Session = Depends(get_db),
current_user=Depends(require_admin),
):
from app.models import PersonalDocument, BusinessDocument, DocumentReviewEvent
model_map = {
'admin': AdminDocument,
'personal': PersonalDocument,
'business': BusinessDocument
}
if doc_kind not in model_map:
raise HTTPException(status_code=400, detail="Invalid doc_kind")
doc = db.query(model_map[doc_kind]).filter_by(id=doc_id).first()
if not doc:
raise HTTPException(status_code=404, detail="Document not found")
doc.review_status = payload.status
if payload.note is not None:
doc.review_note = payload.note
if payload.status == 'approved':
from datetime import datetime
doc.reviewed_at = datetime.utcnow()
db.commit()
event = DocumentReviewEvent(
doc_kind=doc_kind,
doc_id=doc.id,
owner_user_id=doc.user_id,
actor_id=current_user.id,
actor_role="admin",
action="status_updated",
to_status=payload.status,
note=payload.note,
tax_year=doc.tax_year
)
db.add(event)
db.commit()
return {"status": "ok", "review_status": doc.review_status}
# ─────────────────────────────────────────────
# Get filing deadlines for a user
+305 -151
View File
@@ -4,7 +4,7 @@ import uuid
import logging
from datetime import datetime
import filetype
from fastapi import APIRouter, BackgroundTasks, UploadFile, File, HTTPException, Depends, Form, Query
from fastapi import APIRouter, BackgroundTasks, UploadFile, File, HTTPException, Depends, Form, Query, Response
from fastapi.responses import FileResponse, StreamingResponse
from sqlalchemy.orm import Session
from sqlalchemy.exc import IntegrityError
@@ -14,7 +14,7 @@ from datetime import timedelta
from app.models import AdminDocument, PersonalDocument, BusinessDocument, User, UserRole, ChatSession, ChatMessage
from app.db import get_db, SessionLocal
import app.crud as crud
from app.auth.security import get_current_user, require_admin, SECRET_KEY, ALGORITHM
from app.auth.security import get_current_user, require_admin, assert_admin_can_access, SECRET_KEY, ALGORITHM
from app.utils.emailer import send_email
from app.utils.ai import extract_document_data, summarize_admin_document, extract_raw_text
from app.utils.vector_store import add_document_embedding, delete_document_embeddings, delete_user_embeddings
@@ -128,7 +128,9 @@ async def upload_to_storage(file: UploadFile) -> str:
"application/msword",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"application/vnd.ms-excel",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"application/zip",
"application/x-zip-compressed"
}
kind = filetype.guess(contents)
@@ -137,8 +139,8 @@ async def upload_to_storage(file: UploadFile) -> str:
ext = os.path.splitext(file.filename or "file")[1].lower()
# Fallback for legacy .doc, .xls, and inconsistent .docx/.xlsx sniffing (which are just zip files)
if sniffed_mime is None or sniffed_mime == "application/zip":
if ext in [".doc", ".docx", ".xls", ".xlsx"] and file.content_type in ALLOWED_TYPES:
if sniffed_mime is None or sniffed_mime in ["application/zip", "application/x-zip-compressed"]:
if ext in [".doc", ".docx", ".xls", ".xlsx", ".zip"] and file.content_type in ALLOWED_TYPES:
sniffed_mime = file.content_type
if sniffed_mime not in ALLOWED_TYPES:
@@ -256,21 +258,14 @@ async def upload_admin_document(
doc_key: str = Form(...),
doc_label: str = Form(...),
user_id: int = Form(...),
tax_year: int | None = Form(None),
background: BackgroundTasks = BackgroundTasks(),
db: Session = Depends(get_db),
current_user=Depends(get_current_user),
_=Depends(require_admin),
):
target_user = crud.get_user_by_id(db, user_id)
if not target_user:
raise HTTPException(status_code=404, detail="Target user not found")
# Regular admins can only upload documents for user-role accounts
if current_user.role == UserRole.admin and target_user.role != UserRole.user:
raise HTTPException(
status_code=403,
detail="Admins can only upload documents for regular user accounts"
)
assert_admin_can_access(current_user, target_user)
logger.info(f"Admin {current_user.id} uploading document for user {user_id}")
storage_key = await upload_to_storage(file)
@@ -283,33 +278,33 @@ async def upload_admin_document(
content_type=file.content_type,
uploaded_by=current_user.id,
user_id=user_id,
ai_summary="",
tax_year=tax_year,
review_status="pending"
)
db.add(record)
db.commit()
db.refresh(record)
# Append uploaded event
from app.models import DocumentReviewEvent
event = DocumentReviewEvent(
doc_kind="admin",
doc_id=record.id,
owner_user_id=user_id,
actor_id=current_user.id,
actor_role="admin",
action="uploaded",
to_status="pending",
tax_year=tax_year
)
db.add(event)
db.commit()
crud.log_action(db, "upload_admin_doc", user_id=current_user.id, target=f"admin_doc:{record.id}", detail=f"for user {user_id}")
# Background task 1: AI summary generation
def _generate_summary(doc_id: int, file_path: str, label: str):
summary = summarize_admin_document(str(file_path), label)
if summary:
_db = SessionLocal()
try:
doc = _db.query(AdminDocument).filter_by(id=doc_id).first()
if doc:
doc.ai_summary = summary
_db.commit()
logger.info(f"[Summary] Saved summary for admin_doc:{doc_id}")
finally:
_db.close()
# AI extraction removed as per request
background.add_task(
_generate_summary,
record.id,
str(UPLOAD_DIR / storage_key),
doc_label,
)
# Background task 2: Notify client by email
background.add_task(
@@ -334,10 +329,14 @@ async def upload_admin_document(
@router.get("/admin-documents")
def list_admin_documents(
response: Response,
user_id: int | None = None,
db: Session = Depends(get_db),
current_user=Depends(get_current_user),
):
response.headers["Cache-Control"] = "no-store"
bookmarked_doc_ids = set()
if current_user.role in [UserRole.admin, UserRole.super_admin]:
if not user_id:
raise HTTPException(status_code=400, detail="user_id is required for admin")
@@ -347,6 +346,13 @@ def list_admin_documents(
.order_by(AdminDocument.uploaded_at.desc())
.all()
)
from app.models import AdminDocumentBookmark
bookmarks = db.query(AdminDocumentBookmark).filter(
AdminDocumentBookmark.admin_id == current_user.id,
AdminDocumentBookmark.doc_kind == 'admin'
).all()
bookmarked_doc_ids = {b.doc_id for b in bookmarks}
elif current_user.role == UserRole.user:
docs = (
db.query(AdminDocument)
@@ -365,8 +371,11 @@ def list_admin_documents(
"filename": d.filename,
"storage_key": d.storage_key,
"created_at": d.uploaded_at.isoformat() + "Z" if d.uploaded_at else None,
# None = AI still processing; string = ready to show
"ai_summary": d.ai_summary,
"tax_year": d.tax_year,
"review_status": d.review_status,
"review_note": d.review_note,
"bookmarked": d.id in bookmarked_doc_ids
}
for d in docs
]
@@ -458,6 +467,7 @@ async def upload_personal_document(
storage_key=storage_key,
content_type=file.content_type,
tax_year=tax_year,
review_status="pending"
)
db.add(record)
try:
@@ -472,6 +482,22 @@ async def upload_personal_document(
"Delete the existing one to replace it."
)
db.refresh(record)
# Append uploaded event
from app.models import DocumentReviewEvent
event = DocumentReviewEvent(
doc_kind="personal",
doc_id=record.id,
owner_user_id=current_user.id,
actor_id=current_user.id,
actor_role="user",
action="uploaded",
to_status="pending",
tax_year=tax_year
)
db.add(event)
db.commit()
crud.log_action(db, "upload_personal", user_id=current_user.id, target=f"personal_doc:{record.id}", detail=file.filename)
admins = db.query(User).filter(User.role.in_([UserRole.admin, UserRole.super_admin])).all()
@@ -482,7 +508,13 @@ async def upload_personal_document(
subject="New Personal Document Uploaded — BookKeepro",
body=f"<p><strong>{current_user.email}</strong> uploaded a personal document: <strong>{file.filename}</strong> ({doc_type}).</p>",
)
background.add_task(
send_email,
to=current_user.email,
subject="Personal Document Upload Confirmation — BookKeepro",
body=f"<p>We have successfully received your personal document: <strong>{file.filename}</strong> ({doc_type}).</p>",
)
background.add_task(
process_ai_extraction,
"personal",
@@ -490,7 +522,6 @@ async def upload_personal_document(
str(UPLOAD_DIR / storage_key),
doc_type
)
return {
"id": record.id,
"filename": record.filename,
@@ -583,6 +614,7 @@ async def upload_business_document(
storage_key=storage_key,
content_type=file.content_type,
tax_year=tax_year,
review_status="pending"
)
db.add(record)
try:
@@ -596,6 +628,22 @@ async def upload_business_document(
"Delete the existing one to replace it."
)
db.refresh(record)
# Append uploaded event
from app.models import DocumentReviewEvent
event = DocumentReviewEvent(
doc_kind="business",
doc_id=record.id,
owner_user_id=current_user.id,
actor_id=current_user.id,
actor_role="user",
action="uploaded",
to_status="pending",
tax_year=tax_year
)
db.add(event)
db.commit()
crud.log_action(db, "upload_business", user_id=current_user.id, target=f"business_doc:{record.id}", detail=file.filename)
admins = db.query(User).filter(User.role.in_([UserRole.admin, UserRole.super_admin])).all()
@@ -607,6 +655,12 @@ async def upload_business_document(
body=f"<p><strong>{current_user.email}</strong> uploaded a business document: <strong>{file.filename}</strong> ({doc_type}).</p>",
)
background.add_task(
send_email,
to=current_user.email,
subject="Business Document Upload Confirmation — BookKeepro",
body=f"<p>We have successfully received your business document: <strong>{file.filename}</strong> ({doc_type}).</p>",
)
background.add_task(
process_ai_extraction,
"business",
@@ -614,7 +668,6 @@ async def upload_business_document(
str(UPLOAD_DIR / storage_key),
doc_type
)
return {
"id": record.id,
"filename": record.filename,
@@ -746,22 +799,18 @@ def update_business_extracted_data(
@router.get("/admin/users/{user_id}/documents")
def get_user_all_documents(
response: Response,
user_id: int,
tax_year: int | None = None,
tax_year: int | None = Query(None),
status: str | None = Query(None),
bookmarked: bool | None = Query(None),
db: Session = Depends(get_db),
current_user=Depends(get_current_user),
_=Depends(require_admin),
):
user = db.query(User).filter(User.id == user_id).first()
if not user:
raise HTTPException(status_code=404, detail="User not found")
# Regular admins can only view documents for user-role accounts
if current_user.role == UserRole.admin and user.role != UserRole.user:
raise HTTPException(
status_code=403,
detail="Admins can only view documents for regular user accounts"
)
response.headers["Cache-Control"] = "no-store"
user = crud.get_user_by_id(db, user_id)
assert_admin_can_access(current_user, user)
logger.info(f"Admin {current_user.id} retrieving all documents for user {user_id}")
@@ -771,15 +820,29 @@ def get_user_all_documents(
bq = db.query(BusinessDocument).filter(
BusinessDocument.user_id == user_id, BusinessDocument.deleted_at == None
)
if tax_year:
pq = pq.filter(PersonalDocument.tax_year == tax_year)
bq = bq.filter(BusinessDocument.tax_year == tax_year)
if status:
pq = pq.filter(PersonalDocument.review_status == status)
bq = bq.filter(BusinessDocument.review_status == status)
from app.models import AdminDocumentBookmark
bookmarks = db.query(AdminDocumentBookmark).filter(
AdminDocumentBookmark.admin_id == current_user.id
).all()
bookmarked_personal = {b.doc_id for b in bookmarks if b.doc_kind == 'personal'}
bookmarked_business = {b.doc_id for b in bookmarks if b.doc_kind == 'business'}
personal_docs = pq.order_by(PersonalDocument.uploaded_at.desc()).all()
business_docs = bq.order_by(BusinessDocument.uploaded_at.desc()).all()
documents = [
{
documents = []
for d in personal_docs:
is_bm = d.id in bookmarked_personal
if bookmarked and not is_bm: continue
documents.append({
"id": d.id,
"table": "personal",
"doc_type": d.doc_type,
@@ -790,10 +853,12 @@ def get_user_all_documents(
"review_status": d.review_status,
"review_note": d.review_note,
"extracted_data": d.extracted_data,
}
for d in personal_docs
] + [
{
"bookmarked": is_bm,
})
for d in business_docs:
is_bm = d.id in bookmarked_business
if bookmarked and not is_bm: continue
documents.append({
"id": d.id,
"table": "business",
"doc_type": d.business_type,
@@ -804,9 +869,8 @@ def get_user_all_documents(
"review_status": d.review_status,
"review_note": d.review_note,
"extracted_data": d.extracted_data,
}
for d in business_docs
]
"bookmarked": is_bm,
})
return {
"user": {
@@ -1113,12 +1177,12 @@ def export_user_documents_excel(
current_user=Depends(get_current_user),
_=Depends(require_admin),
):
"""Export all documents for a user as a formatted Excel (.xlsx) workbook."""
"""Export all documents for a user as separate Excel files in a ZIP archive."""
import io
import zipfile
import json as _json
from openpyxl import Workbook
from openpyxl.styles import Font, PatternFill, Alignment, Border, Side
from openpyxl.utils import get_column_letter
from openpyxl.styles import Font, PatternFill, Alignment
user = db.query(User).filter(User.id == user_id).first()
if not user:
@@ -1133,109 +1197,199 @@ def export_user_documents_excel(
BusinessDocument.user_id == user_id, BusinessDocument.deleted_at == None
).all()
# Collect all extracted_data keys to build dynamic columns
extracted_keys = set()
all_docs = []
def sanitize_val(v):
s = str(v)
if s.startswith(('=', '+', '-', '@')):
return "'" + s
return s
for d in personal_docs:
row = {
"ID": f"P-{d.id}",
"Table": "Personal",
"Doc Type": d.doc_type,
"Filename": d.filename,
"Uploaded At": d.uploaded_at.strftime("%Y-%m-%d %H:%M") if d.uploaded_at else "",
"Tax Year": d.tax_year or "",
"Review Status": (d.review_status or "").capitalize(),
"Review Note": d.review_note or "",
}
if d.extracted_data:
try:
data = _json.loads(d.extracted_data) if isinstance(d.extracted_data, str) else d.extracted_data
if isinstance(data, dict):
for k, v in data.items():
col = f"AI: {k}"
extracted_keys.add(col)
row[col] = str(v)
except Exception:
pass
all_docs.append(row)
zip_buffer = io.BytesIO()
with zipfile.ZipFile(zip_buffer, "a", zipfile.ZIP_DEFLATED, False) as zip_file:
for docs, prefix in [(personal_docs, "Personal"), (business_docs, "Business")]:
for d in docs:
wb = Workbook()
ws = wb.active
ws.title = "AI Summary"
ws.append(["Field", "Value"])
header_font = Font(bold=True, color="FFFFFF")
header_fill = PatternFill(start_color="2C7A5B", end_color="2C7A5B", fill_type="solid")
for cell in ws[1]:
cell.font = header_font
cell.fill = header_fill
for d in business_docs:
row = {
"ID": f"B-{d.id}",
"Table": "Business",
"Doc Type": d.business_type,
"Filename": d.filename,
"Uploaded At": d.uploaded_at.strftime("%Y-%m-%d %H:%M") if d.uploaded_at else "",
"Tax Year": d.tax_year or "",
"Review Status": (d.review_status or "").capitalize(),
"Review Note": d.review_note or "",
}
if d.extracted_data:
try:
data = _json.loads(d.extracted_data) if isinstance(d.extracted_data, str) else d.extracted_data
if isinstance(data, dict):
for k, v in data.items():
col = f"AI: {k}"
extracted_keys.add(col)
row[col] = str(v)
except Exception:
pass
all_docs.append(row)
ws.append(["ID", f"{prefix[0]}-{d.id}"])
doc_type_val = getattr(d, 'doc_type', getattr(d, 'business_type', 'Unknown'))
ws.append(["Type", sanitize_val(doc_type_val)])
ws.append(["Filename", sanitize_val(d.filename)])
ws.append(["Tax Year", d.tax_year or ""])
ws.append(["Status", sanitize_val(d.review_status or "")])
base_cols = ["ID", "Table", "Doc Type", "Filename", "Uploaded At", "Tax Year", "Review Status", "Review Note"]
all_cols = base_cols + sorted(list(extracted_keys))
if d.extracted_data:
ws.append([])
ws.append(["--- Extracted AI Data ---", ""])
try:
data = _json.loads(d.extracted_data) if isinstance(d.extracted_data, str) else d.extracted_data
if isinstance(data, dict):
for k, v in data.items():
val_str = str(v)
if isinstance(v, list):
val_str = "; ".join(map(str, v))
elif isinstance(v, dict):
val_str = _json.dumps(v)
ws.append([sanitize_val(str(k)), sanitize_val(val_str)])
except Exception:
ws.append(["Error parsing data", ""])
# ── Build workbook ────────────────────────────────────────────────────────
wb = Workbook()
ws = wb.active
ws.title = "Documents"
ws.column_dimensions['A'].width = 30
ws.column_dimensions['B'].width = 60
# Header style
header_fill = PatternFill(fill_type="solid", fgColor="2C7A5B")
header_font = Font(bold=True, color="FFFFFF", size=11)
center_align = Alignment(horizontal="center", vertical="center", wrap_text=True)
thin_border = Border(
left=Side(style="thin"), right=Side(style="thin"),
top=Side(style="thin"), bottom=Side(style="thin"),
excel_buffer = io.BytesIO()
wb.save(excel_buffer)
excel_buffer.seek(0)
safe_filename = d.filename.replace("/", "_").replace("\\", "_")
file_name = f"{prefix}_{d.id}_{safe_filename}.xlsx"
zip_file.writestr(file_name, excel_buffer.read())
zip_buffer.seek(0)
headers = {
"Content-Disposition": f'attachment; filename="user_{user_id}_excel_summaries.zip"',
"Content-Type": "application/zip",
}
return StreamingResponse(zip_buffer, headers=headers)
@router.get("/admin/users/{user_id}/export-zip")
def export_user_documents_zip(
user_id: int,
tax_year: int | None = None,
db: Session = Depends(get_db),
current_user=Depends(get_current_user),
_=Depends(require_admin),
):
target_user = db.query(User).filter(User.id == user_id).first()
if not target_user:
raise HTTPException(status_code=404, detail="Target user not found")
if current_user.role == UserRole.admin and target_user.role != UserRole.user:
raise HTTPException(status_code=403, detail="Admins can only export documents for regular user accounts")
pq = db.query(PersonalDocument).filter(
PersonalDocument.user_id == user_id, PersonalDocument.deleted_at == None
)
bq = db.query(BusinessDocument).filter(
BusinessDocument.user_id == user_id, BusinessDocument.deleted_at == None
)
aq = db.query(AdminDocument).filter(
AdminDocument.user_id == user_id, AdminDocument.deleted_at == None
)
# Write headers
for col_idx, col_name in enumerate(all_cols, 1):
cell = ws.cell(row=1, column=col_idx, value=col_name)
cell.fill = header_fill
cell.font = header_font
cell.alignment = center_align
cell.border = thin_border
if tax_year:
pq = pq.filter(PersonalDocument.tax_year == tax_year)
bq = bq.filter(BusinessDocument.tax_year == tax_year)
ws.row_dimensions[1].height = 30
ws.freeze_panes = "A2"
p_docs = pq.all()
b_docs = bq.all()
a_docs = aq.all()
# Alternate row fill
alt_fill = PatternFill(fill_type="solid", fgColor="EAF4EF")
import zipfile
import io
# Write data
for row_idx, doc in enumerate(all_docs, 2):
fill = alt_fill if row_idx % 2 == 0 else None
for col_idx, col_name in enumerate(all_cols, 1):
cell = ws.cell(row=row_idx, column=col_idx, value=doc.get(col_name, ""))
cell.alignment = Alignment(vertical="center", wrap_text=False)
cell.border = thin_border
if fill:
cell.fill = fill
zip_buffer = io.BytesIO()
with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zip_file:
for docs, prefix in [(p_docs, "Personal"), (b_docs, "Business"), (a_docs, "Admin")]:
for doc in docs:
if doc.storage_key:
s_key = doc.storage_key
if s_key.startswith("uploads/"):
s_key = s_key.split("uploads/")[1]
file_path = UPLOAD_DIR / s_key
if file_path.exists():
# Make sure filename is safe and unique
filename = doc.filename or "document"
zip_file.write(file_path, f"{prefix}/{doc.id}_{filename}")
# Auto-fit column widths (capped at 40)
for col_idx, col_name in enumerate(all_cols, 1):
max_len = max(len(col_name), *(len(str(doc.get(col_name, ""))) for doc in all_docs)) if all_docs else len(col_name)
ws.column_dimensions[get_column_letter(col_idx)].width = min(max_len + 3, 40)
zip_buffer.seek(0)
buf = io.BytesIO()
wb.save(buf)
buf.seek(0)
filename = f"user_{user_id}_documents_{tax_year if tax_year else 'all'}.zip"
headers = {
"Content-Disposition": f'attachment; filename="user_{user_id}_documents.xlsx"',
"Content-Type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
return StreamingResponse(
zip_buffer,
media_type="application/zip",
headers={"Content-Disposition": f"attachment; filename={filename}"}
)
# ─────────────────────────────────────────────
# Bookmarks
# ─────────────────────────────────────────────
@router.get("/bookmarks")
def get_bookmarks(
db: Session = Depends(get_db),
current_user=Depends(require_admin),
):
from app.models import AdminDocumentBookmark
bookmarks = db.query(AdminDocumentBookmark).filter_by(
admin_id=current_user.id
).all()
return {
"bookmarks": [
{"doc_kind": b.doc_kind, "doc_id": b.doc_id}
for b in bookmarks
]
}
return StreamingResponse(buf, headers=headers)
@router.put("/bookmarks/{doc_kind}/{doc_id}")
def add_bookmark(
doc_kind: str,
doc_id: int,
db: Session = Depends(get_db),
current_user=Depends(require_admin),
):
if doc_kind not in ['admin', 'personal', 'business']:
raise HTTPException(status_code=400, detail="Invalid doc_kind")
from app.models import AdminDocumentBookmark
existing = db.query(AdminDocumentBookmark).filter_by(
admin_id=current_user.id,
doc_kind=doc_kind,
doc_id=doc_id
).first()
if not existing:
bm = AdminDocumentBookmark(
admin_id=current_user.id,
doc_kind=doc_kind,
doc_id=doc_id
)
db.add(bm)
try:
db.commit()
except IntegrityError:
db.rollback()
raise HTTPException(status_code=400, detail="Bookmark failed")
return {"status": "ok", "bookmarked": True}
@router.delete("/bookmarks/{doc_kind}/{doc_id}")
def remove_bookmark(
doc_kind: str,
doc_id: int,
db: Session = Depends(get_db),
current_user=Depends(require_admin),
):
from app.models import AdminDocumentBookmark
existing = db.query(AdminDocumentBookmark).filter_by(
admin_id=current_user.id,
doc_kind=doc_kind,
doc_id=doc_id
).first()
if existing:
db.delete(existing)
db.commit()
return {"status": "ok", "bookmarked": False}