Compare commits
10
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
84a1396265 | ||
|
|
fec7c682f0 | ||
|
|
5adc9f0923 | ||
|
|
d87808af23 | ||
|
|
57005859c3 | ||
|
|
b221ad45fa | ||
|
|
9ec1259403 | ||
|
|
84165f47ff | ||
|
|
067e0d9080 | ||
|
|
e90b34e6ca |
@@ -0,0 +1,147 @@
|
|||||||
|
import { NextResponse } from "next/server";
|
||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
|
import { assertAgencyAdmin, getRouteAgencyContextOrThrow } from "@/lib/agency";
|
||||||
|
import { assertRateLimit } from "@/lib/api/rate-limit";
|
||||||
|
import { assertSameOrigin } from "@/lib/api/security";
|
||||||
|
import {
|
||||||
|
suspendPleskSubscription,
|
||||||
|
unsuspendPleskSubscription,
|
||||||
|
} from "@/lib/plesk/client";
|
||||||
|
import { createSupabaseRouteClient } from "@/lib/supabase/route";
|
||||||
|
|
||||||
|
const actionSchema = z.object({
|
||||||
|
action: z.enum(["suspend", "unsuspend"]),
|
||||||
|
});
|
||||||
|
|
||||||
|
export async function POST(
|
||||||
|
req: Request,
|
||||||
|
{ params }: { params: { id: string } },
|
||||||
|
) {
|
||||||
|
const supabase = createSupabaseRouteClient() as any;
|
||||||
|
let context: Awaited<ReturnType<typeof getRouteAgencyContextOrThrow>> | null =
|
||||||
|
null;
|
||||||
|
let domain: { id: string; subscription_id: string | null } | null = null;
|
||||||
|
let subscription: { id: string; plesk_subscription_id: string } | null = null;
|
||||||
|
let requestedAction: "suspend" | "unsuspend" | null = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
assertSameOrigin(req);
|
||||||
|
|
||||||
|
context = await getRouteAgencyContextOrThrow();
|
||||||
|
assertAgencyAdmin(context);
|
||||||
|
assertRateLimit(`plesk-domain-action:${context.userId}`, 10, 60_000);
|
||||||
|
|
||||||
|
const { action } = actionSchema.parse(await req.json());
|
||||||
|
requestedAction = action;
|
||||||
|
|
||||||
|
const { data: domainRow, error: domainError } = await supabase
|
||||||
|
.from("plesk_domains")
|
||||||
|
.select("id, agency_id, plesk_instance_id, subscription_id")
|
||||||
|
.eq("id", params.id)
|
||||||
|
.eq("agency_id", context.agencyId)
|
||||||
|
.single();
|
||||||
|
|
||||||
|
if (domainError || !domainRow) {
|
||||||
|
throw new Error("Domain not found");
|
||||||
|
}
|
||||||
|
|
||||||
|
domain = {
|
||||||
|
id: String(domainRow.id),
|
||||||
|
subscription_id: domainRow.subscription_id
|
||||||
|
? String(domainRow.subscription_id)
|
||||||
|
: null,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!domain.subscription_id) {
|
||||||
|
throw new Error("Domain is not linked to a subscription");
|
||||||
|
}
|
||||||
|
|
||||||
|
const { data: subscriptionRow, error: subscriptionError } = await supabase
|
||||||
|
.from("plesk_subscriptions")
|
||||||
|
.select("id, plesk_subscription_id, plesk_instance_id")
|
||||||
|
.eq("id", domain.subscription_id)
|
||||||
|
.eq("agency_id", context.agencyId)
|
||||||
|
.single();
|
||||||
|
|
||||||
|
if (subscriptionError || !subscriptionRow) {
|
||||||
|
throw new Error("Subscription not found");
|
||||||
|
}
|
||||||
|
|
||||||
|
subscription = {
|
||||||
|
id: String(subscriptionRow.id),
|
||||||
|
plesk_subscription_id: String(subscriptionRow.plesk_subscription_id),
|
||||||
|
};
|
||||||
|
|
||||||
|
const { data: instance, error: instanceError } = await supabase
|
||||||
|
.from("plesk_instances")
|
||||||
|
.select("id, base_url, auth_type, encrypted_secret")
|
||||||
|
.eq("id", subscriptionRow.plesk_instance_id)
|
||||||
|
.eq("agency_id", context.agencyId)
|
||||||
|
.single();
|
||||||
|
|
||||||
|
if (instanceError || !instance) {
|
||||||
|
throw new Error("Plesk instance not found");
|
||||||
|
}
|
||||||
|
|
||||||
|
const connection = {
|
||||||
|
baseUrl: instance.base_url,
|
||||||
|
authType: instance.auth_type,
|
||||||
|
encryptedSecret: instance.encrypted_secret,
|
||||||
|
} as const;
|
||||||
|
|
||||||
|
if (action === "suspend") {
|
||||||
|
await suspendPleskSubscription(
|
||||||
|
connection,
|
||||||
|
subscription.plesk_subscription_id,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
await unsuspendPleskSubscription(
|
||||||
|
connection,
|
||||||
|
subscription.plesk_subscription_id,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
await supabase.from("actions_log").insert({
|
||||||
|
agency_id: context.agencyId,
|
||||||
|
actor_user_id: context.userId,
|
||||||
|
target_type: "plesk_domain",
|
||||||
|
target_id: domain.id,
|
||||||
|
action: `domain_${action}`,
|
||||||
|
status: "success",
|
||||||
|
metadata: {
|
||||||
|
domain_id: domain.id,
|
||||||
|
subscription_id: subscription.id,
|
||||||
|
plesk_subscription_id: subscription.plesk_subscription_id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return NextResponse.json({ ok: true });
|
||||||
|
} catch (error) {
|
||||||
|
if (context && domain) {
|
||||||
|
const message =
|
||||||
|
error instanceof Error ? error.message : "Domain action failed";
|
||||||
|
await supabase.from("actions_log").insert({
|
||||||
|
agency_id: context.agencyId,
|
||||||
|
actor_user_id: context.userId,
|
||||||
|
target_type: "plesk_domain",
|
||||||
|
target_id: domain.id,
|
||||||
|
action: requestedAction ? `domain_${requestedAction}` : "domain_action",
|
||||||
|
status: "failed",
|
||||||
|
error_message: message,
|
||||||
|
metadata: {
|
||||||
|
domain_id: domain.id,
|
||||||
|
subscription_id: subscription?.id ?? domain.subscription_id,
|
||||||
|
plesk_subscription_id: subscription?.plesk_subscription_id ?? null,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
error: error instanceof Error ? error.message : "Domain action failed",
|
||||||
|
},
|
||||||
|
{ status: 400 },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,7 +3,6 @@ import { DashboardControls } from "@/components/dashboard/dashboard-controls";
|
|||||||
import { getServerAgencyContextOrRedirect } from "@/lib/agency";
|
import { getServerAgencyContextOrRedirect } from "@/lib/agency";
|
||||||
import type { Database } from "@/lib/types";
|
import type { Database } from "@/lib/types";
|
||||||
import { createSupabaseServerClient } from "@/lib/supabase/server";
|
import { createSupabaseServerClient } from "@/lib/supabase/server";
|
||||||
import type { PostgrestError } from "@supabase/supabase-js";
|
|
||||||
|
|
||||||
type AgencySummary = Pick<
|
type AgencySummary = Pick<
|
||||||
Database["public"]["Tables"]["agencies"]["Row"],
|
Database["public"]["Tables"]["agencies"]["Row"],
|
||||||
@@ -51,7 +50,7 @@ export default async function DashboardPage() {
|
|||||||
supabase
|
supabase
|
||||||
.from("plesk_domains")
|
.from("plesk_domains")
|
||||||
.select(
|
.select(
|
||||||
"id, plesk_instance_id, domain_name, status, hosting_type, source_created_at, aliases_count, updated_at",
|
"id, plesk_instance_id, subscription_id, domain_name, status, hosting_type, source_created_at, aliases_count, updated_at",
|
||||||
)
|
)
|
||||||
.eq("agency_id", context.agencyId)
|
.eq("agency_id", context.agencyId)
|
||||||
.order("updated_at", { ascending: false })
|
.order("updated_at", { ascending: false })
|
||||||
@@ -101,6 +100,7 @@ export default async function DashboardPage() {
|
|||||||
data: Array<{
|
data: Array<{
|
||||||
id: string;
|
id: string;
|
||||||
plesk_instance_id: string;
|
plesk_instance_id: string;
|
||||||
|
subscription_id: string | null;
|
||||||
domain_name: string;
|
domain_name: string;
|
||||||
status: string | null;
|
status: string | null;
|
||||||
hosting_type: string | null;
|
hosting_type: string | null;
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ type Subscription = {
|
|||||||
type Domain = {
|
type Domain = {
|
||||||
id: string;
|
id: string;
|
||||||
plesk_instance_id: string;
|
plesk_instance_id: string;
|
||||||
|
subscription_id: string | null;
|
||||||
domain_name: string;
|
domain_name: string;
|
||||||
status: string | null;
|
status: string | null;
|
||||||
hosting_type: string | null;
|
hosting_type: string | null;
|
||||||
@@ -56,6 +57,10 @@ function getDomainStatusBadge(status: string | null) {
|
|||||||
return "bg-rose-100 text-rose-700";
|
return "bg-rose-100 text-rose-700";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (normalized.includes("expired")) {
|
||||||
|
return "bg-amber-100 text-amber-700";
|
||||||
|
}
|
||||||
|
|
||||||
if (normalized.includes("active") || normalized.includes("enabled")) {
|
if (normalized.includes("active") || normalized.includes("enabled")) {
|
||||||
return "bg-emerald-100 text-emerald-700";
|
return "bg-emerald-100 text-emerald-700";
|
||||||
}
|
}
|
||||||
@@ -67,6 +72,26 @@ function getDomainStatusBadge(status: string | null) {
|
|||||||
return "bg-slate-100 text-slate-600";
|
return "bg-slate-100 text-slate-600";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isDomainSuspended(status: string | null) {
|
||||||
|
const normalized = (status ?? "unknown").toLowerCase();
|
||||||
|
return normalized.includes("suspend") || normalized.includes("disabled");
|
||||||
|
}
|
||||||
|
|
||||||
|
function isDomainActive(status: string | null) {
|
||||||
|
const normalized = (status ?? "unknown").toLowerCase();
|
||||||
|
return (
|
||||||
|
normalized.includes("active") ||
|
||||||
|
normalized.includes("ok") ||
|
||||||
|
normalized.includes("enabled")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getDomainHref(domainName: string) {
|
||||||
|
const normalized = domainName.trim();
|
||||||
|
if (/^https?:\/\//i.test(normalized)) return normalized;
|
||||||
|
return `https://${normalized}`;
|
||||||
|
}
|
||||||
|
|
||||||
export function DashboardControls({
|
export function DashboardControls({
|
||||||
instances,
|
instances,
|
||||||
subscriptions,
|
subscriptions,
|
||||||
@@ -213,6 +238,25 @@ export function DashboardControls({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function onDomainAction(
|
||||||
|
domainId: string,
|
||||||
|
action: "suspend" | "unsuspend",
|
||||||
|
) {
|
||||||
|
setLoading(true);
|
||||||
|
setMessage(null);
|
||||||
|
try {
|
||||||
|
await runRequest(`/api/plesk/domains/${domainId}/action`, { action });
|
||||||
|
setMessage("Domain action completed.");
|
||||||
|
window.location.reload();
|
||||||
|
} catch (error) {
|
||||||
|
setMessage(
|
||||||
|
error instanceof Error ? error.message : "Domain action failed",
|
||||||
|
);
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function onCheckout() {
|
async function onCheckout() {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
try {
|
try {
|
||||||
@@ -499,7 +543,7 @@ export function DashboardControls({
|
|||||||
{message ? <p className="text-sm text-slate-600">{message}</p> : null}
|
{message ? <p className="text-sm text-slate-600">{message}</p> : null}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="space-y-3 rounded-xl border border-slate-200 bg-white p-4">
|
<div className="space-y-3 rounded-xl border border-slate-200 bg-white p-4 lg:col-span-2">
|
||||||
<div className="flex flex-wrap items-end gap-2">
|
<div className="flex flex-wrap items-end gap-2">
|
||||||
<div className="flex-1 min-w-[220px]">
|
<div className="flex-1 min-w-[220px]">
|
||||||
<label className="text-sm font-medium">Search domains</label>
|
<label className="text-sm font-medium">Search domains</label>
|
||||||
@@ -536,25 +580,38 @@ export function DashboardControls({
|
|||||||
<thead>
|
<thead>
|
||||||
<tr className="border-b border-slate-200 text-xs uppercase text-slate-500">
|
<tr className="border-b border-slate-200 text-xs uppercase text-slate-500">
|
||||||
<th className="px-2 py-2">Domain</th>
|
<th className="px-2 py-2">Domain</th>
|
||||||
<th className="px-2 py-2">Status</th>
|
<th className="px-2 py-2">Subscription Status</th>
|
||||||
<th className="px-2 py-2">Hosting</th>
|
<th className="px-2 py-2">Hosting</th>
|
||||||
<th className="px-2 py-2">Created</th>
|
<th className="px-2 py-2">Created</th>
|
||||||
<th className="px-2 py-2">Aliases</th>
|
<th className="px-2 py-2">Aliases</th>
|
||||||
<th className="px-2 py-2">Last seen</th>
|
<th className="px-2 py-2">Last seen</th>
|
||||||
|
<th className="px-2 py-2">Actions</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
{filteredDomains.length === 0 ? (
|
{filteredDomains.length === 0 ? (
|
||||||
<tr>
|
<tr>
|
||||||
<td className="px-2 py-3 text-slate-500" colSpan={6}>
|
<td className="px-2 py-3 text-slate-500" colSpan={7}>
|
||||||
No domains found.
|
No domains found.
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
) : (
|
) : (
|
||||||
filteredDomains.map((domain) => (
|
filteredDomains.map((domain) => (
|
||||||
<tr key={domain.id} className="border-b border-slate-100">
|
<tr key={domain.id} className="border-b border-slate-100">
|
||||||
<td className="font-medium text-slate-900">
|
<td className="px-2 py-2 font-medium text-slate-900">
|
||||||
{domain.domain_name}
|
<a
|
||||||
|
href={getDomainHref(domain.domain_name)}
|
||||||
|
target="_blank"
|
||||||
|
rel="noopener noreferrer"
|
||||||
|
className="underline decoration-slate-300 underline-offset-2 hover:text-brand-700"
|
||||||
|
>
|
||||||
|
{domain.domain_name}
|
||||||
|
</a>
|
||||||
|
{domain.subscription_id ? (
|
||||||
|
<p className="text-xs font-normal text-slate-500">
|
||||||
|
Subscription: {domain.subscription_id}
|
||||||
|
</p>
|
||||||
|
) : null}
|
||||||
</td>
|
</td>
|
||||||
<td className="px-2 py-2">
|
<td className="px-2 py-2">
|
||||||
<span
|
<span
|
||||||
@@ -573,6 +630,28 @@ export function DashboardControls({
|
|||||||
<td className="px-2 py-2">
|
<td className="px-2 py-2">
|
||||||
{formatDateTime(domain.updated_at)}
|
{formatDateTime(domain.updated_at)}
|
||||||
</td>
|
</td>
|
||||||
|
<td className="px-2 py-2">
|
||||||
|
<div className="flex gap-2">
|
||||||
|
<button
|
||||||
|
onClick={() => onDomainAction(domain.id, "suspend")}
|
||||||
|
disabled={
|
||||||
|
loading || isDomainSuspended(domain.status)
|
||||||
|
}
|
||||||
|
className="rounded border border-rose-300 px-2 py-1 text-xs text-rose-700 disabled:cursor-not-allowed disabled:opacity-60"
|
||||||
|
>
|
||||||
|
Suspend
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
onClick={() =>
|
||||||
|
onDomainAction(domain.id, "unsuspend")
|
||||||
|
}
|
||||||
|
disabled={loading || isDomainActive(domain.status)}
|
||||||
|
className="rounded border border-emerald-300 px-2 py-1 text-xs text-emerald-700 disabled:cursor-not-allowed disabled:opacity-60"
|
||||||
|
>
|
||||||
|
Unsuspend
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
))
|
))
|
||||||
)}
|
)}
|
||||||
|
|||||||
+54
-17
@@ -16,6 +16,8 @@ export type PleskSubscription = {
|
|||||||
external_id?: string;
|
external_id?: string;
|
||||||
name?: string;
|
name?: string;
|
||||||
status?: string;
|
status?: string;
|
||||||
|
status_raw?: string | null;
|
||||||
|
status_sync_error?: string | null;
|
||||||
owner_login?: string;
|
owner_login?: string;
|
||||||
owner?: { login?: string };
|
owner?: { login?: string };
|
||||||
plan_name?: string;
|
plan_name?: string;
|
||||||
@@ -148,16 +150,27 @@ export async function listPleskSubscriptions(connection: PleskConnection) {
|
|||||||
const subscriptions: PleskSubscription[] = [];
|
const subscriptions: PleskSubscription[] = [];
|
||||||
|
|
||||||
for (const subscriptionName of subscriptionNames.slice(0, 500)) {
|
for (const subscriptionName of subscriptionNames.slice(0, 500)) {
|
||||||
let status: "active" | "suspended" | "unknown" = "unknown";
|
let status: "active" | "suspended" | "disabled" | "expired" | "unknown" =
|
||||||
|
"unknown";
|
||||||
|
let statusRaw: string | null = null;
|
||||||
|
let statusSyncError: string | null = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const infoResult = await pleskCliCall(connection, "subscription", [
|
const infoResult = await pleskCliCall(connection, "subscription", [
|
||||||
"--info",
|
"--info",
|
||||||
subscriptionName,
|
subscriptionName,
|
||||||
]);
|
]);
|
||||||
status = parseSubscriptionStatusFromInfo(infoResult.stdout ?? "");
|
const parsed = parseSubscriptionStatusDetailsFromInfo(
|
||||||
} catch {
|
infoResult.stdout ?? "",
|
||||||
|
);
|
||||||
|
status = parsed.status;
|
||||||
|
statusRaw = parsed.statusRaw;
|
||||||
|
} catch (error) {
|
||||||
status = "unknown";
|
status = "unknown";
|
||||||
|
statusSyncError =
|
||||||
|
error instanceof Error
|
||||||
|
? error.message.slice(0, 1000)
|
||||||
|
: "status_sync_failed";
|
||||||
}
|
}
|
||||||
|
|
||||||
subscriptions.push({
|
subscriptions.push({
|
||||||
@@ -165,33 +178,57 @@ export async function listPleskSubscriptions(connection: PleskConnection) {
|
|||||||
subscription_id: subscriptionName,
|
subscription_id: subscriptionName,
|
||||||
name: subscriptionName,
|
name: subscriptionName,
|
||||||
status,
|
status,
|
||||||
|
status_raw: statusRaw,
|
||||||
|
status_sync_error: statusSyncError,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return subscriptions;
|
return subscriptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function parseSubscriptionStatusFromInfo(
|
export function parseSubscriptionStatusDetailsFromInfo(stdout: string): {
|
||||||
stdout: string,
|
status: "active" | "suspended" | "disabled" | "expired" | "unknown";
|
||||||
): "active" | "suspended" | "unknown" {
|
statusRaw: string | null;
|
||||||
|
} {
|
||||||
const line = stdout
|
const line = stdout
|
||||||
.split(/\r?\n/)
|
.split(/\r?\n/)
|
||||||
.map((entry) => entry.trim())
|
.map((entry) => entry.trim())
|
||||||
.find((entry) => /^domain\s+status\s*:/i.test(entry));
|
.find((entry) => /^domain\s+status\s*:/i.test(entry));
|
||||||
|
|
||||||
if (!line) return "unknown";
|
if (!line) {
|
||||||
|
return { status: "unknown", statusRaw: null };
|
||||||
const value = line
|
|
||||||
.replace(/^domain\s+status\s*:/i, "")
|
|
||||||
.trim()
|
|
||||||
.toLowerCase();
|
|
||||||
|
|
||||||
if (value.includes("ok")) return "active";
|
|
||||||
if (value.includes("suspend") || value.includes("disabled")) {
|
|
||||||
return "suspended";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return "unknown";
|
const value = line.replace(/^domain\s+status\s*:/i, "").trim();
|
||||||
|
const normalized = value.toLowerCase();
|
||||||
|
|
||||||
|
if (normalized.includes("expired")) {
|
||||||
|
return { status: "expired", statusRaw: value };
|
||||||
|
}
|
||||||
|
|
||||||
|
if (normalized.includes("disabled")) {
|
||||||
|
return { status: "disabled", statusRaw: value };
|
||||||
|
}
|
||||||
|
|
||||||
|
if (normalized.includes("suspend")) {
|
||||||
|
return { status: "suspended", statusRaw: value };
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
normalized.includes("ok") ||
|
||||||
|
normalized.includes("active") ||
|
||||||
|
normalized.includes("enabled")
|
||||||
|
) {
|
||||||
|
return { status: "active", statusRaw: value };
|
||||||
|
}
|
||||||
|
|
||||||
|
return { status: "unknown", statusRaw: value };
|
||||||
|
}
|
||||||
|
|
||||||
|
export function parseSubscriptionStatusFromInfo(
|
||||||
|
stdout: string,
|
||||||
|
): "active" | "suspended" | "disabled" | "expired" | "unknown" {
|
||||||
|
return parseSubscriptionStatusDetailsFromInfo(stdout).status;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function pleskCliCall(
|
export async function pleskCliCall(
|
||||||
|
|||||||
@@ -32,6 +32,31 @@ export async function syncPleskInstance(
|
|||||||
): Promise<SyncResult> {
|
): Promise<SyncResult> {
|
||||||
const startedAtIso = new Date().toISOString();
|
const startedAtIso = new Date().toISOString();
|
||||||
|
|
||||||
|
const logStatusSyncEvent = async (
|
||||||
|
eventAction:
|
||||||
|
| "subscription_status_sync_started"
|
||||||
|
| "subscription_status_sync_completed"
|
||||||
|
| "subscription_status_sync_failed",
|
||||||
|
metadata?: Record<string, unknown>,
|
||||||
|
status: "success" | "failed" = "success",
|
||||||
|
errorMessage?: string | null,
|
||||||
|
) => {
|
||||||
|
try {
|
||||||
|
await supabase.from("actions_log").insert({
|
||||||
|
agency_id: instance.agency_id,
|
||||||
|
actor_user_id: actorUserId ?? null,
|
||||||
|
target_type: "plesk_instance",
|
||||||
|
target_id: instance.id,
|
||||||
|
action: eventAction,
|
||||||
|
status,
|
||||||
|
error_message: errorMessage ?? null,
|
||||||
|
metadata: metadata ?? {},
|
||||||
|
});
|
||||||
|
} catch {
|
||||||
|
// Logging should never block sync flow.
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
await supabase
|
await supabase
|
||||||
.from("plesk_instances")
|
.from("plesk_instances")
|
||||||
.update({
|
.update({
|
||||||
@@ -67,6 +92,10 @@ export async function syncPleskInstance(
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
await logStatusSyncEvent("subscription_status_sync_started", {
|
||||||
|
started_at: startedAtIso,
|
||||||
|
});
|
||||||
|
|
||||||
const [subscriptionsRaw, domainsRaw] = await Promise.all([
|
const [subscriptionsRaw, domainsRaw] = await Promise.all([
|
||||||
listPleskSubscriptions({
|
listPleskSubscriptions({
|
||||||
baseUrl: instance.base_url,
|
baseUrl: instance.base_url,
|
||||||
@@ -95,6 +124,8 @@ export async function syncPleskInstance(
|
|||||||
plesk_subscription_id: String(sourceId),
|
plesk_subscription_id: String(sourceId),
|
||||||
name: item?.name ? String(item.name) : String(sourceId),
|
name: item?.name ? String(item.name) : String(sourceId),
|
||||||
status: item?.status ? String(item.status) : "unknown",
|
status: item?.status ? String(item.status) : "unknown",
|
||||||
|
status_raw: item?.status_raw ? String(item.status_raw) : null,
|
||||||
|
last_status_sync_at: startedAtIso,
|
||||||
owner_login: item?.owner_login
|
owner_login: item?.owner_login
|
||||||
? String(item.owner_login)
|
? String(item.owner_login)
|
||||||
: item?.owner?.login
|
: item?.owner?.login
|
||||||
@@ -109,6 +140,10 @@ export async function syncPleskInstance(
|
|||||||
})
|
})
|
||||||
.filter(Boolean) as Array<Record<string, unknown>>;
|
.filter(Boolean) as Array<Record<string, unknown>>;
|
||||||
|
|
||||||
|
const subscriptionStatusSyncFailures = subscriptionsRaw.filter(
|
||||||
|
(item: any) => Boolean(item?.status_sync_error),
|
||||||
|
);
|
||||||
|
|
||||||
if (subscriptionsForUpsert.length > 0) {
|
if (subscriptionsForUpsert.length > 0) {
|
||||||
const { error } = await supabase
|
const { error } = await supabase
|
||||||
.from("plesk_subscriptions")
|
.from("plesk_subscriptions")
|
||||||
@@ -221,6 +256,40 @@ export async function syncPleskInstance(
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
await logStatusSyncEvent("subscription_status_sync_completed", {
|
||||||
|
synced_subscriptions: subscriptionsRaw.length,
|
||||||
|
failed_subscriptions: subscriptionStatusSyncFailures.length,
|
||||||
|
failed_subscription_ids: subscriptionStatusSyncFailures
|
||||||
|
.slice(0, 50)
|
||||||
|
.map((item: any) =>
|
||||||
|
String(
|
||||||
|
item?.plesk_subscription_id ?? item?.subscription_id ?? "unknown",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
completed_at: new Date().toISOString(),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (subscriptionStatusSyncFailures.length > 0) {
|
||||||
|
await logStatusSyncEvent(
|
||||||
|
"subscription_status_sync_failed",
|
||||||
|
{
|
||||||
|
failed_subscriptions: subscriptionStatusSyncFailures.length,
|
||||||
|
sample_errors: subscriptionStatusSyncFailures
|
||||||
|
.slice(0, 20)
|
||||||
|
.map((item: any) => ({
|
||||||
|
subscription: String(
|
||||||
|
item?.plesk_subscription_id ??
|
||||||
|
item?.subscription_id ??
|
||||||
|
"unknown",
|
||||||
|
),
|
||||||
|
error: String(item?.status_sync_error ?? "status_sync_failed"),
|
||||||
|
})),
|
||||||
|
},
|
||||||
|
"failed",
|
||||||
|
`${subscriptionStatusSyncFailures.length} subscription status sync failures`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
instanceId: instance.id,
|
instanceId: instance.id,
|
||||||
agencyId: instance.agency_id,
|
agencyId: instance.agency_id,
|
||||||
@@ -232,6 +301,15 @@ export async function syncPleskInstance(
|
|||||||
const message =
|
const message =
|
||||||
error instanceof Error ? error.message.slice(0, 1000) : "Sync failed";
|
error instanceof Error ? error.message.slice(0, 1000) : "Sync failed";
|
||||||
|
|
||||||
|
await logStatusSyncEvent(
|
||||||
|
"subscription_status_sync_failed",
|
||||||
|
{
|
||||||
|
failed_at: new Date().toISOString(),
|
||||||
|
},
|
||||||
|
"failed",
|
||||||
|
message,
|
||||||
|
);
|
||||||
|
|
||||||
await supabase
|
await supabase
|
||||||
.from("plesk_instances")
|
.from("plesk_instances")
|
||||||
.update({
|
.update({
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
alter table public.plesk_subscriptions
|
||||||
|
add column if not exists status text default 'unknown',
|
||||||
|
add column if not exists status_raw text,
|
||||||
|
add column if not exists last_status_sync_at timestamptz;
|
||||||
|
|
||||||
|
update public.plesk_subscriptions
|
||||||
|
set status = 'unknown'
|
||||||
|
where status is null;
|
||||||
Reference in New Issue
Block a user