search method

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

Implementation

@override
FutureOr<List<AttendanceRegisterModel>> search(
  AttendanceRegisterSearchModel query, [
  String? userId,
]) async {
  return retryLocalCallOperation<List<AttendanceRegisterModel>>(() async {
    var attendanceRegisterQuery = sql.select(sql.attendanceRegister).join([]);

    if (query.limit != null && query.offSet != null) {
      attendanceRegisterQuery.limit(query.limit!, offset: query.offSet);
    }

    attendanceRegisterQuery = attendanceRegisterQuery
      ..where(buildAnd([
        if (query.id != null) sql.attendanceRegister.id.equals(query.id!),
        if (query.referenceId != null)
          sql.attendanceRegister.referenceId.equals(query.referenceId!),
      ]));

    final attendanceRegisterResults = await attendanceRegisterQuery.get();
    final registerIds = attendanceRegisterResults.map((e) {
      return e.readTable(sql.attendanceRegister).id!;
    }).toList();

    var selectQuery = sql.select(sql.attendanceRegister).join([
      leftOuterJoin(
        sql.staff,
        sql.staff.registerId.equalsExp(sql.attendanceRegister.id),
      ),
      leftOuterJoin(
        sql.attendee,
        sql.attendee.registerId.equalsExp(sql.attendanceRegister.id),
      ),
    ]);

    selectQuery = selectQuery
      ..where(sql.attendanceRegister.id.isIn(registerIds));

    selectQuery = selectQuery
      ..where(buildAnd([
        if (query.id != null)
          sql.attendanceRegister.id.equals(
            query.id!,
          ),
        if (query.staffId != null)
          sql.staff.userId.equals(
            query.staffId!,
          ),
        if (query.referenceId != null)
          sql.attendanceRegister.referenceId.equals(
            query.referenceId!,
          ),
      ]));

    final results = await selectQuery.get();

    final registerMap = <String, AttendanceRegisterModel>{};

    for (final e in results) {
      final register = e.readTableOrNull(sql.attendanceRegister);
      final staffs = e.readTableOrNull(sql.staff);
      final attendees = e.readTableOrNull(sql.attendee);
      if (register == null) continue;

      if (registerMap.containsKey(register.id)) {
        registerMap[register.id]!.attendees?.add(
              AttendeeModel(
                id: attendees?.id.toString(),
                registerId: attendees?.registerId,
                tenantId: attendees?.tenantId,
                individualId: attendees?.individualId,
                enrollmentDate: attendees?.enrollmentDate,
                denrollmentDate: attendees?.denrollmentDate,
              ),
            );

        registerMap[register.id]!.staff?.add(
              StaffModel(
                id: staffs?.id,
                registerId: staffs?.registerId,
                tenantId: register.tenantId,
                userId: staffs?.userId,
                enrollmentDate: staffs?.enrollmentDate,
                denrollmentDate: staffs?.denrollmentDate,
              ),
            );
      } else {
        registerMap[register.id.toString()] = AttendanceRegisterModel(
          id: register.id.toString(),
          attendees: attendees == null
              ? null
              : [
                  AttendeeModel(
                    id: attendees.id.toString(),
                    registerId: attendees.registerId,
                    tenantId: attendees.tenantId,
                    individualId: attendees.individualId,
                    enrollmentDate: attendees.enrollmentDate,
                    denrollmentDate: attendees.denrollmentDate,
                  ),
                ],
          staff: staffs == null
              ? null
              : [
                  StaffModel(
                    id: staffs.id,
                    registerId: staffs.registerId,
                    tenantId: register.tenantId,
                    userId: staffs.userId,
                    enrollmentDate: staffs.enrollmentDate,
                    denrollmentDate: staffs.denrollmentDate,
                  ),
                ],
          name: register.name,
          registerNumber: register.registerNumber,
          tenantId: register.tenantId,
          referenceId: register.referenceId,
          serviceCode: register.serviceCode,
          status: register.status,
          startDate: register.startDate,
          endDate: register.endDate,
          additionalDetails: register.additionalFields == null
              ? null
              : jsonDecode(register.additionalFields!.toString()),
          auditDetails: AuditDetails(
            createdBy: register.auditCreatedBy ?? '',
            createdTime: register.auditCreatedTime ?? 0,
          ),
        );
      }
    }
    final uniqueTasks = registerMap.values.toList();

    return uniqueTasks.where((element) => element.isDeleted != true).toList();
  });
}