Search code examples
mongodbkotlintransactionsrealm

How to block data in MongoDB Realm until payment is done


I have this function to add an Order inside my Realm Repository

suspend fun addOrder(products: List<Product>) {
           realm.write {
            val order = Order().apply {
                products.forEach {
                    this.products.add(it)
                }
            }
            copyToRealm(order)
        }
}

I know that a payment is approved when a new object is inserted with the message "Approved" inside isPaid collection: {"message":"Approved"}

What is the best way to block the data before the payment starts and unblock it after the payment is done? If the payment fails, the process should be reverted. Its like Transaction in normal DB I guess.

Note: the AddOrder(list) function is called from UI (Kotlin button).


Solution

  • In my opinion, you can "lock" them, by temporary deleting them, and if the payment is succeeded, you dont change them, but IF the payment is failed, you then add them back. This is actually what a real transaction does, add and deletes if failed or other way arround. But now you would have a more control on the deletion and adding. Create separate methods for them.

    Hope it helps!