Skip to content

Commit

Permalink
Updating dateNextPurchased with smart calculation of new date (#33)
Browse files Browse the repository at this point in the history
* created getDaysBetweenDates function

Co-authored-by: Ross Clettenberg <RossaMania@users.noreply.github.com>

* getting the last updated date of the item

Co-authored-by: Ross Clettenberg <RossaMania@users.noreply.github.com>

* update getDaysBetweenDates to only return whole numbers

Co-authored-by: Ross Clettenberg <RossaMania@users.noreply.github.com>

* update updateItem function to calculate a new dateNextPurchased and save to DB

Co-authored-by: Ross Clettenberg <RossaMania@users.noreply.github.com>

* added clarification comment for previousEstimate variable

Co-authored-by: Ross Clettenberg <RossaMania@users.noreply.github.com>

* removing accidental import of random last func

* put testing console.log in for showing the changes

Co-authored-by: Ross Clettenberg <RossaMania@users.noreply.github.com>

* removed console.logs

* switch to Math.floor to make sure focus is on full days passed

---------

Co-authored-by: Ross Clettenberg <RossaMania@users.noreply.github.com>
  • Loading branch information
bbland1 and RossaMania authored Sep 15, 2024
1 parent 7bacfe6 commit 5ea939b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"npm": ">=8.19.0"
},
"dependencies": {
"@the-collab-lab/shopping-list-utils": "^2.2.0",
"firebase": "^10.12.5",
"fp-ts": "^2.16.9",
"io-ts": "^2.2.21",
Expand Down
32 changes: 30 additions & 2 deletions src/api/firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
} from "firebase/firestore";
import { useEffect, useState } from "react";
import { db } from "./config";
import { getFutureDate } from "../utils";
import { getFutureDate, getDaysBetweenDates } from "../utils";
import { calculateEstimate } from "@the-collab-lab/shopping-list-utils";

import * as t from "io-ts";
import { isLeft } from "fp-ts/lib/Either";
Expand Down Expand Up @@ -253,9 +254,36 @@ export async function addItem(
export async function updateItem(listPath: string, item: ListItem) {
const itemDocRef = doc(db, listPath, "items", item.id);

const updates: Pick<ListItem, "totalPurchases" | "dateLastPurchased"> = {
const lastUpdatedDate = item.dateLastPurchased
? item.dateLastPurchased
: item.dateCreated;

// Last estimated date of next purchase, or previous dateNextPurchased in whole number
const previousEstimate = getDaysBetweenDates(
lastUpdatedDate.toDate(),
item.dateNextPurchased.toDate(),
);

const daysSinceLastPurchased = getDaysBetweenDates(
new Date(),
lastUpdatedDate.toDate(),
);

const newDateNextPurchased = getFutureDate(
calculateEstimate(
previousEstimate,
daysSinceLastPurchased,
item.totalPurchases,
),
);

const updates: Pick<
ListItem,
"totalPurchases" | "dateLastPurchased" | "dateNextPurchased"
> = {
totalPurchases: item.totalPurchases + 1,
dateLastPurchased: Timestamp.fromDate(new Date()),
dateNextPurchased: Timestamp.fromDate(newDateNextPurchased),
};

try {
Expand Down
13 changes: 12 additions & 1 deletion src/utils/dates.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const ONE_DAY_IN_MILLISECONDS = 86400000;
export const ONE_DAY_IN_MILLISECONDS = 86400000;

/**
* Get a new JavaScript Date that is `offset` days in the future.
Expand All @@ -19,3 +19,14 @@ export function moreThan24HoursPassed(purchaseDate: Date): boolean {

return timeElapsedInMilliseconds >= ONE_DAY_IN_MILLISECONDS;
}

export function getDaysBetweenDates(date1: Date, date2: Date): number {
const numberOfDaysBetween =
Math.abs(date1.getTime() - date2.getTime()) / ONE_DAY_IN_MILLISECONDS;

if (numberOfDaysBetween < 1) {
return 0;
}

return Math.floor(numberOfDaysBetween);
}

0 comments on commit 5ea939b

Please sign in to comment.