Use dataStartDate for resetDate.

This commit is contained in:
Mike Cao 2023-07-25 15:17:50 -07:00
parent ee8de858d1
commit 0f98fb5959
15 changed files with 172 additions and 85 deletions

View file

@ -26,7 +26,6 @@ import {
differenceInCalendarMonths,
differenceInCalendarYears,
format,
parseISO,
} from 'date-fns';
import { getDateLocale } from 'lib/lang';
@ -43,6 +42,14 @@ export function parseDateRange(value, locale = 'en-US') {
return value;
}
if (value === 'all') {
return {
startDate: new Date(0),
endDate: new Date(1),
value,
};
}
if (value?.startsWith?.('range')) {
const [, startAt, endAt] = value.split(':');
@ -148,17 +155,32 @@ export function parseDateRange(value, locale = 'en-US') {
}
}
export function getDateRangeValues(startDate, endDate) {
let unit = 'year';
if (differenceInHours(endDate, startDate) <= 48) {
unit = 'hour';
export function getAllowedUnits(unit) {
const units = ['minute', 'hour', 'day', 'month', 'year'];
return units.splice(units.indexOf(unit));
}
export function getMinimumUnit(startDate, endDate) {
if (differenceInMinutes(endDate, startDate) <= 60) {
return 'minute';
} else if (differenceInHours(endDate, startDate) <= 48) {
return 'hour';
} else if (differenceInCalendarDays(endDate, startDate) <= 90) {
unit = 'day';
return 'day';
} else if (differenceInCalendarMonths(endDate, startDate) <= 24) {
unit = 'month';
return 'month';
}
return { startDate: startOfDay(startDate), endDate: endOfDay(endDate), unit };
return 'year';
}
export function getDateRangeValues(startDate, endDate) {
return {
startDate: startOfDay(startDate),
endDate: endOfDay(endDate),
unit: getMinimumUnit(startDate, endDate),
};
}
export function getDateFromString(str) {

View file

@ -1,8 +1,9 @@
import cache from 'lib/cache';
import { getWebsite, getSession, getUser } from 'queries';
import { User, Website, Session } from '@prisma/client';
import { DEFAULT_RESET_DATE } from './constants';
export async function loadWebsite(websiteId: string): Promise<Website> {
export async function loadWebsite(websiteId: string): Promise<Website & { dataStartDate: Date }> {
let website;
if (cache.enabled) {
@ -11,6 +12,8 @@ export async function loadWebsite(websiteId: string): Promise<Website> {
website = await getWebsite({ id: websiteId });
}
website.dataStartDate = new Date(website?.resetAt || DEFAULT_RESET_DATE);
if (!website || website.deletedAt) {
return null;
}