individualGlobalSearch method

dynamic individualGlobalSearch(
  1. GlobalSearchParameters params
)

Implementation

individualGlobalSearch(GlobalSearchParameters params) async {
  dynamic selectQuery;
  late int? count = params.totalCount == 0 ? 0 : params.totalCount;

  // Check if the filter contains status for registered or not registered
  if (params.filter!.contains(Status.registered.name) ||
      params.filter!.contains(Status.notRegistered.name)) {
    var proximitySelectQuery =
        await proximitySearch(selectQuery, params, super.sql);

    var nameSelectQuery =
        await nameSearch(proximitySelectQuery, params, super.sql);

    var filterSelectQuery = nameSelectQuery;

    if (params.filter != null && params.filter!.isNotEmpty) {
      for (var filter in params.filter!) {
        filterSelectQuery =
            await filterSearch(filterSelectQuery, params, filter, super.sql);
      }
    } else {
      filterSelectQuery = nameSelectQuery;
    }

    if (filterSelectQuery == null) {
      return [];
    }
    // Get total count if offset is zero and filters are applied
    else {
      if (params.offset == 0 &&
          params.filter != null &&
          params.filter!.isNotEmpty) {
        count = await _getTotalCount(filterSelectQuery, params, super.sql);
      }

      await filterSelectQuery.limit(params.limit ?? 50,
          offset: params.offset ?? 0);

      final results = await filterSelectQuery.get();

      return _returnIndividualModel(results, count);
    }
  } else if (params.filter!.isNotEmpty && params.filter != null) {
    var proximitySelectQuery =
        await proximitySearch(selectQuery, params, super.sql);

    var nameSelectQuery =
        await nameSearch(proximitySelectQuery, params, super.sql);

    var filterSelectQuery = nameSelectQuery;

    // Apply filters if present
    if (params.filter != null && params.filter!.isNotEmpty) {
      for (var filter in params.filter!) {
        filterSelectQuery =
            await filterSearch(filterSelectQuery, params, filter, super.sql);
      }
    } else {
      filterSelectQuery = nameSelectQuery;
    }

    // Return empty list if no results found
    if (filterSelectQuery == null) {
      return [];
    } else {
      if (params.offset == 0 &&
          params.filter != null &&
          params.filter!.isNotEmpty) {
        count = await _getTotalCount(filterSelectQuery, params, super.sql);
      }
      await filterSelectQuery.limit(params.limit ?? 50,
          offset: params.offset ?? 0);

      final results = await filterSelectQuery.get();
      var data = results
          .map((e) {
            final task = e.readTableOrNull(sql.task);
            final resources = e.readTableOrNull(sql.taskResource);

            return TaskModel(
              id: task.id,
              createdBy: task.createdBy,
              clientReferenceId: task.clientReferenceId,
              rowVersion: task.rowVersion,
              tenantId: task.tenantId,
              isDeleted: task.isDeleted,
              projectId: task.projectId,
              projectBeneficiaryId: task.projectBeneficiaryId,
              projectBeneficiaryClientReferenceId:
                  task.projectBeneficiaryClientReferenceId,
              createdDate: task.createdDate,
              status: task.status,
              resources: resources == null
                  ? null
                  : [
                      TaskResourceModel(
                        taskclientReferenceId:
                            resources.taskclientReferenceId,
                        clientReferenceId: resources.clientReferenceId,
                        id: resources.id,
                        productVariantId: resources.productVariantId,
                        taskId: resources.taskId,
                        deliveryComment: resources.deliveryComment,
                        quantity: resources.quantity,
                        rowVersion: resources.rowVersion,
                      ),
                    ],
            );
          })
          .where((element) => element.isDeleted != true)
          .toList();

      return {"data": data, "total_count": count};
    }
  } else {
    var proximitySelectQuery =
        await proximitySearch(selectQuery, params, super.sql);

    var nameSelectQuery =
        await nameSearch(proximitySelectQuery, params, super.sql);

    // Return empty list if no results found
    if (nameSelectQuery == null) {
      return [];
    } else {
      // Get total count if offset is zero and filters are applied
      if (params.offset == 0 &&
          params.filter != null &&
          params.filter!.isNotEmpty) {
        count = await _getTotalCount(nameSelectQuery, params, super.sql);
      }
      await nameSelectQuery.limit(params.limit ?? 50,
          offset: params.offset ?? 0);

      final results = await nameSelectQuery.get();

      return _returnIndividualModel(results, count);
    }
  }
}