Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

233
Views
Cloud Firestore - prevent overbooking of packages or products

We have a booking app that uses Vuejs and Cloud Firestore (client library web version 9).

Scenario: Two customers come to our booking site at the same time and try to book the last available package at the same time. To save the reservation, we are using firestore transactions. However, if we try to book the last package from two devices and hit the submit button "simultaneously", the code below allows both orders to be accepted. Also, the package "sold" field is only incremented. Is there any way we can improve the following code to prevent overbooking?

const packageDocRef = doc(db, ...);

try {
    const orderId = await runTransaction(db, async (transaction) => {
        const packageDoc = await transaction.get(packageDocRef);
        if (!packageDoc.exists()) {
            throw "Package does not exist!";
        }
    
        const quantity = packageDoc.data().quantity;
        const sold = parseInt(packageDoc.data().sold + 1);
        
        if (quantity >= sold) {
            transaction.update(packageDocRef, { sold: increment(1) });
            const orderRef = await addDoc(collection(db, collections.ORDERS), reservation);
            if(orderRef.id){
                dispatch ("saveTravellerDetails", { orderId: orderRef.id });
                return orderRef.id;
            }else{
                throw "Error saving reservation!";
            }
        } else {
            return Promise.reject("Sorry! Not enough spots available.");
        }

    });

    console.log("Reservation created ");
    return { orderId: orderId };
} catch (e) {
    // This will be a "Not enough spots available" error.
    console.error(e);
    return { error: e };
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Ok, I saw the error in my ways. I'm posting here in the event it can help someone else.

Instead of await addDoc(collection(db, collections.ORDERS), reservation);

I needed to use transaction.set(orderDocRef, reservation);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!