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 #33

Merged
merged 3 commits into from
Mar 9, 2024

Conversation

eonflower
Copy link
Collaborator

@eonflower eonflower commented Mar 6, 2024

Description

We imported the @the-collab-lab/shopping-list-utils module to utilize the calculateEstimate function.

In our ListItem.jsx we:

  • Added a previousEstimate variable that checks if dateLastPurchased is true and subtracts it from dateNextPurchased, else it will subtract the dateCreated from dateNextPurchased to get the estimated number of days till next purchase
  • Added a daysSinceLastTransaction variable that checks if dateLastPurchased is true and subtracts it from todaysDate, else it will subtract the dateCreated from todaysDate to get the number of days since the last transaction
  • Implemented the calculateEstimate function from the @the-collab-lab/shopping-list-utils module, utilizing the previousEstimate and daysSinceLastPurchase we calculated, and the total number of purchases of the item
  • Passed todaysDate (as a timestamp) and our nextPurchaseEstimate (calculated from the calculateEstimate functionality), when item is checked, into our firebase updateItem function

In firebase.js we:

  • Utilized the getFutureDate function from our date utils and passed our nextPurchaseEstimate as the parameter to set our dateNextPurchased
  • Incremented our totalPurchases by 1 to update the total in the database

We put the logic for calculating the dateLastPurchased and or nextPurchase on the frontend instead of in firebase to avoid passing logic back and forth and creating clutter in the code. We were unsure if that is the most efficient or "best" route, and are open to feedback for better alternatives.

While testing, we found it difficult to check the exact math for the calculateEstimate function, but everything seemed to be right. Basically, that function takes the amount of days between purchases, averages them, and returns a whole number for the average amount of days between purchases.

Related Issue

closes #11

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

Some notes
It felt like our acceptance criteria was based on previous logic for this project. getDaysBetweenDates is not a function in our utils, but getFutureDate was, so we wrote our logic/functionality utilizing that function instead. Because of this, we utilized the calculateEstimate function to give us the amount of days until our next purchase and the utilized the getFutureDate to convert the amount of days into our future date for purchase.

Type of Changes

enhancement

Updates

Before

Not UI / Changes are in database

After

Not UI / Changes are in database

Testing Steps / QA Criteria

  1. git pull from main
  2. git checkout an-hm-issue-11
  3. npm install
  4. npm start
  5. click a list to navigate to its list page
    • note: it is easier to test on a list that you are the owner of, but will work on any list. Additionally, you will need at least one item in your list, so choosing a list with item(s) or adding new items to a list is necessary
  6. check the box of an item and then navigate to the corresponding list in the database
    • you will need to navigate to the correct collection to find the list and corresponding items; this is why it is easier if you add to your own list, that way you can click your uid in the collection list
    • the path of navigation should look like: uid --> list you are testing --> items --> id of item
  7. read through fields of item
  8. go back to the localhost app and check the box of the item you previously checked
  9. navigate back to the database to check how the fields have changed
    • note: if the totalPurchases is 2 or less, the dateNextPurchased will either be the initial date set at time of item added (based on users dropdown input) or the current date because the calculateEstimate function is creating an average and does not recognize 2 purchases as being enough data.
  10. to test the averaging, you will need to manually change the dateLastPurchased to something before today's date, it will then update the dateNextPurchased
  11. after manually changing the date you can navigate back to the localhost app and check the item again
  12. navigate back to the database to see how it has affected the data (this can be repeated for testing)

@eonflower eonflower added the enhancement New feature or request label Mar 6, 2024
Copy link

github-actions bot commented Mar 6, 2024

Visit the preview URL for this PR (updated for commit feccffa):

https://tcl-66-smart-shopping-list--pr33-an-hm-issue-11-uhmz7rd0.web.app

(expires Wed, 13 Mar 2024 19:05:40 GMT)

🔥 via Firebase Hosting GitHub Action 🌎

Sign: 2dc16cb2a79cbd6723fdc511b73e0743df1d18d0

Copy link
Collaborator

@3campos 3campos left a comment

Choose a reason for hiding this comment

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

I reviewed your code, ran it locally, and checked firebase to confirm that dates were changing appropriately. I agree with writing the logic for calculating the dateLastPurchased and dateNextPurchased on the front end as it makes your code more readily understandable. Your code ran as expected. Great job on this complex issue!

Copy link
Collaborator

@nick-zanetti nick-zanetti left a comment

Choose a reason for hiding this comment

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

Everything here worked as expected for me. Great job with this, this a very clean concise solution to one of the most complicated issues on the project. Also thank you for providing clear testing instructions, as they were important for this PR.

One thing to consider: it might be nice, in my opinion, if users were able to un-check an item for a period of time after they first check it, to account for erroneously checking an item. Currently, if I check an item, it's stuck on "checked" and is logged as a purchase until enough time passes (I think 24 hours?). I actually believe as things stand there is no way to manually uncheck an item. This is not because of how you've coded your specific project, and following all the previous issues leads to this behavior.

Implementing something to change that would be completely optional and up to the team, but it's an idea I wanted to highlight.

const todaysDate = Timestamp.now();

// if dateLastPurchased is true subtract it from dateNextPurchased, else subtract dateCreated from dateNextPurchased to get the estimated number of days till next purchase
const previousEstimate = Math.ceil(
(dateNextPurchased.toDate() -
Copy link
Collaborator

@StefieCaff StefieCaff Mar 9, 2024

Choose a reason for hiding this comment

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

When I navigated to the Groceries list I got this error:
Screenshot 2024-03-09 at 7 40 05 AM
There were several items on the list that were created before the logic for all of the date fields (dateCreated, dateLastPurchased, etc) was implemented, so the previousEstimate function was getting an undefined variable.
I do not think that this will be an issue now that the logic exists for all of the date fields. I am not sure, but it might be a good idea to add some error handling in case somehow one of these fields is not calculated? What do you think?

**note I deleted all the items in the Grocery Firebase Database that did not have calculated Date fields and the error didn't throw.

Copy link
Collaborator

@StefieCaff StefieCaff left a comment

Choose a reason for hiding this comment

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

BooYa! great work! I found an minor glitch due to development items being on a list (see comment on line 20). Once the items were deleted from the collection, it was solved. May be some error handling we want to consider down the road -- or may be completely void?

@hun-ah hun-ah merged commit f1cbcf9 into main Mar 9, 2024
2 checks passed
@hun-ah hun-ah deleted the an-hm-issue-11 branch March 9, 2024 15:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
5 participants