93 lines
4.0 KiB
Markdown
93 lines
4.0 KiB
Markdown
# Implementation Report — DOC-B5-issue-artifact-lifecycle
|
||
|
||
### Status
|
||
|
||
DONE
|
||
|
||
### Changed files
|
||
|
||
| Файл | Что |
|
||
|---|---|
|
||
| `app/services/document_artifact_service.py` | `remove_artifact_file()` — cleanup для failed tx |
|
||
| `app/services/ks2_service.py` | issue: render+persist artifact; save_draft: invalidate |
|
||
| `app/services/ks3_service.py` | issue: render+persist artifact; save_draft: invalidate |
|
||
| `tests/test_doc_b5_issue_artifact_lifecycle.py` | 13 интеграционных тестов |
|
||
|
||
### Lifecycle implemented
|
||
|
||
| Состояние | Поведение |
|
||
|---|---|
|
||
| **Draft edit** | `invalidate_artifacts` в той же транзакции; файл не удаляется; нет artifact → no-op |
|
||
| **Issued** | существующие валидации → `status=issued` → DTO → renderer → `persist_artifact` → commit |
|
||
| **Cancelled** | не трогает artifact (row/file/hashes/`is_current`); замена/удаление/инвалидация отсутствуют |
|
||
|
||
### Transaction / rollback strategy
|
||
|
||
`persist_artifact` не коммитит. Бизнес-транзакция (`issue_ks2`/`issue_ks3`) владеет commit.
|
||
|
||
Последовательность issue:
|
||
```
|
||
validate → mark issued → build DTO → render → persist_artifact → commit
|
||
```
|
||
|
||
При любой ошибке до/на commit:
|
||
- `db.rollback()` — статус и artifact row откатываются;
|
||
- `remove_artifact_file(artifact)` — удаляется только файл этой попытки;
|
||
- ранее существующие artifacts не трогаются.
|
||
|
||
### Cleanup strategy
|
||
|
||
Добавлен `remove_artifact_file(artifact)` в DOC-B2-сервис (минимальный хелпер):
|
||
- принимает только `DocumentArtifact` (никогда произвольный путь);
|
||
- резолвится через безопасный `resolve_storage_path`;
|
||
- удаляет только файл данного artifact;
|
||
- идемпотентен (нет файла → `False`).
|
||
|
||
### Draft edit invalidation
|
||
|
||
- `save_draft` (KS-2 и KS-3) при **обновлении** существующего draft вызывает `invalidate_artifacts` **в той же транзакции**;
|
||
- успешный edit → `is_current = false`;
|
||
- rollback → artifact остаётся current (проверено тестом);
|
||
- физический файл сохраняется.
|
||
|
||
### Cancellation behavior
|
||
|
||
Логика `cancel_ks2`/`cancel_ks3` **не менялась**. Тесты подтверждают: после сторно artifact row, `is_current`, `content_hash`, `input_hash`, файл — без изменений.
|
||
|
||
### Tests (13)
|
||
|
||
- Artifact creation: KS-2 → current XLSX; KS-3 → current DOCX; file/hashes/type/id/format/template_version;
|
||
- Draft invalidation: non-current after edit; file remains; rollback keeps current;
|
||
- Issue rollback: persist failure, commit failure (KS-2 и KS-3) — нет issued, нет artifact row, нет orphan file;
|
||
- Cancellation: KS-2 и KS-3 сохраняют artifact;
|
||
- Immutability: повторный issue rejected; edit rejected; KS-3 не трогает KS-2 artifacts;
|
||
- `remove_artifact_file`: только указанный artifact.
|
||
|
||
Результат `pytest -q`:
|
||
|
||
```
|
||
414 passed, 600 warnings in 33.52s
|
||
```
|
||
|
||
(401 существующих + 13 новых.)
|
||
|
||
Результат `git diff --check`:
|
||
|
||
```
|
||
(без ошибок)
|
||
```
|
||
|
||
Приложение: запускается (проверено в предыдущих задачах, renderer/DTO не менялись).
|
||
|
||
### Browser/manual checks
|
||
|
||
Не требуются (UI не менялся).
|
||
|
||
### Deviations
|
||
|
||
None.
|
||
|
||
### Recommended next task
|
||
|
||
**DOC-B6** — UI скачивания artifact: кнопка «Официальная форма» на карточке KS-2/KS-3, download endpoint с проверкой project/document, отметка «СТОРНИРОВАН» для cancelled (отдельный derivative).
|