Files

73 lines
2.2 KiB
JavaScript

import { query, run } from "@/lib/db";
import { cookies } from "next/headers";
import { verifyToken } from "@/app/api/auth/[...nextauth]/route";
export async function POST(request, context) {
try {
// Verify user authentication
const cookieStore = await cookies();
const sessionToken = cookieStore.get("next-auth.session-token")?.value;
if (!sessionToken) {
return Response.json({ error: "Unauthorized" }, { status: 401 });
}
const userData = verifyToken(sessionToken);
if (!userData) {
return Response.json({ error: "Invalid session" }, { status: 401 });
}
// Get teamId from URL params
const params = await Promise.resolve(context.params);
const teamId = params?.teamId;
// Validate teamId
if (!teamId || isNaN(parseInt(teamId, 10))) {
return Response.json({ error: "Invalid team ID" }, { status: 400 });
}
// Try to update the last_updated timestamp for this team
try {
await run("UPDATE teams SET last_updated = ? WHERE id = ?", [
new Date().toISOString(),
teamId,
]);
} catch (updateError) {
// Log but don't fail if we can't update timestamp
console.warn(
"Failed to update last_updated timestamp:",
updateError.message
);
// Try to alter the table to add the column
try {
await run(
"ALTER TABLE teams ADD COLUMN last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP"
);
console.log("Added missing last_updated column to teams table");
// Try the update again
await run("UPDATE teams SET last_updated = ? WHERE id = ?", [
new Date().toISOString(),
teamId,
]);
} catch (alterError) {
console.error("Failed to alter teams table:", alterError.message);
}
}
// Return success response
return Response.json({
success: true,
message: "Successfully updated team members list",
timestamp: new Date().toISOString(),
});
} catch (error) {
console.error("Team members update error:", error);
return Response.json(
{ error: "Failed to update team members: " + error.message },
{ status: 500 }
);
}
}