61 lines
1.9 KiB
TypeScript
61 lines
1.9 KiB
TypeScript
import { getAuthContext } from "../../../../../utils/auth";
|
|
import { prisma } from "../../../../../utils/prisma";
|
|
|
|
function normalizeStatus(input: {
|
|
pendingCount: number;
|
|
linkedPendingCount: number;
|
|
connectedCount: number;
|
|
enabledCount: number;
|
|
replyEnabledCount: number;
|
|
}) {
|
|
if (input.connectedCount > 0) {
|
|
if (input.replyEnabledCount > 0 && input.enabledCount > 0) return "connected";
|
|
if (input.enabledCount === 0) return "disabled";
|
|
return "no_reply_rights";
|
|
}
|
|
if (input.linkedPendingCount > 0) return "pending_business_connection";
|
|
if (input.pendingCount > 0) return "pending_link";
|
|
return "not_connected";
|
|
}
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const auth = await getAuthContext(event);
|
|
const rows = await prisma.telegramBusinessConnection.findMany({
|
|
where: { teamId: auth.teamId },
|
|
orderBy: { updatedAt: "desc" },
|
|
take: 50,
|
|
});
|
|
|
|
const pending = rows.filter((r) => r.businessConnectionId.startsWith("pending:"));
|
|
const active = rows.filter((r) => !r.businessConnectionId.startsWith("pending:"));
|
|
|
|
const linkedPendingCount = pending.filter((r) => {
|
|
const raw = (r.rawJson ?? {}) as any;
|
|
return Boolean(raw?.link?.telegramUserId || raw?.link?.chatId);
|
|
}).length;
|
|
|
|
const enabledCount = active.filter((r) => r.isEnabled !== false).length;
|
|
const replyEnabledCount = active.filter((r) => r.canReply === true).length;
|
|
|
|
const status = normalizeStatus({
|
|
pendingCount: pending.length,
|
|
linkedPendingCount,
|
|
connectedCount: active.length,
|
|
enabledCount,
|
|
replyEnabledCount,
|
|
});
|
|
|
|
return {
|
|
ok: true,
|
|
status,
|
|
pendingCount: pending.length,
|
|
connectedCount: active.length,
|
|
connections: active.map((r) => ({
|
|
businessConnectionId: r.businessConnectionId,
|
|
isEnabled: r.isEnabled,
|
|
canReply: r.canReply,
|
|
updatedAt: r.updatedAt.toISOString(),
|
|
})),
|
|
};
|
|
});
|