getting lost files back and making some changes p1

This commit is contained in:
2026-01-14 20:15:07 +01:00
parent 26d140f910
commit bcc5164abb
30609 changed files with 18693 additions and 77811 deletions
+21
View File
@@ -0,0 +1,21 @@
// make api route to get all sellables
import { NextResponse } from "next/server";
import { auth } from "~/server/auth";
import { db } from "~/server/db";
export async function GET() {
const session = await auth();
if (!session) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
const sellables = await db.sellable.findMany({
include: {
shop: { select: { label: true } },
item: { select: { stock: true } },
},
});
return NextResponse.json(sellables);
}
+175
View File
@@ -0,0 +1,175 @@
// make api route to get user data if user is logged in (nextauth)
import { NextResponse } from "next/server";
import { auth } from "~/server/auth";
import type { Session } from "next-auth";
import { db } from "~/server/db";
export async function PATCH(request: Request) {
const session = await auth();
if (!session?.user?.id) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
const { adress } = (await request.json()) as { adress: string };
if (!adress || typeof adress !== "string") {
return NextResponse.json({ error: "Invalid adress" }, { status: 400 });
}
const result = await db.adress.create({
data: {
adress,
userId: session.user.id,
},
});
return NextResponse.json(result, { status: 201 });
}
export async function GET() {
const session: Session | null = await auth();
if (!session?.user?.id) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
const user = await db.user.findUnique({
where: { id: session.user.id },
select: {
id: true,
name: true,
adresses: {
select: { adress: true },
},
shops: {
select: { id: true, label: true, sellables: true },
},
},
});
if (!user) {
return NextResponse.json({ error: "User not found" }, { status: 404 });
}
return NextResponse.json({
...user,
adresses: user.adresses.map((a) => a.adress),
});
}
type ShopInput = {
id: number;
label: string;
};
export async function POST(request: Request) {
const session: Session | null = await auth();
if (!session?.user?.id) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
const body = (await request.json()) as {
adresses: string[];
shops: ShopInput[];
};
if (!Array.isArray(body.adresses) || !Array.isArray(body.shops)) {
return NextResponse.json(
{ error: "Invalid input format" },
{ status: 400 },
);
}
/* ─────────────── ADDRESSES ─────────────── */
const currentAdresses = await db.adress.findMany({
where: { userId: session.user.id },
select: { id: true, adress: true },
});
const currentAdressSet = new Set(currentAdresses.map((a) => a.adress));
const incomingAdressSet = new Set(body.adresses);
// Add only new addresses
const adressesToAdd = body.adresses.filter((a) => !currentAdressSet.has(a));
// Remove only missing addresses
const adressesToDelete = currentAdresses
.filter((a) => !incomingAdressSet.has(a.adress))
.map((a) => a.id);
if (adressesToAdd.length > 0) {
await db.adress.createMany({
data: adressesToAdd.map((adress) => ({
adress,
userId: session.user.id,
})),
});
}
if (adressesToDelete.length > 0) {
await db.adress.deleteMany({
where: { id: { in: adressesToDelete } },
});
}
/* ─────────────── SHOPS ─────────────── */
const currentShops = await db.shop.findMany({
where: { userId: session.user.id },
select: { id: true, label: true },
});
const incomingShopMap = new Map(body.shops.map((s) => [s.id, s.label]));
const currentShopIds = new Set(currentShops.map((s) => s.id));
const incomingShopIds = new Set(body.shops.map((s) => s.id));
if (!Array.from(incomingShopIds).every((id) => Number.isInteger(id))) {
throw new Error("Invalid shop id");
}
// Create new shops
const shopsToCreate = body.shops.filter((s) => !currentShopIds.has(s.id));
if (shopsToCreate.length > 0) {
await db.shop.createMany({
data: shopsToCreate.map((s) => ({
id: s.id,
label: s.label,
userId: session.user.id,
})),
});
}
// Update labels ONLY if changed
const shopsToUpdate = currentShops.filter(
(s) => incomingShopMap.has(s.id) && incomingShopMap.get(s.id) !== s.label,
);
await Promise.all(
shopsToUpdate.map((s) =>
db.shop.update({
where: { id: s.id },
data: { label: incomingShopMap.get(s.id)! },
}),
),
);
// Delete removed shops (Sellables cascade)
const shopsToDelete = currentShops
.filter((s) => !incomingShopIds.has(s.id))
.map((s) => s.id);
if (shopsToDelete.length > 0) {
await db.shop.deleteMany({
where: { id: { in: shopsToDelete } },
});
}
/* ─────────────── RESPONSE ─────────────── */
return NextResponse.json({ success: true });
}