Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

238
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda