search method

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

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

Implementation

@override
FutureOr<List<PgrServiceModel>> search(
  PgrServiceSearchModel query, [
  String? userId,
]) async {
  return retryLocalCallOperation<List<PgrServiceModel>>(() async {
    final selectQuery = sql.select(sql.pgrService).join([
      leftOuterJoin(
        sql.pgrComplainant,
        sql.pgrComplainant.complaintClientReferenceId.equalsExp(
          sql.pgrService.clientReferenceId,
        ),
      ),
      leftOuterJoin(
        sql.address,
        sql.address.relatedClientReferenceId.equalsExp(
          sql.pgrService.clientReferenceId,
        ),
      ),
    ]);

    final results = await (selectQuery
          ..where(
            buildAnd(
              [
                if (userId != null)
                  sql.pgrService.auditCreatedBy.equals(userId),
                if (query.tenantId != null)
                  sql.pgrService.tenantId.equals(query.tenantId!),
                if (query.clientReferenceId != null)
                  sql.pgrService.clientReferenceId
                      .equals(query.clientReferenceId!),
                if (query.serviceRequestId != null)
                  sql.pgrService.serviceRequestId
                      .equals(query.serviceRequestId!),
                if (query.complaintAssignedTo != null) ...[
                  if (query.complaintAssignedTo ==
                      "COMPLAINTS_ASSIGNED_TO_SELF") ...[
                    sql.pgrComplainant.name.equals(query.currentUserName!),
                  ] else ...[
                    sql.pgrComplainant.name
                        .equals(query.currentUserName!)
                        .not(),
                  ],
                ],
                if (query.complaintTypeCode != null)
                  sql.pgrService.serviceCode.equals(query.complaintTypeCode!),
                if (query.locality != null)
                  sql.address.boundary.equals(query.locality!),
                if (query.complaintStatus != null)
                  if (query.complaintStatus?.isNotEmpty ?? false)
                    sql.pgrService.applicationStatus.isIn(
                        query.complaintStatus?.map((e) => e.index) ?? []),
                if (query.complainantMobileNumber != null)
                  sql.pgrComplainant.userName
                      .contains(query.complainantMobileNumber!),
                if (query.complaintNumber != null)
                  sql.pgrService.serviceRequestId
                      .contains(query.complaintNumber!),
              ],
            ),
          ))
        .get();

    return results.map((e) {
      final pgrService = e.readTable(sql.pgrService);
      final pgrComplainant = e.readTable(sql.pgrComplainant);
      final address = e.readTable(sql.address);

      return PgrServiceModel(
        clientReferenceId: pgrService.clientReferenceId,
        tenantId: pgrService.tenantId,
        serviceCode: pgrService.serviceCode,
        description: pgrService.description,
        applicationStatus: pgrService.applicationStatus,
        user: PgrComplainantModel(
          complaintClientReferenceId:
              pgrComplainant.complaintClientReferenceId,
          clientReferenceId: pgrComplainant.clientReferenceId,
          tenantId: pgrComplainant.tenantId,
          name: pgrComplainant.name,
          mobileNumber: pgrComplainant.mobileNumber,
          emailId: pgrComplainant.emailId,
          type: pgrComplainant.type,
          auditDetails: AuditDetails(
            createdBy: pgrComplainant.auditCreatedBy!,
            createdTime: pgrComplainant.auditCreatedTime!,
            lastModifiedBy: pgrComplainant.auditModifiedBy,
            lastModifiedTime: pgrComplainant.auditModifiedTime,
          ),
          id: pgrComplainant.id,
          uuid: pgrComplainant.uuid,
        ),
        address: PgrAddressModel(
          relatedClientReferenceId: pgrService.clientReferenceId,
          tenantId: address.tenantId,
          doorNo: address.doorNo,
          buildingName: address.addressLine1,
          street: address.addressLine2,
          locality: LocalityModel(
            name: address.localityBoundaryName ?? "",
            code: address.localityBoundaryCode ?? "",
          ),

          /// boundary: address.boundary,
          geoLocation: GeoLocation(
            latitude: address.latitude,
            longitude: address.longitude,
          ),
          landmark: address.landmark,
          city: address.city,
          pincode: address.pincode,
          rowVersion: address.rowVersion,
        ),
        id: pgrService.id,
        auditDetails: AuditDetails(
          createdBy: pgrService.auditCreatedBy!,
          createdTime: pgrService.auditCreatedTime!,
          lastModifiedBy: pgrService.auditModifiedBy,
          lastModifiedTime: pgrService.auditModifiedTime,
        ),
        accountId: pgrService.accountId,
        serviceRequestId: pgrService.serviceRequestId,
        active: pgrService.active,
        source: pgrService.source,
        additionalDetail: pgrService.additionalFields,
      );
    }).toList();
  });
}