borrow and return Ressource -- need to test

This commit is contained in:
tuanvu
2025-12-13 12:02:20 +01:00
parent 730a9882c7
commit 91fe9f849b
2 changed files with 45 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
import { Ressources } from "@/class/class";
import { db } from "@/firebase_config";
import { arrayRemove, doc, increment, runTransaction } from "firebase/firestore";
export async function returnRessource(chantierId: string, ressource: Ressources) {
const chantierRef = doc(db, "chantier", chantierId);
const ressourceRef = doc(db, "ressources", ressource.id.toString());
await runTransaction(db, async (transaction) => {
const resSnap = await transaction.get(ressourceRef);
if (!resSnap.exists()) throw new Error("Ressource not found");
transaction.update(ressourceRef, {
available_quantity: increment(ressource.quantity),
});
transaction.update(chantierRef, {
vehicules: arrayRemove(ressource),
});
});
}