Model/repositories/uploaded_file/uploaded_file_postgres_repository.py
Khalim Conn-Kowlessar 135dd9abda Fetch all uploaded files for a set of properties by landlord_property_id 🟩
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-08 09:23:47 +00:00

40 lines
1.6 KiB
Python

from __future__ import annotations
from typing import Optional, Sequence
from sqlalchemy import select
from sqlmodel import Session, col
from infrastructure.postgres.uploaded_file_table import FileTypeEnum, UploadedFile
class UploadedFilePostgresRepository:
def __init__(self, session: Session) -> None:
self._session = session
def get_latest_by_hubspot_deal_id(
self, hubspot_deal_id: str, file_type: FileTypeEnum
) -> Optional[UploadedFile]:
stmt = (
select(UploadedFile)
.where(col(UploadedFile.hubspot_deal_id) == hubspot_deal_id)
.where(col(UploadedFile.file_type) == file_type.value)
.order_by(col(UploadedFile.s3_upload_timestamp).desc())
.limit(1)
)
return self._session.execute(stmt).scalars().one_or_none() # pyright: ignore[reportDeprecated]
def by_landlord_property_ids(
self, landlord_property_ids: Sequence[str]
) -> list[UploadedFile]:
"""Every uploaded file held for the given properties, matched by
`landlord_property_id` (ADR-0060), across all Document Types. Returns
the raw rows — latest-per-Document-Type selection and null-type
skipping are the Download Package plan's job, not the query's."""
stmt = select(UploadedFile).where(
col(UploadedFile.landlord_property_id).in_(list(landlord_property_ids))
)
return list(self._session.execute(stmt).scalars().all()) # pyright: ignore[reportDeprecated]
def insert(self, uploaded_file: UploadedFile) -> None:
self._session.add(uploaded_file)