sanitizeCloudDataForUse function

Map<String, dynamic> sanitizeCloudDataForUse(
  1. Map<String, dynamic> data, {
  2. required String docId,
})

Returns a sanitized version of the given data to be used in the SDK and variables. Updates the SDKConstants.createdAt and SDKConstants.updatedAt fields with the current time.

Implementation

Map<String, dynamic> sanitizeCloudDataForUse(
  Map<String, dynamic> data, {
  required String docId,
}) {
  if (data.isEmpty) return data;
  // breaks reference and allows to modify the data.
  data = {...data};

  // Late because it can potentially be unused;
  late final DateTime now = DateTime.now();

  data[SDKConstants.createdAt] =
      deserializeCosmicValue(data[SDKConstants.createdAt] ?? now);
  data[SDKConstants.updatedAt] =
      deserializeCosmicValue(data[SDKConstants.updatedAt] ?? now);
  data[SDKConstants.id] = docId;

  // Sort private fields to the bottom.
  for (final field in privateDocumentFields) {
    if (data.containsKey(field)) {
      final value = data.remove(field);
      data[field] = value;
    }
  }

  return data;
}