Compare commits
2 Commits
fix/action
...
feat/cl4-s
| Author | SHA1 | Date | |
|---|---|---|---|
| 9deac1388d | |||
| e87f86ae86 |
@@ -5,8 +5,6 @@ import { assertAgencyAdmin, getRouteAgencyContextOrThrow } from "@/lib/agency";
|
|||||||
import { assertRateLimit } from "@/lib/api/rate-limit";
|
import { assertRateLimit } from "@/lib/api/rate-limit";
|
||||||
import { assertSameOrigin } from "@/lib/api/security";
|
import { assertSameOrigin } from "@/lib/api/security";
|
||||||
import {
|
import {
|
||||||
parseSubscriptionStatusDetailsFromInfo,
|
|
||||||
pleskCliCall,
|
|
||||||
suspendPleskSubscription,
|
suspendPleskSubscription,
|
||||||
unsuspendPleskSubscription,
|
unsuspendPleskSubscription,
|
||||||
} from "@/lib/plesk/client";
|
} from "@/lib/plesk/client";
|
||||||
@@ -82,59 +80,12 @@ export async function POST(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const fallbackStatus =
|
const newStatus = payload.action === "suspend" ? "suspended" : "active";
|
||||||
payload.action === "suspend" ? "suspended" : "active";
|
|
||||||
let refreshedStatus = fallbackStatus;
|
|
||||||
let refreshedStatusRaw: string | null = null;
|
|
||||||
let refreshErrorMessage: string | null = null;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const infoResult = await pleskCliCall(connection, "subscription", [
|
|
||||||
"--info",
|
|
||||||
subscription.plesk_subscription_id,
|
|
||||||
]);
|
|
||||||
const parsed = parseSubscriptionStatusDetailsFromInfo(
|
|
||||||
infoResult.stdout ?? "",
|
|
||||||
);
|
|
||||||
refreshedStatus = parsed.status;
|
|
||||||
refreshedStatusRaw = parsed.statusRaw;
|
|
||||||
} catch (refreshError) {
|
|
||||||
refreshErrorMessage =
|
|
||||||
refreshError instanceof Error
|
|
||||||
? refreshError.message.slice(0, 1000)
|
|
||||||
: "subscription_status_refresh_failed";
|
|
||||||
|
|
||||||
await supabase.from("actions_log").insert({
|
|
||||||
agency_id: context.agencyId,
|
|
||||||
actor_user_id: context.userId,
|
|
||||||
target_type: "plesk_subscription",
|
|
||||||
target_id: subscription.id,
|
|
||||||
action: "subscription_status_refresh_failed",
|
|
||||||
status: "failed",
|
|
||||||
error_message: refreshErrorMessage,
|
|
||||||
metadata: {
|
|
||||||
subscription_id: subscription.id,
|
|
||||||
plesk_subscription_id: subscription.plesk_subscription_id,
|
|
||||||
plesk_instance_id: subscription.plesk_instance_id,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const { error: updateError } = await supabase
|
const { error: updateError } = await supabase
|
||||||
.from("plesk_subscriptions")
|
.from("plesk_subscriptions")
|
||||||
.update({
|
.update({ status: newStatus })
|
||||||
status: refreshedStatus,
|
|
||||||
status_raw: refreshedStatusRaw,
|
|
||||||
last_status_sync_at: new Date().toISOString(),
|
|
||||||
})
|
|
||||||
.eq("id", subscription.id);
|
.eq("id", subscription.id);
|
||||||
|
|
||||||
await supabase
|
|
||||||
.from("plesk_domains")
|
|
||||||
.update({ status: refreshedStatus })
|
|
||||||
.eq("agency_id", context.agencyId)
|
|
||||||
.eq("subscription_id", subscription.id);
|
|
||||||
|
|
||||||
await supabase.from("actions_log").insert({
|
await supabase.from("actions_log").insert({
|
||||||
agency_id: context.agencyId,
|
agency_id: context.agencyId,
|
||||||
actor_user_id: context.userId,
|
actor_user_id: context.userId,
|
||||||
@@ -144,17 +95,14 @@ export async function POST(
|
|||||||
status: "success",
|
status: "success",
|
||||||
metadata: {
|
metadata: {
|
||||||
plesk_subscription_id: subscription.plesk_subscription_id,
|
plesk_subscription_id: subscription.plesk_subscription_id,
|
||||||
refreshed_status: refreshedStatus,
|
|
||||||
refresh_error: refreshErrorMessage,
|
|
||||||
db_status_update_error: updateError?.message ?? null,
|
db_status_update_error: updateError?.message ?? null,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
return NextResponse.json({
|
return NextResponse.json({
|
||||||
ok: true,
|
ok: true,
|
||||||
status: refreshedStatus,
|
status: newStatus,
|
||||||
dbStatusUpdated: !updateError,
|
dbStatusUpdated: !updateError,
|
||||||
refreshError: refreshErrorMessage,
|
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (supabase && context && subscription) {
|
if (supabase && context && subscription) {
|
||||||
|
|||||||
@@ -17,10 +17,6 @@ type ActionLogSummary = Pick<
|
|||||||
"target_id" | "status" | "action" | "created_at" | "error_message"
|
"target_id" | "status" | "action" | "created_at" | "error_message"
|
||||||
>;
|
>;
|
||||||
|
|
||||||
function normalizeDomainLike(value: string) {
|
|
||||||
return value.trim().toLowerCase().replace(/\.$/, "");
|
|
||||||
}
|
|
||||||
|
|
||||||
export default async function DashboardPage() {
|
export default async function DashboardPage() {
|
||||||
const context = await getServerAgencyContextOrRedirect();
|
const context = await getServerAgencyContextOrRedirect();
|
||||||
const supabase = createSupabaseServerClient() as any;
|
const supabase = createSupabaseServerClient() as any;
|
||||||
@@ -54,7 +50,7 @@ export default async function DashboardPage() {
|
|||||||
supabase
|
supabase
|
||||||
.from("plesk_domains")
|
.from("plesk_domains")
|
||||||
.select(
|
.select(
|
||||||
"id, plesk_instance_id, subscription_id, external_subscription_id, status, domain_name, 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 })
|
||||||
@@ -105,7 +101,6 @@ export default async function DashboardPage() {
|
|||||||
id: string;
|
id: string;
|
||||||
plesk_instance_id: string;
|
plesk_instance_id: string;
|
||||||
subscription_id: string | null;
|
subscription_id: string | null;
|
||||||
external_subscription_id: string | null;
|
|
||||||
domain_name: string;
|
domain_name: string;
|
||||||
status: string | null;
|
status: string | null;
|
||||||
hosting_type: string | null;
|
hosting_type: string | null;
|
||||||
@@ -118,59 +113,6 @@ export default async function DashboardPage() {
|
|||||||
{ data: ActionLogSummary[] | null },
|
{ data: ActionLogSummary[] | null },
|
||||||
];
|
];
|
||||||
|
|
||||||
const domainSubscriptionIds = Array.from(
|
|
||||||
new Set(
|
|
||||||
(domains ?? [])
|
|
||||||
.map((domain) => domain.subscription_id)
|
|
||||||
.filter((id): id is string => Boolean(id)),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
const domainInstanceIds = Array.from(
|
|
||||||
new Set((domains ?? []).map((domain) => domain.plesk_instance_id)),
|
|
||||||
);
|
|
||||||
|
|
||||||
const { data: domainSubscriptions } =
|
|
||||||
domainInstanceIds.length > 0
|
|
||||||
? await supabase
|
|
||||||
.from("plesk_subscriptions")
|
|
||||||
.select("id, plesk_instance_id, plesk_subscription_id, name, status")
|
|
||||||
.eq("agency_id", context.agencyId)
|
|
||||||
.in("plesk_instance_id", domainInstanceIds)
|
|
||||||
: { data: [] };
|
|
||||||
|
|
||||||
const subscriptionStatusById = new Map<string, string | null>(
|
|
||||||
(domainSubscriptions ?? []).map((subscription: any) => [
|
|
||||||
String(subscription.id),
|
|
||||||
subscription?.status ? String(subscription.status) : null,
|
|
||||||
]),
|
|
||||||
);
|
|
||||||
|
|
||||||
const subscriptionStatusByExternalId = new Map<string, string | null>(
|
|
||||||
(domainSubscriptions ?? [])
|
|
||||||
.filter((subscription: any) =>
|
|
||||||
Boolean(
|
|
||||||
subscription?.plesk_instance_id &&
|
|
||||||
subscription?.plesk_subscription_id,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
.map((subscription: any) => [
|
|
||||||
`${String(subscription.plesk_instance_id)}:${String(subscription.plesk_subscription_id)}`,
|
|
||||||
subscription?.status ? String(subscription.status) : null,
|
|
||||||
]),
|
|
||||||
);
|
|
||||||
|
|
||||||
const subscriptionStatusByInstanceAndName = new Map<string, string | null>(
|
|
||||||
(domainSubscriptions ?? [])
|
|
||||||
.filter((subscription: any) =>
|
|
||||||
Boolean(subscription?.plesk_instance_id && subscription?.name),
|
|
||||||
)
|
|
||||||
.map((subscription: any) => [
|
|
||||||
`${String(subscription.plesk_instance_id)}:${normalizeDomainLike(String(subscription.name))}`,
|
|
||||||
subscription?.status ? String(subscription.status) : null,
|
|
||||||
]),
|
|
||||||
);
|
|
||||||
|
|
||||||
const autoSyncByInstance = new Map<
|
const autoSyncByInstance = new Map<
|
||||||
string,
|
string,
|
||||||
{ status: string; created_at: string; error_message: string | null }
|
{ status: string; created_at: string; error_message: string | null }
|
||||||
@@ -225,28 +167,7 @@ export default async function DashboardPage() {
|
|||||||
<DashboardControls
|
<DashboardControls
|
||||||
instances={instances ?? []}
|
instances={instances ?? []}
|
||||||
subscriptions={subscriptions ?? []}
|
subscriptions={subscriptions ?? []}
|
||||||
domains={(domains ?? []).map((domain) => {
|
domains={domains ?? []}
|
||||||
const joinedStatusByLocalId = domain.subscription_id
|
|
||||||
? subscriptionStatusById.get(domain.subscription_id)
|
|
||||||
: null;
|
|
||||||
const joinedStatusByExternalId = domain.external_subscription_id
|
|
||||||
? subscriptionStatusByExternalId.get(
|
|
||||||
`${domain.plesk_instance_id}:${domain.external_subscription_id}`,
|
|
||||||
)
|
|
||||||
: null;
|
|
||||||
const joinedStatusByName = subscriptionStatusByInstanceAndName.get(
|
|
||||||
`${domain.plesk_instance_id}:${normalizeDomainLike(domain.domain_name)}`,
|
|
||||||
);
|
|
||||||
const joinedStatus =
|
|
||||||
joinedStatusByLocalId ??
|
|
||||||
joinedStatusByExternalId ??
|
|
||||||
joinedStatusByName;
|
|
||||||
|
|
||||||
return {
|
|
||||||
...domain,
|
|
||||||
status: joinedStatus ?? "unknown",
|
|
||||||
};
|
|
||||||
})}
|
|
||||||
autoSyncByInstance={Object.fromEntries(autoSyncByInstance.entries())}
|
autoSyncByInstance={Object.fromEntries(autoSyncByInstance.entries())}
|
||||||
/>
|
/>
|
||||||
</main>
|
</main>
|
||||||
|
|||||||
@@ -22,13 +22,6 @@ type InstanceRow = Pick<
|
|||||||
| "last_connected_at"
|
| "last_connected_at"
|
||||||
>;
|
>;
|
||||||
|
|
||||||
type LocalSubscriptionLookupRow = {
|
|
||||||
id: string;
|
|
||||||
plesk_subscription_id: string;
|
|
||||||
name: string | null;
|
|
||||||
status: string | null;
|
|
||||||
};
|
|
||||||
|
|
||||||
const MIN_INTERVAL_MS = 3 * 60 * 1000;
|
const MIN_INTERVAL_MS = 3 * 60 * 1000;
|
||||||
|
|
||||||
export async function syncPleskInstance(
|
export async function syncPleskInstance(
|
||||||
@@ -163,7 +156,7 @@ export async function syncPleskInstance(
|
|||||||
const { data: localSubscriptions, error: localSubscriptionsError } =
|
const { data: localSubscriptions, error: localSubscriptionsError } =
|
||||||
await supabase
|
await supabase
|
||||||
.from("plesk_subscriptions")
|
.from("plesk_subscriptions")
|
||||||
.select("id, plesk_subscription_id, name, status")
|
.select("id, plesk_subscription_id, status")
|
||||||
.eq("agency_id", instance.agency_id)
|
.eq("agency_id", instance.agency_id)
|
||||||
.eq("plesk_instance_id", instance.id);
|
.eq("plesk_instance_id", instance.id);
|
||||||
|
|
||||||
@@ -171,49 +164,19 @@ export async function syncPleskInstance(
|
|||||||
throw new Error(localSubscriptionsError.message);
|
throw new Error(localSubscriptionsError.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
const subscriptionsIndexed = ((localSubscriptions ?? []) as Array<any>)
|
|
||||||
.filter((row): row is LocalSubscriptionLookupRow =>
|
|
||||||
Boolean(row?.plesk_subscription_id && row?.id),
|
|
||||||
)
|
|
||||||
.map((row) => ({
|
|
||||||
id: String(row.id),
|
|
||||||
pleskSubscriptionId: String(row.plesk_subscription_id),
|
|
||||||
status: row?.status ? String(row.status) : null,
|
|
||||||
name: row?.name ? String(row.name) : null,
|
|
||||||
}));
|
|
||||||
|
|
||||||
const subscriptionByExternalId = new Map<
|
const subscriptionByExternalId = new Map<
|
||||||
string,
|
string,
|
||||||
{
|
{ id: string; status: string | null }
|
||||||
id: string;
|
|
||||||
pleskSubscriptionId: string;
|
|
||||||
status: string | null;
|
|
||||||
name: string | null;
|
|
||||||
}
|
|
||||||
>(subscriptionsIndexed.map((row) => [row.pleskSubscriptionId, row]));
|
|
||||||
|
|
||||||
const subscriptionByLocalId = new Map<
|
|
||||||
string,
|
|
||||||
{
|
|
||||||
id: string;
|
|
||||||
pleskSubscriptionId: string;
|
|
||||||
status: string | null;
|
|
||||||
name: string | null;
|
|
||||||
}
|
|
||||||
>(subscriptionsIndexed.map((row) => [row.id, row]));
|
|
||||||
|
|
||||||
const subscriptionByName = new Map<
|
|
||||||
string,
|
|
||||||
{
|
|
||||||
id: string;
|
|
||||||
pleskSubscriptionId: string;
|
|
||||||
status: string | null;
|
|
||||||
name: string | null;
|
|
||||||
}
|
|
||||||
>(
|
>(
|
||||||
subscriptionsIndexed
|
(localSubscriptions ?? [])
|
||||||
.filter((row) => row.name)
|
.filter((row: any) => row?.plesk_subscription_id && row?.id)
|
||||||
.map((row) => [String(row.name).trim().toLowerCase(), row]),
|
.map((row: any) => [
|
||||||
|
String(row.plesk_subscription_id),
|
||||||
|
{
|
||||||
|
id: String(row.id),
|
||||||
|
status: row?.status ? String(row.status) : null,
|
||||||
|
},
|
||||||
|
]),
|
||||||
);
|
);
|
||||||
|
|
||||||
const domainsForUpsert = domainsRaw
|
const domainsForUpsert = domainsRaw
|
||||||
@@ -222,37 +185,12 @@ export async function syncPleskInstance(
|
|||||||
const domainName = item?.name ?? item?.domain_name ?? item?.domain;
|
const domainName = item?.name ?? item?.domain_name ?? item?.domain;
|
||||||
const subExternalId =
|
const subExternalId =
|
||||||
item?.subscription?.id ?? item?.subscription_id ?? item?.webspace_id;
|
item?.subscription?.id ?? item?.subscription_id ?? item?.webspace_id;
|
||||||
const rawExternalSubscriptionId = subExternalId
|
const externalSubscriptionId = subExternalId
|
||||||
? String(subExternalId)
|
? String(subExternalId)
|
||||||
: null;
|
: null;
|
||||||
const subscriptionNameHint =
|
const localSubscription = externalSubscriptionId
|
||||||
item?.subscription?.name ??
|
? subscriptionByExternalId.get(externalSubscriptionId)
|
||||||
item?.subscription_name ??
|
|
||||||
item?.webspace_name ??
|
|
||||||
item?.webspace ??
|
|
||||||
null;
|
|
||||||
|
|
||||||
const localSubscriptionByExternal = rawExternalSubscriptionId
|
|
||||||
? subscriptionByExternalId.get(rawExternalSubscriptionId)
|
|
||||||
: undefined;
|
: undefined;
|
||||||
const localSubscriptionByLocalId = rawExternalSubscriptionId
|
|
||||||
? subscriptionByLocalId.get(rawExternalSubscriptionId)
|
|
||||||
: undefined;
|
|
||||||
const localSubscriptionByName = subscriptionNameHint
|
|
||||||
? subscriptionByName.get(
|
|
||||||
String(subscriptionNameHint).trim().toLowerCase(),
|
|
||||||
)
|
|
||||||
: undefined;
|
|
||||||
const localSubscription =
|
|
||||||
localSubscriptionByExternal ??
|
|
||||||
localSubscriptionByLocalId ??
|
|
||||||
localSubscriptionByName;
|
|
||||||
|
|
||||||
const externalSubscriptionId = localSubscription
|
|
||||||
? localSubscription.pleskSubscriptionId
|
|
||||||
: subscriptionNameHint
|
|
||||||
? String(subscriptionNameHint)
|
|
||||||
: rawExternalSubscriptionId;
|
|
||||||
const rawCreated = item?.created_at ?? item?.created;
|
const rawCreated = item?.created_at ?? item?.created;
|
||||||
const sourceCreatedAt = rawCreated
|
const sourceCreatedAt = rawCreated
|
||||||
? new Date(String(rawCreated)).toISOString()
|
? new Date(String(rawCreated)).toISOString()
|
||||||
|
|||||||
Reference in New Issue
Block a user