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 schema changes for TeamWebsite, update deleted logic for team tables

Francis Cao5fd946cfde99
diff --git a/db/postgresql/migrations/04_add_team_website/migration.sql b/db/postgresql/migrations/04_add_team_website/migration.sql
new file mode 100644
index 000000000..05ddd53cb
--- /dev/null
+++ b/db/postgresql/migrations/04_add_team_website/migration.sql
@@ -0,0 +1,42 @@
+/*
+ Warnings:
+
+ - You are about to drop the column `deleted_at` on the `team` table. All the data in the column will be lost.
+ - You are about to drop the column `deleted_at` on the `team_user` table. All the data in the column will be lost.
+ - You are about to drop the column `team_id` on the `website` table. All the data in the column will be lost.
+
+*/
+-- DropIndex
+DROP INDEX "website_team_id_idx";
+
+-- AlterTable
+ALTER TABLE "team" DROP COLUMN "deleted_at";
+
+-- AlterTable
+ALTER TABLE "team_user" DROP COLUMN "deleted_at";
+
+-- AlterTable
+ALTER TABLE "website" DROP COLUMN "team_id";
+
+-- CreateTable
+CREATE TABLE "team_website" (
+ "team_website_id" UUID NOT NULL,
+ "team_id" UUID NOT NULL,
+ "user_id" UUID NOT NULL,
+ "website_id" UUID NOT NULL,
+ "created_at" TIMESTAMPTZ(6) DEFAULT CURRENT_TIMESTAMP,
+
+ CONSTRAINT "team_website_pkey" PRIMARY KEY ("team_website_id")
+);
+
+-- CreateIndex
+CREATE UNIQUE INDEX "team_website_team_website_id_key" ON "team_website"("team_website_id");
+
+-- CreateIndex
+CREATE INDEX "team_website_team_id_idx" ON "team_website"("team_id");
+
+-- CreateIndex
+CREATE INDEX "team_website_user_id_idx" ON "team_website"("user_id");
+
+-- CreateIndex
+CREATE INDEX "team_website_website_id_idx" ON "team_website"("website_id");
diff --git a/db/postgresql/schema.prisma b/db/postgresql/schema.prisma
index 7e4e20d16..ed1c4fe9d 100644
--- a/db/postgresql/schema.prisma
+++ b/db/postgresql/schema.prisma
@@ -17,8 +17,9 @@ model User {
updatedAt DateTime? @map("updated_at") @db.Timestamptz(6)
deletedAt DateTime? @map("deleted_at") @db.Timestamptz(6)
- teamUser TeamUser[]
Website Website[]
+ teamUser TeamUser[]
+ teamWebsite TeamWebsite[]
@@map("user")
}
@@ -47,15 +48,13 @@ model Website {
shareId String? @unique @map("share_id") @db.VarChar(50)
revId Int @default(0) @map("rev_id") @db.Integer
userId String? @map("user_id") @db.Uuid
- teamId String? @map("team_id") @db.Uuid
createdAt DateTime? @default(now()) @map("created_at") @db.Timestamptz(6)
updatedAt DateTime? @map("updated_at") @db.Timestamptz(6)
deletedAt DateTime? @map("deleted_at") @db.Timestamptz(6)
- team Team? @relation(fields: [teamId], references: [id])
user User? @relation(fields: [userId], references: [id])
+ teamWebsite TeamWebsite[]
- @@index([teamId])
@@index([userId])
@@index([createdAt])
@@index([shareId])
@@ -88,10 +87,9 @@ model Team {
accessCode String? @unique @map("access_code") @db.VarChar(50)
createdAt DateTime? @default(now()) @map("created_at") @db.Timestamptz(6)
updatedAt DateTime? @map("updated_at") @db.Timestamptz(6)
- deletedAt DateTime? @map("deleted_at") @db.Timestamptz(6)
- teamUsers TeamUser[]
- Website Website[]
+ teamUser TeamUser[]
+ teamWebsite TeamWebsite[]
@@index([userId])
@@index([accessCode])
@@ -105,7 +103,6 @@ model TeamUser {
role String @map("role") @db.VarChar(50)
createdAt DateTime? @default(now()) @map("created_at") @db.Timestamptz(6)
updatedAt DateTime? @map("updated_at") @db.Timestamptz(6)
- deletedAt DateTime? @map("deleted_at") @db.Timestamptz(6)
team Team @relation(fields: [teamId], references: [id])
user User @relation(fields: [userId], references: [id])
@@ -114,3 +111,20 @@ model TeamUser {
@@index([userId])
@@map("team_user")
}
+
+model TeamWebsite {
+ id String @id() @unique() @map("team_website_id") @db.Uuid
+ teamId String @map("team_id") @db.Uuid
+ userId String @map("user_id") @db.Uuid
+ websiteId String @map("website_id") @db.Uuid
+ createdAt DateTime? @default(now()) @map("created_at") @db.Timestamptz(6)
+
+ team Team @relation(fields: [teamId], references: [id])
+ user User @relation(fields: [userId], references: [id])
+ website Website @relation(fields: [websiteId], references: [id])
+
+ @@index([teamId])
+ @@index([userId])
+ @@index([websiteId])
+ @@map("team_website")
+}
diff --git a/queries/admin/team.ts b/queries/admin/team.ts
index add282a94..9d98aa75e 100644
--- a/queries/admin/team.ts
+++ b/queries/admin/team.ts
@@ -61,10 +61,7 @@ export async function updateTeam(
}
export async function deleteTeam(teamId: string): Promise<Team> {
- return prisma.client.team.update({
- data: {
- deletedAt: new Date(),
- },
+ return prisma.client.team.delete({
where: {
id: teamId,
},
diff --git a/queries/admin/teamUser.ts b/queries/admin/teamUser.ts
index 2dc20d117..a2afcf95d 100644
--- a/queries/admin/teamUser.ts
+++ b/queries/admin/teamUser.ts
@@ -48,10 +48,7 @@ export async function updateTeamUser(
}
export async function deleteTeamUser(teamUserId: string): Promise<TeamUser> {
- return prisma.client.teamUser.update({
- data: {
- deletedAt: new Date(),
- },
+ return prisma.client.teamUser.delete({
where: {
id: teamUserId,
},
diff --git a/queries/admin/teamWebsite.ts b/queries/admin/teamWebsite.ts
new file mode 100644
index 000000000..1a2b38914
--- /dev/null
+++ b/queries/admin/teamWebsite.ts
@@ -0,0 +1,47 @@
+import { TeamWebsite } from '@prisma/client';
+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({
+ where: {
+ teamId,
+ userId,
+ },
+ });
+}
+
+export async function getTeamWebsites(teamId: string): Promise<TeamWebsite[]> {
+ return prisma.client.TeamWebsite.findMany({
+ where: {
+ teamId,
+ },
+ include: {
+ user: true,
+ website: true,
+ },
+ });
+}
+
+export async function createTeamWebsite(
+ userId: string,
+ teamId: string,
+ websiteId: string,
+): Promise<TeamWebsite> {
+ return prisma.client.TeamWebsite.create({
+ data: {
+ id: uuid(),
+ userId,
+ teamId,
+ websiteId,
+ },
+ });
+}
+
+export async function deleteTeamWebsite(TeamWebsiteId: string): Promise<TeamWebsite> {
+ return prisma.client.teamUser.delete({
+ where: {
+ id: TeamWebsiteId,
+ },
+ });
+}