mirror of
https://github.com/kremalicious/umami.git
synced 2024-06-28 16:57:52 +02:00
Add queries for new schema.
This commit is contained in:
parent
b0c7980a20
commit
5aa8187e42
|
@ -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])
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
41
queries/admin/permission.ts
Normal file
41
queries/admin/permission.ts
Normal file
|
@ -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,
|
||||
},
|
||||
});
|
||||
}
|
57
queries/admin/role.ts
Normal file
57
queries/admin/role.ts
Normal file
|
@ -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,
|
||||
},
|
||||
});
|
||||
}
|
56
queries/admin/team.ts
Normal file
56
queries/admin/team.ts
Normal file
|
@ -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,
|
||||
},
|
||||
});
|
||||
}
|
41
queries/admin/teamUser.ts
Normal file
41
queries/admin/teamUser.ts
Normal file
|
@ -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,
|
||||
},
|
||||
});
|
||||
}
|
43
queries/admin/teamWebsite.ts
Normal file
43
queries/admin/teamWebsite.ts
Normal file
|
@ -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,
|
||||
},
|
||||
});
|
||||
}
|
152
queries/admin/user.ts
Normal file
152
queries/admin/user.ts
Normal file
|
@ -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;
|
||||
});
|
||||
}
|
|
@ -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,
|
||||
},
|
||||
});
|
||||
}
|
|
@ -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;
|
||||
});
|
||||
}
|
|
@ -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,
|
||||
},
|
||||
});
|
||||
}
|
|
@ -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,
|
||||
},
|
||||
});
|
||||
}
|
|
@ -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,
|
||||
},
|
||||
});
|
||||
}
|
41
queries/admin/userRole.ts
Normal file
41
queries/admin/userRole.ts
Normal file
|
@ -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,
|
||||
},
|
||||
});
|
||||
}
|
43
queries/admin/userWebsite.ts
Normal file
43
queries/admin/userWebsite.ts
Normal file
|
@ -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,
|
||||
},
|
||||
});
|
||||
}
|
176
queries/admin/website.ts
Normal file
176
queries/admin/website.ts
Normal file
|
@ -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;
|
||||
});
|
||||
}
|
|
@ -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;
|
||||
});
|
||||
}
|
|
@ -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;
|
||||
});
|
||||
}
|
|
@ -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 })));
|
||||
}
|
|
@ -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',
|
||||
},
|
||||
});
|
||||
}
|
|
@ -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,
|
||||
});
|
||||
}
|
|
@ -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;
|
||||
});
|
||||
}
|
|
@ -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,
|
||||
});
|
||||
}
|
|
@ -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';
|
||||
|
|
Loading…
Reference in New Issue
Block a user