forked from fahrPool/fahrPool
feat: add Kanban board server actions (result-based, no redirect)
This commit is contained in:
parent
d0017a45b1
commit
f62ef64863
|
|
@ -0,0 +1,86 @@
|
||||||
|
"use server";
|
||||||
|
|
||||||
|
// Diese drei Actions dupliziert bewusst die Kern-Update-Logik von
|
||||||
|
// `uebernehmen`/`stornieren` aus web/app/auftraege/[id]/actions.ts, statt sie
|
||||||
|
// zu refactoren: die bestehenden Actions enden mit redirect() (fuer den
|
||||||
|
// Formular-Submit-Flow auf der Detailseite), waehrend das Kanban-Board eine
|
||||||
|
// Client-Component ist, die nach einem Drop auf derselben Seite bleiben und
|
||||||
|
// bei Fehlschlag die Karte lokal zuruecksetzen muss -- das erfordert ein
|
||||||
|
// Ergebnis-Objekt statt eines Redirects. Ein Refactoring der bestehenden,
|
||||||
|
// bereits produktiven Actions ist bewusst nicht Teil dieses Plans.
|
||||||
|
|
||||||
|
import { createClient } from "@/lib/supabase/server";
|
||||||
|
import { loadProfile } from "@/lib/auth/profile";
|
||||||
|
|
||||||
|
export type MutationResult = { success: true } | { success: false; error: string };
|
||||||
|
|
||||||
|
export async function uebernehmenVomBoard(auftragId: string): Promise<MutationResult> {
|
||||||
|
const supabase = await createClient();
|
||||||
|
const {
|
||||||
|
data: { user },
|
||||||
|
} = await supabase.auth.getUser();
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
return { success: false, error: "Nicht angemeldet." };
|
||||||
|
}
|
||||||
|
|
||||||
|
const { data, error } = await supabase
|
||||||
|
.from("auftraege")
|
||||||
|
.update({ status: "zugewiesen", zugewiesen_an: user.id, start_zeit: new Date().toISOString() })
|
||||||
|
.eq("id", auftragId)
|
||||||
|
.eq("status", "offen")
|
||||||
|
.select("id");
|
||||||
|
|
||||||
|
if (error || !data || data.length === 0) {
|
||||||
|
return { success: false, error: "Auftrag wurde bereits übernommen oder ist nicht mehr offen." };
|
||||||
|
}
|
||||||
|
|
||||||
|
return { success: true };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function stornierenVomBoard(auftragId: string): Promise<MutationResult> {
|
||||||
|
const profileResult = await loadProfile();
|
||||||
|
if (
|
||||||
|
profileResult.status !== "ok" ||
|
||||||
|
(profileResult.profile.role !== "admin" && profileResult.profile.role !== "disponent")
|
||||||
|
) {
|
||||||
|
return { success: false, error: "Nur Disponent/Admin duerfen einen Auftrag stornieren." };
|
||||||
|
}
|
||||||
|
|
||||||
|
const supabase = await createClient();
|
||||||
|
const { data, error } = await supabase
|
||||||
|
.from("auftraege")
|
||||||
|
.update({ status: "storniert" })
|
||||||
|
.eq("id", auftragId)
|
||||||
|
.select("id");
|
||||||
|
|
||||||
|
if (error || !data || data.length === 0) {
|
||||||
|
return { success: false, error: "Stornieren fehlgeschlagen." };
|
||||||
|
}
|
||||||
|
|
||||||
|
return { success: true };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function zuweisenAnFahrer(auftragId: string, fahrerId: string): Promise<MutationResult> {
|
||||||
|
const profileResult = await loadProfile();
|
||||||
|
if (
|
||||||
|
profileResult.status !== "ok" ||
|
||||||
|
(profileResult.profile.role !== "admin" && profileResult.profile.role !== "disponent")
|
||||||
|
) {
|
||||||
|
return { success: false, error: "Nur Disponent/Admin duerfen Auftraege zuweisen." };
|
||||||
|
}
|
||||||
|
|
||||||
|
const supabase = await createClient();
|
||||||
|
const { data, error } = await supabase
|
||||||
|
.from("auftraege")
|
||||||
|
.update({ status: "zugewiesen", zugewiesen_an: fahrerId, start_zeit: new Date().toISOString() })
|
||||||
|
.eq("id", auftragId)
|
||||||
|
.eq("status", "offen")
|
||||||
|
.select("id");
|
||||||
|
|
||||||
|
if (error || !data || data.length === 0) {
|
||||||
|
return { success: false, error: "Zuweisen fehlgeschlagen (Auftrag ist evtl. nicht mehr offen)." };
|
||||||
|
}
|
||||||
|
|
||||||
|
return { success: true };
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue