I have a list of items in Firestore with a field representing the available quantity of that item.
I have a cart that is a list of document references to items.
At the time of checkout I want to make sure that the quantity of each item in the cart matches the quantity of items available, incase someone already purchased an item and the number of items available no longer matches the number of items the customer has in their cart. Basically cartItemCount > availableItemCount
I want to go through each item in my cart, do this check and if cartItemCount > availableItemCount then I want to update the cart such that cartItemCount == availableItemCount and show the user a message saying their cart was updated.
I am not able to find any documentation on how to query Firebase in a custom function. This is what I have so far and it gets all the way to 'Cart not empty' but it doesnt fetch anything from Firestore after that. Can someone please point out what I am doing wrong here or if there is official documentation for this?
String cartUpdateQuantityToAvailability(List<DocumentReference>? cart) {
/// MODIFY CODE ONLY BELOW THIS LINE
String returnString = 'Start function ';
// query firebase document from reference
if (cart != null) {
returnString += 'Cart not empty ';
for (var item in cart) {
item.get().then((itemSnapshot) {
if (itemSnapshot.exists) {
returnString += itemSnapshot['quantity'].toString();
}
});
}
}
return returnString;
/// MODIFY CODE ONLY ABOVE THIS LINE
}