1)kyc update features added approve & reject

This commit is contained in:
saritabirare 2024-10-01 18:03:09 +05:30
parent 545b43aef5
commit 199de6b104
7 changed files with 404 additions and 154 deletions

View File

@ -13,7 +13,7 @@ class KycController extends GetxController {
@override @override
void onInit() { void onInit() {
super.onInit(); super.onInit();
loadKycFromLocalStorage(); // Load KYC data from local storage when initialized // loadKycFromLocalStorage(); // Load KYC data from local storage when initialized
} }
Future<void> fetchKycData() async { Future<void> fetchKycData() async {
@ -28,24 +28,44 @@ class KycController extends GetxController {
if (data != null && data.isNotEmpty) { if (data != null && data.isNotEmpty) {
// Parse the list of KYC objects // Parse the list of KYC objects
kycList.value = KycModel.fromJsonList(data); // Convert to List<KycModel> kycList.value = KycModel.fromJsonList(data); // Convert to List<KycModel>
saveKycToLocalStorage(); // Save the fetched KYC data to local storage // saveKycToLocalStorage(); // Save the fetched KYC data to local storage
} else { } else {
print("No KYC data found or API response is empty."); print("No KYC data found or API response is empty.");
} }
print("KYC details: ${kycList}"); print("KYC details: ${kycList[0].id}");
} finally { } finally {
isLoading(false); isLoading(false);
} }
} }
void approveKyc(String kycId) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String? token = prefs.getString('token');
if (token != null && kycId.isNotEmpty) {
print("Approving KYC with ID: $kycId");
bool result = await KycService().approveKycStatus(token, kycId, "approved");
if (result) {
print("KYC approved successfully.");
} else {
print("Failed to approve KYC.");
}
} else {
print("Token or KYC ID is missing.");
}
}
// Update KYC status locally and persist the changes // Update KYC status locally and persist the changes
Future<void> updateKycStatus(KycModel kycModel, String status, String comment) async { Future<void> updateKycStatus(KycModel kycModel, String status, String comment) async {
final index = kycList.indexOf(kycModel); final index = kycList.indexOf(kycModel);
if (index != -1) { if (index != -1) {
kycList[index].status = status; // Update status locally kycList[index].status = status; // Update status locally
saveKycToLocalStorage(); // Persist the changes locally // saveKycToLocalStorage(); // Persist the changes locally
// Show a success message after updating // Show a success message after updating
Get.snackbar( Get.snackbar(
@ -58,26 +78,20 @@ class KycController extends GetxController {
} }
} }
// Save the current KYC list to SharedPreferences
Future<void> saveKycToLocalStorage() async { void rejectKyc(String kycId,String? rejectionReason,String? user) async {
SharedPreferences prefs = await SharedPreferences.getInstance(); SharedPreferences prefs = await SharedPreferences.getInstance();
List<String> kycListJson = kycList.map((kyc) => jsonEncode(kyc.toJson())).toList(); String? token = prefs.getString('token');
prefs.setStringList('kycList', kycListJson); // Save the updated list locally //String? kycId = kycList[0].id; // Replace with the KYC ID
} // var rejectionReason = kycList[0].notes; // Example rejection reason
// var principalDistributer = kycList[0].notes!.name;
// Load KYC data from SharedPreferences bool result = await KycService().rejectKycStatus(token!, kycId, rejectionReason, user , "reject");
Future<void> loadKycFromLocalStorage() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
List<String>? kycListJson = prefs.getStringList('kycList');
if (kycListJson != null) {
kycList.value = kycListJson.map((jsonStr) => KycModel.fromJson(jsonDecode(jsonStr))).toList();
print("Loaded KYC data from local storage.");
if (result) {
print("KYC rejected successfully.");
} else { } else {
// If no data found, fetch from API print("Failed to reject KYC.");
fetchKycData(); }
}
} }
}
}

View File

