11. As an item, I want my estimated next purchase date to be computed at the time my purchase is recorded in the database so the app can learn how often I buy different items #11
In order to advise users about when to purchase things, the app needs to be able to calculate that guess and store it a future date.
The calculateEstimate function from the @the-collab-lab/shopping-list-utils module will help us know how many days in the future a purchase should happen. In order to use calculateEstimate, we need to know how many days have passed since the time the item was last purchased.
Acceptance criteria
[x] When the user purchases an item, the item’s dateNextPurchased property is calculated using the calculateEstimate function and saved to the Firestore database
[x] dateNextPurchased is saved as a date, not a number
[x] A getDaysBetweenDates function is exported from utils/dates.js and imported into api/firebase.js
[x] This function takes two JavaScript Dates and return the number of days that have passed between them
Notes
Working with Firestore’s dates
Firestore returns dates as special objects called Timestamps. You will need to convert the Timestamp to a JavaScript Date in order to work with it and complete this feature. The Timestamp object has a .toDate() method – see the Firestore docs.
Calculating the time between JavaScript dates
Comparing and manipulating JavaScript Dates is notoriously tricky. Here's a hint: all Date objects have a getTime() method that returns a representation of that date in milliseconds (MDN docs).
Calculating the next purchase date
The @the-collab-lab/shopping-list-utils module should already be installed in your project. This module exports calculateEstimate.
calculateEstimate accepts three arguments. They are, in order:
previousEstimate (a number): The last estimated purchase interval.
daysSinceLastPurchase (a number): The number of days that have elapsed between the last purchased date and now. You will need to figure out how to calculate this argument!
totalPurchases (a number): The total number of purchases made for the item,
Given this information, calculateEstimate returns a whole number indicating the number of days until the user is likely to make their next purchase.
Summary
In order to advise users about when to purchase things, the app needs to be able to calculate that guess and store it a future date.
The
calculateEstimate
function from the@the-collab-lab/shopping-list-utils
module will help us know how many days in the future a purchase should happen. In order to usecalculateEstimate
, we need to know how many days have passed since the time the item was last purchased.Acceptance criteria
dateNextPurchased
property is calculated using thecalculateEstimate
function and saved to the Firestore databasedateNextPurchased
is saved as a date, not a numbergetDaysBetweenDates
function is exported fromutils/dates.js
and imported intoapi/firebase.js
Notes
Working with Firestore’s dates
Firestore returns dates as special objects called
Timestamps.
You will need to convert the Timestamp to a JavaScript Date in order to work with it and complete this feature. TheTimestamp
object has a.toDate()
method – see the Firestore docs.Calculating the time between JavaScript dates
Comparing and manipulating JavaScript Dates is notoriously tricky. Here's a hint: all Date objects have a
getTime()
method that returns a representation of that date in milliseconds (MDN docs).Calculating the next purchase date
The
@the-collab-lab/shopping-list-utils
module should already be installed in your project. This module exportscalculateEstimate
.calculateEstimate
accepts three arguments. They are, in order:previousEstimate
(a number): The last estimated purchase interval.daysSinceLastPurchase
(a number): The number of days that have elapsed between the last purchased date and now. You will need to figure out how to calculate this argument!totalPurchases
(a number): The total number of purchases made for the item,Given this information,
calculateEstimate
returns a whole number indicating the number of days until the user is likely to make their next purchase.