Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

📈 Update space group in posthog (#1987)

Luke Vellafe6883ba1756
diff --git a/apps/web/src/app/api/stripe/webhook/handlers/checkout/completed.ts b/apps/web/src/app/api/stripe/webhook/handlers/checkout/completed.ts
index 13d198d51..22dc9d246 100644
--- a/apps/web/src/app/api/stripe/webhook/handlers/checkout/completed.ts
+++ b/apps/web/src/app/api/stripe/webhook/handlers/checkout/completed.ts
@@ -1,35 +1,10 @@
import type { Stripe } from "@rallly/billing";
-import { stripe } from "@rallly/billing";
import { posthog } from "@rallly/posthog/server";
import { env } from "@/env";
import { licenseCheckoutMetadataSchema } from "@/features/licensing/schema";
import { licenseManager } from "@/features/licensing/server";
-import { subscriptionCheckoutMetadataSchema } from "@/features/subscription/schema";
import { getEmailClient } from "@/utils/emails";
-async function handleSubscriptionCheckoutSessionCompleted(
- checkoutSession: Stripe.Checkout.Session,
-) {
- const { userId } = subscriptionCheckoutMetadataSchema.parse(
- checkoutSession.metadata,
- );
-
- const subscription = await stripe.subscriptions.retrieve(
- checkoutSession.subscription as string,
- );
-
- posthog?.capture({
- distinctId: userId,
- event: "upgrade",
- properties: {
- interval: subscription.items.data[0].price.recurring?.interval,
- $set: {
- tier: "pro",
- },
- },
- });
-}
-
async function handleSelfHostedCheckoutSessionCompleted(
checkoutSession: Stripe.Checkout.Session,
) {
@@ -109,7 +84,5 @@ export async function onCheckoutSessionCompleted(event: Stripe.Event) {
if (checkoutSession.subscription === null) {
await handleSelfHostedCheckoutSessionCompleted(checkoutSession);
- } else {
- await handleSubscriptionCheckoutSessionCompleted(checkoutSession);
}
}
diff --git a/apps/web/src/app/api/stripe/webhook/handlers/customer-subscription/created.ts b/apps/web/src/app/api/stripe/webhook/handlers/customer-subscription/created.ts
index 90f852998..c67bc45fa 100644
--- a/apps/web/src/app/api/stripe/webhook/handlers/customer-subscription/created.ts
+++ b/apps/web/src/app/api/stripe/webhook/handlers/customer-subscription/created.ts
@@ -1,5 +1,7 @@
import type { Stripe } from "@rallly/billing";
import { prisma } from "@rallly/database";
+import { posthog } from "@rallly/posthog/server";
+import { updateSpaceGroup } from "@/features/space/analytics";
import { subscriptionMetadataSchema } from "@/features/subscription/schema";
import {
getExpandedSubscription,
@@ -38,7 +40,7 @@ export async function onCustomerSubscriptionCreated(event: Stripe.Event) {
const quantity = subscriptionItem.quantity ?? 1;
// If user already has a subscription, update it or replace it
// Update the existing subscription with new data
- await prisma.subscription.upsert({
+ const updatedSubscription = await prisma.subscription.upsert({
where: { id: subscription.id },
create: {
id: subscription.id,
@@ -74,5 +76,46 @@ export async function onCustomerSubscriptionCreated(event: Stripe.Event) {
cancelAtPeriodEnd: subscription.cancel_at_period_end,
spaceId,
},
+ include: {
+ space: true,
+ },
});
+
+ if (updatedSubscription.space) {
+ const space = await prisma.space.findUniqueOrThrow({
+ where: {
+ id: spaceId,
+ },
+ });
+
+ updateSpaceGroup({
+ spaceId,
+ properties: {
+ name: space?.name,
+ seatCount: quantity,
+ tier: isActive ? "pro" : "hobby",
+ },
+ });
+
+ posthog?.capture({
+ distinctId: userId,
+ uuid: event.id,
+ event: "upgrade",
+ properties: {
+ interval,
+ $set: {
+ tier: isActive ? "pro" : "hobby",
+ },
+ },
+ groups: {
+ space: spaceId,
+ },
+ });
+ } else {
+ console.error("Failed to update space group on subscription.created", {
+ eventId: event.id,
+ subscriptionId: subscription.id,
+ spaceId: spaceId,
+ });
+ }
}
diff --git a/apps/web/src/app/api/stripe/webhook/handlers/customer-subscription/updated.ts b/apps/web/src/app/api/stripe/webhook/handlers/customer-subscription/updated.ts
index 743d36d60..713eca7bf 100644
--- a/apps/web/src/app/api/stripe/webhook/handlers/customer-subscription/updated.ts
+++ b/apps/web/src/app/api/stripe/webhook/handlers/customer-subscription/updated.ts
@@ -39,7 +39,7 @@ export async function onCustomerSubscriptionUpdated(event: Stripe.Event) {
const quantity = subscriptionItem.quantity ?? 1;
// Update the subscription in the database
- await prisma.subscription.update({
+ const updatedSubscription = await prisma.subscription.update({
where: {
id: subscription.id,
},
@@ -56,27 +56,41 @@ export async function onCustomerSubscriptionUpdated(event: Stripe.Event) {
periodEnd: toDate(subscription.current_period_end),
cancelAtPeriodEnd: subscription.cancel_at_period_end,
},
- });
-
- updateSpaceGroup({
- spaceId: res.data.spaceId,
- properties: {
- seatCount: quantity,
- tier: isActive ? "pro" : "hobby",
+ include: {
+ space: true,
},
});
- posthog?.capture({
- distinctId: res.data.userId,
- event: "subscription change",
- properties: {
- type: event.type,
- $set: {
+ if (updatedSubscription.space) {
+ updateSpaceGroup({
+ spaceId: updatedSubscription.space.id,
+ properties: {
+ name: updatedSubscription.space.name,
+ seatCount: quantity,
tier: isActive ? "pro" : "hobby",
},
- },
- groups: {
- space: res.data.spaceId,
- },
- });
+ });
+
+ posthog?.capture({
+ distinctId: res.data.userId,
+ uuid: event.id,
+ event: "subscription change",
+ properties: {
+ type: event.type,
+ interval,
+ $set: {
+ tier: isActive ? "pro" : "hobby",
+ },
+ },
+ groups: {
+ space: res.data.spaceId,
+ },
+ });
+ } else {
+ console.error("Failed to update space group on subscription.updated", {
+ eventId: event.id,
+ subscriptionId: subscription.id,
+ spaceId: res.data.spaceId,
+ });
+ }
}