-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const maxDuration = 30 * 24 * 60 * 60 * 1000 // 30 days | ||
const defaultDuration = 7 * 24 * 60 * 60 * 1000 // 7 days | ||
|
||
/** | ||
* Validates that the given time range doesn't exceed the max allowed duration. | ||
* | ||
* @param startAt | ||
* @param endAt | ||
* @returns | ||
*/ | ||
export const validateDateRange = (startAt: string, endAt: string) => { | ||
const start = new Date(startAt) | ||
const end = new Date(endAt || new Date()) | ||
const durationMs = end.getTime() - start.getTime() | ||
return durationMs <= maxDuration | ||
} | ||
|
||
/** | ||
* Utility to get default dates if not provided. | ||
* Default to last 7 days | ||
* | ||
* @param startAt | ||
* @param endAt | ||
* @returns | ||
*/ | ||
export const getValidatedDates = (startAt?: string, endAt?: string) => { | ||
const now = new Date() | ||
|
||
if (!startAt && !endAt) { | ||
return { | ||
startAt: new Date(now.getTime() - defaultDuration).toISOString(), | ||
endAt: null | ||
} | ||
} | ||
|
||
if (startAt && !endAt) { | ||
const start = new Date(startAt) | ||
return { | ||
startAt, | ||
endAt: new Date(Math.min(start.getTime() + defaultDuration, now.getTime())).toISOString() | ||
} | ||
} | ||
|
||
if (!startAt && endAt) { | ||
const end = new Date(endAt) | ||
return { | ||
startAt: new Date(end.getTime() - defaultDuration).toISOString(), | ||
endAt | ||
} | ||
} | ||
|
||
return { startAt, endAt } | ||
} |