57 lines
No EOL
1.9 KiB
TypeScript
57 lines
No EOL
1.9 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { createSbServer } from "@/lib/supabaseServer";
|
|
|
|
export async function POST(req: Request) {
|
|
try {
|
|
const sb = createSbServer();
|
|
const { data: { user } } = await sb.auth.getUser();
|
|
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
|
|
const { data: me } = await sb.from("staff_users").select("is_staff").eq("user_id", user.id).maybeSingle();
|
|
if (!me?.is_staff) return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
|
|
|
const { updates } = await req.json();
|
|
|
|
if (!updates || !Array.isArray(updates) || updates.length === 0) {
|
|
return NextResponse.json({ error: "Updates are required" }, { status: 400 });
|
|
}
|
|
|
|
// Valider les données
|
|
for (const update of updates) {
|
|
if (!update.contractId || typeof update.grossPay !== 'number' || update.grossPay < 0) {
|
|
return NextResponse.json({ error: "Invalid update data" }, { status: 400 });
|
|
}
|
|
}
|
|
|
|
const updatedContracts = [];
|
|
|
|
// Mettre à jour chaque contrat individuellement pour avoir un meilleur contrôle
|
|
for (const update of updates) {
|
|
const { data: updatedContract, error } = await sb
|
|
.from("cddu_contracts")
|
|
.update({ gross_pay: update.grossPay })
|
|
.eq("id", update.contractId)
|
|
.select("id, gross_pay")
|
|
.single();
|
|
|
|
if (error) {
|
|
console.error(`Error updating contract ${update.contractId}:`, error);
|
|
continue; // Continue avec les autres même si un échec
|
|
}
|
|
|
|
if (updatedContract) {
|
|
updatedContracts.push(updatedContract);
|
|
}
|
|
}
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
contracts: updatedContracts,
|
|
message: `${updatedContracts.length} salaire(s) mis à jour`
|
|
});
|
|
|
|
} catch (err: any) {
|
|
console.error("Bulk salary update error:", err);
|
|
return NextResponse.json({ error: "Internal server error" }, { status: 500 });
|
|
}
|
|
} |