Files
daily_mobileapp_customer/lib/domain/provider/authentication/auth_provider.dart
2026-05-26 18:01:57 +05:30

121 lines
3.6 KiB
Dart

import 'dart:convert';
import 'package:nearledaily/constants/api_constants.dart';
import '../../../Helper/Logger.dart';
import '../../../data/authentication/auth_request.dart';
import '../../../data/authentication/auth_response.dart';
import '../../../modules/authentication/auth.dart';
import '../../../modules/authentication/getbyid.dart';
import '../../../service/dio.dart';
class LoginProvider {
final CustomDio _customDio = CustomDio(); // ✅ Declare the CustomDio instance
Future<LoginResponse?> signIn(String urlData, LoginRequest data) async {
logger.i('signInUrlData $urlData');
LoginResponse? loginResponse;
final customDio = CustomDio();
try {
final response = await customDio.postData(
urlData,
data.toJson(),
);
logger.i("Raw response type: ${response.runtimeType}");
logger.i("Raw response: $response");
if (response != null) {
if (response is Map<String, dynamic>) {
loginResponse = LoginResponse.fromJson(response);
} else if (response is String) {
try {
final Map<String, dynamic> jsonMap = jsonDecode(response);
loginResponse = LoginResponse.fromJson(jsonMap);
} catch (e) {
loginResponse = LoginResponse(
status: false,
message: "Something went wrong",
);
}
} else {
loginResponse = LoginResponse(
status: false,
message: "something went wrong",
);
}
logger.i('loginResponse: ${loginResponse.toJson()}');
}
} catch (e, stacktrace) {
logger.e("Error occurred in signIn: $e");
logger.e(stacktrace);
loginResponse = LoginResponse(
status: false,
message: "Something went wrong",
);
}
return loginResponse;
}
Future<CustomerFullView?> getProfile(String customerId) async {
logger.i('🔹 GetProfile API customerId: $customerId');
CustomerFullView? profile;
final customDio = CustomDio();
final url = "${ApiConstants.fetchProfile}customerid=$customerId&contactno=''";
try {
final response = await customDio.getData(url,
headers: {
'x-hasura-admin-secret': 'nearle-admin-secret',
},
);
logger.i("Raw response type: ${response.runtimeType}");
logger.i("Raw response: $response");
if (response != null) {
Map<String, dynamic> jsonMap;
if (response is Map<String, dynamic>) {
jsonMap = response;
} else if (response is String) {
jsonMap = jsonDecode(response);
} else {
throw Exception("Something went wrong");
}
// Parse the list and take the first item
final list = jsonMap['customer_full_view'] as List?;
logger.i('🔹 Full jsonMap keys: ${jsonMap.keys.toList()}'); // ← see all keys
logger.i('🔹 customerFullView list: $list'); // ← see the list
logger.i('🔹 list length: ${list?.length}');
if (list != null && list.isNotEmpty) {
profile = CustomerFullView.fromJson(list[0] as Map<String, dynamic>);
logger.i('Profile parsed successfully');
}
}
} catch (e, stacktrace) {
logger.e("Error occurred in ProfileProvider.getProfile: $e");
logger.e(stacktrace);
}
return profile;
}
Future<dynamic> putData(String endpoint, Map<String, dynamic> data) async {
try {
final response = await _customDio.putData(endpoint, data);
return response;
} catch (e) {
return {"status": false, "message": e.toString()};
}
}
}