createDocument method

  1. @override
Future<DeskDocument> createDocument(
  1. String documentType,
  2. String title,
  3. Map<String, dynamic> data, {
  4. String? slug,
  5. bool isDefault = false,
})
override

Creates a new document with an initial version.

documentType - The type of document to create title - The document title data - The initial version data as a map slug - Optional URL-friendly slug isDefault - Whether this is the default document for this type

Returns the created DeskDocument with its assigned ID.

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

Implementation

@override
Future<DeskDocument> createDocument(
  String documentType,
  String title,
  Map<String, dynamic> data, {
  String? slug,
  bool isDefault = false,
}) async {
  final docId = _genDocId();
  final versionId = _genVersionId();
  final now = DateTime.now();

  // Determine effective isDefault: auto-assign if this is the first doc for this type
  final isFirstForType = !_documents.values.any(
    (d) => d.documentType == documentType,
  );
  final effectiveIsDefault = isDefault || isFirstForType;

  final doc = DeskDocument(
    id: docId,
    clientId: 'mock-client-1',
    documentType: documentType,
    title: title,
    slug: slug ?? _generateSlug(title),
    isDefault: effectiveIsDefault,
    activeVersionData: data,
    createdAt: now,
    updatedAt: now,
  );

  _documents[docId] = doc;
  _versions[docId] = {
    versionId: DocumentVersion(
      id: versionId,
      documentId: docId,
      versionNumber: 1,
      status: DocumentVersionStatus.draft,
      changeLog: 'Initial version',
      createdAt: now,
    ),
  };
  _versionData[versionId] = Map<String, dynamic>.from(data);

  return doc;
}