"""Persist Builder entries into the resume document.""" from __future__ import annotations from typing import Any from ..fsm import FSMError from ..resume_document_core import find_entry, new_id, normalize_document from .constants import SECTION_HEADINGS def save_entry(resume_content: dict[str, Any], section_kind: str, entry: dict[str, Any], *, entry_id: str | None) -> dict[str, Any]: content = normalize_document(resume_content) if entry_id: found = find_entry(content, entry_id) if found is None: raise FSMError("builder_entry_not_found", "The selected resume entry no longer exists", status_code=409) _, existing = found entry["id"] = existing["id"] existing.clear() existing.update(entry) return content sections = content.setdefault("sections", []) section = next((item for item in sections if item.get("kind") == section_kind), None) if section is None: section = {"id": new_id("sec"), "kind": section_kind, "heading": SECTION_HEADINGS.get(section_kind, section_kind), "items": []} sections.append(section) entry["id"] = entry.get("id") or new_id("entry") section.setdefault("items", []).append(entry) return content