Updated list of commands to v4.3. Added groups & permissions. Added connections. Updated redemptions and actions to v4.3.
This commit is contained in:
75
app/api/connection/default/route.ts
Normal file
75
app/api/connection/default/route.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
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 data = await db.connection.findMany({
|
||||
where: {
|
||||
userId: user.id,
|
||||
default: true
|
||||
}
|
||||
})
|
||||
|
||||
return NextResponse.json({ error: null, message: "", success: true, data }, { status: 200 });
|
||||
} catch (error: any) {
|
||||
return NextResponse.json({ error, message: "Failed to get default connection", success: false }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function PUT(req: Request) {
|
||||
try {
|
||||
const user = await fetchUserWithImpersonation(req);
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: null, message: "Unauthorized", success: false }, { status: 401 });
|
||||
}
|
||||
|
||||
const { name } = await req.json();
|
||||
if (!name)
|
||||
return NextResponse.json({ error: null, message: 'Requires "name" param to be passed in - name of the connection.', success: false }, { status: 400 })
|
||||
|
||||
const connection = await db.connection.findFirst({
|
||||
where: {
|
||||
userId: user.id,
|
||||
name
|
||||
}
|
||||
})
|
||||
|
||||
if (!connection) {
|
||||
return NextResponse.json({ error: null, message: 'Connection with that name does not exist.', success: false }, { status: 400 })
|
||||
}
|
||||
|
||||
await db.connection.updateMany({
|
||||
where: {
|
||||
userId: user.id,
|
||||
type: connection.type,
|
||||
default: true
|
||||
},
|
||||
data: {
|
||||
default: false
|
||||
}
|
||||
})
|
||||
|
||||
const data = await db.connection.update({
|
||||
where: {
|
||||
userId_name: {
|
||||
userId: user.id,
|
||||
name,
|
||||
}
|
||||
},
|
||||
data: {
|
||||
default: true as boolean
|
||||
}
|
||||
})
|
||||
|
||||
return NextResponse.json({ error: null, message: "", success: true, data }, { status: 200 });
|
||||
} catch (error: any) {
|
||||
return NextResponse.json({ error, message: "Failed to update default connection", success: false }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user