Skip to content

Conversation

@ABradham
Copy link
Contributor

@ABradham ABradham commented Jul 27, 2023

This PR adds support for Cloud Functions and the Cloud Functions Emulator to the "FriendlyEats" Quickstart in firestore/angular-rewrite

This PR also gets rid of the stale data housed in firestore/exported-firestore and firestore/angular-rewrite/exported-firestore. Fresher data is added in #723

const restuarantDocRef = db.doc(
`restaurants/${event.params.restaurtantID}`);

logger.info(`Fetching data for restaurant ` +
Copy link
Collaborator

@christhompsongoogle christhompsongoogle Jul 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove this info or the one above - since we haven't performed any async reads yet having two feels redundant.

`${event.params.restaurtantID}`);
const restaurantDocFromFirebase = await restuarantDocRef.get();
const restaurantData = restaurantDocFromFirebase.data() as Restaurant;
const numRatingsReported = restaurantData.numRatings;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's remove numRatingsReported and inline the restaurantData.numRatings below

const restaurantDocFromFirebase = await restuarantDocRef.get();
const restaurantData = restaurantDocFromFirebase.data() as Restaurant;
const numRatingsReported = restaurantData.numRatings;
const fetchedRatingDocs = (await db.collection(`restaurants/${event.params.restaurtantID}/ratings`).get()).docs
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: line-length

* new reviews (and not the deletion of existing reviews), we can
* expect that when this function is triggered the number of reviews
* listed in the `restaurant.numRatings` field will be strictly less
* than the actual length of the `ratings` sub-collection. In the case
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well said

const numRatingsReported = restaurantData.numRatings;
const fetchedRatingDocs = (await db.collection(`restaurants/${event.params.restaurtantID}/ratings`).get()).docs
const actualRatings: Rating[] = []
fetchedRatingDocs.forEach(rating => actualRatings.push(rating.data() as Rating))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use a .map() instead of forEach and we can assign directly to a variable. Something like this:

const actualRatings: Rating[] = fetchedRatingDocs.map(ratingDoc => return ratingDoc.data() as Rating);


// Calculate average review
let sumOfRatings = 0;
actualRatings.forEach(currentRating => sumOfRatings += currentRating.rating)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the above, rather than forEach we can use a reducer for style points

const sumOfRatings = actualRatings.reduce(
(sum, currentRating) => sum + currentRating.rating,
/* initial sum value */ 0
);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great suggestion, added in next commit!

@ABradham ABradham merged commit 87103cc into master Jul 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants