You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
	
	
		
			147 lines
		
	
	
		
			6.2 KiB
		
	
	
	
		
			Dart
		
	
			
		
		
	
	
			147 lines
		
	
	
		
			6.2 KiB
		
	
	
	
		
			Dart
		
	
import 'dart:convert';
 | 
						|
 | 
						|
import 'package:test_sa/api/api_client.dart';
 | 
						|
import 'package:test_sa/api/user_api_client.dart';
 | 
						|
import 'package:test_sa/models/issue.dart';
 | 
						|
import 'package:test_sa/models/lookup.dart';
 | 
						|
import 'package:test_sa/models/service_report.dart';
 | 
						|
import 'package:test_sa/models/service_request/service_request.dart';
 | 
						|
import 'package:test_sa/models/service_request/service_request_search.dart';
 | 
						|
import 'package:test_sa/models/timer_model.dart';
 | 
						|
 | 
						|
import '../controllers/api_routes/urls.dart';
 | 
						|
 | 
						|
class ServiceRequestApiClient {
 | 
						|
  static final ServiceRequestApiClient _instance = ServiceRequestApiClient._internal();
 | 
						|
  final List<ServiceRequest> serviceRequests = [];
 | 
						|
 | 
						|
  ServiceRequestApiClient._internal();
 | 
						|
 | 
						|
  factory ServiceRequestApiClient() => _instance;
 | 
						|
 | 
						|
  /// ### The result will be added to [serviceRequests]
 | 
						|
  Future createRequest(ServiceRequest serviceRequest) async {
 | 
						|
    final user = UserApiClient().user;
 | 
						|
    await ApiClient().postJsonForObject(
 | 
						|
      (json) {
 | 
						|
        // ServiceRequest.fromJson(json.decode(utf8.decode(response.bodyBytes))[0])
 | 
						|
        serviceRequests.insert(0, ServiceRequest.fromJson(json[0]));
 | 
						|
      },
 | 
						|
      '${URLs.host1}${URLs.createRequest}',
 | 
						|
      {
 | 
						|
        // "uid": user?.id,
 | 
						|
        // "token": user?.token ?? "",
 | 
						|
        // "sn_id": serviceRequest.deviceId ?? "",
 | 
						|
        // "date": (DateTime.now().millisecondsSinceEpoch).toString(),
 | 
						|
        // "client": user?.hospital?.id ?? '',
 | 
						|
        // "complaint": serviceRequest.maintenanceIssue,
 | 
						|
        // "image": json.encode(serviceRequest.devicePhotos),
 | 
						|
        // "priority": (serviceRequest.priority?.id).toString(),
 | 
						|
        // "defect_types": (serviceRequest.defectType?.id).toString(),
 | 
						|
        // "audio": serviceRequest.audio,
 | 
						|
      },
 | 
						|
    );
 | 
						|
  }
 | 
						|
 | 
						|
  /// Get Service Requests and Fill [serviceRequests]
 | 
						|
  Future<List<ServiceRequest>> getRequests({pageItemNumber, ServiceRequestSearch? search}) async {
 | 
						|
    final user = UserApiClient().user;
 | 
						|
    final response = await ApiClient().getJsonForResponse(
 | 
						|
      '${URLs.host1}${URLs.getServiceRequests}',
 | 
						|
      headers: {"Content-Type": "application/json; charset=utf-8"},
 | 
						|
      // queryParameters: {
 | 
						|
      //   'uid': user?.id,
 | 
						|
      //   if (user?.hospital?.id != null) 'client_nid': user?.hospital?.id,
 | 
						|
      //   'token': user?.token,
 | 
						|
      //   'page': '${(serviceRequests.length) ~/ pageItemNumber}',
 | 
						|
      //   if (search != null) ...search.queryParameters(),
 | 
						|
      // },
 | 
						|
    );
 | 
						|
    List requestsListJson = json.decode(utf8.decode(response.bodyBytes));
 | 
						|
    List<ServiceRequest> serviceRequestsPage = requestsListJson.map((request) => ServiceRequest.fromJson(request)).toList();
 | 
						|
    serviceRequests.addAll(serviceRequestsPage);
 | 
						|
    return serviceRequestsPage;
 | 
						|
  }
 | 
						|
 | 
						|
  Future<ServiceRequest> getServiceById(String? requestId) async {
 | 
						|
    final user = UserApiClient().user;
 | 
						|
    final response = await ApiClient().getJsonForResponse(
 | 
						|
      '${URLs.host1}${URLs.getSingleServiceRequest}',
 | 
						|
      queryParameters: {'call_nid': requestId, 'uid': user?.id, 'token': user?.token},
 | 
						|
    );
 | 
						|
    // If the call to the server was successful, parse the JSON.
 | 
						|
    List jsonList = json.decode(utf8.decode(response.bodyBytes));
 | 
						|
    List<ServiceRequest> requests = jsonList.map((i) => ServiceRequest.fromJson(i)).toList();
 | 
						|
    return requests[0];
 | 
						|
  }
 | 
						|
 | 
						|
  Future createIssueReport(Issue issue) async {
 | 
						|
    final user = UserApiClient().user;
 | 
						|
    Map<String, String> body = issue.toMap();
 | 
						|
    body["uid"] = user?.id ?? "";
 | 
						|
    body["token"] = user?.token ?? "";
 | 
						|
    await ApiClient().postJsonForResponse('${URLs.host1}${URLs.createReport}', body);
 | 
						|
  }
 | 
						|
 | 
						|
  Future updateDate({String? newDate, Lookup? employee, ServiceRequest? request}) async {
 | 
						|
    final user = UserApiClient().user;
 | 
						|
    Map<String, String> body = {};
 | 
						|
    body["uid"] = user?.id ?? '';
 | 
						|
    body["token"] = user?.token ?? '';
 | 
						|
    body["nid"] = request?.id ?? '';
 | 
						|
    body["date"] = newDate ?? '';
 | 
						|
    body["ass_emp"] = employee?.id?.toString() ?? '';
 | 
						|
    await ApiClient().postJsonForResponse('${URLs.host1}${URLs.updateRequestDate}', body);
 | 
						|
    request?.engineerName = employee?.label;
 | 
						|
  }
 | 
						|
 | 
						|
  Future createServiceReport({required ServiceReport? report, required ServiceRequest? request}) async {
 | 
						|
    final user = UserApiClient().user;
 | 
						|
    Map<String, String>? body = report?.toMap();
 | 
						|
    body?["uid"] = user?.id ?? "";
 | 
						|
    body?["token"] = user?.token ?? "";
 | 
						|
    body?["job_id"] = request?.id ?? '';
 | 
						|
    await ApiClient().postJsonForResponse('${URLs.host1}${URLs.createServiceReport}', body);
 | 
						|
  }
 | 
						|
 | 
						|
  Future createDuplicatedReport({required ServiceRequest request}) async {
 | 
						|
    final user = UserApiClient().user;
 | 
						|
    await ApiClient().getJsonForResponse(
 | 
						|
      '${URLs.host1}${URLs.createDuplicatedReport}',
 | 
						|
      queryParameters: {'nid': request.id, 'uid': user?.id, 'token': user?.token},
 | 
						|
    );
 | 
						|
  }
 | 
						|
 | 
						|
  Future updateServiceReport({required ServiceReport report, required ServiceRequest request}) async {
 | 
						|
    final user = UserApiClient().user;
 | 
						|
    Map<String, String> body = report.toMap();
 | 
						|
    body["uid"] = user?.id ?? "";
 | 
						|
    body["token"] = user?.token ?? "";
 | 
						|
    body["job_id"] = request.id ?? '';
 | 
						|
    body["report_id"] = request.reportID ?? '';
 | 
						|
    await ApiClient().postJsonForResponse('${URLs.host1}${URLs.updateServiceReport}', body);
 | 
						|
  }
 | 
						|
 | 
						|
  Future updateServiceReportTimer({required TimerModel timer, required ServiceRequest request}) async {
 | 
						|
    final user = UserApiClient().user;
 | 
						|
    Map<String, String> body = {};
 | 
						|
    body["uid"] = user?.id ?? "";
 | 
						|
    body["token"] = user?.token ?? "";
 | 
						|
    body["job_id"] = request.id ?? '';
 | 
						|
    body["start_time"] = ((timer.startAt?.millisecondsSinceEpoch ?? 0) / 1000).toStringAsFixed(0);
 | 
						|
    body["end_time"] = ((timer.endAt?.millisecondsSinceEpoch ?? 0) / 1000).toStringAsFixed(0);
 | 
						|
    body["working_hours"] = ((timer.durationInSecond ?? 0) / 60 / 60).toStringAsFixed(5);
 | 
						|
    body["report_id"] = request.reportID ?? '';
 | 
						|
    await ApiClient().postJsonForResponse('${URLs.host1}${URLs.updateServiceReport}', body);
 | 
						|
  }
 | 
						|
 | 
						|
  Future<ServiceReport> getSingleServiceReport({required String reportId}) async {
 | 
						|
    final user = UserApiClient().user;
 | 
						|
    final response = await ApiClient().getJsonForResponse(
 | 
						|
      '${URLs.host1}${URLs.getServiceReport}',
 | 
						|
      queryParameters: {'report_id': reportId, 'uid': user?.id, 'token': user?.token},
 | 
						|
    );
 | 
						|
    return ServiceReport.fromJson(json.decode(utf8.decode(response.bodyBytes)), reportId);
 | 
						|
  }
 | 
						|
}
 |