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
+28
View File
@@ -362,3 +362,31 @@ def invalidate_artifacts(
stmt = stmt.where(DocumentArtifact.format == _validate_segment(format, "format"))
result = db.execute(stmt)
return int(result.rowcount or 0)
# ---------------------------------------------------------------------------
# Cleanup helper (for failed transactions)
# ---------------------------------------------------------------------------
def remove_artifact_file(artifact: DocumentArtifact) -> bool:
"""
Delete the physical file of a single artifact row.
Narrowly scoped cleanup for a failed transaction that created an artifact
file but could not commit. Accepts only a DocumentArtifact row (never an
arbitrary path). Resolves through the safe path resolver. Never touches
other artifacts. Returns True if a file was removed.
"""
if artifact is None or not artifact.storage_key:
return False
try:
path = resolve_storage_path(artifact.storage_key)
except ArtifactPathError:
return False
try:
if path.is_file():
path.unlink()
return True
except OSError:
return False
return False
+36
View File
@@ -147,6 +147,15 @@ def save_draft(db: Session, project_id: int, number: str, document_date: date, c
price = _decimal(item.unit_price)
document.items.append(KS2Item(work_item_id=item_id, quantity=quantity,
unit_price=price, amount=price * quantity))
# Successful draft change invalidates existing draft artifacts (same tx).
if ks2_id is not None:
from app.services.document_artifact_service import invalidate_artifacts
invalidate_artifacts(
db,
project_id=project_id,
document_type="KS2",
document_id=document.id,
)
db.commit()
db.refresh(document)
return document
@@ -157,6 +166,7 @@ def save_draft(db: Session, project_id: int, number: str, document_date: date, c
def issue_ks2(db: Session, project_id: int, ks2_id: int):
"""Revalidate current balances, attribute quantity to DR lines oldest-first, and issue atomically."""
artifact = None
try:
document = db.query(KS2).filter(KS2.id == ks2_id, KS2.project_id == project_id).with_for_update().first()
if document is None:
@@ -206,11 +216,37 @@ def issue_ks2(db: Session, project_id: int, ks2_id: int):
f"Не удалось распределить весь объём по источникам Daily Reports для позиции {line.work_item.name}"
)
document.status = "issued"
# Generate and persist the official artifact inside this transaction.
from app.exporters.excel.ks2_renderer import (
TEMPLATE_TYPE as KS2_TEMPLATE_TYPE,
TEMPLATE_VERSION as KS2_TEMPLATE_VERSION,
render_ks2_excel,
)
from app.exporters.ks2_dto import build_ks2_export_dto
from app.services.document_artifact_service import persist_artifact, remove_artifact_file
dto = build_ks2_export_dto(db, document)
content = render_ks2_excel(dto)
artifact = persist_artifact(
db,
project_id=project_id,
document_type="KS2",
document_id=document.id,
format="xlsx",
template_type=KS2_TEMPLATE_TYPE,
template_version=KS2_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
+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