Skip to content

armgit / umami

Public Imported from GitHub
0

Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.

Add queries for new schema.

Brian Cao5aa8187e422e
diff --git a/db/postgresql/schema.prisma b/db/postgresql/schema.prisma
index b6a7610a1..538a1fa8c 100644
--- a/db/postgresql/schema.prisma
+++ b/db/postgresql/schema.prisma
@@ -12,6 +12,7 @@ model User {
username String @unique @db.VarChar(255)
password String @db.VarChar(60)
createdAt DateTime? @default(now()) @map("created_at") @db.Timestamptz(6)
+ isDeleted Boolean @default(false) @map("is_deleted")
groupRole GroupRole[]
groupUser GroupUser[]
@@ -118,11 +119,12 @@ model GroupUser {
}
model Permission {
- id String @id() @unique() @map("permission_id") @db.Uuid
- name String @unique() @db.VarChar(255)
- description String? @db.VarChar(255)
- createdAt DateTime? @default(now()) @map("created_at") @db.Timestamptz(6)
- isDeleted Boolean @default(false) @map("is_deleted")
+ id String @id() @unique() @map("permission_id") @db.Uuid
+ name String @unique() @db.VarChar(255)
+ description String? @db.VarChar(255)
+ createdAt DateTime? @default(now()) @map("created_at") @db.Timestamptz(6)
+ isDeleted Boolean @default(false) @map("is_deleted")
+ RolePermission RolePermission[]
@@map("permission")
}
@@ -134,21 +136,37 @@ model Role {
createdAt DateTime? @default(now()) @map("created_at") @db.Timestamptz(6)
isDeleted Boolean @default(false) @map("is_deleted")
- groupRoles GroupRole[]
- userRoles UserRole[]
+ groupRoles GroupRole[]
+ userRoles UserRole[]
+ RolePermission RolePermission[]
@@map("role")
}
+model RolePermission {
+ id String @id() @unique() @map("role_permission_id") @db.Uuid
+ roleId String @map("role_id") @db.Uuid
+ permissionId String @map("permission_id") @db.Uuid
+ createdAt DateTime? @default(now()) @map("created_at") @db.Timestamptz(6)
+ isDeleted Boolean @default(false) @map("is_deleted")
+
+ role Role @relation(fields: [roleId], references: [id])
+ permission Permission @relation(fields: [permissionId], references: [id])
+
+ @@map("role_permission")
+}
+
model UserRole {
id String @id() @unique() @map("user_role_id") @db.Uuid
roleId String @map("role_id") @db.Uuid
userId String @map("user_id") @db.Uuid
+ teamId String? @map("team_id") @db.Uuid
createdAt DateTime? @default(now()) @map("created_at") @db.Timestamptz(6)
isDeleted Boolean @default(false) @map("is_deleted")
- role Role @relation(fields: [roleId], references: [id])
- user User @relation(fields: [userId], references: [id])
+ role Role @relation(fields: [roleId], references: [id])
+ user User @relation(fields: [userId], references: [id])
+ team Team? @relation(fields: [teamId], references: [id])
@@map("user_role")
}
@@ -161,6 +179,7 @@ model Team {
teamWebsites TeamWebsite[]
teamUsers TeamUser[]
+ UserRole UserRole[]
@@map("team")
}
@@ -184,6 +203,7 @@ model TeamUser {
id String @id() @unique() @map("team_user_id") @db.Uuid
teamId String @map("team_id") @db.Uuid
userId String @map("user_id") @db.Uuid
+ isOwner Boolean @default(false) @map("is_owner")
createdAt DateTime? @default(now()) @map("created_at") @db.Timestamptz(6)
isDeleted Boolean @default(false) @map("is_deleted")
@@ -198,6 +218,7 @@ model UserWebsite {
userId String @map("user_id") @db.Uuid
websiteId String @map("website_id") @db.Uuid
createdAt DateTime? @default(now()) @map("created_at") @db.Timestamptz(6)
+ isDeleted Boolean @default(false) @map("is_deleted")
website Website @relation(fields: [websiteId], references: [id])
user User @relation(fields: [userId], references: [id])
diff --git a/pages/api/websites/[id]/stats.ts b/pages/api/websites/[id]/stats.ts
index 0ba65bf21..d9a581a9b 100644
--- a/pages/api/websites/[id]/stats.ts
+++ b/pages/api/websites/[id]/stats.ts
@@ -52,8 +52,8 @@ export default async (
const prevEndDate = new Date(+end_at - distance);
const metrics = await getWebsiteStats(websiteId, {
- start_at: startDate,
- end_at: endDate,
+ startDate,
+ endDate,
filters: {
url,
referrer,
@@ -64,8 +64,8 @@ export default async (
},
});
const prevPeriod = await getWebsiteStats(websiteId, {
- start_at: prevStartDate,
- end_at: prevEndDate,
+ startDate: prevStartDate,
+ endDate: prevEndDate,
filters: {
url,
referrer,
diff --git a/pages/api/websites/index.ts b/pages/api/websites/index.ts
index 1b0b0586b..c4f2b3ea4 100644
--- a/pages/api/websites/index.ts
+++ b/pages/api/websites/index.ts
@@ -1,9 +1,9 @@
-import { createWebsite, getAllWebsites, getUserWebsites } from 'queries';
-import { ok, methodNotAllowed, getRandomChars } from 'next-basics';
-import { useAuth, useCors } from 'lib/middleware';
-import { uuid } from 'lib/crypto';
import { NextApiRequestQueryBody } from 'interface/api/nextApi';
+import { uuid } from 'lib/crypto';
+import { useAuth, useCors } from 'lib/middleware';
import { NextApiResponse } from 'next';
+import { getRandomChars, methodNotAllowed, ok } from 'next-basics';
+import { createWebsiteByUser, getAllWebsites, getWebsitesByUserId } from 'queries';
export interface WebsitesReqeustQuery {
include_all?: boolean;
@@ -30,7 +30,7 @@ export default async (
const { include_all } = req.query;
const websites =
- isAdmin && include_all ? await getAllWebsites() : await getUserWebsites(userId);
+ isAdmin && include_all ? await getAllWebsites() : await getWebsitesByUserId(userId);
return ok(res, websites);
}
@@ -39,7 +39,7 @@ export default async (
const { name, domain, enableShareUrl } = req.body;
const shareId = enableShareUrl ? getRandomChars(8) : null;
- const website = await createWebsite(userId, { id: uuid(), name, domain, shareId });
+ const website = await createWebsiteByUser(userId, { id: uuid(), name, domain, shareId });
return ok(res, website);
}
diff --git a/queries/admin/permission.ts b/queries/admin/permission.ts
new file mode 100644
index 000000000..37d1647ea
--- /dev/null
+++ b/queries/admin/permission.ts
@@ -0,0 +1,41 @@
+import { Prisma, Permission } from '@prisma/client';
+import prisma from 'lib/prisma';
+
+export async function createPermission(data: Prisma.PermissionCreateInput): Promise<Permission> {
+ return prisma.client.permission.create({
+ data,
+ });
+}
+
+export async function getPermission(where: Prisma.PermissionWhereUniqueInput): Promise<Permission> {
+ return prisma.client.permission.findUnique({
+ where,
+ });
+}
+
+export async function getPermissions(where: Prisma.PermissionWhereInput): Promise<Permission[]> {
+ return prisma.client.permission.findMany({
+ where,
+ });
+}
+
+export async function updatePermission(
+ data: Prisma.PermissionUpdateInput,
+ where: Prisma.PermissionWhereUniqueInput,
+): Promise<Permission> {
+ return prisma.client.permission.update({
+ data,
+ where,
+ });
+}
+
+export async function deletePermission(permissionId: string): Promise<Permission> {
+ return prisma.client.permission.update({
+ data: {
+ isDeleted: true,
+ },
+ where: {
+ id: permissionId,
+ },
+ });
+}
diff --git a/queries/admin/role.ts b/queries/admin/role.ts
new file mode 100644
index 000000000..2bf39930b
--- /dev/null
+++ b/queries/admin/role.ts
@@ -0,0 +1,57 @@
+import { Prisma, Role } from '@prisma/client';
+import prisma from 'lib/prisma';
+
+export async function createRole(data: {
+ id: string;
+ name: string;
+ description: string;
+}): Promise<Role> {
+ return prisma.client.role.create({
+ data,
+ });
+}
+
+export async function getRole(where: Prisma.RoleWhereUniqueInput): Promise<Role> {
+ return prisma.client.role.findUnique({
+ where,
+ });
+}
+
+export async function getRoles(where: Prisma.RoleWhereInput): Promise<Role[]> {
+ return prisma.client.role.findMany({
+ where,
+ });
+}
+
+export async function getRolesByUserId(userId: string): Promise<Role[]> {
+ return prisma.client.role.findMany({
+ where: {
+ userRoles: {
+ every: {
+ userId,
+ },
+ },
+ },
+ });
+}
+
+export async function updateRole(
+ data: Prisma.RoleUpdateInput,
+ where: Prisma.RoleWhereUniqueInput,
+): Promise<Role> {
+ return prisma.client.role.update({
+ data,
+ where,
+ });
+}
+
+export async function deleteRole(roleId: string): Promise<Role> {
+ return prisma.client.role.update({
+ data: {
+ isDeleted: true,
+ },
+ where: {
+ id: roleId,
+ },
+ });
+}
diff --git a/queries/admin/team.ts b/queries/admin/team.ts
new file mode 100644
index 000000000..71b5f8074
--- /dev/null
+++ b/queries/admin/team.ts
@@ -0,0 +1,56 @@
+import { Prisma, Role, Team, TeamUser } from '@prisma/client';
+import prisma from 'lib/prisma';
+
+export async function createTeam(data: Prisma.RoleCreateInput): Promise<Role> {
+ return prisma.client.role.create({
+ data,
+ });
+}
+
+export async function getTeam(where: Prisma.RoleWhereUniqueInput): Promise<Role> {
+ return prisma.client.role.findUnique({
+ where,
+ });
+}
+
+export async function getTeams(where: Prisma.RoleWhereInput): Promise<Role[]> {
+ return prisma.client.role.findMany({
+ where,
+ });
+}
+
+export async function getTeamsByUserId(userId: string): Promise<
+ (TeamUser & {
+ team: Team;
+ })[]
+> {
+ return prisma.client.teamUser.findMany({
+ where: {
+ userId,
+ },
+ include: {
+ team: true,
+ },
+ });
+}
+
+export async function updateTeam(
+ data: Prisma.RoleUpdateInput,
+ where: Prisma.RoleWhereUniqueInput,
+): Promise<Role> {
+ return prisma.client.role.update({
+ data,
+ where,
+ });
+}
+
+export async function deleteTeam(teamId: string): Promise<Role> {
+ return prisma.client.role.update({
+ data: {
+ isDeleted: true,
+ },
+ where: {
+ id: teamId,
+ },
+ });
+}
diff --git a/queries/admin/teamUser.ts b/queries/admin/teamUser.ts
new file mode 100644
index 000000000..e110efcff
--- /dev/null
+++ b/queries/admin/teamUser.ts
@@ -0,0 +1,41 @@
+import { Prisma, TeamUser } from '@prisma/client';
+import prisma from 'lib/prisma';
+
+export async function createTeamUser(data: Prisma.TeamUserCreateInput): Promise<TeamUser> {
+ return prisma.client.teamUser.create({
+ data,
+ });
+}
+
+export async function getTeamUser(where: Prisma.TeamUserWhereUniqueInput): Promise<TeamUser> {
+ return prisma.client.teamUser.findUnique({
+ where,
+ });
+}
+
+export async function getTeamUsers(where: Prisma.TeamUserWhereInput): Promise<TeamUser[]> {
+ return prisma.client.teamUser.findMany({
+ where,
+ });
+}
+
+export async function updateTeamUser(
+ data: Prisma.TeamUserUpdateInput,
+ where: Prisma.TeamUserWhereUniqueInput,
+): Promise<TeamUser> {
+ return prisma.client.teamUser.update({
+ data,
+ where,
+ });
+}
+
+export async function deleteTeamUser(teamUserId: string): Promise<TeamUser> {
+ return prisma.client.teamUser.update({
+ data: {
+ isDeleted: true,
+ },
+ where: {
+ id: teamUserId,
+ },
+ });
+}
diff --git a/queries/admin/teamWebsite.ts b/queries/admin/teamWebsite.ts
new file mode 100644
index 000000000..950a70263
--- /dev/null
+++ b/queries/admin/teamWebsite.ts
@@ -0,0 +1,43 @@
+import { Prisma, TeamWebsite } from '@prisma/client';
+import prisma from 'lib/prisma';
+
+export async function createTeamWebsite(data: Prisma.TeamWebsiteCreateInput): Promise<TeamWebsite> {
+ return prisma.client.teamWebsite.create({
+ data,
+ });
+}
+
+export async function getTeamWebsite(
+ where: Prisma.TeamWebsiteWhereUniqueInput,
+): Promise<TeamWebsite> {
+ return prisma.client.teamWebsite.findUnique({
+ where,
+ });
+}
+
+export async function getTeamWebsites(where: Prisma.TeamWebsiteWhereInput): Promise<TeamWebsite[]> {
+ return prisma.client.teamWebsite.findMany({
+ where,
+ });
+}
+
+export async function updateTeamWebsite(
+ data: Prisma.TeamWebsiteUpdateInput,
+ where: Prisma.TeamWebsiteWhereUniqueInput,
+): Promise<TeamWebsite> {
+ return prisma.client.teamWebsite.update({
+ data,
+ where,
+ });
+}
+
+export async function deleteTeamWebsite(teamWebsiteId: string): Promise<TeamWebsite> {
+ return prisma.client.teamWebsite.update({
+ data: {
+ isDeleted: true,
+ },
+ where: {
+ id: teamWebsiteId,
+ },
+ });
+}
diff --git a/queries/admin/user.ts b/queries/admin/user.ts
new file mode 100644
index 000000000..98eadc1bd
--- /dev/null
+++ b/queries/admin/user.ts
@@ -0,0 +1,152 @@
+import { Prisma } from '@prisma/client';
+import { UmamiApi } from 'interface/enum';
+import cache from 'lib/cache';
+import prisma from 'lib/prisma';
+
+export interface User {
+ id: string;
+ username: string;
+ password?: string;
+ createdAt?: Date;
+}
+
+export async function createUser(data: {
+ id: string;
+ username: string;
+ password: string;
+}): Promise<{
+ id: string;
+ username: string;
+}> {
+ return prisma.client.user.create({
+ data,
+ select: {
+ id: true,
+ username: true,
+ },
+ });
+}
+
+export async function getUser(
+ where: Prisma.UserWhereUniqueInput,
+ includePassword = false,
+): Promise<User> {
+ return prisma.client.user.findUnique({
+ where,
+ select: {
+ id: true,
+ username: true,
+ userRole: {
+ include: {
+ role: true,
+ },
+ },
+ password: includePassword,
+ },
+ });
+}
+
+export async function getUsers(): Promise<User[]> {
+ return prisma.client.user.findMany({
+ orderBy: [
+ {
+ username: 'asc',
+ },
+ ],
+ select: {
+ id: true,
+ username: true,
+ createdAt: true,
+ },
+ });
+}
+
+export async function getUsersByTeamId(teamId): Promise<User[]> {
+ return prisma.client.user.findMany({
+ where: {
+ teamUser: {
+ every: {
+ teamId,
+ },
+ },
+ },
+ select: {
+ id: true,
+ username: true,
+ createdAt: true,
+ },
+ });
+}
+
+export async function updateUser(
+ data: Prisma.UserUpdateInput,
+ where: Prisma.UserWhereUniqueInput,
+): Promise<User> {
+ return prisma.client.user
+ .update({
+ where,
+ data,
+ select: {
+ id: true,
+ username: true,
+ createdAt: true,
+ userRole: true,
+ },
+ })
+ .then(user => {
+ const { userRole, ...rest } = user;
+
+ return { ...rest, isAdmin: userRole.some(a => a.roleId === UmamiApi.SystemRole.Admin) };
+ });
+}
+
+export async function deleteUser(
+ userId: string,
+): Promise<[Prisma.BatchPayload, Prisma.BatchPayload, Prisma.BatchPayload, User]> {
+ const { client } = prisma;
+
+ const websites = await client.userWebsite.findMany({
+ where: { userId },
+ });
+
+ let websiteIds = [];
+
+ if (websites.length > 0) {
+ websiteIds = websites.map(a => a.websiteId);
+ }
+
+ return client
+ .$transaction([
+ client.websiteEvent.deleteMany({
+ where: { websiteId: { in: websiteIds } },
+ }),
+ client.session.deleteMany({
+ where: { websiteId: { in: websiteIds } },
+ }),
+ client.website.updateMany({
+ data: {
+ isDeleted: true,
+ },
+ where: { id: { in: websiteIds } },
+ }),
+ client.user.update({
+ data: {
+ isDeleted: true,
+ },
+ where: {
+ id: userId,
+ },
+ }),
+ ])
+ .then(async data => {
+ if (cache.enabled) {
+ const ids = websites.map(a => a.id);
+
+ for (let i = 0; i < ids.length; i++) {
+ await cache.deleteWebsite(`website:${ids[i]}`);
+ }
+ }
+
+ return data;
+ });
+}
diff --git a/queries/admin/user/createUser.ts b/queries/admin/user/createUser.ts
deleted file mode 100644
index c16040441..000000000
--- a/queries/admin/user/createUser.ts
+++ /dev/null
@@ -1,20 +0,0 @@
-import prisma from 'lib/prisma';
-
-export async function createUser(data: {
- id: string;
- username: string;
- password: string;
-}): Promise<{
- id: string;
- username: string;
- isAdmin: boolean;
-}> {
- return prisma.client.user.create({
- data,
- select: {
- id: true,
- username: true,
- isAdmin: true,
- },
- });
-}
diff --git a/queries/admin/user/deleteUser.ts b/queries/admin/user/deleteUser.ts
deleted file mode 100644
index bb5562255..000000000
--- a/queries/admin/user/deleteUser.ts
+++ /dev/null
@@ -1,48 +0,0 @@
-import prisma from 'lib/prisma';
-import cache from 'lib/cache';
-import { Prisma, User } from '@prisma/client';
-
-export async function deleteUser(
- userId: string,
-): Promise<[Prisma.BatchPayload, Prisma.BatchPayload, Prisma.BatchPayload, User]> {
- const { client } = prisma;
-
- const websites = await client.website.findMany({
- where: { userId },
- });
-
- let websiteIds = [];
-
- if (websites.length > 0) {
- websiteIds = websites.map(a => a.id);
- }
-
- return client
- .$transaction([
- client.websiteEvent.deleteMany({
- where: { websiteId: { in: websiteIds } },
- }),
- client.session.deleteMany({
- where: { websiteId: { in: websiteIds } },
- }),
- client.website.deleteMany({
- where: { userId },
- }),
- client.user.delete({
- where: {
- id: userId,
- },
- }),
- ])
- .then(async data => {
- if (cache.enabled) {
- const ids = websites.map(a => a.id);
-
- for (let i = 0; i < ids.length; i++) {
- await cache.deleteWebsite(`website:${ids[i]}`);
- }
- }
-
- return data;
- });
-}
diff --git a/queries/admin/user/getUser.ts b/queries/admin/user/getUser.ts
deleted file mode 100644
index c353886e9..000000000
--- a/queries/admin/user/getUser.ts
+++ /dev/null
@@ -1,25 +0,0 @@
-import prisma from 'lib/prisma';
-import { Prisma } from '@prisma/client';
-
-export interface User {
- id: string;
- username: string;
- isAdmin: boolean;
- password?: string;
- createdAt?: Date;
-}
-
-export async function getUser(
- where: Prisma.UserWhereUniqueInput,
- includePassword = false,
-): Promise<User> {
- return prisma.client.user.findUnique({
- where,
- select: {
- id: true,
- username: true,
- isAdmin: true,
- password: includePassword,
- },
- });
-}
diff --git a/queries/admin/user/getUsers.ts b/queries/admin/user/getUsers.ts
deleted file mode 100644
index e196d2320..000000000
--- a/queries/admin/user/getUsers.ts
+++ /dev/null
@@ -1,19 +0,0 @@
-import prisma from 'lib/prisma';
-import { User } from './getUser';
-
-export async function getUsers(): Promise<User[]> {
- return prisma.client.user.findMany({
- orderBy: [
- { isAdmin: 'desc' },
- {
- username: 'asc',
- },
- ],
- select: {
- id: true,
- username: true,
- isAdmin: true,
- createdAt: true,
- },
- });
-}
diff --git a/queries/admin/user/updateUser.ts b/queries/admin/user/updateUser.ts
deleted file mode 100644
index f5a64e512..000000000
--- a/queries/admin/user/updateUser.ts
+++ /dev/null
@@ -1,19 +0,0 @@
-import { Prisma } from '@prisma/client';
-import prisma from 'lib/prisma';
-import { User } from './getUser';
-
-export async function updateUser(
- data: Prisma.UserUpdateArgs,
- where: Prisma.UserWhereUniqueInput,
-): Promise<User> {
- return prisma.client.user.update({
- where,
- data,
- select: {
- id: true,
- username: true,
- isAdmin: true,
- createdAt: true,
- },
- });
-}
diff --git a/queries/admin/userRole.ts b/queries/admin/userRole.ts
new file mode 100644
index 000000000..c4e365ac3
--- /dev/null
+++ b/queries/admin/userRole.ts
@@ -0,0 +1,41 @@
+import { Prisma, UserRole } from '@prisma/client';
+import prisma from 'lib/prisma';
+
+export async function createUserRole(data: Prisma.UserRoleCreateInput): Promise<UserRole> {
+ return prisma.client.userRole.create({
+ data,
+ });
+}
+
+export async function getUserRole(where: Prisma.UserRoleWhereUniqueInput): Promise<UserRole> {
+ return prisma.client.userRole.findUnique({
+ where,
+ });
+}
+
+export async function getUserRoles(where: Prisma.UserRoleWhereInput): Promise<UserRole[]> {
+ return prisma.client.userRole.findMany({
+ where,
+ });
+}
+
+export async function updateUserRole(
+ data: Prisma.UserRoleUpdateInput,
+ where: Prisma.UserRoleWhereUniqueInput,
+): Promise<UserRole> {
+ return prisma.client.userRole.update({
+ data,
+ where,
+ });
+}
+
+export async function deleteUserRole(userRoleId: string): Promise<UserRole> {
+ return prisma.client.userRole.update({
+ data: {
+ isDeleted: true,
+ },
+ where: {
+ id: userRoleId,
+ },
+ });
+}
diff --git a/queries/admin/userWebsite.ts b/queries/admin/userWebsite.ts
new file mode 100644
index 000000000..313d6cd46
--- /dev/null
+++ b/queries/admin/userWebsite.ts
@@ -0,0 +1,43 @@
+import { Prisma, UserWebsite } from '@prisma/client';
+import prisma from 'lib/prisma';
+
+export async function createUserWebsite(data: Prisma.UserWebsiteCreateInput): Promise<UserWebsite> {
+ return prisma.client.userWebsite.create({
+ data,
+ });
+}
+
+export async function getUserWebsite(
+ where: Prisma.UserWebsiteWhereUniqueInput,
+): Promise<UserWebsite> {
+ return prisma.client.userWebsite.findUnique({
+ where,
+ });
+}
+
+export async function getUserWebsites(where: Prisma.UserWebsiteWhereInput): Promise<UserWebsite[]> {
+ return prisma.client.userWebsite.findMany({
+ where,
+ });
+}
+
+export async function updateUserWebsite(
+ data: Prisma.UserWebsiteUpdateInput,
+ where: Prisma.UserWebsiteWhereUniqueInput,
+): Promise<UserWebsite> {
+ return prisma.client.userWebsite.update({
+ data,
+ where,
+ });
+}
+
+export async function deleteUserWebsite(userWebsiteId: string): Promise<UserWebsite> {
+ return prisma.client.userWebsite.update({
+ data: {
+ isDeleted: true,
+ },
+ where: {
+ id: userWebsiteId,
+ },
+ });
+}
diff --git a/queries/admin/website.ts b/queries/admin/website.ts
new file mode 100644
index 000000000..240798dab
--- /dev/null
+++ b/queries/admin/website.ts
@@ -0,0 +1,176 @@
+import { Prisma, Website } from '@prisma/client';
+import cache from 'lib/cache';
+import prisma from 'lib/prisma';
+
+export async function createWebsiteByUser(
+ userId: string,
+ data: {
+ id: string;
+ name: string;
+ domain: string;
+ shareId?: string;
+ },
+): Promise<Website> {
+ return prisma.client.website
+ .create({
+ data: {
+ userWebsite: {
+ connect: {
+ id: userId,
+ },
+ },
+ ...data,
+ },
+ })
+ .then(async data => {
+ if (cache.enabled) {
+ await cache.storeWebsite(data);
+ }
+
+ return data;
+ });
+}
+
+export async function createWebsiteByTeam(
+ teamId: string,
+ data: {
+ id: string;
+ name: string;
+ domain: string;
+ shareId?: string;
+ },
+): Promise<Website> {
+ return prisma.client.website
+ .create({
+ data: {
+ teamWebsite: {
+ connect: {
+ id: teamId,
+ },
+ },
+ ...data,
+ },
+ })
+ .then(async data => {
+ if (cache.enabled) {
+ await cache.storeWebsite(data);
+ }
+
+ return data;
+ });
+}
+
+export async function updateWebsite(websiteId, data: Prisma.WebsiteUpdateInput): Promise<Website> {
+ return prisma.client.website.update({
+ where: {
+ id: websiteId,
+ },
+ data,
+ });
+}
+
+export async function resetWebsite(
+ websiteId,
+): Promise<[Prisma.BatchPayload, Prisma.BatchPayload, Website]> {
+ const { client, transaction } = prisma;
+
+ const { revId } = await getWebsite({ id: websiteId });
+
+ return transaction([
+ client.websiteEvent.deleteMany({
+ where: { websiteId },
+ }),
+ client.session.deleteMany({
+ where: { websiteId },
+ }),
+ client.website.update({ where: { id: websiteId }, data: { revId: revId + 1 } }),
+ ]).then(async data => {
+ if (cache.enabled) {
+ await cache.storeWebsite(data[2]);
+ }
+
+ return data;
+ });
+}
+
+export async function getWebsite(where: Prisma.WebsiteWhereUniqueInput): Promise<Website> {
+ return prisma.client.website.findUnique({
+ where,
+ });
+}
+
+export async function getWebsitesByUserId(userId): Promise<Website[]> {
+ return prisma.client.website.findMany({
+ where: {
+ userWebsite: {
+ every: {
+ userId,
+ },
+ },
+ },
+ orderBy: {
+ name: 'asc',
+ },
+ });
+}
+
+export async function getWebsitesByTeamId(teamId): Promise<Website[]> {
+ return prisma.client.website.findMany({
+ where: {
+ teamWebsite: {
+ every: {
+ teamId,
+ },
+ },
+ },
+ orderBy: {
+ name: 'asc',
+ },
+ });
+}
+
+export async function getAllWebsites(): Promise<(Website & { user: string })[]> {
+ return await prisma.client.website
+ .findMany({
+ orderBy: [
+ {
+ name: 'asc',
+ },
+ ],
+ include: {
+ userWebsite: {
+ include: {
+ user: true,
+ },
+ },
+ },
+ })
+ .then(data => data.map(i => ({ ...i, user: i.userWebsite[0]?.userId })));
+}
+
+export async function deleteWebsite(
+ websiteId: string,
+): Promise<[Prisma.BatchPayload, Prisma.BatchPayload, Website]> {
+ const { client, transaction } = prisma;
+
+ return transaction([
+ client.websiteEvent.deleteMany({
+ where: { websiteId },
+ }),
+ client.session.deleteMany({
+ where: { websiteId },
+ }),
+ client.website.update({
+ data: {
+ isDeleted: true,
+ },
+ where: { id: websiteId },
+ }),
+ ]).then(async data => {
+ if (cache.enabled) {
+ await cache.deleteWebsite(websiteId);
+ }
+
+ return data;
+ });
+}
diff --git a/queries/admin/website/createWebsite.ts b/queries/admin/website/createWebsite.ts
deleted file mode 100644
index 51aa2e3fa..000000000
--- a/queries/admin/website/createWebsite.ts
+++ /dev/null
@@ -1,32 +0,0 @@
-import { Website } from '@prisma/client';
-import cache from 'lib/cache';
-import prisma from 'lib/prisma';
-
-export async function createWebsite(
- userId: string,
- data: {
- id: string;
- name: string;
- domain: string;
- shareId?: string;
- },
-): Promise<Website> {
- return prisma.client.website
- .create({
- data: {
- user: {
- connect: {
- id: userId,
- },
- },
- ...data,
- },
- })
- .then(async data => {
- if (cache.enabled) {
- await cache.storeWebsite(data);
- }
-
- return data;
- });
-}
diff --git a/queries/admin/website/deleteWebsite.ts b/queries/admin/website/deleteWebsite.ts
deleted file mode 100644
index a3c57e671..000000000
--- a/queries/admin/website/deleteWebsite.ts
+++ /dev/null
@@ -1,27 +0,0 @@
-import prisma from 'lib/prisma';
-import cache from 'lib/cache';
-import { Prisma, Website } from '@prisma/client';
-
-export async function deleteWebsite(
- websiteId: string,
-): Promise<[Prisma.BatchPayload, Prisma.BatchPayload, Website]> {
- const { client, transaction } = prisma;
-
- return transaction([
- client.websiteEvent.deleteMany({
- where: { websiteId },
- }),
- client.session.deleteMany({
- where: { websiteId },
- }),
- client.website.delete({
- where: { id: websiteId },
- }),
- ]).then(async data => {
- if (cache.enabled) {
- await cache.deleteWebsite(websiteId);
- }
-
- return data;
- });
-}
diff --git a/queries/admin/website/getAllWebsites.ts b/queries/admin/website/getAllWebsites.ts
deleted file mode 100644
index 1e68693a5..000000000
--- a/queries/admin/website/getAllWebsites.ts
+++ /dev/null
@@ -1,24 +0,0 @@
-import { Website } from '@prisma/client';
-import prisma from 'lib/prisma';
-
-export async function getAllWebsites(): Promise<(Website & { user: string })[]> {
- return await prisma.client.website
- .findMany({
- orderBy: [
- {
- userId: 'asc',
- },
- {
- name: 'asc',
- },
- ],
- include: {
- user: {
- select: {
- username: true,
- },
- },
- },
- })
- .then(data => data.map(i => ({ ...i, user: i.user.username })));
-}
diff --git a/queries/admin/website/getUserWebsites.ts b/queries/admin/website/getUserWebsites.ts
deleted file mode 100644
index f4fa27b9a..000000000
--- a/queries/admin/website/getUserWebsites.ts
+++ /dev/null
@@ -1,13 +0,0 @@
-import prisma from 'lib/prisma';
-import { Website } from '@prisma/client';
-
-export async function getUserWebsites(userId): Promise<Website[]> {
- return prisma.client.website.findMany({
- where: {
- userId,
- },
- orderBy: {
- name: 'asc',
- },
- });
-}
diff --git a/queries/admin/website/getWebsite.ts b/queries/admin/website/getWebsite.ts
deleted file mode 100644
index 9ec27cb92..000000000
--- a/queries/admin/website/getWebsite.ts
+++ /dev/null
@@ -1,8 +0,0 @@
-import prisma from 'lib/prisma';
-import { Prisma, Website } from '@prisma/client';
-
-export async function getWebsite(where: Prisma.WebsiteWhereUniqueInput): Promise<Website> {
- return prisma.client.website.findUnique({
- where,
- });
-}
diff --git a/queries/admin/website/resetWebsite.ts b/queries/admin/website/resetWebsite.ts
deleted file mode 100644
index 05116f8aa..000000000
--- a/queries/admin/website/resetWebsite.ts
+++ /dev/null
@@ -1,28 +0,0 @@
-import { Prisma, Website } from '@prisma/client';
-import cache from 'lib/cache';
-import prisma from 'lib/prisma';
-import { getWebsite } from 'queries';
-
-export async function resetWebsite(
- websiteId,
-): Promise<[Prisma.BatchPayload, Prisma.BatchPayload, Website]> {
- const { client, transaction } = prisma;
-
- const { revId } = await getWebsite({ id: websiteId });
-
- return transaction([
- client.websiteEvent.deleteMany({
- where: { websiteId },
- }),
- client.session.deleteMany({
- where: { websiteId },
- }),
- client.website.update({ where: { id: websiteId }, data: { revId: revId + 1 } }),
- ]).then(async data => {
- if (cache.enabled) {
- await cache.storeWebsite(data[2]);
- }
-
- return data;
- });
-}
diff --git a/queries/admin/website/updateWebsite.ts b/queries/admin/website/updateWebsite.ts
deleted file mode 100644
index 51787222a..000000000
--- a/queries/admin/website/updateWebsite.ts
+++ /dev/null
@@ -1,11 +0,0 @@
-import { Prisma, Website } from '@prisma/client';
-import prisma from 'lib/prisma';
-
-export async function updateWebsite(websiteId, data: Prisma.WebsiteUpdateInput): Promise<Website> {
- return prisma.client.website.update({
- where: {
- id: websiteId,
- },
- data,
- });
-}
diff --git a/queries/index.js b/queries/index.js
index 4cdcedd95..e14c6d848 100644
--- a/queries/index.js
+++ b/queries/index.js
@@ -1,21 +1,17 @@
-export * from './admin/user/createUser';
-export * from './admin/user/deleteUser';
-export * from './admin/user/getUser';
-export * from './admin/user/getUsers';
-export * from './admin/user/updateUser';
-export * from './admin/website/createWebsite';
-export * from './admin/website/deleteWebsite';
-export * from './admin/website/getAllWebsites';
-export * from './admin/website/getUserWebsites';
-export * from './admin/website/getWebsite';
-export * from './admin/website/resetWebsite';
-export * from './admin/website/updateWebsite';
+export * from './admin/permission';
+export * from './admin/role';
+export * from './admin/team';
+export * from './admin/teamUser';
+export * from './admin/teamWebsite';
+export * from './admin/user';
+export * from './admin/userRole';
+export * from './admin/userWebsite';
+export * from './admin/website';
export * from './analytics/event/getEventMetrics';
export * from './analytics/event/getEvents';
export * from './analytics/event/getEventData';
export * from './analytics/event/saveEvent';
export * from './analytics/pageview/getPageviewMetrics';
-export * from './analytics/pageview/getPageviewParams';
export * from './analytics/pageview/getPageviews';
export * from './analytics/pageview/getPageviewStats';
export * from './analytics/pageview/savePageView';