feat: persist issued document artifacts and invalidate draft artifacts on edit
This commit is contained in:
@@ -0,0 +1,463 @@
|
||||
# DOC-B5 — issue integration and document artifact lifecycle
|
||||
|
||||
## Goal
|
||||
|
||||
Integrate `DocumentArtifact` into the existing KS-2 / KS-3 lifecycle.
|
||||
|
||||
After this task:
|
||||
|
||||
- draft documents remain editable;
|
||||
- if a draft artifact was generated earlier, changing the draft invalidates that artifact;
|
||||
- issuing KS-2 or KS-3 generates the official document exactly once and persists it as an immutable artifact;
|
||||
- transaction failure after artifact creation does not leave an orphan physical file;
|
||||
- cancellation of an issued KS-2/KS-3 does not replace, delete, or invalidate the original issued artifact;
|
||||
- existing KS-2 / KS-3 business rules remain unchanged.
|
||||
|
||||
This task is about service/lifecycle integration. UI for artifact download and document management is **not** part of this task.
|
||||
|
||||
---
|
||||
|
||||
## 1. Read before changing code
|
||||
|
||||
Study the current implementation of:
|
||||
|
||||
- `app/services/document_artifact_service.py`
|
||||
- `app/models/document_artifact.py`
|
||||
- `app/exporters/ks2_dto.py`
|
||||
- `app/exporters/excel/ks2_renderer.py`
|
||||
- `app/exporters/ks3_dto.py`
|
||||
- `app/exporters/word/ks3_renderer.py`
|
||||
- existing KS-2 service/router/tests
|
||||
- existing KS-3 service/router/tests
|
||||
- existing draft edit flows
|
||||
- existing cancellation/stornation flows
|
||||
|
||||
Do not replace the existing lifecycle with a parallel implementation.
|
||||
|
||||
Preserve existing public APIs unless a change is genuinely required for integration.
|
||||
|
||||
---
|
||||
|
||||
# 2. Artifact lifecycle
|
||||
|
||||
Use the following lifecycle as the source of truth.
|
||||
|
||||
### Draft
|
||||
|
||||
A draft artifact may exist because an existing export/download flow generated one.
|
||||
|
||||
When the draft document changes:
|
||||
|
||||
- invalidate existing artifacts for that document;
|
||||
- do not delete the physical file;
|
||||
- do not create a new artifact automatically;
|
||||
- if no artifact exists, this is a no-op.
|
||||
|
||||
Invalidation must participate in the same DB transaction as the draft edit.
|
||||
|
||||
Therefore:
|
||||
|
||||
- successful edit → artifact becomes non-current;
|
||||
- rolled-back edit → previous artifact remains current.
|
||||
|
||||
### Issued
|
||||
|
||||
When KS-2 or KS-3 is issued:
|
||||
|
||||
1. perform all existing issue validations;
|
||||
2. change the document status to `issued`;
|
||||
3. build the existing export DTO from the issued document;
|
||||
4. render using the existing renderer/template;
|
||||
5. persist the resulting file through `document_artifact_service`;
|
||||
6. commit the transaction.
|
||||
|
||||
The artifact created for an issued document is the official immutable artifact.
|
||||
|
||||
Do not regenerate or replace it on subsequent reads/downloads.
|
||||
|
||||
Do not introduce a second renderer.
|
||||
|
||||
### Cancelled
|
||||
|
||||
Cancellation must:
|
||||
|
||||
- leave the issued artifact row untouched;
|
||||
- leave the physical file untouched;
|
||||
- leave its `content_hash` and `input_hash` untouched;
|
||||
- not invalidate it;
|
||||
- not silently generate a replacement artifact.
|
||||
|
||||
The original issued artifact therefore remains available as the historical official document.
|
||||
|
||||
Generation of a separate visible `СТОРНИРОВАН` derivative is **out of scope for DOC-B5**.
|
||||
|
||||
---
|
||||
|
||||
# 3. Transaction and rollback requirements
|
||||
|
||||
`persist_artifact()` from DOC-B2 deliberately does not commit.
|
||||
|
||||
The business transaction owns the commit.
|
||||
|
||||
The integration must handle the important failure case:
|
||||
|
||||
```text
|
||||
issue document
|
||||
↓
|
||||
render
|
||||
↓
|
||||
persist artifact
|
||||
↓
|
||||
DB commit
|
||||
```
|
||||
|
||||
If anything fails before commit:
|
||||
|
||||
- document status must roll back;
|
||||
- artifact DB row must roll back;
|
||||
- previous artifact state must roll back;
|
||||
- newly created physical artifact file must be removed.
|
||||
|
||||
If `commit()` itself fails:
|
||||
|
||||
- perform DB rollback as appropriate;
|
||||
- remove the physical file created by this issue attempt;
|
||||
- do not remove any previously existing artifact.
|
||||
|
||||
Do not leave an orphan artifact file after a failed issue transaction.
|
||||
|
||||
### Support code
|
||||
|
||||
If the current DOC-B2 service has no safe primitive for deleting a newly-created, not-yet-committed artifact file, add the smallest narrowly scoped helper required by DOC-B5.
|
||||
|
||||
It must:
|
||||
|
||||
- accept only a server-generated artifact/storage key or artifact object;
|
||||
- resolve through the existing safe path resolver;
|
||||
- never accept an arbitrary filesystem path;
|
||||
- never delete another artifact;
|
||||
- be usable only for cleanup of the current failed transaction.
|
||||
|
||||
Do not redesign DOC-B2.
|
||||
|
||||
---
|
||||
|
||||
# 4. Existing current-artifact behavior
|
||||
|
||||
`persist_artifact()` currently demotes an existing current artifact and inserts a new current artifact.
|
||||
|
||||
That behavior is acceptable for issuing a draft document once.
|
||||
|
||||
However, an already-issued document must never be issued again or receive another replacement artifact.
|
||||
|
||||
Therefore:
|
||||
|
||||
- rely on the existing lifecycle rule that an issued document cannot be issued again;
|
||||
- do not add a second issuance path;
|
||||
- verify this with tests.
|
||||
|
||||
If a draft artifact existed before issue, it may become historical (`is_current = false`) when the issued artifact is persisted.
|
||||
|
||||
The newly issued artifact must be the current artifact for that document/format.
|
||||
|
||||
---
|
||||
|
||||
# 5. KS-2 integration
|
||||
|
||||
Integrate with the existing KS-2 issue service.
|
||||
|
||||
The issue operation must remain atomic.
|
||||
|
||||
Expected sequence:
|
||||
|
||||
```text
|
||||
validate existing KS-2 issue rules
|
||||
→ mark KS-2 issued
|
||||
→ build KS-2 export DTO
|
||||
→ render Excel through existing KS-2 renderer
|
||||
→ persist artifact
|
||||
→ commit
|
||||
```
|
||||
|
||||
The artifact must contain:
|
||||
|
||||
- `project_id`;
|
||||
- `document_type = KS2`;
|
||||
- `document_id = KS2.id`;
|
||||
- `format = xlsx`;
|
||||
- current KS-2 template type/version;
|
||||
- content hash of rendered bytes;
|
||||
- input hash based on the issued DTO + template type/version;
|
||||
- `is_current = true`.
|
||||
|
||||
The renderer must not persist anything itself.
|
||||
|
||||
---
|
||||
|
||||
# 6. KS-3 integration
|
||||
|
||||
Integrate with the existing KS-3 issue service.
|
||||
|
||||
Do not change existing KS-3 business rules:
|
||||
|
||||
- only eligible/issued KS-2 documents;
|
||||
- no duplicate KS-2 in the same KS-3;
|
||||
- server-side totals;
|
||||
- atomic issue.
|
||||
|
||||
After successful existing validation:
|
||||
|
||||
```text
|
||||
mark KS-3 issued
|
||||
→ build KS-3 export DTO
|
||||
→ render Word through existing KS-3 renderer
|
||||
→ persist artifact
|
||||
→ commit
|
||||
```
|
||||
|
||||
Artifact:
|
||||
|
||||
- `document_type = KS3`;
|
||||
- `document_id = KS3.id`;
|
||||
- `format = docx`;
|
||||
- current KS-3 template type/version;
|
||||
- content hash of rendered bytes;
|
||||
- input hash based on the issued DTO + template type/version;
|
||||
- `is_current = true`.
|
||||
|
||||
Issuing KS-3 must **not regenerate or replace the artifacts of the KS-2 documents included in it**.
|
||||
|
||||
---
|
||||
|
||||
# 7. Draft edit invalidation
|
||||
|
||||
Find the existing edit/update flows for KS-2 and KS-3.
|
||||
|
||||
When a draft is successfully changed, invalidate all artifacts belonging to that document.
|
||||
|
||||
Recommended call:
|
||||
|
||||
```python
|
||||
invalidate_artifacts(
|
||||
db,
|
||||
document_type="KS2",
|
||||
document_id=ks2.id,
|
||||
)
|
||||
```
|
||||
|
||||
and similarly for KS-3.
|
||||
|
||||
Do this inside the same transaction as the edit.
|
||||
|
||||
Do not delete files.
|
||||
|
||||
Do not invalidate issued documents because issued documents are not editable.
|
||||
|
||||
Tests must verify rollback behavior.
|
||||
|
||||
---
|
||||
|
||||
# 8. Cancellation verification
|
||||
|
||||
Do not rewrite existing cancellation logic.
|
||||
|
||||
Add integration tests proving that cancellation:
|
||||
|
||||
### KS-2
|
||||
|
||||
- changes document status to cancelled;
|
||||
- keeps the issued artifact DB row;
|
||||
- keeps `is_current = true`;
|
||||
- keeps physical file;
|
||||
- keeps content hash;
|
||||
- keeps input hash.
|
||||
|
||||
### KS-3
|
||||
|
||||
Same requirements.
|
||||
|
||||
Existing integrity rules for cancellation must continue to work.
|
||||
|
||||
---
|
||||
|
||||
# 9. Failure handling
|
||||
|
||||
At minimum test:
|
||||
|
||||
### KS-2
|
||||
|
||||
1. renderer failure;
|
||||
2. artifact persistence failure;
|
||||
3. DB commit failure after artifact creation.
|
||||
|
||||
For each:
|
||||
|
||||
- KS-2 is not left issued;
|
||||
- no committed artifact row exists for the failed attempt;
|
||||
- no newly-created physical artifact file remains.
|
||||
|
||||
### KS-3
|
||||
|
||||
Repeat the same three failure classes.
|
||||
|
||||
Do not weaken existing transaction semantics merely to make the artifact tests pass.
|
||||
|
||||
---
|
||||
|
||||
# 10. Idempotency / repeated issue
|
||||
|
||||
Verify:
|
||||
|
||||
- draft can be issued once;
|
||||
- second attempt to issue an already-issued document is rejected by the existing lifecycle;
|
||||
- second attempt does not create another issued artifact;
|
||||
- existing issued artifact remains unchanged.
|
||||
|
||||
Also verify that reading/downloading an issued document does not regenerate the artifact.
|
||||
|
||||
Do not implement automatic regeneration for issued documents.
|
||||
|
||||
---
|
||||
|
||||
# 11. Hash verification
|
||||
|
||||
For a successfully issued document:
|
||||
|
||||
- `content_hash` must equal SHA-256 of the stored physical file;
|
||||
- `input_hash` must equal the hash calculated from the same issued DTO + template type/version used for generation.
|
||||
|
||||
Use the existing DOC-B2 canonicalization/hash functions.
|
||||
|
||||
Do not duplicate hash algorithms.
|
||||
|
||||
---
|
||||
|
||||
# 12. Storage
|
||||
|
||||
Use the DOC-B2 storage service.
|
||||
|
||||
Do not build document paths directly in KS-2/KS-3 services.
|
||||
|
||||
Expected physical layout remains:
|
||||
|
||||
```text
|
||||
<storage_root>/
|
||||
projects/
|
||||
<project_id>/
|
||||
documents/
|
||||
KS2/
|
||||
<ks2_id>/
|
||||
...
|
||||
KS3/
|
||||
<ks3_id>/
|
||||
...
|
||||
```
|
||||
|
||||
The exact filename remains the responsibility of the artifact service.
|
||||
|
||||
---
|
||||
|
||||
# 13. No scope creep
|
||||
|
||||
Do NOT implement in this task:
|
||||
|
||||
- new UI for artifact download;
|
||||
- new document detail pages;
|
||||
- new artifact list UI;
|
||||
- cancelled derivative generation;
|
||||
- PDF;
|
||||
- new templates;
|
||||
- historical snapshots of business data;
|
||||
- database reset integration;
|
||||
- project export/import integration;
|
||||
- changes to KS-2/KS-3 business calculations;
|
||||
- changes to cancellation business rules.
|
||||
|
||||
These are separate tasks.
|
||||
|
||||
---
|
||||
|
||||
# 14. Tests
|
||||
|
||||
Add focused integration tests.
|
||||
|
||||
Minimum recommended coverage:
|
||||
|
||||
### Artifact creation
|
||||
|
||||
- issue KS-2 → one current XLSX artifact;
|
||||
- issue KS-3 → one current DOCX artifact;
|
||||
- file exists;
|
||||
- hashes are correct;
|
||||
- correct document type/id/format/template version.
|
||||
|
||||
### Draft invalidation
|
||||
|
||||
- existing draft artifact becomes non-current after draft edit;
|
||||
- physical file remains;
|
||||
- edit rollback keeps artifact current.
|
||||
|
||||
### Issue rollback
|
||||
|
||||
- renderer failure;
|
||||
- persist failure;
|
||||
- commit failure;
|
||||
- no orphan file;
|
||||
- no issued state;
|
||||
- no committed artifact from failed attempt.
|
||||
|
||||
### Cancellation
|
||||
|
||||
- issued KS-2 cancellation preserves artifact;
|
||||
- issued KS-3 cancellation preserves artifact.
|
||||
|
||||
### Immutability
|
||||
|
||||
- issued document cannot be edited;
|
||||
- repeated issue is rejected;
|
||||
- no second issued artifact is created;
|
||||
- KS-3 issue does not touch KS-2 artifacts.
|
||||
|
||||
### Regression
|
||||
|
||||
Run the complete existing test suite.
|
||||
|
||||
---
|
||||
|
||||
# 15. Acceptance criteria
|
||||
|
||||
Task is DONE when:
|
||||
|
||||
- [ ] KS-2 issue creates and persists an immutable official XLSX artifact.
|
||||
- [ ] KS-3 issue creates and persists an immutable official DOCX artifact.
|
||||
- [ ] Issue transaction includes artifact persistence.
|
||||
- [ ] Failed issue leaves no orphan physical artifact file.
|
||||
- [ ] Draft edit invalidates existing artifact in the same transaction.
|
||||
- [ ] Draft edit rollback restores previous artifact state.
|
||||
- [ ] Cancellation does not invalidate/delete/replace the issued artifact.
|
||||
- [ ] Issued documents cannot be edited or issued again.
|
||||
- [ ] KS-3 issue does not regenerate KS-2 artifacts.
|
||||
- [ ] Hashes are verified.
|
||||
- [ ] Existing business rules remain intact.
|
||||
- [ ] Full test suite passes.
|
||||
- [ ] `git diff --check` is clean.
|
||||
- [ ] Application starts normally.
|
||||
|
||||
---
|
||||
|
||||
# 16. Implementation report
|
||||
|
||||
After implementation, report:
|
||||
|
||||
1. changed files;
|
||||
2. exact lifecycle implemented;
|
||||
3. transaction/rollback strategy;
|
||||
4. cleanup strategy for failed artifact creation;
|
||||
5. draft invalidation behavior;
|
||||
6. cancellation behavior;
|
||||
7. tests added and total test count;
|
||||
8. browser/manual checks if relevant;
|
||||
9. deviations from this task, if any;
|
||||
10. recommended next task.
|
||||
|
||||
Do not modify unrelated parts of the application.
|
||||
Reference in New Issue
Block a user