mirror of
https://github.com/domfelipe/mika-agent-assist.git
synced 2026-08-07 17:56:50 +00:00
Changes
Co-authored-by: domfelipe <53182096+domfelipe@users.noreply.github.com>
This commit is contained in:
parent
2b3e62d3f1
commit
5b84b72a91
2 changed files with 42 additions and 19 deletions
|
|
@ -102,20 +102,34 @@ function AgentDetailPage() {
|
|||
`id, user_id, uuid_tenant, status, telegram_bot_username, telegram_user_chat_id,
|
||||
railway_service_id, vps_pool_id, provisioned_at, created_at, model_config,
|
||||
vps_pool:vps_pool_id(railway_project_id, railway_environment_id),
|
||||
profile:profiles!agent_instances_user_id_fkey(full_name, phone, onboarding_completed),
|
||||
subscription:subscriptions!subscriptions_user_id_fkey(plans(slug, name))`,
|
||||
profile:profiles!agent_instances_user_id_fkey(full_name, phone, onboarding_completed)`,
|
||||
)
|
||||
.eq("id", id)
|
||||
.maybeSingle();
|
||||
if (error) throw error;
|
||||
if (!data) return null;
|
||||
|
||||
const { data: subscriptionData, error: subscriptionError } = await supabase
|
||||
.from("subscriptions")
|
||||
.select("status, plans(slug, name)")
|
||||
.eq("user_id", data.user_id)
|
||||
.order("created_at", { ascending: false })
|
||||
.limit(1)
|
||||
.maybeSingle();
|
||||
if (subscriptionError) throw subscriptionError;
|
||||
|
||||
// Normalizar arrays vindos do PostgREST
|
||||
// deno-lint-ignore no-explicit-any
|
||||
const d = data as any;
|
||||
const profile = Array.isArray(d.profile) ? d.profile[0] ?? null : d.profile;
|
||||
const subscription = Array.isArray(d.subscription)
|
||||
? d.subscription.find((s: { plans: unknown }) => s.plans) ?? d.subscription[0] ?? null
|
||||
: d.subscription;
|
||||
const subscription = subscriptionData
|
||||
? {
|
||||
...subscriptionData,
|
||||
plans: Array.isArray(subscriptionData.plans)
|
||||
? subscriptionData.plans[0] ?? null
|
||||
: subscriptionData.plans,
|
||||
}
|
||||
: null;
|
||||
|
||||
// Email não é acessível via client (RLS) — admin pode ver no Railway/Telegram
|
||||
const userEmail: string | null = null;
|
||||
|
|
|
|||
|
|
@ -71,24 +71,33 @@ function AdminPage() {
|
|||
enabled: !!isAdmin,
|
||||
refetchInterval: 15000,
|
||||
queryFn: async () => {
|
||||
const { data, error } = await supabase
|
||||
const { data: agentsData, error: agentsError } = await supabase
|
||||
.from("agent_instances")
|
||||
.select(
|
||||
`*,
|
||||
profile:profiles!agent_instances_user_id_fkey(full_name, phone),
|
||||
subscription:subscriptions!subscriptions_user_id_fkey(status, plans(slug, name))`,
|
||||
)
|
||||
.select(`*, profile:profiles!agent_instances_user_id_fkey(full_name, phone)`)
|
||||
.order("created_at", { ascending: false })
|
||||
.limit(100);
|
||||
if (error) throw error;
|
||||
// Pegar apenas a subscription ativa (primeira) — o relacionamento retorna array
|
||||
if (agentsError) throw agentsError;
|
||||
|
||||
const userIds = [...new Set((agentsData ?? []).map((agent) => agent.user_id).filter(Boolean))];
|
||||
|
||||
const { data: subscriptionsData, error: subscriptionsError } = userIds.length
|
||||
? await supabase
|
||||
.from("subscriptions")
|
||||
.select("user_id, status, plans(name, slug)")
|
||||
.in("user_id", userIds)
|
||||
.order("created_at", { ascending: false })
|
||||
: { data: [], error: null };
|
||||
if (subscriptionsError) throw subscriptionsError;
|
||||
|
||||
const subscriptionsByUserId = new Map(
|
||||
(subscriptionsData ?? []).map((subscription) => [subscription.user_id, subscription]),
|
||||
);
|
||||
|
||||
// deno-lint-ignore no-explicit-any
|
||||
return (data as any[]).map((a) => ({
|
||||
...a,
|
||||
profile: Array.isArray(a.profile) ? a.profile[0] ?? null : a.profile,
|
||||
subscription: Array.isArray(a.subscription)
|
||||
? a.subscription.find((s: { plans: unknown }) => s.plans) ?? a.subscription[0] ?? null
|
||||
: a.subscription,
|
||||
return (agentsData as any[]).map((agent) => ({
|
||||
...agent,
|
||||
profile: Array.isArray(agent.profile) ? agent.profile[0] ?? null : agent.profile,
|
||||
subscription: subscriptionsByUserId.get(agent.user_id) ?? null,
|
||||
})) as AgentRow[];
|
||||
},
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue