publishCurrentVersion method
Atomically publishes the document's current draft as a new version.
The server snapshots the current CRDT HLC, creates a new version row with status=published, and upserts the published_documents row — all in a single transaction.
documentId - The ID of the document to publish
Returns the newly created DocumentVersion with published status.
Throws DeskDataSourceException if the operation fails. Throws DeskAuthenticationException if authentication is required.
Implementation
@override
Future<DocumentVersion> publishCurrentVersion(String documentId) async {
// Snapshot the current draft data BEFORE creating the published version.
// This mirrors the backend transaction: the published_documents row is
// written with the data as it exists at this exact moment.
final doc = _documents[documentId];
final currentData = Map<String, dynamic>.from(doc?.activeVersionData ?? {});
// Create a new version snapshot and immediately publish it.
final version = await createDocumentVersion(documentId);
// Copy the current active data into the new version's data store so that
// getDocumentVersionData returns the published snapshot correctly.
_versionData[version.id!] = Map<String, dynamic>.from(currentData);
final published = _updateVersionStatus(
version.id!,
DocumentVersionStatus.published,
);
// Freeze the public-read snapshot (mirrors published_documents table).
// This is intentionally a separate copy: future draft edits via
// updateDocumentData must NOT mutate this store.
_publishedDataStore[documentId] = Map<String, dynamic>.from(currentData);
// Stamp snapshotHlc so hasUnpublishedChanges can detect the publish boundary.
final hlc = doc?.crdtHlc ?? DateTime.now().microsecondsSinceEpoch.toString();
for (final docVersions in _versions.values) {
if (docVersions.containsKey(published!.id)) {
docVersions[published.id!] = published.copyWith(snapshotHlc: hlc);
return docVersions[published.id!]!;
}
}
return published!;
}