-
Notifications
You must be signed in to change notification settings - Fork 106
cleanup org's repos and shards if it's inactive #194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
...s/db/prisma/migrations/20250214214601_add_stripe_subscription_status_to_org/migration.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
-- CreateEnum | ||
CREATE TYPE "StripeSubscriptionStatus" AS ENUM ('ACTIVE', 'INACTIVE'); | ||
|
||
-- AlterTable | ||
ALTER TABLE "Org" ADD COLUMN "stripeLastUpdatedAt" TIMESTAMP(3), | ||
ADD COLUMN "stripeSubscriptionStatus" "StripeSubscriptionStatus"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { headers } from 'next/headers'; | ||
import { NextRequest } from 'next/server'; | ||
import Stripe from 'stripe'; | ||
import { prisma } from '@/prisma'; | ||
import { STRIPE_WEBHOOK_SECRET } from '@/lib/environment'; | ||
import { getStripe } from '@/lib/stripe'; | ||
import { ConnectionSyncStatus, StripeSubscriptionStatus } from '@sourcebot/db'; | ||
export async function POST(req: NextRequest) { | ||
const body = await req.text(); | ||
const signature = headers().get('stripe-signature'); | ||
|
||
if (!signature) { | ||
return new Response('No signature', { status: 400 }); | ||
} | ||
|
||
try { | ||
const stripe = getStripe(); | ||
const event = stripe.webhooks.constructEvent( | ||
body, | ||
signature, | ||
STRIPE_WEBHOOK_SECRET! | ||
); | ||
|
||
if (event.type === 'customer.subscription.deleted') { | ||
const subscription = event.data.object as Stripe.Subscription; | ||
const customerId = subscription.customer as string; | ||
|
||
const org = await prisma.org.findFirst({ | ||
where: { | ||
stripeCustomerId: customerId | ||
} | ||
}); | ||
|
||
if (!org) { | ||
return new Response('Org not found', { status: 404 }); | ||
} | ||
|
||
await prisma.org.update({ | ||
where: { | ||
id: org.id | ||
}, | ||
data: { | ||
stripeSubscriptionStatus: StripeSubscriptionStatus.INACTIVE, | ||
stripeLastUpdatedAt: new Date() | ||
} | ||
}); | ||
console.log(`Org ${org.id} subscription status updated to INACTIVE`); | ||
|
||
return new Response(JSON.stringify({ received: true }), { | ||
status: 200 | ||
}); | ||
} else if (event.type === 'customer.subscription.created') { | ||
const subscription = event.data.object as Stripe.Subscription; | ||
const customerId = subscription.customer as string; | ||
|
||
const org = await prisma.org.findFirst({ | ||
where: { | ||
stripeCustomerId: customerId | ||
} | ||
}); | ||
|
||
if (!org) { | ||
return new Response('Org not found', { status: 404 }); | ||
} | ||
|
||
await prisma.org.update({ | ||
where: { | ||
id: org.id | ||
}, | ||
data: { | ||
stripeSubscriptionStatus: StripeSubscriptionStatus.ACTIVE, | ||
stripeLastUpdatedAt: new Date() | ||
} | ||
}); | ||
console.log(`Org ${org.id} subscription status updated to ACTIVE`); | ||
|
||
// mark all of this org's connections for sync, since their repos may have been previously garbage collected | ||
await prisma.connection.updateMany({ | ||
where: { | ||
orgId: org.id | ||
}, | ||
data: { | ||
syncStatus: ConnectionSyncStatus.SYNC_NEEDED | ||
} | ||
}); | ||
|
||
return new Response(JSON.stringify({ received: true }), { | ||
status: 200 | ||
}); | ||
} else { | ||
console.log(`Received unknown event type: ${event.type}`); | ||
return new Response(JSON.stringify({ received: true }), { | ||
status: 202 | ||
}); | ||
} | ||
|
||
} catch (err) { | ||
console.error('Error processing webhook:', err); | ||
return new Response( | ||
'Webhook error: ' + (err as Error).message, | ||
{ status: 400 } | ||
); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.