|
| 1 | +"""Operations on the repository for the Family entity.""" |
| 2 | + |
| 3 | +import logging |
| 4 | +from datetime import datetime |
| 5 | +from typing import Optional, Tuple, cast |
| 6 | + |
| 7 | +from sqlalchemy import or_ |
| 8 | +from sqlalchemy.orm import Query, Session |
| 9 | +from sqlalchemy.exc import NoResultFound |
| 10 | +from sqlalchemy_utils import escape_like |
| 11 | + |
| 12 | +from app.clients.db.models.law_policy import ( |
| 13 | + EventStatus, |
| 14 | + FamilyEvent, |
| 15 | + Family, |
| 16 | + FamilyDocument, |
| 17 | +) |
| 18 | +from app.model.event import EventReadDTO |
| 19 | + |
| 20 | + |
| 21 | +_LOGGER = logging.getLogger(__name__) |
| 22 | + |
| 23 | +FamilyEventTuple = Tuple[FamilyEvent, Family, FamilyDocument] |
| 24 | + |
| 25 | + |
| 26 | +def _get_query(db: Session) -> Query: |
| 27 | + # NOTE: SqlAlchemy will make a complete hash of the query generation |
| 28 | + # if columns are used in the query() call. Therefore, entire |
| 29 | + # objects are returned. |
| 30 | + return ( |
| 31 | + db.query(FamilyEvent, Family, FamilyDocument) |
| 32 | + .filter(FamilyEvent.family_import_id == Family.import_id) |
| 33 | + .join( |
| 34 | + FamilyDocument, |
| 35 | + FamilyDocument.family_import_id == FamilyEvent.family_document_import_id, |
| 36 | + isouter=True, |
| 37 | + ) |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | +def _event_to_dto(db: Session, family_event_meta: FamilyEventTuple) -> EventReadDTO: |
| 42 | + family_event = family_event_meta[0] |
| 43 | + |
| 44 | + family_document_import_id = ( |
| 45 | + None |
| 46 | + if family_event.family_document_import_id is None |
| 47 | + else cast(str, family_event.family_document_import_id) |
| 48 | + ) |
| 49 | + |
| 50 | + return EventReadDTO( |
| 51 | + import_id=cast(str, family_event.import_id), |
| 52 | + event_title=cast(str, family_event.title), |
| 53 | + date=cast(datetime, family_event.date), |
| 54 | + family_import_id=cast(str, family_event.family_import_id), |
| 55 | + family_document_import_id=family_document_import_id, |
| 56 | + event_type_value=cast(str, family_event.event_type_name), |
| 57 | + event_status=cast(EventStatus, family_event.status), |
| 58 | + ) |
| 59 | + |
| 60 | + |
| 61 | +def all(db: Session) -> list[EventReadDTO]: |
| 62 | + """ |
| 63 | + Returns all family events. |
| 64 | +
|
| 65 | + :param db Session: The database connection. |
| 66 | + :return Optional[EventReadDTO]: All family events in the database. |
| 67 | + """ |
| 68 | + family_event_metas = _get_query(db).all() |
| 69 | + |
| 70 | + if not family_event_metas: |
| 71 | + return [] |
| 72 | + |
| 73 | + result = [_event_to_dto(db, event_meta) for event_meta in family_event_metas] |
| 74 | + return result |
| 75 | + |
| 76 | + |
| 77 | +def get(db: Session, import_id: str) -> Optional[EventReadDTO]: |
| 78 | + """ |
| 79 | + Gets a single family event from the repository. |
| 80 | +
|
| 81 | + :param db Session: The database connection. |
| 82 | + :param str import_id: The import_id of the event. |
| 83 | + :return Optional[EventReadDTO]: A single family event or nothing. |
| 84 | + """ |
| 85 | + try: |
| 86 | + family_event_meta = ( |
| 87 | + _get_query(db).filter(FamilyEvent.import_id == import_id).one() |
| 88 | + ) |
| 89 | + except NoResultFound as e: |
| 90 | + _LOGGER.error(e) |
| 91 | + return |
| 92 | + |
| 93 | + return _event_to_dto(db, family_event_meta) |
| 94 | + |
| 95 | + |
| 96 | +def search(db: Session, search_term: str) -> Optional[list[EventReadDTO]]: |
| 97 | + """ |
| 98 | + Get family events matching a search term on the event title or type. |
| 99 | +
|
| 100 | + :param db Session: The database connection. |
| 101 | + :param str search_term: Any search term to filter on the event title |
| 102 | + or event type name. |
| 103 | + :return Optional[list[EventReadDTO]]: A list of matching family |
| 104 | + events or none. |
| 105 | + """ |
| 106 | + term = f"%{escape_like(search_term)}%" |
| 107 | + search = or_(FamilyEvent.title.ilike(term), FamilyEvent.event_type_name.ilike(term)) |
| 108 | + |
| 109 | + try: |
| 110 | + found = _get_query(db).filter(search).all() |
| 111 | + except NoResultFound as e: |
| 112 | + _LOGGER.error(e) |
| 113 | + return |
| 114 | + |
| 115 | + if not found: |
| 116 | + return [] |
| 117 | + |
| 118 | + return [_event_to_dto(db, f) for f in found] |
| 119 | + |
| 120 | + |
| 121 | +def count(db: Session) -> Optional[int]: |
| 122 | + """ |
| 123 | + Counts the number of family events in the repository. |
| 124 | +
|
| 125 | + :param db Session: The database connection. |
| 126 | + :return Optional[int]: The number of family events in the repository |
| 127 | + or nothing. |
| 128 | + """ |
| 129 | + try: |
| 130 | + n_events = _get_query(db).count() |
| 131 | + except NoResultFound as e: |
| 132 | + _LOGGER.error(e) |
| 133 | + return |
| 134 | + |
| 135 | + return n_events |
0 commit comments