Check deletedAt. (#1796)

This commit is contained in:
Brian Cao 2023-02-27 16:01:34 -08:00 committed by GitHub
parent 5657a64c77
commit 84430e38eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 52 deletions

View File

@ -36,7 +36,7 @@ export default async (
const { username, password, id } = req.body; const { username, password, id } = req.body;
const existingUser = await getUser({ username }); const existingUser = await getUser({ username }, { showDeleted: true });
if (existingUser) { if (existingUser) {
return badRequest(res, 'User already exists'); return badRequest(res, 'User already exists');

View File

@ -3,7 +3,7 @@ import { uuid } from 'lib/crypto';
import prisma from 'lib/prisma'; import prisma from 'lib/prisma';
export async function getTeamWebsite(teamId: string, userId: string): Promise<TeamWebsite> { export async function getTeamWebsite(teamId: string, userId: string): Promise<TeamWebsite> {
return prisma.client.TeamWebsite.findFirst({ return prisma.client.teamWebsite.findFirst({
where: { where: {
teamId, teamId,
userId, userId,
@ -12,7 +12,7 @@ export async function getTeamWebsite(teamId: string, userId: string): Promise<Te
} }
export async function getTeamWebsites(teamId: string): Promise<TeamWebsite[]> { export async function getTeamWebsites(teamId: string): Promise<TeamWebsite[]> {
return prisma.client.TeamWebsite.findMany({ return prisma.client.teamWebsite.findMany({
where: { where: {
teamId, teamId,
}, },
@ -28,7 +28,7 @@ export async function createTeamWebsite(
teamId: string, teamId: string,
websiteId: string, websiteId: string,
): Promise<TeamWebsite> { ): Promise<TeamWebsite> {
return prisma.client.TeamWebsite.create({ return prisma.client.teamWebsite.create({
data: { data: {
id: uuid(), id: uuid(),
userId, userId,
@ -37,11 +37,3 @@ export async function createTeamWebsite(
}, },
}); });
} }
export async function deleteTeamWebsite(TeamWebsiteId: string): Promise<TeamWebsite> {
return prisma.client.teamUser.delete({
where: {
id: TeamWebsiteId,
},
});
}

View File

@ -11,13 +11,13 @@ export interface User {
} }
export async function getUser( export async function getUser(
where: Prisma.UserWhereUniqueInput, where: Prisma.UserWhereInput | Prisma.UserWhereUniqueInput,
options: { includePassword?: boolean } = {}, options: { includePassword?: boolean; showDeleted?: boolean } = {},
): Promise<User> { ): Promise<User> {
const { includePassword = false } = options; const { includePassword = false, showDeleted = false } = options;
return prisma.client.user.findUnique({ return prisma.client.user.findFirst({
where, where: { ...where, ...(showDeleted ? {} : { deletedAt: null }) },
select: { select: {
id: true, id: true,
username: true, username: true,
@ -69,6 +69,7 @@ export async function getUserWebsites(userId: string): Promise<Website[]> {
return prisma.client.website.findMany({ return prisma.client.website.findMany({
where: { where: {
userId, userId,
deletedAt: null,
}, },
orderBy: [ orderBy: [
{ {
@ -118,6 +119,7 @@ export async function deleteUser(
userId: string, userId: string,
): Promise<[Prisma.BatchPayload, Prisma.BatchPayload, Prisma.BatchPayload, User]> { ): Promise<[Prisma.BatchPayload, Prisma.BatchPayload, Prisma.BatchPayload, User]> {
const { client } = prisma; const { client } = prisma;
const cloudMode = process.env.CLOUD_MODE;
const websites = await client.website.findMany({ const websites = await client.website.findMany({
where: { userId }, where: { userId },
@ -137,20 +139,30 @@ export async function deleteUser(
client.session.deleteMany({ client.session.deleteMany({
where: { websiteId: { in: websiteIds } }, where: { websiteId: { in: websiteIds } },
}), }),
client.website.updateMany({ cloudMode
data: { ? client.website.updateMany({
deletedAt: new Date(), data: {
}, deletedAt: new Date(),
where: { id: { in: websiteIds } }, },
}), where: { id: { in: websiteIds } },
client.user.update({ })
data: { : client.website.deleteMany({
deletedAt: new Date(), where: { id: { in: websiteIds } },
}, }),
where: { cloudMode
id: userId, ? client.user.update({
}, data: {
}), deletedAt: new Date(),
},
where: {
id: userId,
},
})
: client.user.delete({
where: {
id: userId,
},
}),
]) ])
.then(async data => { .then(async data => {
if (cache.enabled) { if (cache.enabled) {

View File

@ -1,7 +1,6 @@
import { Prisma, Website } from '@prisma/client'; import { Prisma, Website } from '@prisma/client';
import cache from 'lib/cache'; import cache from 'lib/cache';
import prisma from 'lib/prisma'; import prisma from 'lib/prisma';
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
export async function getWebsite(where: Prisma.WebsiteWhereUniqueInput): Promise<Website> { export async function getWebsite(where: Prisma.WebsiteWhereUniqueInput): Promise<Website> {
return prisma.client.website.findUnique({ return prisma.client.website.findUnique({
@ -69,17 +68,11 @@ export async function resetWebsite(
}); });
} }
export async function deleteWebsite(websiteId: string) { export async function deleteWebsite(
return runQuery({
[PRISMA]: () => deleteWebsiteRelationalQuery(websiteId),
[CLICKHOUSE]: () => deleteWebsiteClickhouseQuery(websiteId),
});
}
async function deleteWebsiteRelationalQuery(
websiteId, websiteId,
): Promise<[Prisma.BatchPayload, Prisma.BatchPayload, Website]> { ): Promise<[Prisma.BatchPayload, Prisma.BatchPayload, Website]> {
const { client, transaction } = prisma; const { client, transaction } = prisma;
const cloudMode = process.env.CLOUD_MODE;
return transaction([ return transaction([
client.websiteEvent.deleteMany({ client.websiteEvent.deleteMany({
@ -88,9 +81,16 @@ async function deleteWebsiteRelationalQuery(
client.session.deleteMany({ client.session.deleteMany({
where: { websiteId }, where: { websiteId },
}), }),
client.website.delete({ cloudMode
where: { id: websiteId }, ? prisma.client.website.update({
}), data: {
deletedAt: new Date(),
},
where: { id: websiteId },
})
: client.website.delete({
where: { id: websiteId },
}),
]).then(async data => { ]).then(async data => {
if (cache.enabled) { if (cache.enabled) {
await cache.deleteWebsite(websiteId); await cache.deleteWebsite(websiteId);
@ -99,12 +99,3 @@ async function deleteWebsiteRelationalQuery(
return data; return data;
}); });
} }
async function deleteWebsiteClickhouseQuery(websiteId): Promise<Website> {
return prisma.client.website.update({
data: {
deletedAt: new Date(),
},
where: { id: websiteId },
});
}