0

I have an application in which I store the purchase record.

When a customer makes a purchase, an update is made in the document that represents the customer. The value of the purchase is added to the customer's total purchase and the product is added to an array that stores all the products that the client carries.

The error I have is that when there is a bad connection, sometimes it is registered as if the product had been bought twice. Then the total is added twice as much as the product and in the fix the duplicate product appears. I don't know how to fix it.

Here is the code in charge of updating in firestore

val data = hashMapOf(
  "totalSale" to FieldValue.increment(purchaseValue), // -->
  "soldProducts" to FieldValue.arrayUnion(product))  //this is the information that is repeated

FirebaseFirestore.getInstance().collection("client").document(id).update(data)
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

0

The quickest way I can think of preventing is to:

  1. Give each purchase a unique ID, for example by generating a UUID.
  2. Including this UUID in a new array field, say transactionIDs.
  3. Have your security rules reject a purchase if its UUID is already present in the transactionIDs field (see for an example here).
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807