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.

feat: Product and Transaction Log tables (#2618)

Ivan Starkov32e5f032b3e4
diff --git a/apps/builder/app/routes/rest.current.products.ts b/apps/builder/app/routes/rest.current.products.ts
new file mode 100644
index 0000000000..3ed28f62b0
--- /dev/null
+++ b/apps/builder/app/routes/rest.current.products.ts
@@ -0,0 +1,48 @@
+import { json, redirect, type LoaderArgs } from "@remix-run/server-runtime";
+import { findAuthenticatedUser } from "~/services/auth.server";
+import { loginPath } from "~/shared/router-utils";
+import { prisma } from "@webstudio-is/prisma-client";
+
+/**
+ * Created for ebugging purposes, to check that payments and products are working
+ * Showing current user's checkouts with products.
+ */
+export const loader = async ({ request, params }: LoaderArgs) => {
+ const user = await findAuthenticatedUser(request);
+
+ if (user === null) {
+ const url = new URL(request.url);
+ throw redirect(
+ loginPath({
+ returnTo: `${url.pathname}?${url.searchParams.toString()}`,
+ })
+ );
+ }
+
+ const userCheckoutsWithProducts = await prisma.user.findUnique({
+ where: { id: user.id },
+ select: {
+ id: true,
+ username: true,
+ checkout: {
+ select: {
+ eventId: true,
+ sessionId: true,
+ createdAt: true,
+ product: {
+ select: {
+ id: true,
+ name: true,
+ description: true,
+ features: true,
+ meta: true,
+ images: true,
+ },
+ },
+ },
+ },
+ },
+ });
+
+ return json(userCheckoutsWithProducts);
+};
diff --git a/packages/prisma-client/prisma/migrations/20231116173417_product/migration.sql b/packages/prisma-client/prisma/migrations/20231116173417_product/migration.sql
new file mode 100644
index 0000000000..aec5f55443
--- /dev/null
+++ b/packages/prisma-client/prisma/migrations/20231116173417_product/migration.sql
@@ -0,0 +1,12 @@
+-- CreateTable
+CREATE TABLE "Product" (
+ "id" TEXT NOT NULL,
+ "name" TEXT NOT NULL,
+ "description" TEXT,
+ "features" TEXT[],
+ "images" TEXT[],
+ "meta" JSONB NOT NULL,
+ "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
+
+ CONSTRAINT "Product_pkey" PRIMARY KEY ("id")
+);
diff --git a/packages/prisma-client/prisma/migrations/20231116174151_transaction/migration.sql b/packages/prisma-client/prisma/migrations/20231116174151_transaction/migration.sql
new file mode 100644
index 0000000000..a4e46f6f6a
--- /dev/null
+++ b/packages/prisma-client/prisma/migrations/20231116174151_transaction/migration.sql
@@ -0,0 +1,3 @@
+-- AlterTable
+ALTER TABLE "TransactionLog" DROP CONSTRAINT "TransactionLog_pkey",
+DROP COLUMN "id";
diff --git a/packages/prisma-client/prisma/schema.prisma b/packages/prisma-client/prisma/schema.prisma
index c7ba3fee26..bfd5b714c2 100644
--- a/packages/prisma-client/prisma/schema.prisma
+++ b/packages/prisma-client/prisma/schema.prisma
@@ -60,6 +60,7 @@ model User {
teamId String?
projects Project[]
clientReferences ClientReferences[]
+ checkout TransactionLog[]
}
// User client references in external services like stripe etc
@@ -77,6 +78,35 @@ model ClientReferences {
@@unique([reference, service])
}
+model Product {
+ id String @id
+ name String
+ description String?
+ features String[]
+ images String[]
+ meta Json
+
+ createdAt DateTime @default(now())
+ checkout TransactionLog[]
+}
+
+model TransactionLog {
+ // Stripe event id (debug and idempotency purposes)
+ eventId String
+ // Stripe session id (debug and idempotency purposes)
+ sessionId String
+ productId String
+ product Product @relation(fields: [productId], references: [id])
+ userId String
+ user User @relation(fields: [userId], references: [id])
+ // paid
+ status String
+ createdAt DateTime @default(now())
+
+ @@unique([eventId, productId])
+ @@unique([sessionId, productId])
+}
+
model Project {
id String @id @default(uuid())
createdAt DateTime @default(now())