searchHouseHoldByIndividual method

FutureOr<List<IndividualModel>> searchHouseHoldByIndividual(
  1. AddressSearchModel query, [
  2. String? userId
])

Implementation

FutureOr<List<IndividualModel>> searchHouseHoldByIndividual(
  AddressSearchModel query, [
  String? userId,
]) async {
  return retryLocalCallOperation<List<IndividualModel>>(() async {
    final selectQuery = sql.select(sql.address).join(
      [
        leftOuterJoin(
          sql.individual,
          sql.individual.clientReferenceId.equalsExp(
            sql.address.relatedClientReferenceId,
          ),
        ),
      ],
    );

    (selectQuery
          ..where(buildAnd([
            sql.address.relatedClientReferenceId.isNotNull(),
            sql.individual.clientReferenceId.isNotNull(),
            if (query.latitude != null &&
                query.longitude != null &&
                query.maxRadius != null)
              CustomExpression<bool>('''
        (6371393 * acos(
            cos(${query.latitude! * math.pi / 180.0}) * cos((address.latitude * ${math.pi / 180.0}))
            * cos((address.longitude * ${math.pi / 180.0}) - ${query.longitude! * math.pi / 180.0})
            + sin(${query.latitude! * math.pi / 180.0}) * sin((address.latitude * ${math.pi / 180.0}))
        )) <= ${query.maxRadius!}
    '''),
            if (query.latitude != null &&
                query.longitude != null &&
                query.maxRadius != null)
              sql.address.longitude.isNotNull(),
            sql.address.latitude.isNotNull(),
          ])))
        .orderBy([
      if (query.latitude != null &&
          query.longitude != null &&
          query.maxRadius != null)
        OrderingTerm(
          expression: CustomExpression<double>('''
          (6371393 * acos(
              cos(${query.latitude! * math.pi / 180.0}) * cos((address.latitude * ${math.pi / 180.0}))
              * cos((address.longitude * ${math.pi / 180.0}) - ${query.longitude! * math.pi / 180.0})
              + sin(${query.latitude! * math.pi / 180.0}) * sin((address.latitude * ${math.pi / 180.0}))
          ))
        '''),
          mode: OrderingMode.asc,
        ),
    ]);

    selectQuery.limit(query.limit ?? 50, offset: query.offset ?? 0);

    final results = await selectQuery.get();

    return results
        .map((e) {
          final individual = e.readTableOrNull(sql.individual);
          final address = e.readTableOrNull(sql.address);
          final name = e.readTableOrNull(sql.name);

          return IndividualModel(
            id: individual?.id,
            tenantId: individual?.tenantId,
            clientReferenceId: individual!.clientReferenceId,
            dateOfBirth: individual.dateOfBirth,
            name: NameModel(
              givenName: name?.givenName,
              individualClientReferenceId: individual.clientReferenceId,
              tenantId: individual.tenantId,
              auditDetails: AuditDetails(
                createdBy: individual.auditCreatedBy!,
                createdTime: individual.auditCreatedTime!,
                lastModifiedBy: individual.auditModifiedBy,
                lastModifiedTime: individual.auditModifiedTime,
              ),
            ),
            rowVersion: individual.rowVersion,
            isDeleted: individual.isDeleted,
            auditDetails: AuditDetails(
              createdBy: individual.auditCreatedBy!,
              createdTime: individual.auditCreatedTime!,
              lastModifiedBy: individual.auditModifiedBy,
              lastModifiedTime: individual.auditModifiedTime,
            ),
            address: address == null
                ? null
                : [
                    AddressModel(
                      id: address.id,
                      relatedClientReferenceId:
                          address.relatedClientReferenceId,
                      tenantId: address.tenantId,
                      doorNo: address.doorNo,
                      latitude: address.latitude,
                      longitude: address.longitude,
                      landmark: address.landmark,
                      locationAccuracy: address.locationAccuracy,
                      addressLine1: address.addressLine1,
                      addressLine2: address.addressLine2,
                      city: address.city,
                      pincode: address.pincode,
                      locality: address.localityBoundaryCode != null
                          ? LocalityModel(
                              code: address.localityBoundaryCode!,
                              name: address.localityBoundaryName,
                            )
                          : null,
                      type: address.type,
                      rowVersion: address.rowVersion,
                      auditDetails: AuditDetails(
                        createdBy: individual.auditCreatedBy!,
                        createdTime: individual.auditCreatedTime!,
                        lastModifiedBy: individual.auditModifiedBy,
                        lastModifiedTime: individual.auditModifiedTime,
                      ),
                    ),
                  ],
          );
        })
        .where((element) => element.isDeleted != true)
        .toList();
  });
}