import type { Context } from "grammy"; import { db } from "../../db.js"; export async function handleGroups(ctx: Context): Promise { const text = ctx.message?.text ?? ""; const label = text .replace(/^\/groups\s*/, "") .trim() .replace(/^["'“”‘’]|["'“”‘’]$/g, ""); if (!label) { await ctx.reply('Usage: /groups "Account Label"'); return; } const operatorId = ctx.from?.id; if (!operatorId) return; const operatorRow = await db.query.operators.findFirst({ where: (o, { eq }) => eq(o.telegramUserId, operatorId), }); if (!operatorRow) return; const account = await db.query.whatsappAccounts.findFirst({ where: (a, { eq, and }) => and(eq(a.operatorId, operatorRow.id), eq(a.label, label)), }); if (!account) { await ctx.reply(`No account labelled "${label}".`); return; } const groups = await db.query.whatsappGroups.findMany({ where: (g, { eq }) => eq(g.accountId, account.id), orderBy: (g, { asc }) => [asc(g.name)], }); if (groups.length === 0) { await ctx.reply(`No groups synced for "${label}" yet.`); return; } const lines = groups.slice(0, 50).map((g) => `• ${g.name} (${g.participantCount})`); const overflow = groups.length > 50 ? `\n…and ${groups.length - 50} more` : ""; await ctx.reply(`👥 Groups in "${label}":\n${lines.join("\n")}${overflow}`); }