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: Support refund events (#2673)

Ivan Starkove65ec39eef29
diff --git a/apps/builder/app/routes/rest.current.products.ts b/apps/builder/app/routes/rest.current.products.ts
index 3ed28f62b0..512ccae5fc 100644
--- a/apps/builder/app/routes/rest.current.products.ts
+++ b/apps/builder/app/routes/rest.current.products.ts
@@ -23,12 +23,27 @@ export const loader = async ({ request, params }: LoaderArgs) => {
where: { id: user.id },
select: {
id: true,
+
username: true,
+ products: {
+ select: {
+ product: {
+ select: {
+ id: true,
+ name: true,
+ description: true,
+ features: true,
+ meta: true,
+ images: true,
+ },
+ },
+ },
+ },
checkout: {
select: {
eventId: true,
- sessionId: true,
createdAt: true,
+
product: {
select: {
id: true,
diff --git a/apps/builder/docker-compose.yaml b/apps/builder/docker-compose.yaml
index e6303b1b57..349952e4e1 100644
--- a/apps/builder/docker-compose.yaml
+++ b/apps/builder/docker-compose.yaml
@@ -7,7 +7,7 @@ services:
db:
image: postgres
# Uncomment to log all queries
- # command: ["postgres", "-c", "log_statement=all"]
+ command: ["postgres", "-c", "log_statement=all"]
restart: always
environment:
# postgresql://user:pass@localhost:5432/webstudio
diff --git a/packages/prisma-client/prisma/migrations/20231129164239_event-data/migration.sql b/packages/prisma-client/prisma/migrations/20231129164239_event-data/migration.sql
new file mode 100644
index 0000000000..7f1a604c78
--- /dev/null
+++ b/packages/prisma-client/prisma/migrations/20231129164239_event-data/migration.sql
@@ -0,0 +1,58 @@
+DROP VIEW "UserProduct";
+
+ALTER TABLE "public"."TransactionLog" ALTER COLUMN "productId" DROP NOT NULL;
+
+ALTER TABLE "TransactionLog" DROP CONSTRAINT "TransactionLog_productId_fkey";
+
+-- AddForeignKey
+ALTER TABLE "TransactionLog" ADD CONSTRAINT "TransactionLog_productId_fkey" FOREIGN KEY ("productId") REFERENCES "Product"("id") ON DELETE SET NULL ON UPDATE CASCADE;
+
+
+ALTER TABLE "TransactionLog"
+DROP COLUMN "eventCreated",
+DROP COLUMN "eventType",
+DROP COLUMN "status",
+DROP COLUMN "customerEmail",
+DROP COLUMN "subscriptionId",
+DROP COLUMN "customerId",
+DROP COLUMN "sessionId";
+
+ALTER TABLE "TransactionLog"
+ADD COLUMN "eventData" JSONB,
+ADD COLUMN "eventCreated" INT GENERATED ALWAYS AS (("eventData"#>>'{created}')::INTEGER) STORED,
+ADD COLUMN "eventType" TEXT GENERATED ALWAYS AS ("eventData"#>>'{type}') STORED,
+ADD COLUMN "status" TEXT GENERATED ALWAYS AS ("eventData"#>>'{data,object,status}') STORED,
+ADD COLUMN "customerId" TEXT GENERATED ALWAYS AS ("eventData"#>>'{data,object,customer}') STORED,
+ADD COLUMN "customerEmail" TEXT GENERATED ALWAYS AS ("eventData"#>>'{data,object,customer_details,email}') STORED,
+ADD COLUMN "subscriptionId" TEXT GENERATED ALWAYS AS (
+ CASE
+ WHEN "eventData"#>>'{data,object,object}' = 'subscription'
+ THEN "eventData"#>>'{data,object,id}'
+ ELSE "eventData"#>>'{data,object,subscription}'
+ END
+) STORED,
+ADD COLUMN "paymentIntent" TEXT GENERATED ALWAYS AS ("eventData"#>>'{data,object,payment_intent}') STORED;
+
+
+CREATE VIEW "UserProduct" AS
+SELECT "userId", "subscriptionId", "productId", "customerId", "customerEmail"
+FROM "TransactionLog" AS tl
+WHERE "status" = 'complete' AND "eventType" = 'checkout.session.completed'
+ AND NOT EXISTS (
+ SELECT 1
+ FROM "TransactionLog" AS tlexsists
+ WHERE tlexsists."subscriptionId" = tl."subscriptionId"
+ AND tlexsists."eventType" = 'customer.subscription.deleted'
+ AND tlexsists."status" = 'canceled'
+ AND tlexsists."eventCreated" > tl."eventCreated"
+ )
+ AND NOT EXISTS (
+ SELECT 1
+ FROM "TransactionLog" AS tlexsists
+ WHERE tlexsists."paymentIntent" = tl."paymentIntent"
+ AND tlexsists."eventType" = 'charge.refunded'
+ AND tlexsists."status" = 'succeeded'
+ 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.prisma
index e2646d5a36..d60e8301bf 100644
--- a/packages/prisma-client/prisma/schema.prisma
+++ b/packages/prisma-client/prisma/schema.prisma
@@ -91,23 +91,30 @@ model Product {
model TransactionLog {
// Stripe event id (debug and idempotency purposes)
- 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])
+ eventId String @id
+ userId String
+ user User @relation(fields: [userId], references: [id])
+
+ productId String?
+ product Product? @relation(fields: [productId], references: [id])
+
+ createdAt DateTime @default(now())
+
+ eventData Json? @default(dbgenerated())
+
+ eventType String? @default(dbgenerated())
+ eventCreated Int? @default(dbgenerated())
+
// paid
- status String
- createdAt DateTime @default(now())
+ status String? @default(dbgenerated())
- // Helper fields we can use for faster api calls and to have additional information for debugging purposes
- customerId String?
- subscriptionId String?
- customerEmail String?
+ customerId String? @default(dbgenerated())
+ customerEmail String? @default(dbgenerated())
+
+ subscriptionId String? @default(dbgenerated())
+
+ // Used for Refund
+ paymentIntent String? @default(dbgenerated())
@@unique([eventId, productId])
}