Compare commits
9 Commits
cl3.6a-dom
...
fix/subscr
| Author | SHA1 | Date | |
|---|---|---|---|
| fa43428153 | |||
| 37a70cafdd | |||
| 878ef7555d | |||
| 3599ccbb39 | |||
| 09d66ea601 | |||
| 698bd85c7d | |||
| 736cee870c | |||
| 0b7a95947f | |||
| 0dba5d2ebf |
@@ -49,6 +49,20 @@ type Props = {
|
||||
>;
|
||||
};
|
||||
|
||||
function getDomainStatusBadge(status: string | null) {
|
||||
const normalized = (status ?? "unknown").toLowerCase();
|
||||
|
||||
if (normalized.includes("suspend") || normalized.includes("disabled")) {
|
||||
return "bg-rose-100 text-rose-700";
|
||||
}
|
||||
|
||||
if (normalized.includes("active") || normalized.includes("enabled")) {
|
||||
return "bg-emerald-100 text-emerald-700";
|
||||
}
|
||||
|
||||
return "bg-slate-100 text-slate-600";
|
||||
}
|
||||
|
||||
export function DashboardControls({
|
||||
instances,
|
||||
subscriptions,
|
||||
@@ -351,14 +365,28 @@ export function DashboardControls({
|
||||
<span className="font-medium">{instance.status}</span>
|
||||
</p>
|
||||
<p className="text-xs text-slate-500">
|
||||
Last sync: {formatDateTime(instance.last_sync_at)}
|
||||
{instance.last_sync_status
|
||||
? ` (${instance.last_sync_status})`
|
||||
: ""}
|
||||
Subscriptions: {instance.last_sync_subscriptions ?? 0}
|
||||
</p>
|
||||
<p className="text-xs text-slate-500">
|
||||
Last counts: {instance.last_sync_subscriptions ?? 0}{" "}
|
||||
subscriptions, {instance.last_sync_domains ?? 0} domains
|
||||
Domains: {instance.last_sync_domains ?? 0}
|
||||
</p>
|
||||
<p className="text-xs text-slate-500">
|
||||
Last sync status:{" "}
|
||||
<span
|
||||
className={`inline-flex rounded px-2 py-0.5 text-[11px] font-medium ${
|
||||
instance.last_sync_status === "success"
|
||||
? "bg-emerald-100 text-emerald-700"
|
||||
: instance.last_sync_status === "failed" ||
|
||||
instance.last_sync_status === "error"
|
||||
? "bg-rose-100 text-rose-700"
|
||||
: "bg-slate-100 text-slate-600"
|
||||
}`}
|
||||
>
|
||||
{instance.last_sync_status ?? "unknown"}
|
||||
</span>
|
||||
</p>
|
||||
<p className="text-xs text-slate-500">
|
||||
Last sync time: {formatDateTime(instance.last_sync_at)}
|
||||
</p>
|
||||
<p className="text-xs text-slate-500">
|
||||
Last connected:{" "}
|
||||
@@ -521,11 +549,15 @@ export function DashboardControls({
|
||||
) : (
|
||||
filteredDomains.map((domain) => (
|
||||
<tr key={domain.id} className="border-b border-slate-100">
|
||||
<td className="px-2 py-2 font-medium text-slate-900">
|
||||
<td className="font-medium text-slate-900">
|
||||
{domain.domain_name}
|
||||
</td>
|
||||
<td className="px-2 py-2">
|
||||
{domain.status ?? "unknown"}
|
||||
<span
|
||||
className={`inline-flex rounded px-2 py-0.5 text-xs font-medium ${getDomainStatusBadge(domain.status)}`}
|
||||
>
|
||||
{domain.status ?? "unknown"}
|
||||
</span>
|
||||
</td>
|
||||
<td className="px-2 py-2">
|
||||
{domain.hosting_type ?? "unknown"}
|
||||
|
||||
@@ -9,6 +9,7 @@ export type PleskConnection = {
|
||||
};
|
||||
|
||||
export type PleskSubscription = {
|
||||
plesk_subscription_id?: string;
|
||||
id?: string | number;
|
||||
guid?: string;
|
||||
subscription_id?: string | number;
|
||||
@@ -137,51 +138,50 @@ export async function testPleskConnection(connection: PleskConnection) {
|
||||
}
|
||||
|
||||
export async function listPleskSubscriptions(connection: PleskConnection) {
|
||||
try {
|
||||
const all: PleskSubscription[] = [];
|
||||
const listOutput = await pleskCliCall(connection, "subscription", ["--list"]);
|
||||
|
||||
for (let page = 1; page <= 20; page += 1) {
|
||||
const payload = await pleskRequest<PleskSubscription[]>(
|
||||
connection,
|
||||
`/api/v2/subscriptions?page=${page}&per_page=100`,
|
||||
);
|
||||
const rows = Array.isArray(payload) ? payload : [];
|
||||
all.push(...rows);
|
||||
if (rows.length < 100) break;
|
||||
const subscriptionNames = listOutput
|
||||
.split(/\r?\n/)
|
||||
.map((line) => line.trim())
|
||||
.filter(Boolean);
|
||||
|
||||
const subscriptions: PleskSubscription[] = [];
|
||||
|
||||
for (const subscriptionName of subscriptionNames) {
|
||||
let status = "unknown";
|
||||
|
||||
try {
|
||||
const infoOutput = await pleskCliCall(connection, "subscription", [
|
||||
"--info",
|
||||
subscriptionName,
|
||||
]);
|
||||
const normalizedInfo = infoOutput.toLowerCase();
|
||||
|
||||
const hasSuspendedMarker =
|
||||
normalizedInfo.includes("suspended") ||
|
||||
normalizedInfo.includes("webspace-off") ||
|
||||
normalizedInfo.includes("disabled");
|
||||
|
||||
const hasActiveMarker =
|
||||
normalizedInfo.includes("active") ||
|
||||
normalizedInfo.includes("enabled") ||
|
||||
normalizedInfo.includes("ok");
|
||||
|
||||
if (hasSuspendedMarker) status = "suspended";
|
||||
else if (hasActiveMarker || !hasSuspendedMarker) status = "active";
|
||||
} catch {
|
||||
status = "unknown";
|
||||
}
|
||||
|
||||
return all;
|
||||
} catch {
|
||||
const cli = await pleskRequest<PleskCliResult>(
|
||||
connection,
|
||||
"/api/v2/cli/subscription/call",
|
||||
{
|
||||
method: "POST",
|
||||
body: JSON.stringify({ params: ["--list"] }),
|
||||
},
|
||||
);
|
||||
|
||||
const stdout = cli?.stdout ?? "";
|
||||
if (!stdout.trim()) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return stdout
|
||||
.split(/\r?\n/)
|
||||
.map((line) => line.trim())
|
||||
.filter(Boolean)
|
||||
.map((line) => {
|
||||
const columns = line.split(/\s+/);
|
||||
const externalId = columns[0] ?? line;
|
||||
return {
|
||||
id: externalId,
|
||||
subscription_id: externalId,
|
||||
external_id: externalId,
|
||||
name: columns.slice(1).join(" ") || externalId,
|
||||
status: null,
|
||||
};
|
||||
});
|
||||
subscriptions.push({
|
||||
plesk_subscription_id: subscriptionName,
|
||||
subscription_id: subscriptionName,
|
||||
name: subscriptionName,
|
||||
status,
|
||||
});
|
||||
}
|
||||
|
||||
return subscriptions;
|
||||
}
|
||||
|
||||
export async function pleskCliCall(
|
||||
@@ -208,7 +208,7 @@ export async function pleskCliCall(
|
||||
);
|
||||
}
|
||||
|
||||
return { code, stdout, stderr };
|
||||
return stdout;
|
||||
}
|
||||
|
||||
export async function listPleskDomains(connection: PleskConnection) {
|
||||
|
||||
@@ -82,13 +82,18 @@ export async function syncPleskInstance(
|
||||
|
||||
const subscriptionsForUpsert = subscriptionsRaw
|
||||
.map((item: any) => {
|
||||
const sourceId = item?.id ?? item?.guid ?? item?.subscription_id;
|
||||
const sourceId =
|
||||
item?.plesk_subscription_id ??
|
||||
item?.name ??
|
||||
item?.id ??
|
||||
item?.guid ??
|
||||
item?.subscription_id;
|
||||
if (!sourceId) return null;
|
||||
return {
|
||||
agency_id: instance.agency_id,
|
||||
plesk_instance_id: instance.id,
|
||||
plesk_subscription_id: String(sourceId),
|
||||
name: item?.name ? String(item.name) : null,
|
||||
name: item?.name ? String(item.name) : String(sourceId),
|
||||
status: item?.status ? String(item.status) : null,
|
||||
owner_login: item?.owner_login
|
||||
? String(item.owner_login)
|
||||
@@ -113,12 +118,44 @@ export async function syncPleskInstance(
|
||||
if (error) throw new Error(error.message);
|
||||
}
|
||||
|
||||
const { data: localSubscriptions, error: localSubscriptionsError } =
|
||||
await supabase
|
||||
.from("plesk_subscriptions")
|
||||
.select("id, plesk_subscription_id, status")
|
||||
.eq("agency_id", instance.agency_id)
|
||||
.eq("plesk_instance_id", instance.id);
|
||||
|
||||
if (localSubscriptionsError) {
|
||||
throw new Error(localSubscriptionsError.message);
|
||||
}
|
||||
|
||||
const subscriptionByExternalId = new Map<
|
||||
string,
|
||||
{ id: string; status: string | null }
|
||||
>(
|
||||
(localSubscriptions ?? [])
|
||||
.filter((row: any) => row?.plesk_subscription_id && row?.id)
|
||||
.map((row: any) => [
|
||||
String(row.plesk_subscription_id),
|
||||
{
|
||||
id: String(row.id),
|
||||
status: row?.status ? String(row.status) : null,
|
||||
},
|
||||
]),
|
||||
);
|
||||
|
||||
const domainsForUpsert = domainsRaw
|
||||
.map((item: any) => {
|
||||
const domainId = item?.id ?? item?.guid ?? item?.name;
|
||||
const domainName = item?.name ?? item?.domain_name ?? item?.domain;
|
||||
const externalSubscriptionId =
|
||||
const subExternalId =
|
||||
item?.subscription?.id ?? item?.subscription_id ?? item?.webspace_id;
|
||||
const externalSubscriptionId = subExternalId
|
||||
? String(subExternalId)
|
||||
: null;
|
||||
const localSubscription = externalSubscriptionId
|
||||
? subscriptionByExternalId.get(externalSubscriptionId)
|
||||
: undefined;
|
||||
const rawCreated = item?.created_at ?? item?.created;
|
||||
const sourceCreatedAt = rawCreated
|
||||
? new Date(String(rawCreated)).toISOString()
|
||||
@@ -135,18 +172,12 @@ export async function syncPleskInstance(
|
||||
return {
|
||||
agency_id: instance.agency_id,
|
||||
plesk_instance_id: instance.id,
|
||||
subscription_id: null,
|
||||
external_subscription_id: externalSubscriptionId
|
||||
? String(externalSubscriptionId)
|
||||
: null,
|
||||
subscription_id: localSubscription?.id ?? null,
|
||||
external_subscription_id: externalSubscriptionId,
|
||||
plesk_domain_id: String(domainId),
|
||||
domain_name: String(domainName),
|
||||
status: item?.status ? String(item.status) : null,
|
||||
hosting_type: item?.hosting_type
|
||||
? String(item.hosting_type)
|
||||
: item?.hosting?.type
|
||||
? String(item.hosting.type)
|
||||
: null,
|
||||
status: localSubscription?.status ?? "unknown",
|
||||
hosting_type: item?.hosting_type ? String(item.hosting_type) : null,
|
||||
source_created_at: sourceCreatedAt,
|
||||
aliases_count: aliasesCount,
|
||||
};
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
-- One-time backfill: align existing domain status with linked subscription status
|
||||
update public.plesk_domains d
|
||||
set status = s.status
|
||||
from public.plesk_subscriptions s
|
||||
where d.subscription_id = s.id
|
||||
and d.agency_id = s.agency_id
|
||||
and (d.status is null or d.status = 'unknown');
|
||||
|
||||
update public.plesk_subscriptions
|
||||
set status = 'unknown'
|
||||
where status is null;
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user