@ -1,3 +1,4 @@
import 'package:cheminova/models/kyc_model.dart';
import 'package:cheminova/utils/api_urls.dart'; import 'package:cheminova/utils/api_urls.dart';
import 'package:dio/dio.dart'; import 'package:dio/dio.dart';
@ -28,6 +29,192 @@ class KycService {
return []; return [];
} }
} }
Future<bool> approveKycStatus(String token, String kycId,String status) async {
try {
// Prepare the payload for approval directly
var payload = {
"status": "approved", // Set status to approve
};
// Log the payload
// print("Payload: $payload");
// Make a PATCH request to update the KYC status
var response = await Dio().patch(
'https://api.cnapp.co.in/api/kyc/update/$kycId', // URL with the KYC ID
data: payload, // Payload with the status
options: Options(
headers: {
'Authorization': 'Bearer $token',
'Content-Type': 'application/json',
},
),
);
// Check if the response indicates success
if (response.statusCode == 200) {
print("KYC status approved successfully: ${response.data}");
return true; // Return true if the update was successful
} else {
print("Failed to approve KYC status: ${response.statusCode} - ${response.data}");
return false;
}
} catch (e) {
print("Error approving KYC status: ${e}");
if (e is DioError) {
// Check if the error has a response
if (e.response != null) {
print("Error response data: ${e.response!.data}");
print("Error response status: ${e.response!.statusCode}");
}
}
return false;
}
}
Future<bool> rejectKycStatus(
String token, String kycId, String? rejectionReason, String? user, String status) async {
try {
// Prepare the payload for the PATCH request
var data = {
"status": status, // The KYC status (e.g., "reject")
"rejectionReason": rejectionReason, // Reason for rejection
"user": user, // The role of the user (e.g., "Principal Distributer")
// "notes": [
// {
// "message": rejectionReason, // Rejection message
// "user": user, // User role
// "replyDate": DateTime.now().toUtc().toIso8601String(), // Current UTC date for replyDate
// }
// ]
};
print("Payload: $data");
// Make a PATCH request to update the KYC status
var response = await Dio().patch(
'https://api.cnapp.co.in/api/kyc/update/$kycId',
data: data,
options: Options(
headers: {
'Authorization': 'Bearer $token',
'Content-Type': 'application/json',
},
),
);
// Check if the response indicates success
if (response.statusCode == 200) {
print("KYC status rejected successfully: ${response.data}");
return true; // Return true if the update was successful
} else {
print("Failed to reject KYC status: ${response.statusCode}");
return false;
}
} catch (e) {
print("Error rejecting KYC status: ${e}");
if (e is DioError) {
// Check if the error has a response
if (e.response != null) {
print("Error response data: ${e.response!.data}");
print("Error response status: ${e.response!.statusCode}");
}
}
return false;
}
}
//
// Future<bool> rejectKycStatus(String token, String kycId, String? rejectionReason,
// PrincipalDistributer? user, String status) async {
// try {
// // Prepare the payload with proper rejectionReason structure
// var data = {
// "status": status,
// "rejectionReason": rejectionReason,
// "user": user,
// };
//
// print("Payload: $data");
//
// // Make a PATCH request to update the KYC status
// var response = await Dio().patch(
// 'https://api.cnapp.co.in/api/kyc/update/$kycId',
// data: data,
// options: Options(
// headers: {
// 'Authorization': 'Bearer $token',
// 'Content-Type': 'application/json',
// },
// ),
// );
//
// // Check if the response indicates success
// if (response.statusCode == 200) {
// print("KYC status rejected successfully: ${response.data}");
// return true; // Return true if the update was successful
// } else {
// print("Failed to reject KYC status: ${response.statusCode}");
// return false;
// }
// } catch (e) {
// print("Error rejecting KYC status: ${e}");
// if (e is DioError) {
// // Check if the error has a response
// if (e.response != null) {
// print("Error response data: ${e.response!.data}");
// print("Error response status: ${e.response!.statusCode}");
// }
// }
// return false;
// }
// }
//
// Future<bool> updateKycStatus(String token, String kycId,
// {List<dynamic>? rejectionReason, PrincipalDistributer? user}) async {
// try {
// // Prepare the payload based on the action (approve or reject)
// var data;
//
//
// // Payload for approve action
//
// data = {
// "status": "reject", // Set status to reject
// "rejectionReason": rejectionReason, // Pass the rejection reason
// "user": user, // Pass the user (e.g., 'Principal Distributer')
// };
//
// else {
// throw Exception("Invalid action: must be 'approve' or 'reject'");
// }
//
// // Make a PATCH request to update the KYC status
// var response = await Dio().patch(
// 'https://api.cnapp.co.in/api/kyc/update/$kycId', // URL with the KYC ID
// data: data, // Payload with the status
// options: Options(
// headers: {
// 'Authorization': 'Bearer $token',
// 'Content-Type': 'application/json',
// },
// ),
// );
//
// // Check if the response indicates success
// if (response.statusCode == 200) {
// print("KYC status updated successfully: ${response.data}");
// return true; // Return true if the update was successful
// } else {
// print("Failed to update KYC status: ${response.statusCode}");
// return false;
// }
// } catch (e) {
// print("Error updating KYC status: $e");
// return false;
// }
// }
} }

View File

