Skip to content

Commit

Permalink
add notifications of subscription create & churn
Browse files Browse the repository at this point in the history
  • Loading branch information
d-ivashchuk committed Apr 1, 2024
1 parent ff341f0 commit 3024399
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const Billing = () => {
{userSubscriptionQuery.isLoading && (
<Skeleton className="h-[100px] max-w-lg" />
)}
{userSubscriptionQuery.data ? (
{subscription ? (
<Card className="min-h-[100px] max-w-lg p-4">
<div className="flex justify-between">
<h2 className="text-xl">Subscription</h2>
Expand Down
34 changes: 31 additions & 3 deletions src/app/api/lemon-squeezy/webhook/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
type LemonsqueezySubscriptionAttributes,
type LemonsqueezyWebhookPayload,
} from "~/types/lemonsqueezy";
import { isTriggerEnabled } from "~/lib/trigger";
import { slackNewChurnNotification, slackNewPaymentNotification } from "~/jobs";

export async function POST(request: Request) {
if (!env.LEMON_SQUEEZY_WEBHOOK_SECRET) {
Expand Down Expand Up @@ -51,6 +53,31 @@ export async function POST(request: Request) {
where: { lemonSqueezyId: lemonSqueezySubscriptionId },
});

if (isTriggerEnabled) {
if (event === "subscription_created") {
await slackNewPaymentNotification.invoke({
user: {
email: subscriptionData.user_email,
id: userIdInDatabase,
},
productName: subscriptionData.product_name,
});
}
if (
event === "subscription_cancelled" ||
(event === "subscription_updated" &&
subscriptionData.status === "cancelled")
) {
await slackNewChurnNotification.invoke({
user: {
email: subscriptionData.user_email,
id: userIdInDatabase,
},
productName: subscriptionData.product_name,
});
}
}

switch (event) {
case "subscription_created":
case "subscription_updated":
Expand All @@ -61,9 +88,10 @@ export async function POST(request: Request) {
renewsAt: subscriptionData.renews_at
? new Date(subscriptionData.renews_at)
: null,
endsAt: subscriptionData.ends_at
? new Date(subscriptionData.ends_at)
: null,
endsAt:
subscriptionData.status === "cancelled"
? new Date(existingSubscription?.renewsAt ?? new Date())
: subscriptionData.ends_at ?? null,
trialEndsAt: subscriptionData.trial_ends_at
? new Date(subscriptionData.trial_ends_at)
: null,
Expand Down
48 changes: 48 additions & 0 deletions src/jobs/jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,51 @@ export const slackNewUserNotification = triggerClient.defineJob({
});
},
});
export const slackNewPaymentNotification = triggerClient.defineJob({
id: "cascade-new-payment",
name: "Cascade new payment",
version: "0.0.1",
trigger: eventTrigger({
name: "cascade.new.payment",
schema: z.object({
user: z.object({
email: z.string().email(),
id: z.string(),
}),
productName: z.string(),
}),
}),
integrations: {
slack,
},
run: async (payload, io) => {
await io.slack.postMessage("post message", {
channel: "C06RZ0QNP6W",
text: `🔥 *New payment*\n\nEmail: ${payload.user.email}\nID:${payload.user.id}`,
});
},
});
export const slackNewChurnNotification = triggerClient.defineJob({
id: "cascade-new-churn",
name: "Cascade new churn",
version: "0.0.1",
trigger: eventTrigger({
name: "cascade.new.churn",
schema: z.object({
user: z.object({
email: z.string().email(),
id: z.string(),
}),
productName: z.string(),
}),
}),
integrations: {
slack,
},
run: async (payload, io) => {
await io.slack.postMessage("post message", {
channel: "C06RZ0QNP6W",
text: `👋🏼 *User churned*\n\nEmail: ${payload.user.email}`,
});
},
});

0 comments on commit 3024399

Please sign in to comment.