Skip to content

Commit c9b56fc

Browse files
committed
Use generated updated_at for subscription table
1 parent 0a72e18 commit c9b56fc

File tree

5 files changed

+37
-14
lines changed

5 files changed

+37
-14
lines changed

packages/billing/src/subscription-webhooks.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ export async function handleSubscriptionInvoicePaid(params: {
146146
),
147147
billing_period_end: new Date(stripeSub.current_period_end * 1000),
148148
cancel_at_period_end: stripeSub.cancel_at_period_end,
149-
updated_at: new Date(),
150149
},
151150
})
152151

@@ -197,7 +196,6 @@ export async function handleSubscriptionInvoicePaymentFailed(params: {
197196
.update(schema.subscription)
198197
.set({
199198
status: 'past_due',
200-
updated_at: new Date(),
201199
})
202200
.where(eq(schema.subscription.stripe_subscription_id, subscriptionId))
203201

@@ -310,7 +308,6 @@ export async function handleSubscriptionUpdated(params: {
310308
billing_period_end: new Date(
311309
stripeSubscription.current_period_end * 1000,
312310
),
313-
updated_at: new Date(),
314311
},
315312
})
316313

@@ -356,7 +353,6 @@ export async function handleSubscriptionDeleted(params: {
356353
status: 'canceled',
357354
scheduled_tier: null,
358355
canceled_at: new Date(),
359-
updated_at: new Date(),
360356
})
361357
.where(eq(schema.subscription.stripe_subscription_id, subscriptionId))
362358

@@ -456,7 +452,6 @@ export async function handleSubscriptionScheduleCreatedOrUpdated(params: {
456452
.update(schema.subscription)
457453
.set({
458454
scheduled_tier: scheduledTier,
459-
updated_at: new Date(),
460455
})
461456
.where(eq(schema.subscription.stripe_subscription_id, subscriptionId))
462457
.returning({ tier: schema.subscription.tier })
@@ -526,7 +521,6 @@ export async function handleSubscriptionScheduleReleasedOrCanceled(params: {
526521
.update(schema.subscription)
527522
.set({
528523
scheduled_tier: null,
529-
updated_at: new Date(),
530524
})
531525
.where(eq(schema.subscription.stripe_subscription_id, subscriptionId))
532526
.returning({ tier: schema.subscription.tier })
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
-- Create a reusable function that sets updated_at to NOW()
2+
CREATE OR REPLACE FUNCTION set_updated_at()
3+
RETURNS TRIGGER AS $$
4+
BEGIN
5+
NEW.updated_at = NOW();
6+
RETURN NEW;
7+
END;
8+
$$ LANGUAGE plpgsql;
9+
10+
--> statement-breakpoint
11+
12+
-- Add trigger to subscription table
13+
CREATE TRIGGER trigger_subscription_updated_at
14+
BEFORE UPDATE ON "subscription"
15+
FOR EACH ROW
16+
EXECUTE FUNCTION set_updated_at();
17+
18+
--> statement-breakpoint
19+
20+
-- Add trigger to limit_override table
21+
CREATE TRIGGER trigger_limit_override_updated_at
22+
BEFORE UPDATE ON "limit_override"
23+
FOR EACH ROW
24+
EXECUTE FUNCTION set_updated_at();

web/src/app/api/stripe/cancel-subscription/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export async function POST() {
4444
try {
4545
await db
4646
.update(schema.subscription)
47-
.set({ cancel_at_period_end: true, scheduled_tier: null, updated_at: new Date() })
47+
.set({ cancel_at_period_end: true, scheduled_tier: null })
4848
.where(
4949
eq(
5050
schema.subscription.stripe_subscription_id,

web/src/app/api/stripe/change-subscription-tier/route.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ export async function POST(req: NextRequest) {
122122
if (isCancelDowngrade) {
123123
await db
124124
.update(schema.subscription)
125-
.set({ scheduled_tier: null, updated_at: new Date() })
125+
.set({ scheduled_tier: null })
126126
.where(
127127
eq(
128128
schema.subscription.stripe_subscription_id,
@@ -137,7 +137,6 @@ export async function POST(req: NextRequest) {
137137
tier,
138138
stripe_price_id: newPriceId,
139139
scheduled_tier: null,
140-
updated_at: new Date(),
141140
})
142141
.where(
143142
eq(
@@ -158,7 +157,6 @@ export async function POST(req: NextRequest) {
158157
.update(schema.subscription)
159158
.set({
160159
scheduled_tier: tier,
161-
updated_at: new Date(),
162160
})
163161
.where(
164162
eq(

web/src/app/api/user/subscription/route.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ import { getServerSession } from 'next-auth'
1010
import { authOptions } from '@/app/api/auth/[...nextauth]/auth-options'
1111
import { logger } from '@/util/logger'
1212

13+
import type {
14+
NoSubscriptionResponse,
15+
ActiveSubscriptionResponse,
16+
} from '@codebuff/common/types/subscription'
17+
1318
export async function GET() {
1419
const session = await getServerSession(authOptions)
1520
if (!session?.user?.id) {
@@ -19,16 +24,17 @@ export async function GET() {
1924
const userId = session.user.id
2025
const subscription = await getActiveSubscription({ userId, logger })
2126

22-
if (!subscription) {
23-
return NextResponse.json({ hasSubscription: false })
27+
if (!subscription || !subscription.tier) {
28+
const response: NoSubscriptionResponse = { hasSubscription: false }
29+
return NextResponse.json(response)
2430
}
2531

2632
const [rateLimit, limits] = await Promise.all([
2733
checkRateLimit({ userId, subscription, logger }),
2834
getSubscriptionLimits({ userId, logger, tier: subscription.tier }),
2935
])
3036

31-
return NextResponse.json({
37+
const response: ActiveSubscriptionResponse = {
3238
hasSubscription: true,
3339
displayName: SUBSCRIPTION_DISPLAY_NAME,
3440
subscription: {
@@ -52,5 +58,6 @@ export async function GET() {
5258
weeklyPercentUsed: rateLimit.weeklyPercentUsed,
5359
},
5460
limits,
55-
})
61+
}
62+
return NextResponse.json(response)
5663
}

0 commit comments

Comments
 (0)