Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

fix validDateRangeForPlan getDaysDifference

Steven Tey20720d88dff6
diff --git a/apps/web/lib/analytics/utils/valid-date-range-for-plan.ts b/apps/web/lib/analytics/utils/valid-date-range-for-plan.ts
index 81e0add44b..5d78cc88a3 100644
--- a/apps/web/lib/analytics/utils/valid-date-range-for-plan.ts
+++ b/apps/web/lib/analytics/utils/valid-date-range-for-plan.ts
@@ -21,7 +21,7 @@ export const validDateRangeForPlan = ({
start?: Date | null;
end?: Date | null;
}): DateRangeValidationResult => {
- const now = new Date(Date.now());
+ end = end ?? new Date(Date.now());
if (interval === "all" && dataAvailableFrom && !start) {
start = dataAvailableFrom;
}
@@ -32,7 +32,7 @@ export const validDateRangeForPlan = ({
(interval === "90d" ||
interval === "1y" ||
interval === "ytd" ||
- (start && getDaysDifference(start, end || now) > 31))
+ (start && getDaysDifference(start, end) > 31))
) {
return {
valid: false,
@@ -43,7 +43,7 @@ export const validDateRangeForPlan = ({
}
// Pro plan users can only get analytics for 1 year
- if (plan === "pro" && start && getDaysDifference(start, end || now) > 366) {
+ if (plan === "pro" && start && getDaysDifference(start, end) > 366) {
return {
valid: false,
code: "pro-limit",
diff --git a/packages/utils/src/functions/datetime/get-days-difference.ts b/packages/utils/src/functions/datetime/get-days-difference.ts
index e27d0b190a..ada55936aa 100644
--- a/packages/utils/src/functions/datetime/get-days-difference.ts
+++ b/packages/utils/src/functions/datetime/get-days-difference.ts
@@ -1,5 +1,10 @@
// Function to get the number of days between two dates
-export const getDaysDifference = (startDate: Date, endDate: Date) => {
+export const getDaysDifference = (
+ startDate: Date | string,
+ endDate: Date | string,
+) => {
+ startDate = new Date(startDate);
+ endDate = new Date(endDate);
const diffTime = Math.abs(endDate.getTime() - startDate.getTime());
return Math.ceil(diffTime / (1000 * 60 * 60 * 24));
};