|  | 
| 1 |  | -const ONE_DAY_IN_MILLISECONDS = 86400000; | 
|  | 1 | +import { calculateEstimate } from '@the-collab-lab/shopping-list-utils'; | 
|  | 2 | + | 
|  | 3 | +export const ONE_DAY_IN_MILLISECONDS = 86400000; | 
| 2 | 4 | 
 | 
| 3 | 5 | /** | 
| 4 | 6 |  * Get a new JavaScript Date that is `offset` days in the future. | 
| 5 | 7 |  * @example | 
| 6 | 8 |  * // Returns a Date 3 days in the future | 
| 7 |  | - * getFutureDate(3) | 
| 8 |  | - * @param {number} offset | 
|  | 9 | + * addDaysFromToday(3) | 
|  | 10 | + * @param {number} daysOffset | 
|  | 11 | + */ | 
|  | 12 | +export function addDaysFromToday(daysOffset) { | 
|  | 13 | +	return new Date(Date.now() + daysOffset * ONE_DAY_IN_MILLISECONDS); | 
|  | 14 | +} | 
|  | 15 | + | 
|  | 16 | +/** | 
|  | 17 | + * Calculates the estimated date for the next purchase based on current date, purchase history, | 
|  | 18 | + * and total purchases. | 
|  | 19 | + * @param {Date} currentDate - The current date to calculate against. | 
|  | 20 | + * @param {Object} item - The item object containing purchase data. | 
|  | 21 | + * @param {Date} item.dateCreated - The date the item was created. | 
|  | 22 | + * @param {Date} item.dateNextPurchased - The previously estimated next purchase date. | 
|  | 23 | + * @param {Date|null} item.dateLastPurchased - The last date the item was actually purchased, or null if not purchased yet. | 
|  | 24 | + * @param {number} item.totalPurchases - The total number of purchases made for the item. | 
|  | 25 | + * @returns {Date} - The estimated date of the next purchase. | 
|  | 26 | + * @throws {Error} - Throws an error if the next purchase date cannot be calculated. | 
|  | 27 | + */ | 
|  | 28 | +export const calculateDateNextPurchased = (currentDate, item) => { | 
|  | 29 | +	try { | 
|  | 30 | +		// get purchase intervals and get new estimation for next purchase date | 
|  | 31 | +		const purchaseIntervals = calculatePurchaseIntervals( | 
|  | 32 | +			currentDate, | 
|  | 33 | +			item.dateCreated, | 
|  | 34 | +			item.dateNextPurchased, | 
|  | 35 | +			item.dateLastPurchased, | 
|  | 36 | +		); | 
|  | 37 | +		const estimatedNextPurchaseDate = getNextPurchaseEstimate( | 
|  | 38 | +			purchaseIntervals, | 
|  | 39 | +			item.totalPurchases + 1, | 
|  | 40 | +		); | 
|  | 41 | + | 
|  | 42 | +		return estimatedNextPurchaseDate; | 
|  | 43 | +	} catch (error) { | 
|  | 44 | +		throw new Error(`Failed getting next purchase date: ${error}`); | 
|  | 45 | +	} | 
|  | 46 | +}; | 
|  | 47 | + | 
|  | 48 | +/** | 
|  | 49 | + * Calculate the number of days between two dates. | 
|  | 50 | + * @param {Date} earlierDate The starting date. | 
|  | 51 | + * @param {Date} laterDate The ending date. | 
|  | 52 | + * @returns {number} The number of days between the two dates. | 
|  | 53 | + */ | 
|  | 54 | +function getDaysBetweenDates(earlierDate, laterDate) { | 
|  | 55 | +	return Math.floor( | 
|  | 56 | +		(laterDate.getTime() - earlierDate.getTime()) / ONE_DAY_IN_MILLISECONDS, | 
|  | 57 | +	); | 
|  | 58 | +} | 
|  | 59 | + | 
|  | 60 | +/** | 
|  | 61 | + * Calculate the purchase intervals between current, next, and last purchase dates. | 
|  | 62 | + * @param {Date} currentDate The current date. | 
|  | 63 | + * @param {Date} dateNextPurchased The previously estimated next purchase date. | 
|  | 64 | + * @param {Date|null} dateLastPurchased The date the item was last purchased (can be null). | 
|  | 65 | + * @returns {Object} An object containing the last estimated interval and days since last purchase. | 
|  | 66 | + */ | 
|  | 67 | +function calculatePurchaseIntervals( | 
|  | 68 | +	currentDate, | 
|  | 69 | +	dateCreated, | 
|  | 70 | +	dateNextPurchased, | 
|  | 71 | +	dateLastPurchased, | 
|  | 72 | +) { | 
|  | 73 | +	const lastPurchaseDate = dateLastPurchased?.toDate(); | 
|  | 74 | + | 
|  | 75 | +	const lastEstimatedIntervalStartDate = | 
|  | 76 | +		lastPurchaseDate ?? dateCreated.toDate(); | 
|  | 77 | + | 
|  | 78 | +	const lastEstimatedInterval = getDaysBetweenDates( | 
|  | 79 | +		lastEstimatedIntervalStartDate, | 
|  | 80 | +		dateNextPurchased.toDate(), | 
|  | 81 | +	); | 
|  | 82 | + | 
|  | 83 | +	const daysSinceLastPurchase = | 
|  | 84 | +		lastPurchaseDate === undefined | 
|  | 85 | +			? 0 | 
|  | 86 | +			: getDaysBetweenDates(lastPurchaseDate, currentDate); | 
|  | 87 | + | 
|  | 88 | +	return { lastEstimatedInterval, daysSinceLastPurchase }; | 
|  | 89 | +} | 
|  | 90 | + | 
|  | 91 | +/** | 
|  | 92 | + * Calculate the next purchase estimate based on purchase intervals and total purchases. | 
|  | 93 | + * @param {Object} purchaseIntervals The intervals between the previous and current purchases. | 
|  | 94 | + * @param {number} purchaseIntervals.lastEstimatedInterval The previously estimated number of days between purchases. | 
|  | 95 | + * @param {number} purchaseIntervals.daysSinceLastPurchase The number of days since the last purchase. | 
|  | 96 | + * @param {number} totalPurchases The total number of purchases made. | 
|  | 97 | + * @returns {Date} The estimated next purchase date. | 
|  | 98 | + * @throws {Error} If an error occurs during the next purchase estimation process. | 
| 9 | 99 |  */ | 
| 10 |  | -export function getFutureDate(offset) { | 
| 11 |  | -	return new Date(Date.now() + offset * ONE_DAY_IN_MILLISECONDS); | 
|  | 100 | +function getNextPurchaseEstimate(purchaseIntervals, totalPurchases) { | 
|  | 101 | +	const { lastEstimatedInterval, daysSinceLastPurchase } = purchaseIntervals; | 
|  | 102 | + | 
|  | 103 | +	try { | 
|  | 104 | +		const estimatedDaysUntilPurchase = calculateEstimate( | 
|  | 105 | +			lastEstimatedInterval, | 
|  | 106 | +			daysSinceLastPurchase, | 
|  | 107 | +			totalPurchases, | 
|  | 108 | +		); | 
|  | 109 | + | 
|  | 110 | +		const nextPurchaseEstimate = addDaysFromToday(estimatedDaysUntilPurchase); | 
|  | 111 | + | 
|  | 112 | +		return nextPurchaseEstimate; | 
|  | 113 | +	} catch (error) { | 
|  | 114 | +		throw new Error(`Failed updaing date next purchased: ${error}`); | 
|  | 115 | +	} | 
| 12 | 116 | } | 
0 commit comments