* feat: setup onboarding and select plan page * feat: add workspace details step * refactor: optimize board rendering in workspace details * feat: tweak padding in boards mockup * fix: validate/convert slug input * refactor: tweak onboarding steps UX * feat: add team to workspace plans * feat: allow setting description when creating a workspace * feat: add logging to next api routes * feat: create worspace on checkout.session.completed * chore: update types * refactor: improve webhook logs * feat: create workspace on successful activation * feat: set slug on workspace name change * feat: add returnUrl params
59 lines
1.7 KiB
TypeScript
59 lines
1.7 KiB
TypeScript
import type { NextApiRequest, NextApiResponse } from "next";
|
|
import { addYears } from "date-fns";
|
|
|
|
import { createNextApiContext } from "@kan/api/trpc";
|
|
import { withApiLogging } from "@kan/api/utils/apiLogging";
|
|
import { withRateLimit } from "@kan/api/utils/rateLimit";
|
|
import { integrations } from "@kan/db/schema";
|
|
|
|
export default withRateLimit(
|
|
{ points: 100, duration: 60 },
|
|
withApiLogging(async (req: NextApiRequest, res: NextApiResponse) => {
|
|
if (req.method !== "POST") {
|
|
return res.status(405).json({ message: "Method not allowed" });
|
|
}
|
|
|
|
const { user } = await createNextApiContext(req);
|
|
|
|
if (!user)
|
|
return res.status(401).json({ message: "User not authenticated" });
|
|
|
|
const apiKey = process.env.TRELLO_APP_API_KEY;
|
|
|
|
if (!apiKey)
|
|
return res
|
|
.status(500)
|
|
.json({ message: "Trello API key not set in Environment Variables" });
|
|
|
|
const token = req.body.token;
|
|
|
|
if (!token) return res.status(400).json({ message: "No token found" });
|
|
|
|
try {
|
|
const { db } = await createNextApiContext(req);
|
|
|
|
await db
|
|
.insert(integrations)
|
|
.values({
|
|
provider: "trello",
|
|
userId: user.id,
|
|
accessToken: token,
|
|
expiresAt: addYears(new Date(), 1),
|
|
})
|
|
.onConflictDoUpdate({
|
|
set: {
|
|
accessToken: token,
|
|
expiresAt: addYears(new Date(), 1),
|
|
},
|
|
target: [integrations.userId, integrations.provider],
|
|
});
|
|
|
|
return res
|
|
.status(200)
|
|
.json({ message: "Trello authentication successful" });
|
|
} catch (err) {
|
|
return res.status(400).json({ message: "Trello authentication failed" });
|
|
}
|
|
}),
|
|
);
|