ticket information collection in firestore
The question is how to retrieve the data of each user (each user information and ticket information is stored in a file in Firestore with their UID, you can refer to the picture above) with the role as an admin. The admin account is in the same Firebase project as the user. The aim of this project is to retrieve all the user info and the related ticket info and make them a report on the admin side. Thanks if you can help.
CodePudding user response:
To be able to provide the admin access to read the data inside the "users" and "ticket_information" collection, you have to use Firestore security rules.
Assuming that you have the rules in place, to be able to get all users from the "users" collection allowing with their corresponding tickets, please use the following lines of code:
FirebaseFirestore db = FirebaseFirestore.getInstance();
CollectionReference usersRef = db.collection("users");
usersRef.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
String email = document.getString("email");
String fname = document.getString("fName");
String uid = document.getId();
DocumentReference uidRef = db.collection("ticket_information").document(uid);
uidRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
String icPassport = document.getString("ic_passport");
String phone = document.getString("phone");
String ticketDate = document.getString("ticket_date");
Log.d(TAG, email "/" fname "/" icPassport "/" phone "/" ticketDate);
} else {
Log.d(TAG, "No such document");
}
} else {
Log.d(TAG, "get failed with ", task.getException());
}
}
});
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
The result in the logcat will be:
jett@gmail.com/jett/1919191/191991919/9 Oct 2021
//...