getPendingUpSync method

Future<List<OpLogEntry<T>>> getPendingUpSync(
  1. DataModelType type, {
  2. required String createdBy,
})

Implementation

Future<List<OpLogEntry<T>>> getPendingUpSync(
  DataModelType type, {
  required String createdBy,
}) async {
  final createOpLogs = isar.opLogs
      .filter()
      .entityTypeEqualTo(type)
      .operationEqualTo(DataOperation.create)
      .serverGeneratedIdIsNull()
      .syncedUpEqualTo(false)
      .syncedDownEqualTo(false)
      .createdByEqualTo(createdBy)
      .findAllSync();

  final singleCreateOpLogs = isar.opLogs
      .filter()
      .entityTypeEqualTo(type)
      .operationEqualTo(DataOperation.singleCreate)
      .serverGeneratedIdIsNull()
      .syncedUpEqualTo(false)
      .syncedDownEqualTo(false)
      .createdByEqualTo(createdBy)
      .findAllSync();

  final updateOpLogs = isar.opLogs
      .filter()
      .entityTypeEqualTo(type)
      .operationEqualTo(DataOperation.update)
      .serverGeneratedIdIsNotNull()
      .nonRecoverableErrorEqualTo(false)
      .syncedUpEqualTo(false)
      .syncedDownEqualTo(false)
      .createdByEqualTo(createdBy)
      .findAllSync();

  final errorOpLogs = isar.opLogs
      .filter()
      .entityTypeEqualTo(type)
      .syncedDownEqualTo(false)
      .nonRecoverableErrorEqualTo(true)
      .createdByEqualTo(createdBy)
      .findAllSync();

  final deleteOpLogs = isar.opLogs
      .filter()
      .entityTypeEqualTo(type)
      .operationEqualTo(DataOperation.delete)
      .serverGeneratedIdIsNotNull()
      .syncedUpEqualTo(false)
      .syncedDownEqualTo(false)
      .createdByEqualTo(createdBy)
      .findAllSync();

  final nonRecoverableOpLogs = isar.opLogs
      .filter()
      .entityTypeEqualTo(type)
      .syncedUpEqualTo(true)
      .syncedDownEqualTo(false)
      .nonRecoverableErrorEqualTo(false)
      .syncDownRetryCountGreaterThan(
        DigitDataModelSingleton().syncDownRetryCount - 1,
      )
      .createdByEqualTo(createdBy)
      .findAllSync();

  var entries = [
    createOpLogs,
    updateOpLogs,
    deleteOpLogs,
    singleCreateOpLogs,
    errorOpLogs,
    nonRecoverableOpLogs,
  ].expand((element) => element);

  entries = entries.sortedBy((element) => element.createdAt);

  final groupedEntries = entries.groupListsBy(
    (element) => element.clientReferenceId,
  );

  final entriesForUpSync = groupedEntries.entries
      .map<OpLog?>((entry) {
        if (entry.key == null) return null;
        if (entry.value.isEmpty) return null;

        return entry.value.first;
      })
      .whereNotNull()
      .toList();

  return entriesForUpSync.map((e) => OpLogEntry.fromOpLog<T>(e)).toList();
}