Initial Commit

This commit is contained in:
Ahmad 2024-02-14 21:30:10 -05:00
commit f3e2f01bd7
No known key found for this signature in database
GPG key ID: 8FD8A93530D182BF
150 changed files with 13612 additions and 0 deletions

View file

@ -0,0 +1,35 @@
import { auth } from "@clerk/nextjs";
import { ENTITY_TYPE } from "@prisma/client";
import { NextResponse } from "next/server";
import { db } from "@/lib/db";
export async function GET(
req: Request,
{ params }: { params: { cardId: string } }
) {
try {
const { orgId, userId } = auth();
if (!orgId || !userId)
return new NextResponse(JSON.stringify({ error: "Unauthorized" }), {
status: 401,
});
const auditLogs = await db.auditLog.findMany({
where: {
orgId,
entityId: params.cardId,
entityType: ENTITY_TYPE.CARD,
},
orderBy: {
createdAt: "desc",
},
take: 3,
});
return NextResponse.json(auditLogs);
} catch (error) {
return new NextResponse(JSON.stringify(error), { status: 500 });
}
}

View file

@ -0,0 +1,40 @@
import { auth } from "@clerk/nextjs";
import { NextResponse } from "next/server";
import { db } from "@/lib/db";
export async function GET(
req: Request,
{ params }: { params: { cardId: string } }
) {
try {
const { orgId, userId } = auth();
if (!orgId || !userId)
return new NextResponse(JSON.stringify({ error: "Unauthorized" }), {
status: 401,
});
const card = await db.card.findUnique({
where: {
id: params.cardId,
list: {
board: {
orgId,
},
},
},
include: {
list: {
select: {
title: true,
},
},
},
});
return NextResponse.json(card);
} catch (error) {
return new NextResponse(JSON.stringify(error), { status: 500 });
}
}

69
app/api/webhook/route.ts Normal file
View file

@ -0,0 +1,69 @@
import Stripe from "stripe";
import { headers } from "next/headers";
import { NextResponse } from "next/server";
import { db } from "@/lib/db";
import { stripe } from "@/lib/stripe";
export async function POST(req: Request) {
const body = await req.text();
const signature = headers().get("Stripe-Signature") as string;
let event: Stripe.Event;
try {
event = stripe.webhooks.constructEvent(
body,
signature,
process.env.STRIPE_WEBHOOK_SECRET!
);
} catch (error) {
return new NextResponse(JSON.stringify(error), { status: 400 });
}
const session = event.data.object as Stripe.Checkout.Session;
if (event.type === "checkout.session.completed") {
const subscription = await stripe.subscriptions.retrieve(
session.subscription as string
);
if (!session?.metadata?.orgId) {
return new NextResponse(JSON.stringify({ error: "Missing orgId" }), {
status: 400,
});
}
await db.orgSubscription.create({
data: {
orgId: session.metadata.orgId,
stripeSubscriptionId: subscription.id,
stripeCustomerId: session.customer as string,
stripePriceId: subscription.items.data[0].price.id,
stripeCurrentPeriodEnd: new Date(
subscription.current_period_end * 1000
),
},
});
}
if (event.type === "invoice.payment_succeeded") {
const subscription = await stripe.subscriptions.retrieve(
session.subscription as string
);
await db.orgSubscription.update({
where: {
stripeSubscriptionId: subscription.id,
},
data: {
stripePriceId: subscription.items.data[0].price.id,
stripeCurrentPeriodEnd: new Date(
subscription.current_period_end * 1000
),
},
});
}
return NextResponse.json(null, { status: 200 });
}