Skip to content
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

WIP: Sync prod to main #105

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions app/api/subs/route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { getAuthTokenOrNull } from '@/helpers/oauth/helpers'
import guildProfileSchema from '@/models/Guild'
import { NextResponse } from 'next/server'
import connectDb from '@/lib/mongodb'
import { stripe } from '@/lib/stripe'
import { prisma } from '@/lib/prisma'

interface StripeSubscriptionRequest {
monthly: string
Expand All @@ -27,7 +27,14 @@ export async function POST(req: Request) {
const { priceId, serverId, monthly } =
(await req.json()) as StripeSubscriptionRequest

const server = await guildProfileSchema.findOne({ guildID: serverId })
const server = await prisma.guild.findFirst({
where: {
guildID: serverId
},
select: {
premiumExpiration: true
}
})

if (!serverId) {
return NextResponse.json(
Expand Down
133 changes: 114 additions & 19 deletions app/api/webhook/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import guildProfileSchema from '@/models/Guild'
import type { NextRequest } from 'next/server'
import { NextResponse } from 'next/server'
import { stripe } from '@/lib/stripe'
import { prisma } from '@/lib/prisma'
import type Stripe from 'stripe'

export async function POST(request: NextRequest) {
Expand Down Expand Up @@ -47,18 +47,29 @@ export async function POST(request: NextRequest) {
)
}
try {
await guildProfileSchema.findOneAndUpdate(
{ guildID: serverId },
{
await prisma.guild.upsert({
where: {
guildID: serverId
},
create: {
guildID: serverId,
premiumUser: userId,
premium: 1,
pending: true,
premiumExpiration: new Date(
subscription.current_period_end * 1000
)
),
language: 'en_US'
},
{ upsert: true }
)
update: {
premiumUser: userId,
premium: 1,
pending: true,
premiumExpiration: new Date(
subscription.current_period_end * 1000
)
}
})
} catch (error) {
console.error(error)
return NextResponse.json(
Expand All @@ -71,6 +82,84 @@ export async function POST(request: NextRequest) {
}
break

case 'invoice.paid':
case 'invoice.payment_succeeded':
const invoice: Stripe.Invoice = event.data.object

if (invoice.subscription_details === null)
return NextResponse.json(
{ message: 'No subscription details found', status: 400 },
{ status: 400 }
)

const userIdInvoice = invoice.subscription_details.metadata?.userId
const serverIdInvoice = invoice.subscription_details.metadata?.serverId
const tierInvoice =
invoice.subscription_details.metadata?.monthly === 'true' ?
'monthly'
: 'yearly'

if (!userIdInvoice || !serverIdInvoice || !tierInvoice) {
console.error('One or more variables are undefined.')
return NextResponse.json(
{ message: 'One or more variables are missing', status: 400 },
{ status: 400 }
)
}
switch (invoice.billing_reason) {
case 'subscription_create':
try {
await prisma.guild.update({
// @ts-ignore
where: {
guildID: serverIdInvoice
},
data: {
pending: false,
premiumExpiration: new Date(
invoice.lines.data[0].period.end * 1000
)
}
})
} catch (error) {
console.error(error)
return NextResponse.json(
{
message: 'An error occurred while updating the database.',
status: 500
},
{ status: 500 }
)
}
break
case 'subscription_update':
try {
await prisma.guild.update({
// @ts-ignore
where: {
guildID: serverIdInvoice
},
data: {
pending: false,
premiumExpiration: new Date(
invoice.lines.data[0].period.end * 1000
)
}
})
} catch (error) {
console.error(error)
return NextResponse.json(
{
message: 'An error occurred while updating the database.',
status: 500
},
{ status: 500 }
)
}
break
}
break

// in the event of a subscription being updated
case 'customer.subscription.updated':
const subscriptionUpdated: Stripe.Subscription = event.data.object
Expand All @@ -89,16 +178,19 @@ export async function POST(request: NextRequest) {
)
}

await guildProfileSchema.findOneAndUpdate(
{ guildID: serverIdUpdated },
{
await prisma.guild.update({
// @ts-ignore
where: {
guildID: serverIdUpdated
},
data: {
premium: 1,
premiumExpiration: new Date(
subscriptionUpdated.current_period_end * 1000
)
},
{ upsert: true }
)
}
})

break

// in the event of a subscription being deleted
Expand All @@ -119,14 +211,17 @@ export async function POST(request: NextRequest) {
)
}
try {
await guildProfileSchema.findOneAndUpdate(
{ guildID: serverIdDeleted },
{
premiumUser: null,
await prisma.guild.update({
// @ts-ignore
where: {
guildID: serverIdDeleted
},
data: {
premium: 0,
premiumExpiration: null
premiumExpiration: null,
premiumUser: null
}
)
})
} catch (error) {
console.error(error)
return NextResponse.json(
Expand Down
Loading