mirror of
https://github.com/hanzo-apps/dataroom.git
synced 2026-10-30 14:47:36 +00:00
The client was already @hanzo/kv; only the vocabulary was still Redis. Every module, symbol and comment now says what it connects to: lib/redis.ts -> lib/kv.ts (exports kv, lockKv) lib/redis-job-store.ts -> lib/kv-job-store.ts (KvJobStore) lib/redis-download-job-store.ts -> lib/kv-download-job-store.ts lib/files/tus-redis-locker.ts -> lib/files/tus-kv-locker.ts (KvLocker, KvLock) lib/api/domains/redis.ts -> lib/api/domains/kv.ts (getKey) lib/redis/ -> lib/kv-queue/ KV_URL is now the only endpoint variable — the REDIS_URL fallback and the redis://localhost default are gone, and the default scheme is kv://, which @hanzo/kv parses natively (utils/index.js). .env.example gained the KV_URL it never documented, and lost UPSTASH_REDIS_REST_LOCKER_URL/TOKEN: nothing has read those since the client changed — the locker opens a second connection to the same KV, so there is no second endpoint to configure. The second connection is `lockKv`, not `locker`: both tus routes already have a local `const locker = new KvLocker(...)`, so naming the client `locker` made the lock take itself as its own client. Verified against the repo's own gate (npx tsc --noEmit): 100 errors before and after, 0 introduced, compared set-wise by file:line:code. Behaviour is untouched — this is naming, one connection string, and dead config. Co-authored-by: Hanzo Dev <[email protected]>
116 lines
3.2 KiB
TypeScript
116 lines
3.2 KiB
TypeScript
import { redirect } from "next/navigation";
|
|
import { NextResponse } from "next/server";
|
|
|
|
import { authOptions } from "@/pages/api/auth/[...nextauth]";
|
|
import { Team } from "@prisma/client";
|
|
import { getServerSession } from "next-auth";
|
|
import z from "zod";
|
|
|
|
import { installIntegration } from "@/lib/integrations/install";
|
|
import { getSlackEnv } from "@/lib/integrations/slack/env";
|
|
import { SlackCredential } from "@/lib/integrations/slack/types";
|
|
import { encryptSlackToken } from "@/lib/integrations/slack/utils";
|
|
import prisma from "@/lib/prisma";
|
|
import { kv } from "@/lib/kv";
|
|
import { CustomUser } from "@/lib/types";
|
|
import { getSearchParams } from "@/lib/utils/get-search-params";
|
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
const oAuthCallbackSchema = z.object({
|
|
code: z.string(),
|
|
state: z.string(),
|
|
});
|
|
|
|
export const GET = async (req: Request) => {
|
|
const env = getSlackEnv();
|
|
|
|
let team: Pick<Team, "id" | "plan"> | null = null;
|
|
|
|
try {
|
|
const session = await getServerSession(authOptions);
|
|
if (!session) {
|
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
}
|
|
|
|
const userId = (session.user as CustomUser).id;
|
|
|
|
const { code, state } = oAuthCallbackSchema.parse(getSearchParams(req.url));
|
|
|
|
// Find workspace that initiated the Stripe app install
|
|
const stateKey = `slack:install:state:${state}`;
|
|
const teamId = await kv.get<string>(stateKey);
|
|
|
|
if (!teamId) {
|
|
return NextResponse.json({ error: "Invalid state" }, { status: 400 });
|
|
}
|
|
await kv.del(stateKey);
|
|
|
|
team = await prisma.team.findUniqueOrThrow({
|
|
where: {
|
|
id: teamId,
|
|
users: {
|
|
some: {
|
|
userId,
|
|
},
|
|
},
|
|
},
|
|
select: {
|
|
id: true,
|
|
plan: true,
|
|
},
|
|
});
|
|
|
|
if (!team) {
|
|
return NextResponse.json({ error: "Access denied" }, { status: 403 });
|
|
}
|
|
|
|
const body = new URLSearchParams({
|
|
code,
|
|
client_id: env.SLACK_CLIENT_ID,
|
|
client_secret: env.SLACK_CLIENT_SECRET,
|
|
redirect_uri: `${process.env.NEXT_PUBLIC_BASE_URL}/api/integrations/slack/oauth/callback`,
|
|
});
|
|
const ac = new AbortController();
|
|
const to = setTimeout(() => ac.abort(), 10000);
|
|
const response = await fetch("https://slack.com/api/oauth.v2.access", {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
Accept: "application/json",
|
|
},
|
|
body: body.toString(),
|
|
signal: ac.signal,
|
|
}).finally(() => clearTimeout(to));
|
|
|
|
const data = await response.json();
|
|
if (!data?.ok) {
|
|
return NextResponse.json(
|
|
{ error: `Slack OAuth error: ${data?.error || "unknown"}` },
|
|
{ status: 400 },
|
|
);
|
|
}
|
|
|
|
const credentials: SlackCredential = {
|
|
appId: data.app_id,
|
|
botUserId: data.bot_user_id,
|
|
scope: data.scope,
|
|
accessToken: encryptSlackToken(data.access_token),
|
|
tokenType: data.token_type,
|
|
authUser: data.authed_user,
|
|
team: data.team,
|
|
};
|
|
|
|
await installIntegration({
|
|
integrationId: env.SLACK_INTEGRATION_ID,
|
|
userId,
|
|
teamId,
|
|
credentials,
|
|
});
|
|
} catch (e: any) {
|
|
return NextResponse.json({ error: e.message }, { status: 500 });
|
|
}
|
|
|
|
redirect(`/settings/slack?success=true`);
|
|
};
|