74 lines
2.4 KiB
TypeScript
74 lines
2.4 KiB
TypeScript
|
import { db } from "@/lib/db"
|
||
|
import { NextResponse } from "next/server";
|
||
|
import fetchUserWithImpersonation from '@/lib/fetch-user-impersonation';
|
||
|
|
||
|
|
||
|
export async function GET(req: Request) {
|
||
|
try {
|
||
|
const user = await fetchUserWithImpersonation(req);
|
||
|
if (!user) {
|
||
|
return NextResponse.json({ error: null, message: "Unauthorized", success: false }, { status: 401 });
|
||
|
}
|
||
|
|
||
|
//const { searchParams } = new URL(req.url)
|
||
|
const data = await db.connection.findMany({
|
||
|
where: {
|
||
|
userId: user.id
|
||
|
}
|
||
|
})
|
||
|
|
||
|
return NextResponse.json({ error: null, message: "", success: true, data }, { status: 200 });
|
||
|
} catch (error: any) {
|
||
|
return NextResponse.json({ error, message: "Failed to fetch connections", success: false }, { status: 500 });
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export async function DELETE(req: Request) {
|
||
|
try {
|
||
|
const user = await fetchUserWithImpersonation(req);
|
||
|
if (!user) {
|
||
|
return NextResponse.json({ error: null, message: "Unauthorized", success: false }, { status: 401 });
|
||
|
}
|
||
|
|
||
|
const { searchParams } = new URL(req.url)
|
||
|
const name = searchParams.get('name') as string
|
||
|
|
||
|
if (!name)
|
||
|
return NextResponse.json({ error: null, message: 'Requires "name" param to be passed in - name of the service.', success: false }, { status: 400 })
|
||
|
|
||
|
const data = await db.connection.delete({
|
||
|
where: {
|
||
|
userId_name: {
|
||
|
userId: user.id,
|
||
|
name: name
|
||
|
}
|
||
|
}
|
||
|
})
|
||
|
|
||
|
const connections = await db.connection.findMany({
|
||
|
where: {
|
||
|
userId: user.id,
|
||
|
type: data.type
|
||
|
}
|
||
|
})
|
||
|
|
||
|
if (connections.length > 0 && connections.every(c => !c.default)) {
|
||
|
const connectionName = connections[0].name
|
||
|
await db.connection.update({
|
||
|
where: {
|
||
|
userId_name: {
|
||
|
userId: user.id,
|
||
|
name: connectionName
|
||
|
}
|
||
|
},
|
||
|
data: {
|
||
|
default: true
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
|
return NextResponse.json({ error: null, message: "", success: true, data }, { status: 200 });
|
||
|
} catch (error: any) {
|
||
|
return NextResponse.json({ error, message: "Failed to delete connection", success: false }, { status: 500 });
|
||
|
}
|
||
|
}
|