search method

  1. @override
FutureOr<List<HFReferralModel>> search(
  1. HFReferralSearchModel query, [
  2. String? userId
])
override

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

Implementation

@override
FutureOr<List<HFReferralModel>> search(
  HFReferralSearchModel query, [
  String? userId,
]) async {
  return retryLocalCallOperation(() async {
    final selectQuery = sql.select(sql.hFReferral).join([]);

    final results = await (selectQuery
          ..where(buildAnd([
            if (query.clientReferenceId != null)
              sql.hFReferral.clientReferenceId.isIn(
                query.clientReferenceId!,
              ),
            if (query.name != null)
              sql.hFReferral.name.contains(
                query.name!,
              ),
            if (query.beneficiaryId != null)
              sql.hFReferral.beneficiaryId.isIn(
                query.beneficiaryId!,
              ),
            if (query.projectId != null)
              sql.hFReferral.projectId.isIn(
                query.projectId!,
              ),
            if (userId != null)
              sql.hFReferral.auditCreatedBy.equals(
                userId,
              ),
          ])))
        .get();

    return results
        .map((e) {
          final referral = e.readTableOrNull(sql.hFReferral);
          if (referral == null) return null;
          final additionalData = referral.additionalFields != null
              ? jsonDecode(referral.additionalFields!)
              : null;
          List<Map<String, dynamic>> data =
              List<Map<String, dynamic>>.from(additionalData['fields']);
          final HFReferralAdditionalFields additionalFields =
              HFReferralAdditionalFields(
            version: 1,
            fields: [
              if (getValueByKey(
                    data,
                    ReferralReconAdditionalFields.gender.toValue(),
                  ) !=
                  null)
                AdditionalField(
                  ReferralReconAdditionalFields.gender.toValue(),
                  getValueByKey(
                    data,
                    ReferralReconAdditionalFields.gender.toValue(),
                  ),
                ),
              if (getValueByKey(
                    data,
                    ReferralReconAdditionalFields.hFCoordinator.toValue(),
                  ) !=
                  null)
                AdditionalField(
                  ReferralReconAdditionalFields.hFCoordinator.toValue(),
                  getValueByKey(
                    data,
                    ReferralReconAdditionalFields.hFCoordinator.toValue(),
                  ),
                ),
              if (getValueByKey(
                    data,
                    ReferralReconAdditionalFields.referredBy.toValue(),
                  ) !=
                  null)
                AdditionalField(
                  ReferralReconAdditionalFields.referredBy.toValue(),
                  getValueByKey(
                    data,
                    ReferralReconAdditionalFields.referredBy.toValue(),
                  ),
                ),
              if (getValueByKey(
                    data,
                    ReferralReconAdditionalFields.dateOfEvaluation.toValue(),
                  ) !=
                  null)
                AdditionalField(
                  ReferralReconAdditionalFields.dateOfEvaluation.toValue(),
                  getValueByKey(
                    data,
                    ReferralReconAdditionalFields.dateOfEvaluation.toValue(),
                  ),
                ),
              if (getValueByKey(
                    data,
                    ReferralReconAdditionalFields.nameOfReferral.toValue(),
                  ) !=
                  null)
                AdditionalField(
                  ReferralReconAdditionalFields.nameOfReferral.toValue(),
                  getValueByKey(
                    data,
                    ReferralReconAdditionalFields.nameOfReferral.toValue(),
                  ),
                ),
              if (getValueByKey(
                    data,
                    ReferralReconAdditionalFields.age.toValue(),
                  ) !=
                  null)
                AdditionalField(
                  ReferralReconAdditionalFields.age.toValue(),
                  getValueByKey(
                    data,
                    ReferralReconAdditionalFields.age.toValue(),
                  ),
                ),
              if (getValueByKey(
                    data,
                    ReferralReconAdditionalFields.cycle.toValue(),
                  ) !=
                  null)
                AdditionalField(
                  ReferralReconAdditionalFields.cycle.toValue(),
                  getValueByKey(
                    data,
                    ReferralReconAdditionalFields.cycle.toValue(),
                  ),
                ),
            ],
          );

          return HFReferralModel(
            id: referral.id,
            clientReferenceId: referral.clientReferenceId,
            rowVersion: referral.rowVersion,
            tenantId: referral.tenantId,
            name: referral.name,
            projectId: referral.projectId,
            projectFacilityId: referral.projectFacilityId,
            symptom: referral.symptom,
            symptomSurveyId: referral.symptomSurveyId,
            beneficiaryId: referral.beneficiaryId,
            referralCode: referral.referralCode,
            nationalLevelId: referral.nationalLevelId,
            isDeleted: referral.isDeleted,
            additionalFields: additionalFields,
            auditDetails: AuditDetails(
              createdBy: referral.auditCreatedBy!,
              createdTime: referral.auditCreatedTime!,
              lastModifiedBy: referral.auditModifiedBy,
              lastModifiedTime: referral.auditModifiedTime,
            ),
            clientAuditDetails: referral.clientCreatedTime == null ||
                    referral.clientCreatedBy == null
                ? null
                : ClientAuditDetails(
                    createdTime: referral.clientCreatedTime!,
                    createdBy: referral.clientCreatedBy!,
                    lastModifiedBy: referral.clientModifiedBy,
                    lastModifiedTime: referral.clientModifiedTime,
                  ),
          );
        })
        .whereNotNull()
        .where((element) => element.isDeleted != true)
        .toList();
  });
}