Commit 08b88fed authored by Jan Reimes's avatar Jan Reimes
Browse files

♻️ refactor(database): enhance insert_sample_meetings function

- Improve the insert_sample_meetings function to use the database API
- Ensure consistency with MeetingMetadata model and validation
- Add detailed argument descriptions for better clarity
parent cad6f8de
Loading
Loading
Loading
Loading
+77 −28
Original line number Diff line number Diff line
@@ -171,39 +171,88 @@ def populated_db(test_db_path: Path, sample_meetings: list[dict], sample_tdocs:


def insert_sample_meetings(database: TDocDatabase, meetings: list[dict]) -> None:
    """Insert sample meetings into database.
    """Insert sample meetings into database using the proper database API.

    This function uses db.upsert_meeting() instead of raw SQL to ensure
    consistency with the MeetingMetadata model and database layer validation.

    Args:
        database: Database connection
        meetings: List of meeting dictionaries
        meetings: List of meeting dictionaries with keys:
            - meeting_id: Integer meeting ID
            - tbid: Technical body ID (working group)
            - subtb: Sub-technical body ID (subworking group)
            - short_name: Short meeting name (e.g., "RAN1#98")
            - start_date: Start date as ISO string (YYYY-MM-DD)
            - end_date: End date as ISO string (YYYY-MM-DD)
            - location: Meeting location
            - files_url: URL to meeting files
    """
    from datetime import datetime
    from datetime import date

    for meeting in meetings:
        cursor = database.connection.cursor()
        now = datetime.now().isoformat()
        cursor.execute(
            """
            INSERT INTO meetings (
                meeting_id, tbid, subtb, short_name, start_date, end_date,
                location, files_url, created_at, updated_at
            )
            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
            """,
            (
                meeting["meeting_id"],
                meeting["tbid"],
                meeting["subtb"],
                meeting["short_name"],
                meeting["start_date"],
                meeting["end_date"],
                meeting["location"],
                meeting["files_url"],
                now,
                now,
            ),
    from tdoc_crawler.models import MeetingMetadata, WorkingGroup

    # Map tbid to WorkingGroup enum
    tbid_to_wg = {
        373: WorkingGroup.RAN,  # RAN
        375: WorkingGroup.SA,  # SA
        649: WorkingGroup.CT,  # CT
    }

    # Map subtb to subgroup name
    subtb_to_sg = {
        373: "RP",  # RAN Plenary
        379: "R1",  # RAN1
        380: "R2",  # RAN2
        381: "R3",  # RAN3
        382: "R4",  # RAN4
        657: "R5",  # RAN5
        843: "R6",  # RAN6
        375: "SP",  # SA Plenary
        384: "S1",  # SA1
        385: "S2",  # SA2
        386: "S3",  # SA3
        387: "S4",  # SA4
        388: "S5",  # SA5
        825: "S6",  # SA6
        649: "CP",  # CT Plenary
        651: "C1",  # CT1
        652: "C2",  # CT2
        653: "C3",  # CT3
        654: "C4",  # CT4
        655: "C5",  # CT5
        656: "C6",  # CT6
    }

    for meeting_dict in meetings:
        # Parse date strings to date objects
        start_date_str = meeting_dict.get("start_date")
        end_date_str = meeting_dict.get("end_date")

        start_date = date.fromisoformat(start_date_str) if start_date_str else None
        end_date = date.fromisoformat(end_date_str) if end_date_str else None

        # Create MeetingMetadata using the database API
        tbid = meeting_dict["tbid"]
        subtb = meeting_dict["subtb"]

        meeting_metadata = MeetingMetadata(
            meeting_id=meeting_dict["meeting_id"],
            tbid=tbid,
            subtb=subtb,
            working_group=tbid_to_wg.get(tbid, WorkingGroup.RAN),
            subgroup=subtb_to_sg.get(subtb),
            short_name=meeting_dict["short_name"],
            title=None,
            start_date=start_date,
            end_date=end_date,
            location=meeting_dict.get("location"),
            files_url=meeting_dict.get("files_url"),
            portal_url=None,
        )
    database.connection.commit()

        # Use the proper database API instead of raw SQL
        database.upsert_meeting(meeting_metadata)


if __name__ == "__main__":