import { FEATURED_CONNECTOR_LABELS, matchFeaturedConnectorId } from "@rakazo/core"; import type { PrismaClient } from "@rakazo/db"; import { updateUserProfile } from "@rakazo/db"; import { getLogger } from "@rakazo/logging"; /** * The app's own name: a featured app's label, otherwise the label of the user's first account for * it (later accounts may be named "Gmail 2" or renamed). */ async function connectedAppName( prisma: PrismaClient, connection: { userId: string; connectorId: string; provider: string }, ): Promise { const featured = matchFeaturedConnectorId(connection.provider); if (featured) return FEATURED_CONNECTOR_LABELS[featured]; const first = await prisma.connection.findFirst({ where: { userId: connection.userId, connectorId: connection.connectorId, provider: connection.provider, }, orderBy: [{ createdAt: "asc" }, { id: "asc" }], select: { displayName: true }, }); const name = first?.displayName.trim().slice(0, 80); return name || null; } /** * Remembers "Uses " in the user's profile when one of their connections becomes connected. * Revoking the connection keeps the fact; the user removes it in About you. Best effort: the * connection itself has already succeeded, so a full profile or a database error only logs. */ export async function recordConnectedApp( prisma: PrismaClient, connection: { userId: string; connectorId: string; provider: string }, ): Promise { try { const name = await connectedAppName(prisma, connection); if (!name) return; await updateUserProfile(prisma, connection.userId, { addFacts: [{ category: "tools", value: `Uses ${name}`, source: "connector" }], }); } catch (error) { getLogger().error("connected app fact was not recorded", error, { connectorId: connection.connectorId, provider: connection.provider, }); } }