hermes-web/app/api/connection/prepare/route.ts

59 lines
2.2 KiB
TypeScript
Raw Permalink Normal View History

import { db } from "@/lib/db"
import { NextResponse } from "next/server";
import fetchUserWithImpersonation from '@/lib/fetch-user-impersonation';
export async function POST(req: Request) {
try {
const user = await fetchUserWithImpersonation(req);
if (!user) {
return NextResponse.json({ error: null, message: "Unauthorized", success: false }, { status: 401 });
}
let { state, name, type, grantType, clientId } = await req.json();
if (!clientId)
return NextResponse.json({ error: null, message: 'No client id given for the authorization.', success: false }, { status: 400 })
if (!type)
return NextResponse.json({ error: null, message: 'No type given for the authorization.', success: false }, { status: 400 })
if (!state)
return NextResponse.json({ error: null, message: 'No state given for the authorization.', success: false }, { status: 400 })
if (!name)
return NextResponse.json({ error: null, message: 'No name given for the authorization.', success: false }, { status: 400 })
if (!grantType)
return NextResponse.json({ error: null, message: 'No grant type given for the authorization.', success: false }, { status: 400 })
await db.connectionState.upsert({
where: {
userId_name: {
userId: user.id,
name
}
},
create: {
userId: user.id,
name,
type,
state,
grantType,
clientId
},
update: {
name,
state,
grantType,
}
})
return NextResponse.json({ error: null, message: "", success: true }, { status: 200 });
} catch (error: any) {
if (error.name == 'PrismaClientKnownRequestError') {
if (error.code == 'P2002')
return NextResponse.json({ error, message: "Connection already prepared.", success: false }, { status: 500 });
}
return NextResponse.json({ error, message: "Failed to prepare connection", success: false }, { status: 500 });
}
}