Skip to content
Open
8 changes: 5 additions & 3 deletions codelab-initial-state/firestore.rules
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 27 additions & 6 deletions codelab-initial-state/functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});








5 changes: 3 additions & 2 deletions codelab-initial-state/functions/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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();
Expand Down
10 changes: 10 additions & 0 deletions codelab-initial-state/public/js/homepage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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")
Expand Down