createDocumentVersion method

  1. @override
Future<DocumentVersion> createDocumentVersion(
  1. String documentId, {
  2. String status = 'draft',
  3. String? changeLog,
})
override

Creates a new version snapshot for a document at the current state.

documentId - The ID of the document to create a version for status - The initial status (default: 'draft') changeLog - Optional description of what changed

Returns the created DocumentVersion with its assigned ID.

Note: Version data is stored as CRDT operations, not in the version itself. Use getDocumentVersionData() to retrieve the data for a version.

Throws DeskDataSourceException if the operation fails. Throws DeskAuthenticationException if authentication is required.

Implementation

@override
Future<DocumentVersion> createDocumentVersion(
  String documentId, {
  String status = 'draft',
  String? changeLog,
}) async {
  final versionId = _genVersionId();
  final existingVersions = _versions[documentId]?.values ?? [];
  final maxVersion = existingVersions.isEmpty
      ? 0
      : existingVersions
            .map((v) => v.versionNumber)
            .reduce((a, b) => a > b ? a : b);

  final parsedStatus = DocumentVersionStatus.values.firstWhere(
    (s) => s.name == status,
    orElse: () => DocumentVersionStatus.draft,
  );

  final version = DocumentVersion(
    id: versionId,
    documentId: documentId,
    versionNumber: maxVersion + 1,
    status: parsedStatus,
    changeLog: changeLog,
    createdAt: DateTime.now(),
  );

  _versions.putIfAbsent(documentId, () => {});
  _versions[documentId]![versionId] = version;
  _versionData[versionId] = {};

  return version;
}