the-collab-lab / tcl-75-smart-shopping-list

2 stars 2 forks source link

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

Closed fullybaked closed 1 month ago

fullybaked commented 3 months ago

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 use calculateEstimate, we need to know how many days have passed since the time the item was last purchased.

Acceptance criteria

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:

Given this information, calculateEstimate returns a whole number indicating the number of days until the user is likely to make their next purchase.