Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

Do not select deleted projects, make clone predictable (#1024)

Ivan Starkovcd855fe7f22b
diff --git a/apps/builder/app/routes/rest/project/clone.$domain.ts b/apps/builder/app/routes/rest/project/clone.$domain.ts
index 9fe906a2b7..fd1ea5ce11 100644
--- a/apps/builder/app/routes/rest/project/clone.$domain.ts
+++ b/apps/builder/app/routes/rest/project/clone.$domain.ts
@@ -14,11 +14,6 @@ const ensureProject = async (
},
context: AppContext
): Promise<Project> => {
- const projects = await projectDb.project.loadManyByCurrentUserId(context);
- if (projects.length !== 0) {
- return projects[0];
- }
-
return await projectDb.project.cloneByDomain(domain, context);
};
diff --git a/packages/prisma-client/prisma/migrations/20230213220858_is-deleted-uniq/migration.sql b/packages/prisma-client/prisma/migrations/20230213220858_is-deleted-uniq/migration.sql
new file mode 100644
index 0000000000..c64005900a
--- /dev/null
+++ b/packages/prisma-client/prisma/migrations/20230213220858_is-deleted-uniq/migration.sql
@@ -0,0 +1,5 @@
+-- CreateIndex
+CREATE UNIQUE INDEX "Project_id_isDeleted_key" ON "Project"("id", "isDeleted");
+
+-- CreateIndex
+CREATE UNIQUE INDEX "Project_domain_isDeleted_key" ON "Project"("domain", "isDeleted");
diff --git a/packages/prisma-client/prisma/schema.prisma b/packages/prisma-client/prisma/schema.prisma
index 0e47c09e92..9aa5ed8a55 100644
--- a/packages/prisma-client/prisma/schema.prisma
+++ b/packages/prisma-client/prisma/schema.prisma
@@ -64,6 +64,9 @@ model Project {
build Build[]
trees Tree[]
isDeleted Boolean @default(false)
+
+ @@unique([id, isDeleted])
+ @@unique([domain, isDeleted])
}
model Build {
diff --git a/packages/project/src/db/project.ts b/packages/project/src/db/project.ts
index 5eca0243e0..b8fa7d3bc3 100644
--- a/packages/project/src/db/project.ts
+++ b/packages/project/src/db/project.ts
@@ -8,7 +8,7 @@ import {
type AppContext,
} from "@webstudio-is/trpc-interface/server";
import { v4 as uuid } from "uuid";
-import { Project, Projects, Title } from "../shared/schema";
+import { Project, Title } from "../shared/schema";
const nanoid = customAlphabet("1234567890abcdefghijklmnopqrstuvwxyz");
@@ -35,7 +35,7 @@ export const loadById = async (
}
const data = await prisma.project.findUnique({
- where: { id: projectId },
+ where: { id_isDeleted: { id: projectId, isDeleted: false } },
});
return Project.parse(data);
@@ -47,7 +47,9 @@ export const loadByDomain = async (
): Promise<Project | null> => {
// The authorization system needs the project id to check if the user has access to the project
const projectWithId = await prisma.project.findUnique({
- where: { domain: domain.toLowerCase() },
+ where: {
+ domain_isDeleted: { domain: domain.toLowerCase(), isDeleted: false },
+ },
});
if (projectWithId === null) {
@@ -64,25 +66,6 @@ export const loadByDomain = async (
return await loadById(projectWithId.id, context);
};
-export const loadManyByCurrentUserId = async (
- context: AppContext
-): Promise<Projects> => {
- const userId = context.authorization.userId;
- if (userId === undefined) {
- throw new Error("The user must be authenticated to list projects");
- }
-
- const data = await prisma.project.findMany({
- where: {
- user: {
- id: userId,
- },
- },
- });
-
- return Projects.parse(data);
-};
-
const slugifyOptions = { lower: true, strict: true };
const MIN_DOMAIN_LENGTH = 10;