Skip to content

Commit

Permalink
more ts
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminshafii committed Dec 4, 2024
1 parent b981da8 commit 7946ee0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 32 deletions.
47 changes: 27 additions & 20 deletions web/app/api/webhook/handlers/subscription-canceled.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { createWebhookHandler } from '../handler-factory';
import { WebhookEvent, WebhookHandlerResponse, CustomerData } from '../types';
import { updateClerkMetadata } from '@/lib/services/clerk';
import { db, UserUsageTable } from '@/drizzle/schema';
import { eq } from 'drizzle-orm';
import { updateUserSubscriptionData } from '../utils';
import Stripe from 'stripe';
import { createWebhookHandler } from "../handler-factory";
import { WebhookEvent, WebhookHandlerResponse, CustomerData } from "../types";
import { updateClerkMetadata } from "@/lib/services/clerk";
import { db, UserUsageTable } from "@/drizzle/schema";
import { eq } from "drizzle-orm";
import { updateUserSubscriptionData } from "../utils";
import Stripe from "stripe";

function getSubscriptionProduct(subscription: any): string | null {
const productKey = subscription.items?.data?.[0]?.price?.product?.metadata?.srm_product_key;
const productKey =
subscription.items?.data?.[0]?.price?.product?.metadata?.srm_product_key;
return productKey || null;
}

Expand All @@ -16,26 +17,32 @@ function getSubscriptionPrice(subscription: any): string | null {
}

async function deleteUserSubscriptionData(userId: string) {
await db.update(UserUsageTable).set({
subscriptionStatus: 'canceled',
paymentStatus: 'canceled',
}).where(eq(UserUsageTable.userId, userId));
await db
.update(UserUsageTable)
.set({
subscriptionStatus: "canceled",
paymentStatus: "canceled",
})
.where(eq(UserUsageTable.userId, userId));
}

export const handleSubscriptionCanceled = createWebhookHandler(
async (event: Stripe.CustomerSubscriptionDeletedEvent) => {
const subscription = event.data.object;
const userId = subscription.metadata?.userId;

await deleteUserSubscriptionData(userId);

const customerData: CustomerData = {
userId,
customerId: typeof subscription.customer === 'string' ? subscription.customer : subscription.customer.id,
status: 'canceled',
paymentStatus: 'canceled',
product: getSubscriptionProduct(subscription) || 'none',
plan: getSubscriptionPrice(subscription) || 'none',
customerId:
typeof subscription.customer === "string"
? subscription.customer
: subscription.customer.id,
status: "canceled",
paymentStatus: "canceled",
product: getSubscriptionProduct(subscription) || "none",
plan: getSubscriptionPrice(subscription) || "none",
lastPayment: new Date(),
};

Expand All @@ -48,6 +55,6 @@ export const handleSubscriptionCanceled = createWebhookHandler(
};
},
{
requiredMetadata: ['userId'],
requiredMetadata: ["userId"],
}
);
);
12 changes: 0 additions & 12 deletions web/lib/srm.ts

This file was deleted.

0 comments on commit 7946ee0

Please sign in to comment.