mirror of
https://github.com/domfelipe/mika-agent-assist.git
synced 2026-08-07 08:36:44 +00:00
Corrigiu carga de assinaturas
X-Lovable-Edit-ID: edt-ab392987-9408-4358-9a76-e2c23dfd0e36 Co-authored-by: domfelipe <53182096+domfelipe@users.noreply.github.com>
This commit is contained in:
commit
5db84804de
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,
|
`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,
|
railway_service_id, vps_pool_id, provisioned_at, created_at, model_config,
|
||||||
vps_pool:vps_pool_id(railway_project_id, railway_environment_id),
|
vps_pool:vps_pool_id(railway_project_id, railway_environment_id),
|
||||||
profile:profiles!agent_instances_user_id_fkey(full_name, phone, onboarding_completed),
|
profile:profiles!agent_instances_user_id_fkey(full_name, phone, onboarding_completed)`,
|
||||||
subscription:subscriptions!subscriptions_user_id_fkey(plans(slug, name))`,
|
|
||||||
)
|
)
|
||||||
.eq("id", id)
|
.eq("id", id)
|
||||||
.maybeSingle();
|
.maybeSingle();
|
||||||
if (error) throw error;
|
if (error) throw error;
|
||||||
if (!data) return null;
|
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
|
// Normalizar arrays vindos do PostgREST
|
||||||
// deno-lint-ignore no-explicit-any
|
// deno-lint-ignore no-explicit-any
|
||||||
const d = data as any;
|
const d = data as any;
|
||||||
const profile = Array.isArray(d.profile) ? d.profile[0] ?? null : d.profile;
|
const profile = Array.isArray(d.profile) ? d.profile[0] ?? null : d.profile;
|
||||||
const subscription = Array.isArray(d.subscription)
|
const subscription = subscriptionData
|
||||||
? d.subscription.find((s: { plans: unknown }) => s.plans) ?? d.subscription[0] ?? null
|
? {
|
||||||
: d.subscription;
|
...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
|
// Email não é acessível via client (RLS) — admin pode ver no Railway/Telegram
|
||||||
const userEmail: string | null = null;
|
const userEmail: string | null = null;
|
||||||
|
|
|
||||||
|
|
@ -71,24 +71,33 @@ function AdminPage() {
|
||||||
enabled: !!isAdmin,
|
enabled: !!isAdmin,
|
||||||
refetchInterval: 15000,
|
refetchInterval: 15000,
|
||||||
queryFn: async () => {
|
queryFn: async () => {
|
||||||
const { data, error } = await supabase
|
const { data: agentsData, error: agentsError } = await supabase
|
||||||
.from("agent_instances")
|
.from("agent_instances")
|
||||||
.select(
|
.select(`*, profile:profiles!agent_instances_user_id_fkey(full_name, phone)`)
|
||||||
`*,
|
|
||||||
profile:profiles!agent_instances_user_id_fkey(full_name, phone),
|
|
||||||
subscription:subscriptions!subscriptions_user_id_fkey(status, plans(slug, name))`,
|
|
||||||
)
|
|
||||||
.order("created_at", { ascending: false })
|
.order("created_at", { ascending: false })
|
||||||
.limit(100);
|
.limit(100);
|
||||||
if (error) throw error;
|
if (agentsError) throw agentsError;
|
||||||
// Pegar apenas a subscription ativa (primeira) — o relacionamento retorna array
|
|
||||||
|
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
|
// deno-lint-ignore no-explicit-any
|
||||||
return (data as any[]).map((a) => ({
|
return (agentsData as any[]).map((agent) => ({
|
||||||
...a,
|
...agent,
|
||||||
profile: Array.isArray(a.profile) ? a.profile[0] ?? null : a.profile,
|
profile: Array.isArray(agent.profile) ? agent.profile[0] ?? null : agent.profile,
|
||||||
subscription: Array.isArray(a.subscription)
|
subscription: subscriptionsByUserId.get(agent.user_id) ?? null,
|
||||||
? a.subscription.find((s: { plans: unknown }) => s.plans) ?? a.subscription[0] ?? null
|
|
||||||
: a.subscription,
|
|
||||||
})) as AgentRow[];
|
})) as AgentRow[];
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue