Files
pfm-ocr/lib/features/editor/document_save_action.dart
T
fhanyuh caf8e98378 chore: normalize line endings (CRLF -> LF)
No content changes: git diff --ignore-all-space over these files is empty.
The churn came from editing on Windows against a repo checked out with LF.
2026-08-27 10:40:49 +07:00

54 lines
2.4 KiB
Dart

import '../../models/document_model.dart';
enum DocumentSaveActionKind { putExisting, reuploadThenPut, blocked }
/// Decides how an editor's save flow should reach the server, given whether
/// a real server-assigned document is already resolved. Deliberately
/// separated from the Dio call sites (`editor_screen.dart`,
/// `product_editor_logic.dart`) so the guard against PUTting to a
/// fabricated client-side ID - a millisecond timestamp that can never match
/// the int4 `documents.id` (gap G5, `docs/api-contract-map.md`) - is
/// unit-testable without mocking network I/O, mirroring the pure-decision
/// pattern used by `poll_outcome.dart` (task 6.1).
class DocumentSaveAction {
final DocumentSaveActionKind kind;
final String? serverId;
final String? blockedMessage;
const DocumentSaveAction._(this.kind, {this.serverId, this.blockedMessage});
factory DocumentSaveAction.putExisting(String serverId) =>
DocumentSaveAction._(DocumentSaveActionKind.putExisting, serverId: serverId);
factory DocumentSaveAction.reuploadThenPut() =>
const DocumentSaveAction._(DocumentSaveActionKind.reuploadThenPut);
factory DocumentSaveAction.blocked(String message) =>
DocumentSaveAction._(DocumentSaveActionKind.blocked, blockedMessage: message);
}
/// [existingDocument] is the server document already resolved from a
/// completed upload+parse (e.g. `PendingDocument.document`) - when present,
/// its real id is used for the PUT. When absent (e.g. the editor route lost
/// its `pendingId` on process-death restoration), [localImagePath] - the
/// pending item's originally-captured photo - lets the save flow recover a
/// real server id by re-uploading it (safe: the backend dedups by
/// `file_hash`) before PUTting the corrected fields. If neither is
/// available there is nothing to save against, so saving is blocked with an
/// explicit message instead of PUTting to a made-up ID that can never match
/// a real document.
DocumentSaveAction resolveDocumentSaveAction({
required DocumentModel? existingDocument,
required String localImagePath,
}) {
if (existingDocument != null) {
return DocumentSaveAction.putExisting(existingDocument.id);
}
if (localImagePath.isEmpty) {
return DocumentSaveAction.blocked(
'Tidak dapat menyimpan: dokumen asli tidak ditemukan di perangkat. Silakan pindai ulang.',
);
}
return DocumentSaveAction.reuploadThenPut();
}