208 lines
6.5 KiB
Dart
208 lines
6.5 KiB
Dart
import 'dart:convert';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:image_picker/image_picker.dart';
|
|
import 'package:intl/intl.dart';
|
|
import 'package:logger/logger.dart';
|
|
import 'package:marco/helpers/services/storage/local_storage.dart';
|
|
import 'package:marco/helpers/services/auth_service.dart';
|
|
import 'package:marco/helpers/services/api_endpoints.dart';
|
|
|
|
final Logger logger = Logger();
|
|
|
|
class ApiService {
|
|
static const Duration timeout = Duration(seconds: 10);
|
|
static const bool enableLogs = true;
|
|
|
|
// ===== Helpers =====
|
|
|
|
static Future<String?> _getToken() async {
|
|
final token = await LocalStorage.getJwtToken();
|
|
if (token == null && enableLogs) {
|
|
logger.w("No JWT token found. Please log in.");
|
|
}
|
|
return token;
|
|
}
|
|
|
|
static Map<String, String> _headers(String token) => {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Bearer $token',
|
|
};
|
|
|
|
static void _log(String message) {
|
|
if (enableLogs) logger.i(message);
|
|
}
|
|
|
|
static dynamic _parseResponse(http.Response response, {String label = ''}) {
|
|
_log("$label Response: ${response.body}");
|
|
try {
|
|
final json = jsonDecode(response.body);
|
|
if (response.statusCode == 200 && json['success'] == true) {
|
|
return json['data'];
|
|
}
|
|
_log("API Error [$label]: ${json['message'] ?? 'Unknown error'}");
|
|
} catch (e) {
|
|
_log("Response parsing error [$label]: $e");
|
|
}
|
|
return null;
|
|
}
|
|
|
|
static Future<http.Response?> _getRequest(String endpoint,
|
|
{Map<String, String>? queryParams, bool hasRetried = false}) async {
|
|
String? token = await _getToken();
|
|
if (token == null) return null;
|
|
|
|
Uri uri = Uri.parse("${ApiEndpoints.baseUrl}$endpoint")
|
|
.replace(queryParameters: queryParams);
|
|
_log('GET request: $uri');
|
|
|
|
try {
|
|
http.Response response =
|
|
await http.get(uri, headers: _headers(token)).timeout(timeout);
|
|
|
|
if (response.statusCode == 401 && !hasRetried) {
|
|
_log("Unauthorized. Attempting token refresh...");
|
|
bool refreshed = await AuthService.refreshToken();
|
|
if (refreshed) {
|
|
token = await _getToken();
|
|
if (token != null) {
|
|
return await _getRequest(endpoint,
|
|
queryParams: queryParams, hasRetried: true);
|
|
}
|
|
}
|
|
_log("Refresh failed.");
|
|
}
|
|
return response;
|
|
} catch (e) {
|
|
_log("HTTP GET Exception: $e");
|
|
return null;
|
|
}
|
|
}
|
|
|
|
static Future<http.Response?> _postRequest(String endpoint, dynamic body) async {
|
|
String? token = await _getToken();
|
|
if (token == null) return null;
|
|
|
|
final uri = Uri.parse("${ApiEndpoints.baseUrl}$endpoint");
|
|
_log("POST request to $uri with body: $body");
|
|
|
|
try {
|
|
final response = await http
|
|
.post(uri, headers: _headers(token), body: jsonEncode(body))
|
|
.timeout(timeout);
|
|
return response;
|
|
} catch (e) {
|
|
_log("HTTP POST Exception: $e");
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// ===== API Calls =====
|
|
|
|
static Future<List<dynamic>?> getProjects() async {
|
|
final response = await _getRequest(ApiEndpoints.getProjects);
|
|
return response != null ? _parseResponse(response, label: 'Projects') : null;
|
|
}
|
|
|
|
static Future<List<dynamic>?> getEmployeesByProject(String projectId) async {
|
|
final response = await _getRequest(ApiEndpoints.getEmployeesByProject,
|
|
queryParams: {"projectId": projectId});
|
|
return response != null ? _parseResponse(response, label: 'Employees') : null;
|
|
}
|
|
|
|
static Future<List<dynamic>?> getAttendanceLogs(String projectId,
|
|
{DateTime? dateFrom, DateTime? dateTo}) async {
|
|
final query = {
|
|
"projectId": projectId,
|
|
if (dateFrom != null)
|
|
"dateFrom": DateFormat('yyyy-MM-dd').format(dateFrom),
|
|
if (dateTo != null)
|
|
"dateTo": DateFormat('yyyy-MM-dd').format(dateTo),
|
|
};
|
|
|
|
final response =
|
|
await _getRequest(ApiEndpoints.getAttendanceLogs, queryParams: query);
|
|
return response != null ? _parseResponse(response, label: 'Attendance Logs') : null;
|
|
}
|
|
|
|
static Future<List<dynamic>?> getAttendanceLogView(String id) async {
|
|
final response = await _getRequest("${ApiEndpoints.getAttendanceLogView}/$id");
|
|
return response != null ? _parseResponse(response, label: 'Log Details') : null;
|
|
}
|
|
|
|
static Future<List<dynamic>?> getRegularizationLogs(String projectId) async {
|
|
final response = await _getRequest(ApiEndpoints.getRegularizationLogs,
|
|
queryParams: {"projectId": projectId});
|
|
return response != null ? _parseResponse(response, label: 'Regularization Logs') : null;
|
|
}
|
|
|
|
// ===== Upload Attendance Image =====
|
|
|
|
static Future<bool> uploadAttendanceImage(
|
|
String id,
|
|
String employeeId,
|
|
XFile? imageFile,
|
|
double latitude,
|
|
double longitude, {
|
|
required String imageName,
|
|
required String projectId,
|
|
String comment = "",
|
|
required int action,
|
|
bool imageCapture = true,
|
|
}) async {
|
|
final now = DateTime.now();
|
|
final body = {
|
|
"id": id,
|
|
"employeeId": employeeId,
|
|
"projectId": projectId,
|
|
"markTime": DateFormat('hh:mm a').format(now),
|
|
"comment": comment,
|
|
"action": action,
|
|
"date": DateFormat('yyyy-MM-dd').format(now),
|
|
if (imageCapture) "latitude": '$latitude',
|
|
if (imageCapture) "longitude": '$longitude',
|
|
};
|
|
|
|
if (imageCapture && imageFile != null) {
|
|
try {
|
|
final bytes = await imageFile.readAsBytes();
|
|
final base64Image = base64Encode(bytes);
|
|
final fileSize = await imageFile.length();
|
|
final contentType = "image/${imageFile.path.split('.').last}";
|
|
|
|
body["image"] = {
|
|
"fileName": imageName,
|
|
"contentType": contentType,
|
|
"fileSize": fileSize,
|
|
"description": "Employee attendance photo",
|
|
"base64Data": base64Image,
|
|
};
|
|
} catch (e) {
|
|
_log("Image encoding error: $e");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
final response =
|
|
await _postRequest(ApiEndpoints.uploadAttendanceImage, body);
|
|
if (response == null) return false;
|
|
|
|
final json = jsonDecode(response.body);
|
|
if (response.statusCode == 200 && json['success'] == true) {
|
|
return true;
|
|
} else {
|
|
_log("Failed to upload image: ${json['message'] ?? 'Unknown error'}");
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
// ===== Utilities =====
|
|
|
|
static String generateImageName(String employeeId, int count) {
|
|
final now = DateTime.now();
|
|
final dateStr = DateFormat('yyyyMMdd_HHmmss').format(now);
|
|
final imageNumber = count.toString().padLeft(3, '0');
|
|
return "${employeeId}_${dateStr}_$imageNumber.jpg";
|
|
}
|
|
}
|