mirror of
https://github.com/umami-software/umami.git
synced 2026-02-09 07:07:17 +01:00
Added parseDateRangeQuery function.
This commit is contained in:
parent
a0aaeeeb57
commit
09af33c77e
15 changed files with 139 additions and 98 deletions
69
lib/query.ts
69
lib/query.ts
|
|
@ -1,51 +1,30 @@
|
|||
import cache from 'lib/cache';
|
||||
import { getWebsite, getSession, getUser } from 'queries';
|
||||
import { User, Website, Session } from '@prisma/client';
|
||||
import { NextApiRequest } from 'next';
|
||||
import { getAllowedUnits, getMinimumUnit } from './date';
|
||||
import { getWebsiteDateRange } from '../queries';
|
||||
|
||||
export async function loadWebsite(websiteId: string): Promise<Website> {
|
||||
let website;
|
||||
export async function parseDateRangeQuery(req: NextApiRequest) {
|
||||
const { id: websiteId, startAt, endAt, unit } = req.query;
|
||||
|
||||
if (cache.enabled) {
|
||||
website = await cache.fetchWebsite(websiteId);
|
||||
} else {
|
||||
website = await getWebsite({ id: websiteId });
|
||||
// All-time
|
||||
if (+startAt === 0 && +endAt === 1) {
|
||||
const { min, max } = await getWebsiteDateRange(websiteId as string);
|
||||
|
||||
return {
|
||||
websiteId,
|
||||
startDate: min,
|
||||
endDate: max,
|
||||
unit: getMinimumUnit(min, max),
|
||||
};
|
||||
}
|
||||
|
||||
if (!website || website.deletedAt) {
|
||||
return null;
|
||||
}
|
||||
const startDate = new Date(+startAt);
|
||||
const endDate = new Date(+endAt);
|
||||
const minUnit = getMinimumUnit(startDate, endDate);
|
||||
|
||||
return website;
|
||||
}
|
||||
|
||||
export async function loadSession(sessionId: string): Promise<Session> {
|
||||
let session;
|
||||
|
||||
if (cache.enabled) {
|
||||
session = await cache.fetchSession(sessionId);
|
||||
} else {
|
||||
session = await getSession({ id: sessionId });
|
||||
}
|
||||
|
||||
if (!session) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return session;
|
||||
}
|
||||
|
||||
export async function loadUser(userId: string): Promise<User> {
|
||||
let user;
|
||||
|
||||
if (cache.enabled) {
|
||||
user = await cache.fetchUser(userId);
|
||||
} else {
|
||||
user = await getUser({ id: userId });
|
||||
}
|
||||
|
||||
if (!user || user.deletedAt) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return user;
|
||||
return {
|
||||
websiteId,
|
||||
startDate,
|
||||
endDate,
|
||||
unit: getAllowedUnits(startDate, endDate).includes(unit as string) ? unit : minUnit,
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue