mirror of
https://github.com/umami-software/umami.git
synced 2026-02-09 23:27:12 +01:00
Add queries for new schema.
This commit is contained in:
parent
b0c7980a20
commit
5aa8187e42
25 changed files with 699 additions and 306 deletions
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,
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue