feat: persist issued document artifacts and invalidate draft artifacts on edit

This commit is contained in:
Арсентьев Александр
2026-09-29 18:17:36 +03:00
parent 1917bc6513
commit c1be111e1a
6 changed files with 1181 additions and 0 deletions
+37
View File
@@ -123,6 +123,15 @@ def save_draft(db: Session, project_id: int, number: str, document_date: date,
for ks2_id in seen_ks2:
document.links.append(KS3KS2(ks2_id=ks2_id))
# Successful draft change invalidates existing draft artifacts (same tx).
if ks3_id is not None:
from app.services.document_artifact_service import invalidate_artifacts
invalidate_artifacts(
db,
project_id=project_id,
document_type="KS3",
document_id=document.id,
)
db.commit()
db.refresh(document)
return document
@@ -133,6 +142,7 @@ def save_draft(db: Session, project_id: int, number: str, document_date: date,
def issue_ks3(db: Session, project_id: int, ks3_id: int):
"""Issue a KS-3 draft atomically."""
artifact = None
try:
document = db.query(KS3).options(
joinedload(KS3.links).joinedload(KS3KS2.ks2).joinedload(KS2.items)
@@ -153,11 +163,38 @@ def issue_ks3(db: Session, project_id: int, ks3_id: int):
raise KS3ValidationError(f"КС-2 №{ks2.number} не проведён")
document.status = "issued"
# Generate and persist the official artifact inside this transaction.
# KS-3 issue must NOT touch KS-2 artifacts.
from app.exporters.ks3_dto import build_ks3_export_dto
from app.exporters.word.ks3_renderer import (
TEMPLATE_TYPE as KS3_TEMPLATE_TYPE,
TEMPLATE_VERSION as KS3_TEMPLATE_VERSION,
render_ks3_word,
)
from app.services.document_artifact_service import persist_artifact, remove_artifact_file
dto = build_ks3_export_dto(db, document)
content = render_ks3_word(dto)
artifact = persist_artifact(
db,
project_id=project_id,
document_type="KS3",
document_id=document.id,
format="docx",
template_type=KS3_TEMPLATE_TYPE,
template_version=KS3_TEMPLATE_VERSION,
content=content,
dto=dto,
)
db.commit()
db.refresh(document)
return document
except Exception:
db.rollback()
if artifact is not None:
remove_artifact_file(artifact)
raise