diff --git a/codelab-initial-state/firestore.rules b/codelab-initial-state/firestore.rules index d2c966a..3b7a038 100644 --- a/codelab-initial-state/firestore.rules +++ b/codelab-initial-state/firestore.rules @@ -3,14 +3,16 @@ service cloud.firestore { match /databases/{database}/documents { // User's cart metadata match /carts/{cartID} { - // TODO: Change these! Anyone can read or write. - allow read, write: if true; + // TODO: Changed so read or write only ownerUID == logged in auth UID. + allow create: if request.auth.uid == request.resource.data.ownerUID; + allow read, update, delete: if request.auth.uid == resource.data.ownerUID; } // Items inside the user's cart match /carts/{cartID}/items/{itemID} { // TODO: Change these! Anyone can read or write. - allow read, write: if true; + // allow read, write: if false; + allow read, write: if get(/databases/$(database)/documents/carts/$(cartID)).data.ownerUID == request.auth.uid; } // All items available in the store. Users can read diff --git a/codelab-initial-state/functions/index.js b/codelab-initial-state/functions/index.js index 4ac0f5c..b6b0bbe 100644 --- a/codelab-initial-state/functions/index.js +++ b/codelab-initial-state/functions/index.js @@ -27,16 +27,37 @@ exports.calculateCart = functions return; } - let totalPrice = 125.98; - let itemCount = 8; + let totalPrice = 0; //125.98; + let itemCount = 0; //9; try { - const cartRef = db.collection("carts").doc(context.params.cartId); + const itemsSnap = await cartRef.collection("items").get(); + + itemsSnap.docs.forEach(item => { + const itemData = item.data(); + if (itemData.price) { + // If not specified, the quantity is 1 + const quantity = itemData.quantity ? itemData.quantity : 1; + itemCount += quantity; + totalPrice += (itemData.price * quantity); + } + }); await cartRef.update({ totalPrice, itemCount }); - } catch(err) { - } - }); + + console.log("Cart total successfully recalculated!"); + } catch(err) { + console.warn("update error", err); + } + }); + + + + + + + + \ No newline at end of file diff --git a/codelab-initial-state/functions/test.js b/codelab-initial-state/functions/test.js index e767591..57e492e 100755 --- a/codelab-initial-state/functions/test.js +++ b/codelab-initial-state/functions/test.js @@ -17,7 +17,8 @@ const path = require("path"); const TEST_FIREBASE_PROJECT_ID = "test-firestore-rules-project"; // TODO: Change this to your real Firebase Project ID -const REAL_FIREBASE_PROJECT_ID = "changeme"; +// CF: is this the correct PROJECT_ID? +const REAL_FIREBASE_PROJECT_ID = "marmalade-beta"; //"changeme"; const firebase = require("@firebase/rules-unit-testing"); @@ -179,7 +180,7 @@ describe("shopping cart items", async () => { }); }); -describe.skip("adding an item to the cart recalculates the cart total. ", () => { +describe("adding an item to the cart recalculates the cart total. ", () => { const admin = firebase.initializeAdminApp({ projectId: REAL_FIREBASE_PROJECT_ID }).firestore(); diff --git a/codelab-initial-state/public/js/homepage.js b/codelab-initial-state/public/js/homepage.js index 7a23e00..c36789f 100644 --- a/codelab-initial-state/public/js/homepage.js +++ b/codelab-initial-state/public/js/homepage.js @@ -28,6 +28,12 @@ export async function onDocumentReady(firebaseApp) { const auth = firebaseApp.auth(); const db = firebaseApp.firestore(); + if (location.hostname === "127.0.0.1") { + console.log("127.0.0.1 detected!"); + auth.useEmulator("http://127.0.0.1:9099"); + db.useEmulator("127.0.0.1", 8080); + } + const homePage = new HomePage(db, auth); mount(document.body, homePage); } @@ -170,6 +176,10 @@ class HomePage { } addToCart(id, itemData) { + if (this.auth.currentUser === null) { + throw "Not signed in"; + } + if (this.auth) console.log("addToCart", id, JSON.stringify(itemData)); return this.db .collection("carts")