mirror of
https://github.com/umami-software/umami.git
synced 2026-02-14 17:45:38 +01:00
Compare commits
2 commits
1def80ba42
...
f70f98fed0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f70f98fed0 | ||
|
|
b36cd48b4a |
16 changed files with 169 additions and 234 deletions
|
|
@ -1,21 +1,24 @@
|
|||
import { useState } from 'react';
|
||||
import { DateFilter } from '@/components/input/DateFilter';
|
||||
import { Button, Row } from '@umami/react-zen';
|
||||
import { useDateRange, useMessages } from '@/components/hooks';
|
||||
import { DEFAULT_DATE_RANGE_VALUE } from '@/lib/constants';
|
||||
import { useMessages } from '@/components/hooks';
|
||||
import { DATE_RANGE_CONFIG, DEFAULT_DATE_RANGE_VALUE } from '@/lib/constants';
|
||||
import { setItem, getItem } from '@/lib/storage';
|
||||
|
||||
export function DateRangeSetting() {
|
||||
const { formatMessage, labels } = useMessages();
|
||||
const { dateRange, saveDateRange } = useDateRange();
|
||||
const { value } = dateRange;
|
||||
const [date, setDate] = useState(getItem(DATE_RANGE_CONFIG) || DEFAULT_DATE_RANGE_VALUE);
|
||||
|
||||
const handleChange = (value: string) => {
|
||||
saveDateRange(value);
|
||||
setItem(DATE_RANGE_CONFIG, value);
|
||||
setDate(value);
|
||||
};
|
||||
const handleReset = () => saveDateRange(DEFAULT_DATE_RANGE_VALUE);
|
||||
|
||||
const handleReset = () => setItem(DATE_RANGE_CONFIG, DEFAULT_DATE_RANGE_VALUE);
|
||||
|
||||
return (
|
||||
<Row gap="3">
|
||||
<DateFilter value={value} onChange={handleChange} placement="bottom start" />
|
||||
<DateFilter value={date} onChange={handleChange} placement="bottom start" />
|
||||
<Button onPress={handleReset}>{formatMessage(labels.reset)}</Button>
|
||||
</Row>
|
||||
);
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ export function WebsiteChart({
|
|||
const { startDate, endDate, unit, value } = dateRange;
|
||||
const { data, isLoading, isFetching, error } = useWebsitePageviewsQuery({
|
||||
websiteId,
|
||||
compare: compareMode ? dateCompare : undefined,
|
||||
compare: compareMode ? dateCompare?.['value'] : undefined,
|
||||
});
|
||||
const { pageviews, sessions, compare } = (data || {}) as any;
|
||||
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ export * from './context/useWebsite';
|
|||
|
||||
// Query hooks
|
||||
export * from './queries/useActiveUsersQuery';
|
||||
export * from './queries/useDateRangeQuery';
|
||||
export * from './queries/useDeleteQuery';
|
||||
export * from './queries/useEventDataEventsQuery';
|
||||
export * from './queries/useEventDataPropertiesQuery';
|
||||
|
|
@ -76,6 +77,7 @@ export * from './useModified';
|
|||
export * from './useNavigation';
|
||||
export * from './usePagedQuery';
|
||||
export * from './usePageParameters';
|
||||
export * from './useQueryStringDate';
|
||||
export * from './useRegionNames';
|
||||
export * from './useSlug';
|
||||
export * from './useSticky';
|
||||
|
|
|
|||
12
src/components/hooks/queries/useDateRangeQuery.ts
Normal file
12
src/components/hooks/queries/useDateRangeQuery.ts
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
import { useApi } from '../useApi';
|
||||
import { ReactQueryOptions } from '@/lib/types';
|
||||
|
||||
export function useDateRangeQuery(websiteId: string, options?: ReactQueryOptions) {
|
||||
const { get, useQuery } = useApi();
|
||||
return useQuery<any>({
|
||||
queryKey: ['date-range', websiteId],
|
||||
queryFn: () => get(`/websites/${websiteId}/daterange`),
|
||||
enabled: !!websiteId,
|
||||
...options,
|
||||
});
|
||||
}
|
||||
|
|
@ -1,65 +1,35 @@
|
|||
import { useMemo } from 'react';
|
||||
import { getMinimumUnit, parseDateRange, getOffsetDateRange } from '@/lib/date';
|
||||
import { setItem } from '@/lib/storage';
|
||||
import { DATE_RANGE_CONFIG, DEFAULT_DATE_COMPARE, DEFAULT_DATE_RANGE_VALUE } from '@/lib/constants';
|
||||
import { setWebsiteDateCompare, setWebsiteDateRange, useWebsites } from '@/store/websites';
|
||||
import { setDateRangeValue, useApp } from '@/store/app';
|
||||
import { useLocale } from './useLocale';
|
||||
import { useApi } from './useApi';
|
||||
import { useNavigation } from './useNavigation';
|
||||
import { getMinimumUnit, parseDateRange } from '@/lib/date';
|
||||
import { useLocale } from '@/components/hooks/useLocale';
|
||||
import { useApi } from '@/components/hooks//useApi';
|
||||
import { useQueryStringDate } from '@/components/hooks/useQueryStringDate';
|
||||
import { useGlobalState } from '@/components/hooks/useGlobalState';
|
||||
|
||||
export interface UseDateRangeOptions {
|
||||
ignoreOffset?: boolean;
|
||||
}
|
||||
|
||||
export function useDateRange(websiteId?: string, options: UseDateRangeOptions = {}) {
|
||||
export function useDateRange(websiteId: string) {
|
||||
const { get } = useApi();
|
||||
const { locale } = useLocale();
|
||||
const {
|
||||
query: { date, offset = 0 },
|
||||
} = useNavigation();
|
||||
const websiteConfig = useWebsites(state => state[websiteId]?.dateRange);
|
||||
const globalConfig = useApp(state => state.dateRangeValue);
|
||||
const dateValue = websiteConfig?.value || date || globalConfig || DEFAULT_DATE_RANGE_VALUE;
|
||||
const { dateRange: defaultDateRange, dateCompare } = useQueryStringDate();
|
||||
|
||||
const dateRange = useMemo(() => {
|
||||
const dateRangeObject = parseDateRange(dateValue, locale);
|
||||
const [dateRange, setDateRange] = useGlobalState(`date-range:${websiteId}`, defaultDateRange);
|
||||
|
||||
return !options.ignoreOffset && offset
|
||||
? getOffsetDateRange(dateRangeObject, +offset)
|
||||
: dateRangeObject;
|
||||
}, [date, offset, dateValue, options]);
|
||||
const setDateRangeValue = async (value: string) => {
|
||||
if (value === 'all') {
|
||||
const result = await get(`/websites/${websiteId}/daterange`);
|
||||
const { mindate, maxdate } = result;
|
||||
|
||||
const dateCompare = useWebsites(state => state[websiteId]?.dateCompare || DEFAULT_DATE_COMPARE);
|
||||
const startDate = new Date(mindate);
|
||||
const endDate = new Date(maxdate);
|
||||
const unit = getMinimumUnit(startDate, endDate);
|
||||
|
||||
const saveDateRange = async (value: string) => {
|
||||
if (websiteId) {
|
||||
if (value === 'all') {
|
||||
const result: any = await get(`/websites/${websiteId}/daterange`);
|
||||
const { mindate, maxdate } = result;
|
||||
|
||||
const startDate = new Date(mindate);
|
||||
const endDate = new Date(maxdate);
|
||||
const unit = getMinimumUnit(startDate, endDate);
|
||||
|
||||
setWebsiteDateRange(websiteId, {
|
||||
startDate,
|
||||
endDate,
|
||||
unit,
|
||||
value,
|
||||
});
|
||||
} else {
|
||||
setWebsiteDateRange(websiteId, parseDateRange(value, locale));
|
||||
}
|
||||
setDateRange({
|
||||
startDate,
|
||||
endDate,
|
||||
unit,
|
||||
value,
|
||||
});
|
||||
} else {
|
||||
setItem(DATE_RANGE_CONFIG, value);
|
||||
setDateRangeValue(value);
|
||||
setDateRange(parseDateRange(value, locale));
|
||||
}
|
||||
};
|
||||
|
||||
const saveDateCompare = (value: string) => {
|
||||
setWebsiteDateCompare(websiteId, value);
|
||||
};
|
||||
|
||||
return { dateRange, saveDateRange, dateCompare, saveDateCompare };
|
||||
return { dateRange, dateCompare, setDateRange, setDateRangeValue };
|
||||
}
|
||||
|
|
|
|||
24
src/components/hooks/useQueryStringDate.ts
Normal file
24
src/components/hooks/useQueryStringDate.ts
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import { useNavigation } from '@/components/hooks/useNavigation';
|
||||
import { useMemo } from 'react';
|
||||
import { getCompareDate, getOffsetDateRange, parseDateRange } from '@/lib/date';
|
||||
import { useLocale } from '@/components/hooks/useLocale';
|
||||
import { DEFAULT_DATE_RANGE_VALUE } from '@/lib/constants';
|
||||
|
||||
export function useQueryStringDate(options: { ignoreOffset?: boolean } = {}) {
|
||||
const {
|
||||
query: { date = DEFAULT_DATE_RANGE_VALUE, offset = 0, compare = 'prev' },
|
||||
} = useNavigation();
|
||||
const { locale } = useLocale();
|
||||
|
||||
const dateRange = useMemo(() => {
|
||||
const dateRangeObject = parseDateRange(date, locale);
|
||||
|
||||
return !options.ignoreOffset && offset
|
||||
? getOffsetDateRange(dateRangeObject, +offset)
|
||||
: dateRangeObject;
|
||||
}, [date, offset, options]);
|
||||
|
||||
const dateCompare = getCompareDate(compare, dateRange.startDate, dateRange.endDate);
|
||||
|
||||
return { date, offset, dateRange, dateCompare };
|
||||
}
|
||||
|
|
@ -3,6 +3,8 @@ import { isAfter } from 'date-fns';
|
|||
import { ChevronRight } from '@/components/icons';
|
||||
import { useDateRange, useMessages, useNavigation } from '@/components/hooks';
|
||||
import { DateFilter } from './DateFilter';
|
||||
import { getOffsetDateRange } from '@/lib/date';
|
||||
import { useCallback } from 'react';
|
||||
|
||||
export interface WebsiteDateFilterProps {
|
||||
websiteId: string;
|
||||
|
|
@ -18,7 +20,7 @@ export function WebsiteDateFilter({
|
|||
showButtons = true,
|
||||
allowCompare,
|
||||
}: WebsiteDateFilterProps) {
|
||||
const { dateRange, saveDateRange } = useDateRange(websiteId);
|
||||
const { dateRange, setDateRange, setDateRangeValue } = useDateRange(websiteId);
|
||||
const { value, endDate } = dateRange;
|
||||
const { formatMessage, labels } = useMessages();
|
||||
const {
|
||||
|
|
@ -27,18 +29,25 @@ export function WebsiteDateFilter({
|
|||
query: { compare = 'prev', offset = 0 },
|
||||
} = useNavigation();
|
||||
const isAllTime = value === 'all';
|
||||
|
||||
const isCustomRange = value.startsWith('range');
|
||||
|
||||
const disableForward = value === 'all' || isAfter(endDate, new Date());
|
||||
|
||||
const handleChange = (date: string) => {
|
||||
saveDateRange(date);
|
||||
setDateRangeValue(date);
|
||||
router.push(updateParams({ date, offset: undefined }));
|
||||
};
|
||||
|
||||
const handleIncrement = (increment: number) => {
|
||||
router.push(updateParams({ offset: +offset + increment }));
|
||||
};
|
||||
const handleIncrement = useCallback(
|
||||
(increment: number) => {
|
||||
const offsetDate = getOffsetDateRange(dateRange, +offset + increment);
|
||||
|
||||
setDateRange(offsetDate);
|
||||
router.push(updateParams({ offset: +offset + increment }));
|
||||
},
|
||||
[offset],
|
||||
);
|
||||
|
||||
const handleSelect = (compare: any) => {
|
||||
router.push(updateParams({ compare }));
|
||||
|
|
|
|||
|
|
@ -187,9 +187,7 @@ async function rawQuery(sql: string, data: Record<string, any>, name?: string):
|
|||
return `$${params.length}${type ?? ''}`;
|
||||
});
|
||||
|
||||
return process.env.DATABASE_REPLICA_URL
|
||||
? await client.$replica().$queryRawUnsafe(query, ...params)
|
||||
: await client.$queryRawUnsafe(query, ...params);
|
||||
return client.$queryRawUnsafe(query, ...params);
|
||||
}
|
||||
|
||||
async function pagedQuery<T>(model: string, criteria: T, filters?: QueryFilters) {
|
||||
|
|
@ -283,6 +281,7 @@ function getClient() {
|
|||
url: process.env.DATABASE_URL,
|
||||
prismaClient: PrismaClient,
|
||||
logQuery: !!process.env.LOG_QUERY,
|
||||
replicaUrl: process.env.DATABASE_REPLICA_URL,
|
||||
});
|
||||
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
|
|
@ -292,7 +291,7 @@ function getClient() {
|
|||
return prisma.client;
|
||||
}
|
||||
|
||||
const client = globalThis[PRISMA] || getClient();
|
||||
const client: PrismaClient = globalThis[PRISMA] || getClient();
|
||||
|
||||
export default {
|
||||
client,
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
import { Prisma, Link } from '@/generated/prisma/client';
|
||||
import { Prisma } from '@/generated/prisma/client';
|
||||
import prisma from '@/lib/prisma';
|
||||
import { PageResult, QueryFilters } from '@/lib/types';
|
||||
import { QueryFilters } from '@/lib/types';
|
||||
|
||||
export async function findLink(criteria: Prisma.LinkFindUniqueArgs): Promise<Link> {
|
||||
export async function findLink(criteria: Prisma.LinkFindUniqueArgs) {
|
||||
return prisma.client.link.findUnique(criteria);
|
||||
}
|
||||
|
||||
export async function getLink(linkId: string): Promise<Link> {
|
||||
export async function getLink(linkId: string) {
|
||||
return findLink({
|
||||
where: {
|
||||
id: linkId,
|
||||
|
|
@ -14,10 +14,7 @@ export async function getLink(linkId: string): Promise<Link> {
|
|||
});
|
||||
}
|
||||
|
||||
export async function getLinks(
|
||||
criteria: Prisma.LinkFindManyArgs,
|
||||
filters: QueryFilters = {},
|
||||
): Promise<PageResult<Link[]>> {
|
||||
export async function getLinks(criteria: Prisma.LinkFindManyArgs, filters: QueryFilters = {}) {
|
||||
const { search } = filters;
|
||||
const { getSearchParameters, pagedQuery } = prisma;
|
||||
|
||||
|
|
@ -33,10 +30,7 @@ export async function getLinks(
|
|||
return pagedQuery('link', { ...criteria, where }, filters);
|
||||
}
|
||||
|
||||
export async function getUserLinks(
|
||||
userId: string,
|
||||
filters?: QueryFilters,
|
||||
): Promise<PageResult<Link[]>> {
|
||||
export async function getUserLinks(userId: string, filters?: QueryFilters) {
|
||||
return getLinks(
|
||||
{
|
||||
where: {
|
||||
|
|
@ -48,10 +42,7 @@ export async function getUserLinks(
|
|||
);
|
||||
}
|
||||
|
||||
export async function getTeamLinks(
|
||||
teamId: string,
|
||||
filters?: QueryFilters,
|
||||
): Promise<PageResult<Link[]>> {
|
||||
export async function getTeamLinks(teamId: string, filters?: QueryFilters) {
|
||||
return getLinks(
|
||||
{
|
||||
where: {
|
||||
|
|
@ -62,14 +53,14 @@ export async function getTeamLinks(
|
|||
);
|
||||
}
|
||||
|
||||
export async function createLink(data: Prisma.LinkUncheckedCreateInput): Promise<Link> {
|
||||
export async function createLink(data: Prisma.LinkUncheckedCreateInput) {
|
||||
return prisma.client.link.create({ data });
|
||||
}
|
||||
|
||||
export async function updateLink(linkId: string, data: any): Promise<Link> {
|
||||
export async function updateLink(linkId: string, data: any) {
|
||||
return prisma.client.link.update({ where: { id: linkId }, data });
|
||||
}
|
||||
|
||||
export async function deleteLink(linkId: string): Promise<Link> {
|
||||
export async function deleteLink(linkId: string) {
|
||||
return prisma.client.link.delete({ where: { id: linkId } });
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
import { Prisma, Pixel } from '@/generated/prisma/client';
|
||||
import { Prisma } from '@/generated/prisma/client';
|
||||
import prisma from '@/lib/prisma';
|
||||
import { PageResult, QueryFilters } from '@/lib/types';
|
||||
import { QueryFilters } from '@/lib/types';
|
||||
|
||||
export async function findPixel(criteria: Prisma.PixelFindUniqueArgs): Promise<Pixel> {
|
||||
export async function findPixel(criteria: Prisma.PixelFindUniqueArgs) {
|
||||
return prisma.client.pixel.findUnique(criteria);
|
||||
}
|
||||
|
||||
export async function getPixel(pixelId: string): Promise<Pixel> {
|
||||
export async function getPixel(pixelId: string) {
|
||||
return findPixel({
|
||||
where: {
|
||||
id: pixelId,
|
||||
|
|
@ -14,10 +14,7 @@ export async function getPixel(pixelId: string): Promise<Pixel> {
|
|||
});
|
||||
}
|
||||
|
||||
export async function getPixels(
|
||||
criteria: Prisma.PixelFindManyArgs,
|
||||
filters: QueryFilters = {},
|
||||
): Promise<PageResult<Pixel[]>> {
|
||||
export async function getPixels(criteria: Prisma.PixelFindManyArgs, filters: QueryFilters = {}) {
|
||||
const { search } = filters;
|
||||
|
||||
const where: Prisma.PixelWhereInput = {
|
||||
|
|
@ -28,10 +25,7 @@ export async function getPixels(
|
|||
return prisma.pagedQuery('pixel', { ...criteria, where }, filters);
|
||||
}
|
||||
|
||||
export async function getUserPixels(
|
||||
userId: string,
|
||||
filters?: QueryFilters,
|
||||
): Promise<PageResult<Pixel[]>> {
|
||||
export async function getUserPixels(userId: string, filters?: QueryFilters) {
|
||||
return getPixels(
|
||||
{
|
||||
where: {
|
||||
|
|
@ -42,10 +36,7 @@ export async function getUserPixels(
|
|||
);
|
||||
}
|
||||
|
||||
export async function getTeamPixels(
|
||||
teamId: string,
|
||||
filters?: QueryFilters,
|
||||
): Promise<PageResult<Pixel[]>> {
|
||||
export async function getTeamPixels(teamId: string, filters?: QueryFilters) {
|
||||
return getPixels(
|
||||
{
|
||||
where: {
|
||||
|
|
@ -56,14 +47,14 @@ export async function getTeamPixels(
|
|||
);
|
||||
}
|
||||
|
||||
export async function createPixel(data: Prisma.PixelUncheckedCreateInput): Promise<Pixel> {
|
||||
export async function createPixel(data: Prisma.PixelUncheckedCreateInput) {
|
||||
return prisma.client.pixel.create({ data });
|
||||
}
|
||||
|
||||
export async function updatePixel(pixelId: string, data: any): Promise<Pixel> {
|
||||
export async function updatePixel(pixelId: string, data: any) {
|
||||
return prisma.client.pixel.update({ where: { id: pixelId }, data });
|
||||
}
|
||||
|
||||
export async function deletePixel(pixelId: string): Promise<Pixel> {
|
||||
export async function deletePixel(pixelId: string) {
|
||||
return prisma.client.pixel.delete({ where: { id: pixelId } });
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +1,13 @@
|
|||
import { Prisma, Report } from '@/generated/prisma/client';
|
||||
import { Prisma } from '@/generated/prisma/client';
|
||||
import prisma from '@/lib/prisma';
|
||||
import { PageResult, QueryFilters } from '@/lib/types';
|
||||
import { QueryFilters } from '@/lib/types';
|
||||
import ReportFindManyArgs = Prisma.ReportFindManyArgs;
|
||||
|
||||
async function findReport(criteria: Prisma.ReportFindUniqueArgs): Promise<Report> {
|
||||
async function findReport(criteria: Prisma.ReportFindUniqueArgs) {
|
||||
return prisma.client.report.findUnique(criteria);
|
||||
}
|
||||
|
||||
export async function getReport(reportId: string): Promise<Report> {
|
||||
export async function getReport(reportId: string) {
|
||||
return findReport({
|
||||
where: {
|
||||
id: reportId,
|
||||
|
|
@ -15,10 +15,7 @@ export async function getReport(reportId: string): Promise<Report> {
|
|||
});
|
||||
}
|
||||
|
||||
export async function getReports(
|
||||
criteria: ReportFindManyArgs,
|
||||
filters: QueryFilters = {},
|
||||
): Promise<PageResult<Report[]>> {
|
||||
export async function getReports(criteria: ReportFindManyArgs, filters: QueryFilters = {}) {
|
||||
const { search } = filters;
|
||||
|
||||
const where: Prisma.ReportWhereInput = {
|
||||
|
|
@ -48,10 +45,7 @@ export async function getReports(
|
|||
return prisma.pagedQuery('report', { ...criteria, where }, filters);
|
||||
}
|
||||
|
||||
export async function getUserReports(
|
||||
userId: string,
|
||||
filters?: QueryFilters,
|
||||
): Promise<PageResult<Report[]>> {
|
||||
export async function getUserReports(userId: string, filters?: QueryFilters) {
|
||||
return getReports(
|
||||
{
|
||||
where: {
|
||||
|
|
@ -70,10 +64,7 @@ export async function getUserReports(
|
|||
);
|
||||
}
|
||||
|
||||
export async function getWebsiteReports(
|
||||
websiteId: string,
|
||||
filters: QueryFilters = {},
|
||||
): Promise<PageResult<Report[]>> {
|
||||
export async function getWebsiteReports(websiteId: string, filters: QueryFilters = {}) {
|
||||
return getReports(
|
||||
{
|
||||
where: {
|
||||
|
|
@ -84,14 +75,14 @@ export async function getWebsiteReports(
|
|||
);
|
||||
}
|
||||
|
||||
export async function createReport(data: Prisma.ReportUncheckedCreateInput): Promise<Report> {
|
||||
export async function createReport(data: Prisma.ReportUncheckedCreateInput) {
|
||||
return prisma.client.report.create({ data });
|
||||
}
|
||||
|
||||
export async function updateReport(reportId: string, data: any): Promise<Report> {
|
||||
export async function updateReport(reportId: string, data: any) {
|
||||
return prisma.client.report.update({ where: { id: reportId }, data });
|
||||
}
|
||||
|
||||
export async function deleteReport(reportId: string): Promise<Report> {
|
||||
export async function deleteReport(reportId: string) {
|
||||
return prisma.client.report.delete({ where: { id: reportId } });
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
import prisma from '@/lib/prisma';
|
||||
import { Prisma, Segment } from '@/generated/prisma/client';
|
||||
import { PageResult, QueryFilters } from '@/lib/types';
|
||||
import { Prisma } from '@/generated/prisma/client';
|
||||
import { QueryFilters } from '@/lib/types';
|
||||
|
||||
async function findSegment(criteria: Prisma.SegmentFindUniqueArgs): Promise<Segment> {
|
||||
return prisma.client.Segment.findUnique(criteria);
|
||||
async function findSegment(criteria: Prisma.SegmentFindUniqueArgs) {
|
||||
return prisma.client.segment.findUnique(criteria);
|
||||
}
|
||||
|
||||
export async function getSegment(segmentId: string): Promise<Segment> {
|
||||
export async function getSegment(segmentId: string) {
|
||||
return findSegment({
|
||||
where: {
|
||||
id: segmentId,
|
||||
|
|
@ -14,10 +14,7 @@ export async function getSegment(segmentId: string): Promise<Segment> {
|
|||
});
|
||||
}
|
||||
|
||||
export async function getSegments(
|
||||
criteria: Prisma.SegmentFindManyArgs,
|
||||
filters: QueryFilters,
|
||||
): Promise<PageResult<Segment[]>> {
|
||||
export async function getSegments(criteria: Prisma.SegmentFindManyArgs, filters: QueryFilters) {
|
||||
const { search } = filters;
|
||||
const { getSearchParameters, pagedQuery } = prisma;
|
||||
|
||||
|
|
@ -33,17 +30,13 @@ export async function getSegments(
|
|||
return pagedQuery('segment', { ...criteria, where }, filters);
|
||||
}
|
||||
|
||||
export async function getWebsiteSegment(websiteId: string, segmentId: string): Promise<Segment> {
|
||||
return prisma.client.Segment.findFirst({
|
||||
export async function getWebsiteSegment(websiteId: string, segmentId: string) {
|
||||
return prisma.client.segment.findFirst({
|
||||
where: { id: segmentId, websiteId },
|
||||
});
|
||||
}
|
||||
|
||||
export async function getWebsiteSegments(
|
||||
websiteId: string,
|
||||
type: string,
|
||||
filters?: QueryFilters,
|
||||
): Promise<PageResult<Segment[]>> {
|
||||
export async function getWebsiteSegments(websiteId: string, type: string, filters?: QueryFilters) {
|
||||
return getSegments(
|
||||
{
|
||||
where: {
|
||||
|
|
@ -55,17 +48,14 @@ export async function getWebsiteSegments(
|
|||
);
|
||||
}
|
||||
|
||||
export async function createSegment(data: Prisma.SegmentUncheckedCreateInput): Promise<Segment> {
|
||||
return prisma.client.Segment.create({ data });
|
||||
export async function createSegment(data: Prisma.SegmentUncheckedCreateInput) {
|
||||
return prisma.client.segment.create({ data });
|
||||
}
|
||||
|
||||
export async function updateSegment(
|
||||
SegmentId: string,
|
||||
data: Prisma.SegmentUpdateInput,
|
||||
): Promise<Segment> {
|
||||
return prisma.client.Segment.update({ where: { id: SegmentId }, data });
|
||||
export async function updateSegment(SegmentId: string, data: Prisma.SegmentUpdateInput) {
|
||||
return prisma.client.segment.update({ where: { id: SegmentId }, data });
|
||||
}
|
||||
|
||||
export async function deleteSegment(SegmentId: string): Promise<Segment> {
|
||||
return prisma.client.Segment.delete({ where: { id: SegmentId } });
|
||||
export async function deleteSegment(SegmentId: string) {
|
||||
return prisma.client.segment.delete({ where: { id: SegmentId } });
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,7 +9,10 @@ export async function findTeam(criteria: Prisma.TeamFindUniqueArgs): Promise<Tea
|
|||
return prisma.client.team.findUnique(criteria);
|
||||
}
|
||||
|
||||
export async function getTeam(teamId: string, options: { includeMembers?: boolean } = {}) {
|
||||
export async function getTeam(
|
||||
teamId: string,
|
||||
options: { includeMembers?: boolean } = {},
|
||||
): Promise<Team> {
|
||||
const { includeMembers } = options;
|
||||
|
||||
return findTeam({
|
||||
|
|
@ -129,11 +132,9 @@ export async function updateTeam(teamId: string, data: Prisma.TeamUpdateInput):
|
|||
});
|
||||
}
|
||||
|
||||
export async function deleteTeam(
|
||||
teamId: string,
|
||||
): Promise<Promise<[Prisma.BatchPayload, Prisma.BatchPayload, Team]>> {
|
||||
export async function deleteTeam(teamId: string) {
|
||||
const { client, transaction } = prisma;
|
||||
const cloudMode = !!process.env.CLOUD_URL;
|
||||
const cloudMode = !!process.env.CLOUD_MODE;
|
||||
|
||||
if (cloudMode) {
|
||||
return transaction([
|
||||
|
|
|
|||
|
|
@ -1,14 +1,14 @@
|
|||
import { uuid } from '@/lib/crypto';
|
||||
import { Prisma, TeamUser } from '@/generated/prisma/client';
|
||||
import { Prisma } from '@/generated/prisma/client';
|
||||
import prisma from '@/lib/prisma';
|
||||
import { PageResult, QueryFilters } from '@/lib/types';
|
||||
import { QueryFilters } from '@/lib/types';
|
||||
import TeamUserFindManyArgs = Prisma.TeamUserFindManyArgs;
|
||||
|
||||
export async function findTeamUser(criteria: Prisma.TeamUserFindUniqueArgs): Promise<TeamUser> {
|
||||
export async function findTeamUser(criteria: Prisma.TeamUserFindUniqueArgs) {
|
||||
return prisma.client.teamUser.findUnique(criteria);
|
||||
}
|
||||
|
||||
export async function getTeamUser(teamId: string, userId: string): Promise<TeamUser> {
|
||||
export async function getTeamUser(teamId: string, userId: string) {
|
||||
return prisma.client.teamUser.findFirst({
|
||||
where: {
|
||||
teamId,
|
||||
|
|
@ -17,10 +17,7 @@ export async function getTeamUser(teamId: string, userId: string): Promise<TeamU
|
|||
});
|
||||
}
|
||||
|
||||
export async function getTeamUsers(
|
||||
criteria: TeamUserFindManyArgs,
|
||||
filters?: QueryFilters,
|
||||
): Promise<PageResult<TeamUser[]>> {
|
||||
export async function getTeamUsers(criteria: TeamUserFindManyArgs, filters?: QueryFilters) {
|
||||
const { search } = filters;
|
||||
|
||||
const where: Prisma.TeamUserWhereInput = {
|
||||
|
|
@ -38,11 +35,7 @@ export async function getTeamUsers(
|
|||
);
|
||||
}
|
||||
|
||||
export async function createTeamUser(
|
||||
userId: string,
|
||||
teamId: string,
|
||||
role: string,
|
||||
): Promise<TeamUser> {
|
||||
export async function createTeamUser(userId: string, teamId: string, role: string) {
|
||||
return prisma.client.teamUser.create({
|
||||
data: {
|
||||
id: uuid(),
|
||||
|
|
@ -53,10 +46,7 @@ export async function createTeamUser(
|
|||
});
|
||||
}
|
||||
|
||||
export async function updateTeamUser(
|
||||
teamUserId: string,
|
||||
data: Prisma.TeamUserUpdateInput,
|
||||
): Promise<TeamUser> {
|
||||
export async function updateTeamUser(teamUserId: string, data: Prisma.TeamUserUpdateInput) {
|
||||
return prisma.client.teamUser.update({
|
||||
where: {
|
||||
id: teamUserId,
|
||||
|
|
@ -65,7 +55,7 @@ export async function updateTeamUser(
|
|||
});
|
||||
}
|
||||
|
||||
export async function deleteTeamUser(teamId: string, userId: string): Promise<Prisma.BatchPayload> {
|
||||
export async function deleteTeamUser(teamId: string, userId: string) {
|
||||
return prisma.client.teamUser.deleteMany({
|
||||
where: {
|
||||
teamId,
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import { Prisma, User } from '@/generated/prisma/client';
|
||||
import { Prisma } from '@/generated/prisma/client';
|
||||
import { ROLES } from '@/lib/constants';
|
||||
import prisma from '@/lib/prisma';
|
||||
import { PageResult, Role, QueryFilters } from '@/lib/types';
|
||||
import { Role, QueryFilters } from '@/lib/types';
|
||||
import { getRandomChars } from '@/lib/generate';
|
||||
import UserFindManyArgs = Prisma.UserFindManyArgs;
|
||||
|
||||
|
|
@ -10,10 +10,7 @@ export interface GetUserOptions {
|
|||
showDeleted?: boolean;
|
||||
}
|
||||
|
||||
async function findUser(
|
||||
criteria: Prisma.UserFindUniqueArgs,
|
||||
options: GetUserOptions = {},
|
||||
): Promise<User> {
|
||||
async function findUser(criteria: Prisma.UserFindUniqueArgs, options: GetUserOptions = {}) {
|
||||
const { includePassword = false, showDeleted = false } = options;
|
||||
|
||||
return prisma.client.user.findUnique({
|
||||
|
|
@ -47,10 +44,7 @@ export async function getUserByUsername(username: string, options: GetUserOption
|
|||
return findUser({ where: { username } }, options);
|
||||
}
|
||||
|
||||
export async function getUsers(
|
||||
criteria: UserFindManyArgs,
|
||||
filters: QueryFilters = {},
|
||||
): Promise<PageResult<User[]>> {
|
||||
export async function getUsers(criteria: UserFindManyArgs, filters: QueryFilters = {}) {
|
||||
const { search } = filters;
|
||||
|
||||
const where: Prisma.UserWhereInput = {
|
||||
|
|
@ -78,11 +72,7 @@ export async function createUser(data: {
|
|||
username: string;
|
||||
password: string;
|
||||
role: Role;
|
||||
}): Promise<{
|
||||
id: string;
|
||||
username: string;
|
||||
role: string;
|
||||
}> {
|
||||
}) {
|
||||
return prisma.client.user.create({
|
||||
data,
|
||||
select: {
|
||||
|
|
@ -93,7 +83,7 @@ export async function createUser(data: {
|
|||
});
|
||||
}
|
||||
|
||||
export async function updateUser(userId: string, data: Prisma.UserUpdateInput): Promise<User> {
|
||||
export async function updateUser(userId: string, data: Prisma.UserUpdateInput) {
|
||||
return prisma.client.user.update({
|
||||
where: {
|
||||
id: userId,
|
||||
|
|
@ -108,21 +98,9 @@ export async function updateUser(userId: string, data: Prisma.UserUpdateInput):
|
|||
});
|
||||
}
|
||||
|
||||
export async function deleteUser(
|
||||
userId: string,
|
||||
): Promise<
|
||||
[
|
||||
Prisma.BatchPayload,
|
||||
Prisma.BatchPayload,
|
||||
Prisma.BatchPayload,
|
||||
Prisma.BatchPayload,
|
||||
Prisma.BatchPayload,
|
||||
Prisma.BatchPayload,
|
||||
User,
|
||||
]
|
||||
> {
|
||||
export async function deleteUser(userId: string) {
|
||||
const { client, transaction } = prisma;
|
||||
const cloudMode = !!process.env.CLOUD_URL;
|
||||
const cloudMode = !!process.env.CLOUD_MODE;
|
||||
|
||||
const websites = await client.website.findMany({
|
||||
where: { userId },
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
import { Prisma, Website } from '@/generated/prisma/client';
|
||||
import { Prisma } from '@/generated/prisma/client';
|
||||
import redis from '@/lib/redis';
|
||||
import prisma from '@/lib/prisma';
|
||||
import { PageResult, QueryFilters } from '@/lib/types';
|
||||
import { QueryFilters } from '@/lib/types';
|
||||
import { ROLES } from '@/lib/constants';
|
||||
|
||||
export async function findWebsite(criteria: Prisma.WebsiteFindUniqueArgs): Promise<Website> {
|
||||
export async function findWebsite(criteria: Prisma.WebsiteFindUniqueArgs) {
|
||||
return prisma.client.website.findUnique(criteria);
|
||||
}
|
||||
|
||||
|
|
@ -25,10 +25,7 @@ export async function getSharedWebsite(shareId: string) {
|
|||
});
|
||||
}
|
||||
|
||||
export async function getWebsites(
|
||||
criteria: Prisma.WebsiteFindManyArgs,
|
||||
filters: QueryFilters,
|
||||
): Promise<PageResult<Website[]>> {
|
||||
export async function getWebsites(criteria: Prisma.WebsiteFindManyArgs, filters: QueryFilters) {
|
||||
const { search } = filters;
|
||||
const { getSearchParameters, pagedQuery } = prisma;
|
||||
|
||||
|
|
@ -46,10 +43,7 @@ export async function getWebsites(
|
|||
return pagedQuery('website', { ...criteria, where }, filters);
|
||||
}
|
||||
|
||||
export async function getAllUserWebsitesIncludingTeamOwner(
|
||||
userId: string,
|
||||
filters?: QueryFilters,
|
||||
): Promise<PageResult<Website[]>> {
|
||||
export async function getAllUserWebsitesIncludingTeamOwner(userId: string, filters?: QueryFilters) {
|
||||
return getWebsites(
|
||||
{
|
||||
where: {
|
||||
|
|
@ -76,10 +70,7 @@ export async function getAllUserWebsitesIncludingTeamOwner(
|
|||
);
|
||||
}
|
||||
|
||||
export async function getUserWebsites(
|
||||
userId: string,
|
||||
filters?: QueryFilters,
|
||||
): Promise<PageResult<Website[]>> {
|
||||
export async function getUserWebsites(userId: string, filters?: QueryFilters) {
|
||||
return getWebsites(
|
||||
{
|
||||
where: {
|
||||
|
|
@ -101,10 +92,7 @@ export async function getUserWebsites(
|
|||
);
|
||||
}
|
||||
|
||||
export async function getTeamWebsites(
|
||||
teamId: string,
|
||||
filters?: QueryFilters,
|
||||
): Promise<PageResult<Website[]>> {
|
||||
export async function getTeamWebsites(teamId: string, filters?: QueryFilters) {
|
||||
return getWebsites(
|
||||
{
|
||||
where: {
|
||||
|
|
@ -125,7 +113,7 @@ export async function getTeamWebsites(
|
|||
|
||||
export async function createWebsite(
|
||||
data: Prisma.WebsiteCreateInput | Prisma.WebsiteUncheckedCreateInput,
|
||||
): Promise<Website> {
|
||||
) {
|
||||
return prisma.client.website.create({
|
||||
data,
|
||||
});
|
||||
|
|
@ -134,7 +122,7 @@ export async function createWebsite(
|
|||
export async function updateWebsite(
|
||||
websiteId: string,
|
||||
data: Prisma.WebsiteUpdateInput | Prisma.WebsiteUncheckedUpdateInput,
|
||||
): Promise<Website> {
|
||||
) {
|
||||
return prisma.client.website.update({
|
||||
where: {
|
||||
id: websiteId,
|
||||
|
|
@ -143,11 +131,9 @@ export async function updateWebsite(
|
|||
});
|
||||
}
|
||||
|
||||
export async function resetWebsite(
|
||||
websiteId: string,
|
||||
): Promise<[Prisma.BatchPayload, Prisma.BatchPayload, Website]> {
|
||||
export async function resetWebsite(websiteId: string) {
|
||||
const { client, transaction } = prisma;
|
||||
const cloudMode = !!process.env.CLOUD_URL;
|
||||
const cloudMode = !!process.env.CLOUD_MODE;
|
||||
|
||||
return transaction([
|
||||
client.eventData.deleteMany({
|
||||
|
|
@ -177,11 +163,9 @@ export async function resetWebsite(
|
|||
});
|
||||
}
|
||||
|
||||
export async function deleteWebsite(
|
||||
websiteId: string,
|
||||
): Promise<[Prisma.BatchPayload, Prisma.BatchPayload, Website]> {
|
||||
export async function deleteWebsite(websiteId: string) {
|
||||
const { client, transaction } = prisma;
|
||||
const cloudMode = !!process.env.CLOUD_URL;
|
||||
const cloudMode = !!process.env.CLOUD_MODE;
|
||||
|
||||
return transaction([
|
||||
client.eventData.deleteMany({
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue