70 lines
1.7 KiB
Dart
70 lines
1.7 KiB
Dart
import 'package:sidak_desa_mobile/core/api/api.dart';
|
|
import 'dart:convert';
|
|
import 'package:http/http.dart' as http;
|
|
|
|
class AttedanceApi {
|
|
Future<Map<String, dynamic>> verifyAttendance({
|
|
required String token,
|
|
required int userId,
|
|
String? deviceInfo,
|
|
double? latitude,
|
|
double? longitude,
|
|
}) async {
|
|
final uri = Uri.parse('${Apiconfig.baseUrl}/api/attendance/verify');
|
|
|
|
final res = await http.post(
|
|
uri,
|
|
headers: {
|
|
'Accept': 'application/json',
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: jsonEncode({
|
|
'token': token,
|
|
'user_id': userId,
|
|
'device_info': deviceInfo, // boleh null
|
|
'latitude': latitude,
|
|
'longitude': longitude,
|
|
}),
|
|
);
|
|
|
|
final data = jsonDecode(res.body);
|
|
|
|
if (res.statusCode >= 200 && res.statusCode < 300) {
|
|
if (data is Map<String, dynamic>) return data;
|
|
return {'data': data};
|
|
}
|
|
|
|
// Laravel biasanya kasih message + errors
|
|
if (data is Map<String, dynamic>) {
|
|
final msg = (data['message'] ?? 'Verifikasi absen gagal').toString();
|
|
throw Exception(msg);
|
|
}
|
|
|
|
throw Exception('Verifikasi absen gagal');
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getDailyAttendance({
|
|
required int userId,
|
|
required String date,
|
|
}) async {
|
|
final url = Uri.parse(
|
|
"${Apiconfig.baseUrl}/api/riwayat/$userId?date=$date",
|
|
);
|
|
final response = await http.get(
|
|
url,
|
|
headers: {'Accept': 'application/json'},
|
|
);
|
|
|
|
print("DAILY STATUS: ${response.statusCode}");
|
|
print("DAILY BODY: ${response.body}");
|
|
|
|
final data = jsonDecode(response.body);
|
|
|
|
if (response.statusCode == 200 && data['ok'] == true) {
|
|
return data;
|
|
}
|
|
|
|
throw Exception("Gagal ambil data");
|
|
}
|
|
}
|