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.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
feat: Support mutiple events in transaction log table (#2641)
Ivan Starkov0395839d5084
diff --git a/apps/builder/app/shared/db/user-plan-features.server.ts b/apps/builder/app/shared/db/user-plan-features.server.tsindex c1723ec2a6..3f2bdb23ab 100644--- a/apps/builder/app/shared/db/user-plan-features.server.ts+++ b/apps/builder/app/shared/db/user-plan-features.server.ts@@ -45,15 +45,9 @@ export const getTokenPlanFeatures = async (token: string) => {export const getUserPlanFeatures = async (userId: string): Promise<UserPlanFeatures> => {- const transactionLog = await prisma.transactionLog.findMany({+ const userProducts = await prisma.userProduct.findMany({where: { userId },- orderBy: {- createdAt: "desc",- },select: {- eventId: true,- sessionId: true,- createdAt: true,customerId: true,subscriptionId: true,product: {@@ -71,8 +65,8 @@ export const getUserPlanFeatures = async (// This is fast and dirty implementation// @todo: implement this using products meta, custom table with aggregated transaction info- if (transactionLog.length > 0) {- const hasSubscription = transactionLog.some(+ if (userProducts.length > 0) {+ const hasSubscription = userProducts.some((log) => log.subscriptionId !== null);diff --git a/packages/prisma-client/prisma/migrations/20231120172840_add-event-type/migration.sql b/packages/prisma-client/prisma/migrations/20231120172840_add-event-type/migration.sqlnew file mode 100644index 0000000000..6ccd1eaf3d--- /dev/null+++ b/packages/prisma-client/prisma/migrations/20231120172840_add-event-type/migration.sql@@ -0,0 +1,33 @@+-- AlterTable+ALTER TABLE "TransactionLog" ADD COLUMN "eventType" TEXT NOT NULL DEFAULT 'checkout.session.completed';++ALTER TABLE "TransactionLog"+ALTER COLUMN "eventType" DROP DEFAULT;++-- AlterTable+ALTER TABLE "TransactionLog" ADD COLUMN "eventCreated" INTEGER NOT NULL DEFAULT 0;+-- Fill Initally+UPDATE "TransactionLog" SET "eventCreated" = EXTRACT(EPOCH FROM "createdAt");++ALTER TABLE "TransactionLog"+ALTER COLUMN "eventCreated" DROP DEFAULT;++-- AlterTable+ALTER TABLE "TransactionLog" ADD CONSTRAINT "TransactionLog_pkey" PRIMARY KEY ("eventId");++-- DropIndex+DROP INDEX "TransactionLog_sessionId_productId_key";+++CREATE VIEW "UserProduct" AS+SELECT "userId", "subscriptionId", "productId", "customerId", "customerEmail"+FROM "TransactionLog" AS tl+WHERE "status" = 'paid'+ AND NOT EXISTS (+ SELECT 1+ FROM "TransactionLog" AS tlexsists+ WHERE tlexsists."subscriptionId" = tl."subscriptionId"+ AND tlexsists."status" = 'canceled'+ AND tlexsists."eventCreated" > tl."eventCreated"+ )+ORDER BY "userId", "eventCreated" DESC;diff --git a/packages/prisma-client/prisma/schema.prisma b/packages/prisma-client/prisma/schema.prismaindex a13b5720c9..e2646d5a36 100644--- a/packages/prisma-client/prisma/schema.prisma+++ b/packages/prisma-client/prisma/schema.prisma@@ -61,6 +61,7 @@ model User {projects Project[]clientReferences ClientReferences[]checkout TransactionLog[]+ products UserProduct[]}// User client references in external services like stripe etc@@ -83,22 +84,25 @@ model Product {images String[]meta Json- createdAt DateTime @default(now())- checkout TransactionLog[]+ createdAt DateTime @default(now())+ checkout TransactionLog[]+ userProducts UserProduct[]}model TransactionLog {// Stripe event id (debug and idempotency purposes)- eventId String+ eventId String @id+ eventType String+ eventCreated Int// 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])+ 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())+ status String+ createdAt DateTime @default(now())// Helper fields we can use for faster api calls and to have additional information for debugging purposescustomerId String?@@ -106,7 +110,20 @@ model TransactionLog {customerEmail String?@@unique([eventId, productId])- @@unique([sessionId, productId])+}++view UserProduct {+ userId String+ user User @relation(fields: [userId], references: [id])+ productId String+ product Product @relation(fields: [productId], references: [id])+ // subscriptionId and customerId not null for subscriptions+ subscriptionId String?+ customerId String?+ // Easier to debug+ customerEmail String?++ @@unique([userId, productId])}model Project {