diff --git a/src/lib/load.ts b/src/lib/load.ts index d4d6c3c7..d143344a 100644 --- a/src/lib/load.ts +++ b/src/lib/load.ts @@ -38,3 +38,15 @@ export async function fetchSession(websiteId: string, sessionId: string): Promis return session; } + +export async function fetchAccount(websiteId: string) { + let account = null; + + const cache = await redis.client.get(`cache:website:${websiteId}`); + + if (cache) { + account = await redis.client.get(`account:${cache.account_id}`); + } + + return account; +} diff --git a/src/lib/request.ts b/src/lib/request.ts index 42c44904..f5b98481 100644 --- a/src/lib/request.ts +++ b/src/lib/request.ts @@ -1,8 +1,9 @@ +import { startOfMonth, subMonths } from 'date-fns'; import { z } from 'zod'; import { checkAuth } from '@/lib/auth'; import { DEFAULT_PAGE_SIZE, FILTER_COLUMNS } from '@/lib/constants'; import { getAllowedUnits, getMinimumUnit, maxDate, parseDateRange } from '@/lib/date'; -import { fetchWebsite } from '@/lib/load'; +import { fetchAccount, fetchWebsite } from '@/lib/load'; import { filtersArrayToObject } from '@/lib/params'; import { badRequest, unauthorized } from '@/lib/response'; import type { QueryFilters } from '@/lib/types'; @@ -82,6 +83,15 @@ export function getRequestFilters(query: Record) { export async function setWebsiteDate(websiteId: string, data: Record) { const website = await fetchWebsite(websiteId); + const cloudMode = !!process.env.CLOUD_MODE; + + if (cloudMode) { + const account = await fetchAccount(websiteId); + + if (!account?.hasSubscription) { + data.startDate = maxDate(data.startDate, startOfMonth(subMonths(new Date(), 6))); + } + } if (website?.resetAt) { data.startDate = maxDate(data.startDate, new Date(website?.resetAt));