Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

3
Help with business logic
Post Flair (click to view more posts with a particular flair)
Post Body

I have an app where grocery shops keep records of their regular customers account.

When the customer buys something, the grocery shop adds the amount to their account.

The grocery shop can also add the payment of the account whenever the customer pays his tab.

The grocery can also reverse a wrongly added amount.

The problem is after the customer makes a payment, the grocery can still reverse amounts made before the payment. This leads to negative amounts which I don't want in my app.

The following is an example:

  1. 5
  2. 2

Now the total is 7.

Then the customer makes a payment:

  1. -5

Now the total is 2.

Now if the grocery shop reverse the first amount of 5, the current total would be -3.

My fix is to add a boolean field called "canCorrect" to all documents in the database.

Whenever, the grocery shop adds a payment, this "canCorrect" field is changed to false.

Any document with a false "canCorrect" value, cannot be reversed.

The problem is I don't feel that it's optimal to check on ALL previous documents everytime the grocery shop adds a payment. What if the customer has a 200 documents before this payment?

What if the customer makes a payment, then adds new amounts after this payment, then another payment? My fix will still go through all amounts even the ones made before the first payment.

Can you please advise me of a better solution?

The following is my code:

// 1. Get the subscriber document
QuerySnapshot snapshot = await FirebaseFirestore.instance
.collection(CurrentUser.getCurrentUser().uid)
.where('mobile', isEqualTo: mobile)
.get();
// 2. Confirm only a single subscriber document exists
if (snapshot.docs.length == 0)
throw UserDocumentNotFound(mobile);
else if (snapshot.docs.length > 1) throw FoundTwoDocumentsWithSameMobile();
// 3. Get the subscriber document reference
DocumentReference subscriberDoc = snapshot.docs.first.reference;
// 4. Get the current server timestamp
FieldValue currentTimestamp = FieldValue.serverTimestamp();
// \****************** Added to prevent reverse hisabs before a payment ***************************
// \********** It changes the "canCorrect" field to false of all previous hisabs ******************
QuerySnapshot hisabHistoryCollection =
await subscriberDoc.collection('hisabHistory').where('type', isEqualTo: 'hisab').get();
WriteBatch wb = FirebaseFirestore.instance.batch();
for (QueryDocumentSnapshot document in hisabHistoryCollection.docs) {
wb.update(
document.reference,
{
'canCorrect': false,
},
);
}
// \*********************************************************************************************
num newTotalHisab = double.parse((currentTotalHisab - paymentAmount).toStringAsFixed(2));
// 5. Update the total hisab and the last payment date in the subscriber document
wb.update(
subscriberDoc,
{
'totalHisab': newTotalHisab,
'lastPayment': currentTimestamp,
},
);
// 6. Add a new "correction" payment document
wb.set(
subscriberDoc.collection('hisabHistory').doc(),
{
'amount': paymentAmount,
'serverTimestamp': currentTimestamp,
'type': 'payment',
'note': 'normal',
'correctedFor': null,
'before': currentTotalHisab,
'after': newTotalHisab,
},
);
await wb.commit();

Author
Account Strength
90%
Account Age
8 years
Verified Email
Yes
Verified Flair
No
Total Karma
3,350
Link Karma
759
Comment Karma
2,440
Profile updated: 4 days ago
Posts updated: 2 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
3 years ago