419 lines
14 KiB
Dart
Executable File
419 lines
14 KiB
Dart
Executable File
import 'package:flutter/material.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:nhance_app_pwa/customAppBar/toastHelper.dart';
|
|
import 'package:nhance_app_pwa/pages/service/TokenService.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'dart:convert';
|
|
|
|
import '../../../config/environment.dart';
|
|
|
|
class ApiService {
|
|
final BuildContext context;
|
|
String? _postToken;
|
|
|
|
ApiService(this.context) {
|
|
_initializeToken();
|
|
}
|
|
|
|
Future<void> _initializeToken() async {
|
|
_postToken = await TokenService.getPostToken();
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getActiveAndInactivePolicyDetails(
|
|
String? clientId,
|
|
String? empCode,
|
|
String? status,
|
|
String? branchID,
|
|
String? mobileNo,String? emailId) async {
|
|
print(_postToken);
|
|
if (_postToken == null) {
|
|
await _initializeToken();
|
|
}
|
|
final url = Uri.parse(
|
|
'${Environment.apiUrl}getEmployeeActiveOrInactivePolicy?client_id=${clientId ?? ''}&emp_code=${empCode ?? ''}&type=${status ?? ''}&client_branch_id=${branchID ?? ''}&mobile_no=${mobileNo ?? ''}&email_id=${emailId ?? ''}');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_postToken' ?? '',
|
|
};
|
|
final response = await _makeGetRequest(url, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getClaimsHistoryToApi(
|
|
String ticket_type_id) async {
|
|
print("getCashDepositDetailsToApi1");
|
|
print(_postToken);
|
|
if (_postToken == null) {
|
|
await _initializeToken();
|
|
}
|
|
final url =
|
|
Uri.parse('${Environment.apiUrl}claimView?ticket_id=$ticket_type_id');
|
|
|
|
final headers = {
|
|
'Authorization': 'Bearer $_postToken' ?? '',
|
|
};
|
|
final response = await _makeGetRequest(url, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getAdvertisementImageToApi(clientID) async {
|
|
print(_postToken);
|
|
if (_postToken == null) {
|
|
await _initializeToken();
|
|
}
|
|
final url = Uri.parse('${Environment.apiUrl}getAdvertisementImage?client_id=$clientID');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_postToken' ?? '',
|
|
};
|
|
final response = await _makeGetRequest(url, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getCashlessAndReimbursementToApi() async {
|
|
print(_postToken);
|
|
if (_postToken == null) {
|
|
await _initializeToken();
|
|
}
|
|
final url = Uri.parse('${Environment.apiUrl}getFEContent');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_postToken' ?? '',
|
|
};
|
|
final response = await _makeGetRequest(url, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getSelfEmployeeProfileDetails(
|
|
String clientId, String empCode, String branchID) async {
|
|
print(_postToken);
|
|
if (_postToken == null) {
|
|
await _initializeToken();
|
|
}
|
|
final url = Uri.parse(
|
|
'${Environment.apiUrl}getEmployeeProfile?emp_code=$empCode&client_id=$clientId&client_branch_id=$branchID');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_postToken' ?? '',
|
|
};
|
|
final response = await _makeGetRequest(url, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getFAQsApiData() async {
|
|
print(_postToken);
|
|
if (_postToken == null) {
|
|
await _initializeToken();
|
|
}
|
|
final url = Uri.parse('${Environment.apiUrl}FAQ');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_postToken' ?? '',
|
|
};
|
|
final response = await _makeGetRequest(url, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getTrackClaimsList(String empPrimaryId,mobileNo,emailID) async {
|
|
print(_postToken);
|
|
if (_postToken == null) {
|
|
await _initializeToken();
|
|
}
|
|
final url = Uri.parse(
|
|
'${Environment.apiUrl}get_ticket_data?emp_id=$empPrimaryId&mobile_number=$mobileNo&email_id=$emailID');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_postToken' ?? '',
|
|
};
|
|
final response = await _makeGetRequest(url, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getTicketListByMobile(String empMobileNo) async {
|
|
print(_postToken);
|
|
if (_postToken == null) {
|
|
await _initializeToken();
|
|
}
|
|
|
|
final url =
|
|
Uri.parse('${Environment.apiUrl}ticketList?mobile=$empMobileNo');
|
|
|
|
final url1 = Uri.parse('${Environment.apiUrl}ticketList?mobile=1234567890');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_postToken' ?? '',
|
|
};
|
|
final response = await _makeGetRequest(url, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getTicketConverList(String thz_id) async {
|
|
print(_postToken);
|
|
if (_postToken == null) {
|
|
await _initializeToken();
|
|
}
|
|
|
|
final url1 = Uri.parse('${Environment.apiUrl}ticketList?mobile=$thz_id');
|
|
|
|
final url =
|
|
Uri.parse('${Environment.apiUrl}ticketConversationList?thz_id=$thz_id');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_postToken' ?? '',
|
|
};
|
|
final response = await _makeGetRequest(url, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getWellnessLink(empPrimaryId,client_policy_id) async {
|
|
print(_postToken);
|
|
if (_postToken == null) {
|
|
await _initializeToken();
|
|
}
|
|
final url = Uri.parse('${Environment.apiUrl}getWellnessURL?emp_id=$empPrimaryId');
|
|
// final url = Uri.parse('${Environment.apiUrl}getWellnessURL?emp_id=$empPrimaryId&client_policy_id=$client_policy_id');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_postToken' ?? '',
|
|
};
|
|
final response = await _makeGetRequest(url, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getEcardRequest(eCardParam) async {
|
|
print(_postToken);
|
|
if (_postToken == null) {
|
|
await _initializeToken();
|
|
}
|
|
final url = Uri.parse('${Environment.apiUrl}ecardRequest');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_postToken' ?? '',
|
|
'Content-Type': 'application/json', // Ensure content type is JSON
|
|
};
|
|
final jsonBody = jsonEncode(eCardParam);
|
|
// Send formDataJson as the body
|
|
final response = await _makePostRequestWithoutFormData(url,jsonBody, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> sendRetailPolicyDetails(formData) async {
|
|
print(_postToken);
|
|
if (_postToken == null) {
|
|
await _initializeToken();
|
|
}
|
|
final url = Uri.parse('${Environment.apiUrl}addEmpRetailPolicy');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_postToken' ?? '',
|
|
'Content-Type': 'application/json', // Ensure content type is JSON
|
|
};
|
|
final jsonBody = jsonEncode(formData);
|
|
// Send formDataJson as the body
|
|
final response = await _makePostRequestWithoutFormData(url,jsonBody, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<bool> updateMobileNumber(Map<String, dynamic> updateParam) async {
|
|
if (_postToken == null) {
|
|
await _initializeToken();
|
|
}
|
|
|
|
final url = Uri.parse('${Environment.apiUrl}updateMobileNumber');
|
|
|
|
final headers = {
|
|
'Authorization': 'Bearer $_postToken',
|
|
'Content-Type': 'application/json',
|
|
};
|
|
|
|
final jsonBody = jsonEncode(updateParam);
|
|
|
|
final response = await _makePostRequestWithoutFormData(
|
|
url,
|
|
jsonBody,
|
|
headers,
|
|
);
|
|
|
|
return response['status'] == 'success';
|
|
}
|
|
|
|
|
|
Future<Map<String, dynamic>> fetchPolicyTypeAndInsurer() async {
|
|
print(_postToken);
|
|
if (_postToken == null) {
|
|
await _initializeToken();
|
|
}
|
|
final url = Uri.parse('${Environment.apiUrl}getPolicyTypeAndInsurer');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_postToken' ?? '',
|
|
};
|
|
final response = await _makeGetRequest(url, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> fetchDepartmentList(String clientId, String empCode) async {
|
|
print(_postToken);
|
|
if (_postToken == null) {
|
|
await _initializeToken();
|
|
}
|
|
final url = Uri.parse('${Environment.apiUrl}get_ticket_type?client_id=$clientId&emp_code=$empCode');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_postToken' ?? '',
|
|
};
|
|
final response = await _makeGetRequest(url, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getClaimTypeMasterApi() async {
|
|
print(_postToken);
|
|
if (_postToken == null) {
|
|
await _initializeToken();
|
|
}
|
|
final url = Uri.parse('${Environment.apiUrl}getClaimTypeMaster');
|
|
final headers = {
|
|
'Authorization': 'Bearer $_postToken' ?? '',
|
|
};
|
|
final response = await _makeGetRequest(url, headers);
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> sendTicketFormDataToApi(
|
|
Map<String, dynamic> formData) async {
|
|
print(_postToken);
|
|
if (_postToken == null) {
|
|
await _initializeToken();
|
|
}
|
|
final url = Uri.parse('${Environment.apiUrl}ticketSave');
|
|
// Convert formData to Map<String, String>
|
|
Map<String, String> stringFormData =
|
|
formData.map((key, value) => MapEntry(key, value.toString()));
|
|
final response = await _makePostRequest(url, stringFormData, {
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
'Authorization': 'Bearer $_postToken' ?? '',
|
|
});
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> sendClaimsFormDataToApi(
|
|
Map<String, dynamic> formData) async {
|
|
print(_postToken);
|
|
if (_postToken == null) {
|
|
await _initializeToken();
|
|
}
|
|
final url = Uri.parse('${Environment.apiUrl}initiateClaim');
|
|
// Convert formData to Map<String, String>
|
|
Map<String, String> stringFormData =
|
|
formData.map((key, value) => MapEntry(key, value.toString()));
|
|
final response = await _makePostRequest(url, stringFormData, {
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
'Authorization': 'Bearer $_postToken' ?? '',
|
|
});
|
|
return response;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> sendClaimsMessageToApi(
|
|
Map<String, dynamic> formData) async {
|
|
print(_postToken);
|
|
if (_postToken == null) {
|
|
await _initializeToken();
|
|
}
|
|
print("sendClaimsMessageToApi");
|
|
final url = Uri.parse('${Environment.apiUrl}ticketConversationSave');
|
|
// final url = Uri.parse('${Environment.apiUrlTicket}/messages/create');
|
|
// Convert formData to Map<String, String>
|
|
print("sendClaimsMessageToApi1");
|
|
Map<String, String> stringFormData =
|
|
formData.map((key, value) => MapEntry(key, value.toString()));
|
|
final response = await _makePostRequest(url, stringFormData, {
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
'Authorization': 'Bearer $_postToken' ?? '',
|
|
});
|
|
return response;
|
|
}
|
|
|
|
Future<void> _clearLocalStorageAndRedirect() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.clear();
|
|
// Assuming you have access to the context
|
|
ToastHelper.showErrorToast(context, 'Session Out');
|
|
context.go('/login');
|
|
// Navigator.pushNamed(context, 'login');
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getBotDetails(
|
|
Map<String, dynamic> formData) async {
|
|
if (_postToken == null) {
|
|
await _initializeToken();
|
|
}
|
|
final url = Uri.parse('${Environment.apiUrl}getChatResponse');
|
|
// Convert formData to Map<String, String>
|
|
Map<String, String> stringFormData =
|
|
formData.map((key, value) => MapEntry(key, value.toString()));
|
|
print(url);
|
|
final response = await _makePostRequest(url, stringFormData, {
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
'Authorization': 'Bearer $_postToken' ?? '',
|
|
});
|
|
return response;
|
|
}
|
|
|
|
// Future<Map<String, dynamic>> getBotDetails(String requestFor, is_option,
|
|
// option, returnMessage, mobileNo, policyID) async {
|
|
// print('requestFor: $requestFor');
|
|
// print('is_option: $is_option');
|
|
// print('option: $option');
|
|
// print('returnMessage: $returnMessage');
|
|
// print('mobileNo: $mobileNo');
|
|
// print('policyID: $policyID');
|
|
// if (_postToken == null) {
|
|
// await _initializeToken();
|
|
// }
|
|
// final url;
|
|
// if (is_option != '0') {
|
|
// url = Uri.parse(
|
|
// '${Environment.apiUrl}getChatResponse?request_for=$requestFor&is_option=$is_option&option=$option&mobile_no=$mobileNo&policy_id=$policyID');
|
|
// } else {
|
|
// url = Uri.parse(
|
|
// '${Environment.apiUrl}getChatResponse?request_for=$requestFor&is_option=$is_option&text=$returnMessage&value=$option&mobile_no=$mobileNo&policy_id=$policyID');
|
|
// }
|
|
// print(url);
|
|
//
|
|
// final headers = {
|
|
// 'Authorization': 'Bearer $_postToken' ?? '',
|
|
// };
|
|
// final response = await _makeGetRequest(url, headers);
|
|
// return response;
|
|
// }
|
|
|
|
Future<Map<String, dynamic>> _makeGetRequest(
|
|
Uri url, Map<String, String> headers) async {
|
|
// Add global APP_SIGNATURE header
|
|
headers['APP-SIGNATURE'] = 'nhance-2025-signature-T8f6gV9k5fK2wf5V5c4vId3b0J8z6cAm2x8Y';
|
|
final response = await http.get(url, headers: headers);
|
|
return _handleResponse(response);
|
|
}
|
|
|
|
Future<Map<String, dynamic>> _makePostRequest(
|
|
Uri url, Map<String, String> body, Map<String, String> headers) async {
|
|
// Add global APP_SIGNATURE header
|
|
headers['APP-SIGNATURE'] = 'nhance-2025-signature-T8f6gV9k5fK2wf5V5c4vId3b0J8z6cAm2x8Y';
|
|
final response = await http.post(url, headers: headers, body: body);
|
|
return _handleResponse(response);
|
|
}
|
|
|
|
Future<Map<String, dynamic>> _makePostRequestWithoutFormData(
|
|
Uri url, String body, Map<String, String> headers) async {
|
|
// Add global APP_SIGNATURE header
|
|
headers['APP-SIGNATURE'] = 'nhance-2025-signature-T8f6gV9k5fK2wf5V5c4vId3b0J8z6cAm2x8Y';
|
|
final response = await http.post(url, headers: headers, body: body);
|
|
return _handleResponse(response);
|
|
}
|
|
|
|
|
|
Future<Map<String, dynamic>> _handleResponse(http.Response response) async {
|
|
if (response.statusCode == 200) {
|
|
return jsonDecode(response.body);
|
|
} else if (response.statusCode == 401) {
|
|
await _clearLocalStorageAndRedirect();
|
|
return {};
|
|
} else if (response.statusCode == 429) {
|
|
final body = jsonDecode(response.body);
|
|
final message = body['message'];
|
|
ToastHelper.showWarningToast(context, message);
|
|
return {};
|
|
} else {
|
|
throw Exception('Failed to load data');
|
|
}
|
|
}
|
|
}
|