Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
4 tasks done
fullybaked opened this issue Jan 29, 2024 · 0 comments · Fixed by #28
Assignees

Comments

@fullybaked
Copy link
Contributor

fullybaked commented Jan 29, 2024

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

  • When the user purchases an item, the item’s dateNextPurchased property is calculated using the calculateEstimate function and saved to the Firestore database
    • dateNextPurchased is saved as a date, not a number
  • A getDaysBetweenDates function is exported from utils/dates.js and imported into api/firebase.js
    • 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.

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 a pull request may close this issue.

3 participants