@@ -11,6 +11,7 @@ class FirebaseUserRepo implements UserRepo {
1111 await _firestore.collection ('users' ).doc (uid).set ({
1212 'email' : email,
1313 'username' : username,
14+ 'roleAllot' : null , //role initialise
1415 });
1516 } catch (e) {
1617 throw Exception ('Failed to save user data: $e ' );
@@ -56,11 +57,37 @@ class FirebaseUserRepo implements UserRepo {
5657 @override
5758 Future <void > deleteUserData (String uid) async {
5859 try {
60+ //delete from users collection
5961 await _firestore.collection ('users' ).doc (uid).delete ();
62+
63+ //delete rom role collections if exists
64+ final userData = await getUserData (uid);
65+ final role = userData? ['roleAllot' ];
66+
67+ if (role != null ) {
68+ String collection = '' ;
69+ if (role == 'customer' ) {
70+ collection = 'customers' ;
71+ }
72+ else if (role == 'retailer' ) {
73+ collection = 'retailers' ;
74+ }
75+ else if (role == 'wholesaler' ) {
76+ collection = 'wholesalers' ;
77+ }
78+
79+
80+ //delete data from collection
81+ if (collection.isNotEmpty) {
82+ await _firestore.collection (collection).doc (uid).delete ();
83+ }
84+ }
6085 } catch (e) {
6186 throw Exception ('Failed to delete user data: $e ' );
6287 }
6388 }
89+
90+ //check if user exists
6491 @override
6592 Future <bool > checkIfUserExists (String uid) async {
6693 try {
@@ -70,4 +97,88 @@ class FirebaseUserRepo implements UserRepo {
7097 throw Exception ('Failed to check if user exists: $e ' );
7198 }
7299 }
100+
101+ //update/set user role in users collection
102+ @override
103+ Future <void > updateUserRole (String uid, String role) async {
104+ try {
105+ await _firestore.collection ('users' ).doc (uid).update ({
106+ 'roleAllot' : role,
107+ });
108+ } catch (e) {
109+ throw Exception ('Failed to update user role: $e ' );
110+ }
111+ }
112+
113+ //get user role data from users collection
114+ @override
115+ Future <String ?> getUserRole (String uid) async {
116+ try {
117+ final doc = await _firestore.collection ('users' ).doc (uid).get ();
118+ return doc.data ()? ['roleAllot' ];
119+ } catch (e) {
120+ throw Exception ('Failed to get user role: $e ' );
121+ }
122+ }
123+
124+ //save role specific data in particular collection
125+ @override
126+ Future <void > saveRoleData (String uid, String role, String address, String pincode, {String ? businessName}) async {
127+ try {
128+ String collection = '' ;
129+ if (role == 'customer' ) {
130+ collection = 'customers' ;
131+ }
132+ else if (role == 'retailer' ){
133+ collection = 'retailers' ;
134+ }
135+ else if (role == 'wholesaler' ){
136+ collection = 'wholesalers' ;
137+ }
138+
139+ if (collection.isEmpty) {
140+ throw Exception ('Invalid role: $role ' );
141+ }
142+
143+ final Map <String , dynamic > data = {
144+ 'uid' : uid,
145+ 'address' : address,
146+ 'pincode' : pincode,
147+ };
148+
149+ if (businessName != null && (role == 'retailer' || role == 'wholesaler' )) {
150+ data['businessName' ] = businessName;
151+ }
152+
153+ await _firestore.collection (collection).doc (uid).set (data);
154+ } catch (e) {
155+ throw Exception ('Failed to save role data: $e ' );
156+ }
157+ }
158+
159+ //get role specific data from respective collection
160+ @override
161+ Future <Map <String , dynamic >?> getRoleData (String uid, String role) async {
162+ try {
163+ String collection = '' ;
164+ if (role == 'customer' ) {
165+ collection = 'customers' ;
166+ }
167+ else if (role == 'retailer' ){
168+ collection = 'retailers' ;
169+ }
170+ else if (role == 'wholesaler' ){
171+ collection = 'wholesalers' ;
172+ }
173+
174+ if (collection.isEmpty) return null ;
175+
176+ final doc = await _firestore.collection (collection).doc (uid).get ();
177+ if (! doc.exists) return null ;
178+ return doc.data ();
179+ } catch (e) {
180+ throw Exception ('Failed to get role data: $e ' );
181+ }
182+ }
183+
73184}
0 commit comments