53 lines
1.5 KiB
TypeScript
53 lines
1.5 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
|
|
import { assertAgencyAdmin, getRouteAgencyContextOrThrow } from "@/lib/agency";
|
|
import { assertRateLimit } from "@/lib/api/rate-limit";
|
|
import { assertSameOrigin } from "@/lib/api/security";
|
|
import { syncPleskInstance } from "@/lib/plesk/sync";
|
|
import { createSupabaseRouteClient } from "@/lib/supabase/route";
|
|
|
|
export async function POST(
|
|
req: Request,
|
|
{ params }: { params: { id: string } },
|
|
) {
|
|
try {
|
|
assertSameOrigin(req);
|
|
|
|
const context = await getRouteAgencyContextOrThrow();
|
|
assertAgencyAdmin(context);
|
|
assertRateLimit(`plesk-sync:${context.userId}`, 8, 60_000);
|
|
|
|
const supabase = createSupabaseRouteClient() as any;
|
|
|
|
const { data: instance, error: instanceError } = await supabase
|
|
.from("plesk_instances")
|
|
.select(
|
|
"id, agency_id, base_url, auth_type, encrypted_secret, last_connected_at",
|
|
)
|
|
.eq("id", params.id)
|
|
.eq("agency_id", context.agencyId)
|
|
.single();
|
|
|
|
if (instanceError || !instance) throw new Error("Plesk instance not found");
|
|
|
|
const result = await syncPleskInstance(
|
|
supabase,
|
|
instance,
|
|
context.userId,
|
|
"sync",
|
|
);
|
|
|
|
return NextResponse.json({
|
|
subscriptionsUpserted: result.subscriptionsUpserted,
|
|
domainsUpserted: result.domainsUpserted,
|
|
skipped: result.skipped,
|
|
reason: result.reason,
|
|
});
|
|
} catch (error) {
|
|
return NextResponse.json(
|
|
{ error: error instanceof Error ? error.message : "Sync failed" },
|
|
{ status: 400 },
|
|
);
|
|
}
|
|
}
|