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.
TypeScript analyticsaudience-segmentationchartscohort-analysisgoogle-analyticsproduct-analyticsstatisticsuser-journeyweb-analytics
Check deletedAt. (#1796)
Brian Cao84430e38eb0a
diff --git a/pages/api/users/index.ts b/pages/api/users/index.tsindex 89d4d5643..6146682e6 100644--- a/pages/api/users/index.ts+++ b/pages/api/users/index.ts@@ -36,7 +36,7 @@ export default async (const { username, password, id } = req.body;- const existingUser = await getUser({ username });+ const existingUser = await getUser({ username }, { showDeleted: true });if (existingUser) {return badRequest(res, 'User already exists');diff --git a/queries/admin/teamWebsite.ts b/queries/admin/teamWebsite.tsindex 1a2b38914..b2c7ee7b0 100644--- a/queries/admin/teamWebsite.ts+++ b/queries/admin/teamWebsite.ts@@ -3,7 +3,7 @@ import { uuid } from 'lib/crypto';import prisma from 'lib/prisma';export async function getTeamWebsite(teamId: string, userId: string): Promise<TeamWebsite> {- return prisma.client.TeamWebsite.findFirst({+ return prisma.client.teamWebsite.findFirst({where: {teamId,userId,@@ -12,7 +12,7 @@ export async function getTeamWebsite(teamId: string, userId: string): Promise<Te}export async function getTeamWebsites(teamId: string): Promise<TeamWebsite[]> {- return prisma.client.TeamWebsite.findMany({+ return prisma.client.teamWebsite.findMany({where: {teamId,},@@ -28,7 +28,7 @@ export async function createTeamWebsite(teamId: string,websiteId: string,): Promise<TeamWebsite> {- return prisma.client.TeamWebsite.create({+ return prisma.client.teamWebsite.create({data: {id: uuid(),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,- },- });-}diff --git a/queries/admin/user.ts b/queries/admin/user.tsindex ba6ef186b..2b7677f9e 100644--- a/queries/admin/user.ts+++ b/queries/admin/user.ts@@ -11,13 +11,13 @@ export interface User {}export async function getUser(- where: Prisma.UserWhereUniqueInput,- options: { includePassword?: boolean } = {},+ where: Prisma.UserWhereInput | Prisma.UserWhereUniqueInput,+ options: { includePassword?: boolean; showDeleted?: boolean } = {},): Promise<User> {- const { includePassword = false } = options;+ const { includePassword = false, showDeleted = false } = options;- return prisma.client.user.findUnique({- where,+ return prisma.client.user.findFirst({+ where: { ...where, ...(showDeleted ? {} : { deletedAt: null }) },select: {id: true,username: true,@@ -69,6 +69,7 @@ export async function getUserWebsites(userId: string): Promise<Website[]> {return prisma.client.website.findMany({where: {userId,+ deletedAt: null,},orderBy: [{@@ -118,6 +119,7 @@ export async function deleteUser(userId: string,): Promise<[Prisma.BatchPayload, Prisma.BatchPayload, Prisma.BatchPayload, User]> {const { client } = prisma;+ const cloudMode = process.env.CLOUD_MODE;const websites = await client.website.findMany({where: { userId },@@ -137,20 +139,30 @@ export async function deleteUser(client.session.deleteMany({where: { websiteId: { in: websiteIds } },}),- client.website.updateMany({- data: {- deletedAt: new Date(),- },- where: { id: { in: websiteIds } },- }),- client.user.update({- data: {- deletedAt: new Date(),- },- where: {- id: userId,- },- }),+ cloudMode+ ? client.website.updateMany({+ data: {+ deletedAt: new Date(),+ },+ where: { id: { in: websiteIds } },+ })+ : client.website.deleteMany({+ where: { id: { in: websiteIds } },+ }),+ cloudMode+ ? client.user.update({+ data: {+ deletedAt: new Date(),+ },+ where: {+ id: userId,+ },+ })+ : client.user.delete({+ where: {+ id: userId,+ },+ }),]).then(async data => {if (cache.enabled) {diff --git a/queries/admin/website.ts b/queries/admin/website.tsindex cf2425a35..a364b26f4 100644--- a/queries/admin/website.ts+++ b/queries/admin/website.ts@@ -1,7 +1,6 @@import { Prisma, Website } from '@prisma/client';import cache from 'lib/cache';import prisma from 'lib/prisma';-import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';export async function getWebsite(where: Prisma.WebsiteWhereUniqueInput): Promise<Website> {return prisma.client.website.findUnique({@@ -69,17 +68,11 @@ export async function resetWebsite(});}-export async function deleteWebsite(websiteId: string) {- return runQuery({- [PRISMA]: () => deleteWebsiteRelationalQuery(websiteId),- [CLICKHOUSE]: () => deleteWebsiteClickhouseQuery(websiteId),- });-}--async function deleteWebsiteRelationalQuery(+export async function deleteWebsite(websiteId,): Promise<[Prisma.BatchPayload, Prisma.BatchPayload, Website]> {const { client, transaction } = prisma;+ const cloudMode = process.env.CLOUD_MODE;return transaction([client.websiteEvent.deleteMany({@@ -88,9 +81,16 @@ async function deleteWebsiteRelationalQuery(client.session.deleteMany({where: { websiteId },}),- client.website.delete({- where: { id: websiteId },- }),+ cloudMode+ ? prisma.client.website.update({+ data: {+ deletedAt: new Date(),+ },+ where: { id: websiteId },+ })+ : client.website.delete({+ where: { id: websiteId },+ }),]).then(async data => {if (cache.enabled) {await cache.deleteWebsite(websiteId);@@ -99,12 +99,3 @@ async function deleteWebsiteRelationalQuery(return data;});}--async function deleteWebsiteClickhouseQuery(websiteId): Promise<Website> {- return prisma.client.website.update({- data: {- deletedAt: new Date(),- },- where: { id: websiteId },- });-}