150 lines
5.8 KiB
TypeScript
150 lines
5.8 KiB
TypeScript
import { db } from "@/lib/db"
|
|
import { NextResponse } from "next/server";
|
|
import fetchUserWithImpersonation from "@/lib/fetch-user-impersonation";
|
|
import { ActionType, Prisma } from "@prisma/client";
|
|
import { JsonSerializer } from "typescript-json-serializer";
|
|
|
|
export async function GET(req: Request) {
|
|
try {
|
|
const user = await fetchUserWithImpersonation(req)
|
|
if (!user) {
|
|
return new NextResponse("Unauthorized", { status: 401 });
|
|
}
|
|
|
|
const actions = await db.action.findMany({
|
|
where: {
|
|
userId: user.id
|
|
}
|
|
})
|
|
|
|
return NextResponse.json(actions.map(({userId, ...attrs}) => attrs));
|
|
} catch (error) {
|
|
console.log("[REDEMPTIONS/ACTIONS]", error);
|
|
return new NextResponse("Internal Error", { status: 500 });
|
|
}
|
|
}
|
|
|
|
export async function POST(req: Request) {
|
|
try {
|
|
const user = await fetchUserWithImpersonation(req)
|
|
if (!user) {
|
|
return new NextResponse("Unauthorized", { status: 401 });
|
|
}
|
|
|
|
const { name, type, scene_name, scene_item_name, rotation, position_x, position_y, file_path, file_content }: { name: string, type: ActionType, scene_name: string, scene_item_name: string, rotation: string, position_x: string, position_y: string, file_path: string, file_content: string } = await req.json();
|
|
if (!name && !type)
|
|
return new NextResponse("Bad Request", { status: 400 });
|
|
if (type == ActionType.OBS_TRANSFORM && (!scene_name || !scene_item_name || !rotation && !position_x && !position_y))
|
|
return new NextResponse("Bad Request", { status: 400 });
|
|
if ((type == ActionType.WRITE_TO_FILE || type == ActionType.APPEND_TO_FILE) && (!file_path || !file_content))
|
|
return new NextResponse("Bad Request", { status: 400 });
|
|
if (type == ActionType.AUDIO_FILE && !file_path)
|
|
return new NextResponse("Bad Request", { status: 400 });
|
|
|
|
let data:any = { }
|
|
if (type == ActionType.WRITE_TO_FILE || type == ActionType.APPEND_TO_FILE) {
|
|
data = { file_path, file_content, ...data }
|
|
} else if (type == ActionType.OBS_TRANSFORM) {
|
|
data = { scene_name, scene_item_name, ...data }
|
|
if (!!rotation)
|
|
data = { rotation, ...data }
|
|
if (!!position_x)
|
|
data = { position_x, ...data }
|
|
if (!!position_y)
|
|
data = { position_y, ...data }
|
|
} else if (type == ActionType.AUDIO_FILE) {
|
|
data = { file_path, ...data }
|
|
}
|
|
|
|
await db.action.create({
|
|
data: {
|
|
userId: user.id,
|
|
name,
|
|
type,
|
|
data: data as Prisma.JsonObject
|
|
}
|
|
});
|
|
|
|
return new NextResponse("", { status: 200 });
|
|
} catch (error) {
|
|
console.log("[REDEMPTIONS/ACTIONS]", error);
|
|
return new NextResponse("Internal Error", { status: 500 });
|
|
}
|
|
}
|
|
|
|
export async function PUT(req: Request) {
|
|
try {
|
|
const user = await fetchUserWithImpersonation(req)
|
|
if (!user) {
|
|
return new NextResponse("Unauthorized", { status: 401 });
|
|
}
|
|
|
|
const { name, type, scene_name, scene_item_name, rotation, position_x, position_y, file_path, file_content }: { name: string, type: ActionType, scene_name: string, scene_item_name: string, rotation: string, position_x: string, position_y: string, file_path: string, file_content: string } = await req.json();
|
|
if (!name && !type)
|
|
return new NextResponse("Bad Request", { status: 400 });
|
|
if (type == ActionType.OBS_TRANSFORM && (!scene_name || !scene_item_name || !rotation && !position_x && !position_y))
|
|
return new NextResponse("Bad Request", { status: 400 });
|
|
if ((type == ActionType.WRITE_TO_FILE || type == ActionType.APPEND_TO_FILE) && (!file_path || !file_content))
|
|
return new NextResponse("Bad Request", { status: 400 });
|
|
if (type == ActionType.AUDIO_FILE && !file_path)
|
|
return new NextResponse("Bad Request", { status: 400 });
|
|
|
|
let data:any = { }
|
|
if (type == ActionType.WRITE_TO_FILE || type == ActionType.APPEND_TO_FILE) {
|
|
data = { file_path, file_content, ...data }
|
|
} else if (type == ActionType.OBS_TRANSFORM) {
|
|
data = { scene_name, scene_item_name, ...data }
|
|
if (!!rotation)
|
|
data = { rotation, ...data }
|
|
if (!!position_x)
|
|
data = { position_x, ...data }
|
|
if (!!position_y)
|
|
data = { position_y, ...data }
|
|
} else if (type == ActionType.AUDIO_FILE) {
|
|
data = { file_path, ...data }
|
|
}
|
|
|
|
await db.action.update({
|
|
where: {
|
|
userId_name: {
|
|
userId: user.id,
|
|
name
|
|
}
|
|
},
|
|
data: {
|
|
type,
|
|
data: data as Prisma.JsonObject
|
|
}
|
|
});
|
|
|
|
return new NextResponse("", { status: 200 });
|
|
} catch (error) {
|
|
console.log("[REDEMPTIONS/ACTIONS]", error);
|
|
return new NextResponse("Internal Error", { status: 500 });
|
|
}
|
|
}
|
|
|
|
export async function DELETE(req: Request) {
|
|
try {
|
|
const user = await fetchUserWithImpersonation(req)
|
|
if (!user) {
|
|
return new NextResponse("Unauthorized", { status: 401 });
|
|
}
|
|
|
|
const { searchParams } = new URL(req.url)
|
|
const name = searchParams.get('action_name') as string
|
|
const redemptions = await db.action.delete({
|
|
where: {
|
|
userId_name: {
|
|
userId: user.id,
|
|
name
|
|
}
|
|
}
|
|
})
|
|
|
|
return NextResponse.json(redemptions);
|
|
} catch (error) {
|
|
console.log("[REDEMPTIONS]", error);
|
|
return new NextResponse("Internal Error", { status: 500 });
|
|
}
|
|
} |