376 lines
10 KiB
Dart
376 lines
10 KiB
Dart
import 'dart:convert';
|
|
import 'package:frontend/utils/auth_utils.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import '../../config/apiUrl.dart';
|
|
|
|
class ApiService {
|
|
Future<List<dynamic>> fetchCountryList() async {
|
|
final String apiUrldata = '$apiUrl/api/getcountryMaster';
|
|
final token = await getToken();
|
|
|
|
if (token == null) {
|
|
throw Exception('Token not found. Please log in.');
|
|
}
|
|
|
|
final response = await http.get(
|
|
Uri.parse(apiUrldata),
|
|
headers: {
|
|
'Authorization': 'Bearer $token',
|
|
'Content-Type': 'application/json',
|
|
},
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
try {
|
|
final data = json.decode(response.body);
|
|
print("Country - $data");
|
|
|
|
if (!data.containsKey('data') || data['data'] is! List) {
|
|
throw Exception(
|
|
"Invalid response format: 'data' field is missing or not a List");
|
|
}
|
|
|
|
return data['data'];
|
|
} catch (e) {
|
|
throw Exception('Error parsing response: $e');
|
|
}
|
|
} else {
|
|
throw Exception('Failed to load country list');
|
|
}
|
|
}
|
|
|
|
Future<List<dynamic>> fetchUsers() async {
|
|
String? ordId = await getOrgId();
|
|
final String apiUrlData = '$apiUrl/api/users?org_id=$ordId';
|
|
final String? token = await getToken();
|
|
|
|
print("Fetch Users");
|
|
print("TOEKRWE: $token");
|
|
|
|
if (token == null) {
|
|
throw Exception('Token not found. Please log in.');
|
|
}
|
|
|
|
final response = await http.get(
|
|
Uri.parse(apiUrlData),
|
|
headers: {
|
|
'Authorization': 'Bearer $token',
|
|
'Content-Type': 'application/json',
|
|
},
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
final data = json.decode(response.body);
|
|
return data['data']; // Returning raw JSON list
|
|
} else {
|
|
throw Exception('Failed to load users');
|
|
}
|
|
}
|
|
|
|
Future<List> fetchCostCenter() async {
|
|
final String apiUrldata = '$apiUrl/api/getCostCenterMaster';
|
|
|
|
final token = await getToken();
|
|
|
|
final userId = await getUserId();
|
|
|
|
// print("SUSRTRT- $userId");
|
|
//
|
|
if (token == null) {
|
|
throw Exception('Token not found. Please log in.');
|
|
}
|
|
|
|
final response = await http.get(
|
|
Uri.parse(apiUrldata),
|
|
headers: {
|
|
'Authorization': 'Bearer $token',
|
|
'Content-Type': 'application/json',
|
|
},
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
try {
|
|
final data = json.decode(response.body);
|
|
print(data);
|
|
|
|
if (!data.containsKey('data') || data['data'] is! List) {
|
|
throw Exception(
|
|
"Invalid response format: 'data' field is missing or not a List");
|
|
}
|
|
|
|
List<dynamic> plansJson = data['data']; // 'data' is a Map, not a List
|
|
// setState(() {
|
|
// apiCostData = plansJson; // Store API response in state
|
|
// if(apiCostData!.isNotEmpty){
|
|
// selectedCostCenterId =apiCostData?.first['department_id'];
|
|
// }
|
|
|
|
// if (apiCostData != null && apiCostData!.isNotEmpty) {
|
|
// selectedCostCenterId ??= apiCostData!.first['department_id']?.toString();
|
|
// }
|
|
// });
|
|
|
|
print('plansJSON');
|
|
|
|
return plansJson;
|
|
} catch (e) {
|
|
throw Exception('Error parsing response: $e');
|
|
}
|
|
} else {
|
|
throw Exception('Failed to load plans');
|
|
}
|
|
}
|
|
|
|
Future<Map<String, dynamic>> fetchMasterDropdown() async {
|
|
final String apiUrldata = '$apiUrl/api/getDropdownMaster';
|
|
|
|
final token = await getToken();
|
|
|
|
if (token == null) {
|
|
throw Exception('Token not found. Please log in.');
|
|
}
|
|
|
|
final response = await http.get(
|
|
Uri.parse(apiUrldata),
|
|
headers: {
|
|
'Authorization': 'Bearer $token',
|
|
'Content-Type': 'application/json',
|
|
},
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
try {
|
|
final data = json.decode(response.body);
|
|
print(data);
|
|
if (!data.containsKey('data') || data['data'] is! Map) {
|
|
throw Exception(
|
|
"Invalid response format: 'data' field is missing or not a Map");
|
|
}
|
|
|
|
Map<String, dynamic> plansJson =
|
|
data['data']; // 'data' is a Map, not a List
|
|
|
|
return plansJson;
|
|
} catch (e) {
|
|
throw Exception('Error parsing response: $e');
|
|
}
|
|
} else {
|
|
throw Exception('Failed to load plans');
|
|
}
|
|
}
|
|
|
|
Future<List<dynamic>> fetchAllServices() async {
|
|
final String apiUrldata = '$apiUrl/api/service';
|
|
|
|
final token = await getToken();
|
|
|
|
if (token == null) {
|
|
throw Exception('Token not found. Please log in.');
|
|
}
|
|
|
|
final response = await http.get(
|
|
Uri.parse(apiUrldata),
|
|
headers: {
|
|
'Authorization': 'Bearer $token',
|
|
'Content-Type': 'application/json',
|
|
},
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
try {
|
|
final data = json.decode(response.body);
|
|
print(data);
|
|
if (!data.containsKey('data') || data['data'] is! List) {
|
|
throw Exception(
|
|
"Invalid response format: 'data' field is missing or not a List");
|
|
}
|
|
return data['data'];
|
|
} catch (e) {
|
|
throw Exception('Error parsing response: $e');
|
|
}
|
|
} else {
|
|
throw Exception('Failed to load plans');
|
|
}
|
|
}
|
|
|
|
Future<List<dynamic>> fetchAllGroup() async {
|
|
String? orgId = await getOrgId();
|
|
|
|
final String apiUrldata = '$apiUrl/api/groups?org_id=$orgId';
|
|
|
|
final token = await getToken();
|
|
|
|
if (token == null) {
|
|
throw Exception('Token not found. Please log in.');
|
|
}
|
|
|
|
final response = await http.get(
|
|
Uri.parse(apiUrldata),
|
|
headers: {
|
|
'Authorization': 'Bearer $token',
|
|
'Content-Type': 'application/json',
|
|
},
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
try {
|
|
final data = json.decode(response.body);
|
|
print(data);
|
|
if (!data.containsKey('data') || data['data'] is! List) {
|
|
throw Exception(
|
|
"Invalid response format: 'data' field is missing or not a List");
|
|
}
|
|
return data['data'];
|
|
} catch (e) {
|
|
throw Exception('Error parsing response: $e');
|
|
}
|
|
} else {
|
|
throw Exception('Failed to load plans');
|
|
}
|
|
}
|
|
|
|
Future<List<dynamic>> fetchAllPolicy() async {
|
|
String? orgId = await getOrgId();
|
|
|
|
final String apiUrldata = '$apiUrl/api/policy?org_id=$orgId';
|
|
|
|
final token = await getToken();
|
|
|
|
if (token == null) {
|
|
throw Exception('Token not found. Please log in.');
|
|
}
|
|
|
|
final response = await http.get(
|
|
Uri.parse(apiUrldata),
|
|
headers: {
|
|
'Authorization': 'Bearer $token',
|
|
'Content-Type': 'application/json',
|
|
},
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
try {
|
|
final data = json.decode(response.body);
|
|
print(data);
|
|
if (!data.containsKey('data') || data['data'] is! List) {
|
|
throw Exception(
|
|
"Invalid response format: 'data' field is missing or not a List");
|
|
}
|
|
return data['data'];
|
|
} catch (e) {
|
|
throw Exception('Error parsing response: $e');
|
|
}
|
|
} else {
|
|
throw Exception('Failed to load plans');
|
|
}
|
|
}
|
|
|
|
Future<Map<String, dynamic>> fetchOrganization() async {
|
|
String? orgId = await getOrgId();
|
|
|
|
// final String apiUrldata = '$apiUrl/api/organizations';
|
|
final String apiUrldata = '$apiUrl/api/organizations/find/$orgId';
|
|
|
|
final token = await getToken();
|
|
|
|
if (token == null) {
|
|
throw Exception('Token not found. Please log in.');
|
|
}
|
|
|
|
final response = await http.get(
|
|
Uri.parse(apiUrldata),
|
|
headers: {
|
|
'Authorization': 'Bearer $token',
|
|
'Content-Type': 'application/json',
|
|
},
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
try {
|
|
final data = json.decode(response.body);
|
|
print(data);
|
|
if (!data.containsKey('data') || data['data'] is! Map) {
|
|
throw Exception(
|
|
"Invalid response format: 'data' field is missing or not a Map");
|
|
}
|
|
|
|
// Make sure each item is a Map<String, dynamic>
|
|
// final List<Map<String, dynamic>> orgList =
|
|
// List<Map<String, dynamic>>.from(data['data']);
|
|
// return orgList;
|
|
|
|
return Map<String, dynamic>.from(data['data']);
|
|
} catch (e) {
|
|
throw Exception('Error parsing response: $e');
|
|
}
|
|
} else {
|
|
throw Exception('Failed to load organizations');
|
|
}
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getViewPlan(
|
|
String planId, List plansJson) async {
|
|
try {
|
|
final plan = plansJson.firstWhere(
|
|
(item) => item["plan_id"].toString() == planId,
|
|
orElse: () => null,
|
|
);
|
|
|
|
if (plan == null) {
|
|
throw Exception("Plan with ID $planId not found.");
|
|
}
|
|
|
|
return Map<String, dynamic>.from(plan);
|
|
} catch (e) {
|
|
throw Exception("Error finding plan: $e");
|
|
}
|
|
}
|
|
|
|
Future<Map<String, dynamic>> fetchUserApprovalList() async {
|
|
String? orgId = await getOrgId();
|
|
String? userId = await getUserId();
|
|
|
|
// final String apiUrldata = '$apiUrl/api/organizations';
|
|
final String apiUrldata =
|
|
'$apiUrl/api/plans/findApprovalList?user_id=$userId&org_id=$orgId';
|
|
|
|
// '$apiUrl/api/findApprovalList?user_id=$userId&org_id=$orgId';
|
|
|
|
final token = await getToken();
|
|
|
|
if (token == null) {
|
|
throw Exception('Token not found. Please log in.');
|
|
}
|
|
|
|
final response = await http.get(
|
|
Uri.parse(apiUrldata),
|
|
headers: {
|
|
'Authorization': 'Bearer $token',
|
|
'Content-Type': 'application/json',
|
|
},
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
try {
|
|
final data = json.decode(response.body);
|
|
print(data);
|
|
if (!data.containsKey('data') || data['data'] is! Map) {
|
|
throw Exception(
|
|
"Invalid response format: 'data' field is missing or not a Map");
|
|
}
|
|
|
|
// Make sure each item is a Map<String, dynamic>
|
|
// final List<Map<String, dynamic>> orgList =
|
|
// List<Map<String, dynamic>>.from(data['data']);
|
|
// return orgList;
|
|
|
|
return Map<String, dynamic>.from(data['data']);
|
|
} catch (e) {
|
|
throw Exception('Error parsing response: $e');
|
|
}
|
|
} else {
|
|
throw Exception('Failed to load organizations');
|
|
}
|
|
}
|
|
}
|