@ -21,7 +21,7 @@ class KycModel {
String? status; String? status;
AddedBy? addedBy; AddedBy? addedBy;
String? userType; String? userType;
List<dynamic>? notes; List<Notes>? notes;
String? createdAt; String? createdAt;
String? updatedAt; String? updatedAt;
int? v; int? v;
@ -87,7 +87,9 @@ class KycModel {
status: json["status"], status: json["status"],
addedBy: json["addedBy"] != null ? AddedBy.fromJson(json["addedBy"]) : null, addedBy: json["addedBy"] != null ? AddedBy.fromJson(json["addedBy"]) : null,
userType: json["userType"], userType: json["userType"],
notes: json["notes"], notes: json['notes'] != null
? (json['notes'] as List).map((i) => Notes.fromJson(i)).toList()
: null,
createdAt: json["createdAt"], createdAt: json["createdAt"],
updatedAt: json["updatedAt"], updatedAt: json["updatedAt"],
v: json["__v"], v: json["__v"],
@ -123,7 +125,7 @@ class KycModel {
"status": status, "status": status,
"addedBy": addedBy?.toJson(), "addedBy": addedBy?.toJson(),
"userType": userType, "userType": userType,
"notes": notes, "notes": notes?.map((note) => note.toJson()).toList(),
"createdAt": createdAt, "createdAt": createdAt,
"updatedAt": updatedAt, "updatedAt": updatedAt,
"__v": v, "__v": v,
@ -162,6 +164,39 @@ class PrincipalDistributer {
} }
} }
class Notes {
String? message;
String? replyDate;
String? id;
String? user;
Notes({this.message, this.replyDate, this.id,this.user});
factory Notes.fromJson(Map<String, dynamic> json) {
return Notes(
message: json["message"],
replyDate: json["replyDate"],
id: json["_id"],
user: json['user']
);
}
Map<String, dynamic> toJson() {
return {
"message": message,
"replyDate": replyDate,
"_id": id,
"user":user
};
}
@override
String toString() {
return 'Notes{message: $message, replyDate: $replyDate, id: $id,user:$user}';
}
}
class ImageModel { class ImageModel {
String? publicId; String? publicId;
String? url; String? url;

View File

@ -21,7 +21,7 @@ class _LoginScreenState extends State<LoginScreen> {
// Controller for handling authentication logic // Controller for handling authentication logic
final authController = Get.put(AuthController()); final authController = Get.put(AuthController());
// Email validation pattern // Email validation pattern
final String emailPattern = r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$'; final String emailPattern = r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,}$';
// Password validation pattern (at least 6 characters) // Password validation pattern (at least 6 characters)
final String passwordPattern = r'^.{6,}$'; // At least 6 characters final String passwordPattern = r'^.{6,}$'; // At least 6 characters

View File

@ -1,4 +1,5 @@
import 'package:cheminova/utils/show_snackbar.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart'; import 'package:flutter_svg/svg.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
@ -22,47 +23,61 @@ class _KycRetailerDetailScreenState
final KycController _kycController = Get.put(KycController()); final KycController _kycController = Get.put(KycController());
final commentController = TextEditingController(); final commentController = TextEditingController();
String currentStatus = "new"; String? currentStatus ;
String selectedStatus = "All";
void _approveKyc() { void _approveKyc() {
if (widget.kycModel!.status == "approved") { if (widget.kycModel!.status == "approved") {
Get.snackbar( showSnackbar("The KYC has already been approved.");
"Error",
"The KYC has already been approved.",
snackPosition: SnackPosition.BOTTOM,
backgroundColor: Colors.red,
colorText: Colors.white,
);
} else { } else {
// Update the status in the model before calling the controller method // Show confirmation dialog
widget.kycModel!.status = "approved"; Get.dialog(
_kycController.updateKycStatus(widget.kycModel!, "approved", ""); AlertDialog(
setState(() { title: Text("Approval Confirmation"),
content: Text("Are you sure you want to approve this KYC?"),
actions: [
TextButton(
onPressed: () {
// Close the dialog without approving
Get.back();
},
child: Text("Cancel"),
),
TextButton(
onPressed: () {
// If "Approve" is pressed, approve the KYC
_kycController.approveKyc(widget.kycModel!.id.toString());
// Update the KYC status and show success message
widget.kycModel!.status = "approved";
showSnackbar("KYC approved successfully");
// Delay closing the dialog to ensure the user sees the success message
Future.delayed(Duration(seconds: 1), () {
Get.back(); // Close the dialog after a delay
}); });
Get.snackbar( setState(() {}); // Refresh the UI
"Success", },
"KYC approved successfully.", child: Text("Approve"),
snackPosition: SnackPosition.BOTTOM, ),
backgroundColor: Colors.green, ],
colorText: Colors.white, ),
); );
// Pass the updated model back to the previous screen
Get.back(result: widget.kycModel);
} }
} }
void _rejectKyc() { void _rejectKyc() {
if (widget.kycModel!.status == "reject") { if (widget.kycModel!.status == "reject") {
Get.snackbar( showSnackbar("The KYC has already been rejected");
"Error", // Get.snackbar(
"The KYC has already been rejected.", // "Error",
snackPosition: SnackPosition.BOTTOM, // "The KYC has already been rejected.",
backgroundColor: Colors.red, // snackPosition: SnackPosition.BOTTOM,
colorText: Colors.white, // backgroundColor: Colors.red,
); // colorText: Colors.white,
// );
} else { } else {
// Show dialog to enter a comment // Show dialog to enter a comment
Get.dialog( Get.dialog(
@ -86,32 +101,30 @@ class _KycRetailerDetailScreenState
// Update the status in the model before calling the controller method // Update the status in the model before calling the controller method
widget.kycModel!.status = "reject"; widget.kycModel!.status = "reject";
// Append the comment to the notes list // Create a new Notes object (assuming Notes has a field for comments)
Notes rejectionNote = Notes(message: comment);
// Append the new Notes object to the notes list
widget.kycModel!.notes ??= []; // Initialize if null widget.kycModel!.notes ??= []; // Initialize if null
widget.kycModel!.notes!.add(comment); widget.kycModel!.notes!.add(rejectionNote);
_kycController.updateKycStatus(widget.kycModel!, "reject", comment); // Call the controller method with the updated notes
_kycController.rejectKyc(widget.kycModel!.id.toString(), widget.kycModel!.notes![0].message,"Principal Distributer");
showSnackbar("KYC rejected successfully");
// Delay closing the dialog to ensure the user sees the success message
Future.delayed(Duration(seconds: 1), () {
Get.back(); // Close the dialog after a delay
});
setState(() {}); setState(() {});
Get.snackbar(
"Success",
"KYC rejected with comment: $comment",
snackPosition: SnackPosition.BOTTOM,
backgroundColor: Colors.green,
colorText: Colors.white,
);
// Pass the updated model back to the previous screen // Pass the updated model back to the previous screen
Get.back(); // Close the dialog // Close the dialog
Get.back(result: widget.kycModel); // Pass the result back // Pass the result back
} else { } else {
Get.snackbar( showSnackbar("Comment is required");
"Error",
"Comment is required",
snackPosition: SnackPosition.BOTTOM,
backgroundColor: Colors.red,
colorText: Colors.white,
);
} }
}, },
child: const Text("Reject"), child: const Text("Reject"),
@ -128,6 +141,11 @@ class _KycRetailerDetailScreenState
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
@ -236,15 +254,16 @@ class _KycRetailerDetailScreenState
), ),
), ),
SizedBox(height: Get.height * 0.01), SizedBox(height: Get.height * 0.01),
SizedBox( SizedBox(
width: Get.width * 0.9, width: Get.width * 0.9,
child: Card( child: Card(
child: Column( child: Column(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
if (widget.kycModel!.status == "new")
Padding( Padding(
padding: const EdgeInsets.fromLTRB( padding: const EdgeInsets.fromLTRB(16, 8, 8, 0),
16, 8, 8, 0),
child: Text( child: Text(
"Verification Options", "Verification Options",
style: GoogleFonts.roboto( style: GoogleFonts.roboto(
@ -253,6 +272,8 @@ class _KycRetailerDetailScreenState
), ),
), ),
), ),
// Conditionally render buttons based on KYC status
if (widget.kycModel!.status == "new" ) ...[
Row( Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly, mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [ children: [
@ -264,11 +285,9 @@ class _KycRetailerDetailScreenState
onPressed: _approveKyc, onPressed: _approveKyc,
style: ElevatedButton.styleFrom( style: ElevatedButton.styleFrom(
foregroundColor: Colors.white, foregroundColor: Colors.white,
backgroundColor: backgroundColor: const Color(0xFF004791),
const Color(0xFF004791),
shape: RoundedRectangleBorder( shape: RoundedRectangleBorder(
borderRadius: borderRadius: BorderRadius.circular(10),
BorderRadius.circular(10),
), ),
), ),
child: Text( child: Text(
@ -289,11 +308,9 @@ class _KycRetailerDetailScreenState
onPressed: _rejectKyc, onPressed: _rejectKyc,
style: ElevatedButton.styleFrom( style: ElevatedButton.styleFrom(
foregroundColor: Colors.white, foregroundColor: Colors.white,
backgroundColor: backgroundColor: const Color(0xFF910000),
const Color(0xFF910000),
shape: RoundedRectangleBorder( shape: RoundedRectangleBorder(
borderRadius: borderRadius: BorderRadius.circular(10),
BorderRadius.circular(10),
), ),
), ),
child: Text( child: Text(
@ -309,10 +326,14 @@ class _KycRetailerDetailScreenState
], ],
), ),
], ],
],
), ),
), ),
), ),
SizedBox(height: Get.height * 0.01), SizedBox(height: Get.height * 0.01),
//SizedBox(height: Get.height * 0.01),
if (widget.kycModel!.status == "reject") ...[
Card( Card(
child: SizedBox( child: SizedBox(
width: Get.width * 0.9, width: Get.width * 0.9,
@ -320,7 +341,7 @@ class _KycRetailerDetailScreenState
child: Padding( child: Padding(
padding: const EdgeInsets.all(12), padding: const EdgeInsets.all(12),
child: Text( child: Text(
"Comment:${widget.kycModel!.notes}", "Comment: ${widget.kycModel!.notes != null ? widget.kycModel!.notes!.map((note) => note.message).join(", ") : 'No comments'}",
style: GoogleFonts.roboto( style: GoogleFonts.roboto(
fontSize: Get.width * 0.04, fontSize: Get.width * 0.04,
fontWeight: FontWeight.w700, fontWeight: FontWeight.w700,
@ -330,6 +351,7 @@ class _KycRetailerDetailScreenState
), ),
), ),
], ],
],
), ),
), ),
), ),

