annehmen ohne selektion eingeichtet
This commit is contained in:
Binary file not shown.
@@ -1,4 +1,4 @@
|
|||||||
import { PrismaClient } from '@prisma/client';
|
import { PrismaClient, Status } from '@prisma/client';
|
||||||
import { json } from '@sveltejs/kit';
|
import { json } from '@sveltejs/kit';
|
||||||
import type { RequestHandler } from './$types';
|
import type { RequestHandler } from './$types';
|
||||||
import fs from 'fs/promises';
|
import fs from 'fs/promises';
|
||||||
@@ -28,6 +28,53 @@ export const GET: RequestHandler = async ({ cookies }) => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const POST: RequestHandler = async ({ cookies, url }) => {
|
||||||
|
const id = Number(url.searchParams.get('id'));
|
||||||
|
if (!id) return json({ error: 'Ungültige ID' }, { status: 400 });
|
||||||
|
|
||||||
|
try {
|
||||||
|
const anmeldung = await prisma.anmeldung.findUnique({
|
||||||
|
where: { id },
|
||||||
|
include: {
|
||||||
|
wunsch1: true,
|
||||||
|
wunsch2: true,
|
||||||
|
wunsch3: true
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!anmeldung) return json({ error: 'Anmeldung nicht gefunden' }, { status: 404 });
|
||||||
|
|
||||||
|
const wuensche = [anmeldung.wunsch1, anmeldung.wunsch2, anmeldung.wunsch3];
|
||||||
|
|
||||||
|
for (const wunsch of wuensche) {
|
||||||
|
if (wunsch && wunsch.plaetze > 0) {
|
||||||
|
await prisma.$transaction([
|
||||||
|
prisma.anmeldung.update({
|
||||||
|
where: { id },
|
||||||
|
data: {
|
||||||
|
status: Status.ANGENOMMEN,
|
||||||
|
zugewiesenId: wunsch.id
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
prisma.dienststelle.update({
|
||||||
|
where: { id: wunsch.id },
|
||||||
|
data: {
|
||||||
|
plaetze: { decrement: 1 }
|
||||||
|
}
|
||||||
|
})
|
||||||
|
]);
|
||||||
|
|
||||||
|
return json({ success: true, message: `Zugewiesen an: ${wunsch.name}` });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return json({ error: 'Keine verfügbaren Plätze bei Wunsch-Dienststellen' }, { status: 409 });
|
||||||
|
} catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
return json({ error: 'Interner Serverfehler' }, { status: 500 });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export const DELETE: RequestHandler = async ({ cookies, url }) => {
|
export const DELETE: RequestHandler = async ({ cookies, url }) => {
|
||||||
if (!checkAuth(cookies)) return new Response('Nicht erlaubt', { status: 401 });
|
if (!checkAuth(cookies)) return new Response('Nicht erlaubt', { status: 401 });
|
||||||
const id = Number(url.searchParams.get('id'));
|
const id = Number(url.searchParams.get('id'));
|
||||||
|
|||||||
Reference in New Issue
Block a user