2023-09-04 17:18:43 +00:00
|
|
|
import { prisma } from './_client';
|
2023-09-12 15:04:39 +00:00
|
|
|
import { QueryFilters, parseFilters, getDateQuery } from '../utils/prisma';
|
|
|
|
import { DEFAULT_RESET_DATE, EVENT_TYPE } from '../utils/const';
|
2023-09-14 16:53:03 +00:00
|
|
|
import { Prisma } from '@prisma/client';
|
2023-09-04 17:18:43 +00:00
|
|
|
|
2023-09-06 06:14:12 +00:00
|
|
|
export async function getWorkspaceUser(workspaceId: string, userId: string) {
|
|
|
|
const info = await prisma.workspacesOnUsers.findFirst({
|
|
|
|
where: {
|
|
|
|
workspaceId,
|
|
|
|
userId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2023-09-04 17:18:43 +00:00
|
|
|
export async function checkIsWorkspaceUser(
|
|
|
|
workspaceId: string,
|
|
|
|
userId: string
|
|
|
|
) {
|
2023-09-06 06:14:12 +00:00
|
|
|
const info = await getWorkspaceUser(workspaceId, userId);
|
2023-09-04 17:18:43 +00:00
|
|
|
|
2023-09-06 06:14:12 +00:00
|
|
|
if (info) {
|
2023-09-04 17:18:43 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getWorkspaceWebsites(workspaceId: string) {
|
|
|
|
const workspace = await prisma.workspace.findUnique({
|
|
|
|
where: {
|
|
|
|
id: workspaceId,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
websites: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return workspace?.websites ?? [];
|
|
|
|
}
|
|
|
|
|
2023-09-05 07:32:16 +00:00
|
|
|
export async function getWorkspaceWebsiteInfo(
|
|
|
|
workspaceId: string,
|
|
|
|
websiteId: string
|
|
|
|
) {
|
|
|
|
const websiteInfo = await prisma.website.findUnique({
|
|
|
|
where: {
|
|
|
|
id: websiteId,
|
|
|
|
workspaceId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return websiteInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function updateWorkspaceWebsiteInfo(
|
|
|
|
workspaceId: string,
|
|
|
|
websiteId: string,
|
|
|
|
name: string,
|
|
|
|
domain: string
|
|
|
|
) {
|
|
|
|
const websiteInfo = await prisma.website.update({
|
|
|
|
where: {
|
|
|
|
id: websiteId,
|
|
|
|
workspaceId,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
name,
|
|
|
|
domain,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return websiteInfo;
|
|
|
|
}
|
|
|
|
|
2023-09-04 17:18:43 +00:00
|
|
|
export async function addWorkspaceWebsite(
|
|
|
|
workspaceId: string,
|
|
|
|
name: string,
|
|
|
|
domain: string
|
|
|
|
) {
|
|
|
|
const website = await prisma.website.create({
|
|
|
|
data: {
|
|
|
|
name,
|
|
|
|
domain,
|
|
|
|
workspaceId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return website;
|
|
|
|
}
|
2023-09-06 06:14:12 +00:00
|
|
|
|
|
|
|
export async function deleteWorkspaceWebsite(
|
|
|
|
workspaceId: string,
|
|
|
|
websiteId: string
|
|
|
|
) {
|
|
|
|
const website = await prisma.website.delete({
|
|
|
|
where: {
|
|
|
|
id: websiteId,
|
|
|
|
workspaceId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return website;
|
|
|
|
}
|
2023-09-12 15:04:39 +00:00
|
|
|
|
2023-09-16 08:12:19 +00:00
|
|
|
export async function getWorkspaceWebsiteDateRange(websiteId: string) {
|
|
|
|
const { params } = await parseFilters(websiteId, {
|
|
|
|
startDate: new Date(DEFAULT_RESET_DATE),
|
|
|
|
});
|
|
|
|
|
|
|
|
const res = await prisma.websiteEvent.aggregate({
|
|
|
|
_max: {
|
|
|
|
createdAt: true,
|
|
|
|
},
|
|
|
|
_min: {
|
|
|
|
createdAt: true,
|
|
|
|
},
|
|
|
|
where: {
|
|
|
|
websiteId,
|
|
|
|
createdAt: {
|
|
|
|
gt: params.startDate,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
max: res._max.createdAt,
|
|
|
|
min: res._min.createdAt,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-09-12 15:04:39 +00:00
|
|
|
export async function getWorkspaceWebsitePageviewStats(
|
|
|
|
websiteId: string,
|
|
|
|
filters: QueryFilters
|
|
|
|
) {
|
|
|
|
const { timezone = 'utc', unit = 'day' } = filters;
|
|
|
|
const { filterQuery, joinSession, params } = await parseFilters(websiteId, {
|
|
|
|
...filters,
|
|
|
|
});
|
|
|
|
|
|
|
|
return prisma.$queryRaw`
|
|
|
|
select
|
2023-09-14 16:53:03 +00:00
|
|
|
${getDateQuery('"WebsiteEvent"."createdAt"', unit, timezone)} x,
|
|
|
|
count(1) y
|
|
|
|
from "WebsiteEvent"
|
2023-09-16 08:12:19 +00:00
|
|
|
${joinSession}
|
2023-09-14 16:53:03 +00:00
|
|
|
where "WebsiteEvent"."websiteId" = ${params.websiteId}::uuid
|
2023-09-16 07:50:36 +00:00
|
|
|
and "WebsiteEvent"."createdAt" between ${
|
|
|
|
params.startDate
|
|
|
|
}::timestamptz and ${params.endDate}::timestamptz
|
2023-09-14 16:53:03 +00:00
|
|
|
and "WebsiteEvent"."eventType" = ${EVENT_TYPE.pageView}
|
2023-09-16 07:50:36 +00:00
|
|
|
${filterQuery}
|
2023-09-12 15:04:39 +00:00
|
|
|
group by 1
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
2023-09-16 08:12:19 +00:00
|
|
|
export async function getWorkspaceWebsiteSessionStats(
|
|
|
|
websiteId: string,
|
|
|
|
filters: QueryFilters
|
|
|
|
) {
|
|
|
|
const { timezone = 'utc', unit = 'day' } = filters;
|
|
|
|
const { filterQuery, joinSession, params } = await parseFilters(websiteId, {
|
|
|
|
...filters,
|
2023-09-12 15:04:39 +00:00
|
|
|
});
|
|
|
|
|
2023-09-16 08:12:19 +00:00
|
|
|
return prisma.$queryRaw`
|
|
|
|
select
|
|
|
|
${getDateQuery('"WebsiteEvent"."createdAt"', unit, timezone)} x,
|
|
|
|
count(distinct "WebsiteEvent"."sessionId") y
|
|
|
|
from "WebsiteEvent"
|
|
|
|
${joinSession}
|
|
|
|
where "WebsiteEvent"."websiteId" = ${params.websiteId}::uuid
|
|
|
|
and "WebsiteEvent"."createdAt" between ${
|
|
|
|
params.startDate
|
|
|
|
}::timestamptz and ${params.endDate}::timestamptz
|
|
|
|
and "WebsiteEvent"."eventType" = ${EVENT_TYPE.pageView}
|
|
|
|
${filterQuery}
|
|
|
|
group by 1
|
|
|
|
`;
|
2023-09-12 15:04:39 +00:00
|
|
|
}
|