search method

The search method searches for entities that match the given query.

Implementation

@override
FutureOr<List<HouseholdMemberModel>> search(
  HouseholdMemberSearchModel query, [
  String? userId,
]) async {
  return retryLocalCallOperation<List<HouseholdMemberModel>>(() async {
    final selectQuery = sql.select(sql.householdMember).join([]);
    final results = await (selectQuery
          ..where(
            buildAnd(
              [
                if (query.householdClientReferenceIds != null)
                  sql.householdMember.householdClientReferenceId.isIn(
                    query.householdClientReferenceIds!,
                  ),
                if (query.individualClientReferenceIds != null)
                  sql.householdMember.individualClientReferenceId.isIn(
                    query.individualClientReferenceIds!,
                  ),
                if (query.householdClientReferenceId != null)
                  sql.householdMember.householdClientReferenceId.isIn(
                    query.householdClientReferenceId!,
                  ),
                if (query.individualClientReferenceId != null)
                  sql.householdMember.individualClientReferenceId.isIn(
                    query.individualClientReferenceId!,
                  ),
                if (query.householdId != null)
                  sql.householdMember.householdId.isIn(
                    query.householdId!,
                  ),
                if (query.individualId != null)
                  sql.householdMember.individualId.isIn(
                    query.individualId!,
                  ),
                if (query.isHeadOfHousehold != null)
                  sql.householdMember.isHeadOfHousehold.equals(
                    query.isHeadOfHousehold!,
                  ),
                if (userId != null)
                  sql.householdMember.auditCreatedBy.equals(
                    userId,
                  ),
              ],
            ),
          ))
        .get();

    return results
        .map((e) {
          final householdMember = e.readTable(sql.householdMember);

          return HouseholdMemberModel(
            id: householdMember.id,
            householdId: householdMember.householdId,
            householdClientReferenceId:
                householdMember.householdClientReferenceId,
            individualId: householdMember.individualId,
            individualClientReferenceId:
                householdMember.individualClientReferenceId,
            isHeadOfHousehold: householdMember.isHeadOfHousehold,
            isDeleted: householdMember.isDeleted,
            tenantId: householdMember.tenantId,
            rowVersion: householdMember.rowVersion,
            auditDetails: (householdMember.auditCreatedBy != null &&
                    householdMember.auditCreatedTime != null)
                ? AuditDetails(
                    createdBy: householdMember.auditCreatedBy!,
                    createdTime: householdMember.auditCreatedTime!,
                    lastModifiedBy: householdMember.auditModifiedBy,
                    lastModifiedTime: householdMember.auditModifiedTime,
                  )
                : null,
            clientAuditDetails: (householdMember.clientCreatedBy != null &&
                    householdMember.clientCreatedTime != null)
                ? ClientAuditDetails(
                    createdBy: householdMember.clientCreatedBy!,
                    createdTime: householdMember.clientCreatedTime!,
                    lastModifiedBy: householdMember.clientModifiedBy,
                    lastModifiedTime: householdMember.clientModifiedTime,
                  )
                : null,
            clientReferenceId: householdMember.clientReferenceId,
          );
        })
        .where((element) => element.isDeleted != true)
        .toList();
  });
}