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.
251 lines
11 KiB
Dart
251 lines
11 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;
|
|
|
|
// 111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
|
|
|
|
/// ### The result will be added to [serviceRequests]
|
|
Future createRequest(ServiceRequest serviceRequest) async {
|
|
print("create request");
|
|
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}',
|
|
{
|
|
"callNo": serviceRequest.id,
|
|
"callCreatedBy": {"name": serviceRequest.engineerName},
|
|
"requestedDate": DateTime.now().millisecondsSinceEpoch.toString(),
|
|
"requestedTime": DateTime.now().millisecondsSinceEpoch.toString(),
|
|
"defectType": {"id": serviceRequest.defectType?.id, "name": serviceRequest.defectType?.label, "value": serviceRequest.defectType?.id},
|
|
// "assets": [0],
|
|
"assignedEmployee": {"id": user?.id.toString(), "name": user?.username},
|
|
"voiceNote": serviceRequest.audio,
|
|
// "callSiteContactPerson": [
|
|
// {"id": 0, "employeeCode": "string", "name": "string", "telephone": "string", "job": "string", "email": "string", "land": "string", "contactUserId": "string"}
|
|
// ],
|
|
"priority": {"id": serviceRequest.priority?.id, "name": serviceRequest.priority?.label, "value": serviceRequest.priority?.id},
|
|
// "requestedThrough": {"id": 0, "name": "string", "value": 0},
|
|
// "typeofRequest": {"id": 0, "name": "string", "value": 0},
|
|
// "callComments": "string",
|
|
// "noofFollowup": 0,
|
|
// "attachmentsCallRequest": [
|
|
// {"id": "", "name": ""}
|
|
// ],
|
|
"status": {"name": serviceRequest.statusLabel, "value": serviceRequest.statusValue},
|
|
// "callLastSituation": {"id": 0, "name": "string", "value": 0},
|
|
// "firstAction": {"id": 0, "name": "string", "value": 0},
|
|
// "loanAvailablity": {"id": 0, "name": "string", "value": 0},
|
|
// "comments": "string",
|
|
// "firstActionDate": "2023-04-17T10:39:59.599Z",
|
|
"visitDate": serviceRequest.visitDate,
|
|
// "callReview": {"id": 0, "name": "string", "value": serviceRequest.}
|
|
},
|
|
// {
|
|
// "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,
|
|
// },
|
|
);
|
|
}
|
|
|
|
// 222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222
|
|
|
|
/// Get Service Requests and Fill [serviceRequests]
|
|
Future<List<ServiceRequest>> getRequests({pageItemNumber, ServiceRequestSearch? search}) async {
|
|
print("get requests xx");
|
|
final user = UserApiClient().user;
|
|
print("user name : ${user?.username}");
|
|
print("user id : ${user?.id}");
|
|
final response = await ApiClient().postJsonForResponse(
|
|
'${URLs.host1}${URLs.getCallRequests}',
|
|
{
|
|
// "pageSize": (serviceRequests.length) ~/ pageItemNumber,
|
|
"pageNumber": pageItemNumber,
|
|
// "callId": "string",
|
|
// "requestedDateSymbol": {
|
|
// "id": 0,
|
|
// "name": "string",
|
|
// "value": 0
|
|
// },
|
|
// "requestedDateFrom": "2023-04-18T08:29:50.708Z",
|
|
// "requestedDateTo": "2023-04-18T08:29:50.708Z",
|
|
// "firstActionSymbol": {
|
|
// "id": 0,
|
|
// "name": "string",
|
|
// "value": 0
|
|
// },
|
|
// "firstActionFrom": "2023-04-18T08:29:50.708Z",
|
|
// "firstActionTo": "2023-04-18T08:29:50.708Z",
|
|
if (search?.hospital != null && (search?.hospital?.isNotEmpty ?? false)) "site": search?.hospital,
|
|
// "assetNo": "string",
|
|
if (search?.deviceSerialNumber != null && (search?.deviceSerialNumber?.isNotEmpty ?? false)) "assetSerialNumber": search?.deviceSerialNumber,
|
|
// "maintenanceSituation": {
|
|
// "id": 0,
|
|
// "name": "string",
|
|
// "value": 0
|
|
// },
|
|
// "status": {
|
|
// "id": 0,
|
|
// "name": "string",
|
|
// "value": 0
|
|
// },
|
|
"assignedEmployee": {
|
|
"id": user?.id,
|
|
"name": user?.username,
|
|
},
|
|
// "firstActionStatus": {
|
|
// "id": 0,
|
|
// "name": "string",
|
|
// "value": 0
|
|
// },
|
|
if (search?.deviceName != null && (search?.deviceName?.isNotEmpty ?? false)) "assetName": search?.deviceName,
|
|
// "manufacturer": "string",
|
|
if (search?.model != null && (search?.model?.isNotEmpty ?? false)) "modelDefinition": search?.model,
|
|
// "typeOfrequest": {
|
|
// "id": 0,
|
|
// "name": "string",
|
|
// "value": 0
|
|
// },
|
|
// "priority": {
|
|
// "id": 0,
|
|
// "name": "string",
|
|
// "value": 0
|
|
// }
|
|
},
|
|
// queryParameters: {
|
|
// // 'uid': user?.id,
|
|
// if (user?.hospital?.id != null) 'client_nid': user?.hospital?.id,
|
|
// 'token': user?.token,
|
|
// 'page': '${(serviceRequests.length) ~/ pageItemNumber}',
|
|
// // if (deviceSerialNumber != null && (deviceSerialNumber?.isNotEmpty ?? false)) 'sn_id': deviceSerialNumber,
|
|
// if (statusValue != null) 'status': statusValue?.toString(),
|
|
// // if (deviceName != null && (deviceName?.isNotEmpty ?? false)) 'equipment_en_name': deviceName,
|
|
// // if (hospital != null && (hospital?.isNotEmpty ?? false)) 'client': hospital,
|
|
// // if (model != null && (model?.isNotEmpty ?? false)) 'model': model,
|
|
// // if (search != null) ...search.queryParameters(),
|
|
// },
|
|
);
|
|
print(response.body);
|
|
List requestsListJson = json.decode(response.body)['data'];
|
|
List<ServiceRequest> serviceRequestsPage = requestsListJson.map((request) => ServiceRequest.fromJson(request)).toList();
|
|
serviceRequests.addAll(serviceRequestsPage);
|
|
return serviceRequestsPage;
|
|
}
|
|
|
|
Future<ServiceRequest> getServiceById(String? requestId) async {
|
|
print("get service by id");
|
|
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 {
|
|
print("Create Issue Report");
|
|
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 {
|
|
print("Update Date");
|
|
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.toString();
|
|
}
|
|
|
|
Future createServiceReport({required ServiceReport? report, required ServiceRequest? request}) async {
|
|
print("Create Service Report");
|
|
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 {
|
|
print("Create Duplicated Report");
|
|
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 {
|
|
print("Update Service Report");
|
|
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 {
|
|
print("Update Service Report Timer");
|
|
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 {
|
|
print("Get Single Service Report");
|
|
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);
|
|
}
|
|
}
|