View File

@ -32,12 +32,12 @@ class _KycRetailerInfoScreenState extends State<KycRetailerInfoScreen> {
@override @override
void initState() { void initState() {
super.initState(); super.initState();
_kycController.loadKycFromLocalStorage(); // _kycController.loadKycFromLocalStorage();
getKycData(); getKycData();
} }
Future<void> _onRefresh() async { Future<void> _onRefresh() async {
// await getKycData();
//navigateToKycDetail(_kycController.kycList as KycModel); //navigateToKycDetail(_kycController.kycList as KycModel);
await Future.delayed(Duration(seconds: 1)); await Future.delayed(Duration(seconds: 1));
} }
@ -62,22 +62,7 @@ class _KycRetailerInfoScreenState extends State<KycRetailerInfoScreen> {
Future<void> navigateToKycDetail(KycModel kycModel) async {
var result = await Get.to(() => KycRetailerDetailScreen(kycModel: kycModel));
if (result != null) {
// Update the list or filter based on the result
KycModel updatedKycModel = result;
setState(() {
// Find and update the KYC in your list
int index = _kycController.kycList.indexWhere((kyc) => kyc.status == updatedKycModel.status);
if (index != -1) {
_kycController.kycList[index] = updatedKycModel;
_kycController.saveKycToLocalStorage(); // Persist updates
}
});
}
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
@ -207,6 +192,7 @@ class _KycRetailerInfoScreenState extends State<KycRetailerInfoScreen> {
itemCount: filteredOrders.length, itemCount: filteredOrders.length,
itemBuilder: (context, index) { itemBuilder: (context, index) {
final order = filteredOrders[index]; final order = filteredOrders[index];
final kyc = _kycController.kycList[index]; final kyc = _kycController.kycList[index];
return Padding( return Padding(
padding: const EdgeInsets.symmetric( padding: const EdgeInsets.symmetric(
@ -321,7 +307,9 @@ class _KycRetailerInfoScreenState extends State<KycRetailerInfoScreen> {
padding: padding:
const EdgeInsets.all(8.0), const EdgeInsets.all(8.0),
child: ElevatedButton( child: ElevatedButton(
onPressed: () => navigateToKycDetail(kyc), // KycRetailerDetailScreen( onPressed: () {
Get.to(KycRetailerDetailScreen(kycModel: order));
}, // KycRetailerDetailScreen(
// kycModel: // kycModel:
// _kycController // _kycController
// .kycList[ // .kycList[

View File

@ -37,4 +37,8 @@ class ApiUrls {
//============================== Kyc Details ==============================// //============================== Kyc Details ==============================//
static const String getKycUrl = '${baseUrl}/api/kyc/getAll'; static const String getKycUrl = '${baseUrl}/api/kyc/getAll';
static const String updateKycUrl = '${baseUrl}/api/kyc/update';
//============================== Rd Order Details ==============================//
static const String getRdOrderUrl = '${baseUrl}/api/pd-get-new-orders';
} }