aboutsummaryrefslogtreecommitdiff
path: root/apps/web/app/api/store/mobile/route.ts
blob: a470a05e5113d9dd26475985f10003f07cfca6dc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { NextRequest } from "next/server";
import { z } from "zod";
import { ensureAuth } from "../../ensureAuth";

export const runtime = "edge";

const vectorBody = z.object({
	spaces: z.array(z.number()).optional(),
	url: z.string(),
});

export async function POST(req: NextRequest) {
	const session = await ensureAuth(req);

	if (!session) {
		return new Response("Unauthorized", { status: 401 });
	}

	try {
		const body = await req.json();
		const validatedBody = vectorBody.parse(body);
		const vectorSaveResponses = await fetch(
			`${process.env.BACKEND_BASE_URL}/api/add`,
			{
				method: "POST",
				body: JSON.stringify({
					url: validatedBody.url,
					spaces: validatedBody.spaces,
					user: session.user.id,
				}),
				headers: {
					"Content-Type": "application/json",
					Authorization: "Bearer " + process.env.BACKEND_SECURITY_KEY,
				},
			},
		);
		const response = (await vectorSaveResponses.json()) as {
			status: string;
			message?: string;
		};

		if (response.status !== "ok") {
			return new Response("Internal server error", { status: 500 });
		}

		if (response.status === "ok") {
			return new Response("Added to queue", { status: 200 });
		}
	} catch (e) {
		return new Response("Bad Request", { status: 400 });
	}
}