// PEP-195: one person — one account. Someone who first logged in with Google got a separate account; // linking that Google to their main (eGov) account merges the two: projects, project membership, // subscriptions they connected, Telegram and their own bot move to the main account, the old one // is switched off (its history stays, attributed to the old login). import { eq, sql } from "drizzle-orm"; import { users, type Db } from "@peppa/db"; /** Moves everything the person works with from account `fromId` to `toId` and disables `fromId`. One transaction. */ export async function mergeUserInto(db: Db, fromId: number, toId: number, o: { identities?: boolean } = {}): Promise { if (fromId === toId) return; const [from] = await db.select().from(users).where(eq(users.id, fromId)); const [to] = await db.select().from(users).where(eq(users.id, toId)); if (!from || !to) return; const moveTelegram = !to.telegramChatId && !!from.telegramChatId; const moveBot = !to.telegramBotTokenEnc && !!from.telegramBotTokenEnc; // PEP-204: a merge made by the superadmin also carries the old account's eGov / Google over when the // main one has none — otherwise that way in would lead into a switched-off account. const moveEgov = !!o.identities && !to.egovIin && !!from.egovIin; const moveGoogle = !!o.identities && !to.googleSub && !!from.googleSub; db.transaction((tx) => { tx.run(sql`UPDATE projects SET owner_id = ${toId} WHERE owner_id = ${fromId}`); // Membership: one row per project, none in the main account's own projects. tx.run(sql`INSERT OR IGNORE INTO project_members (project_id, user_id, added_by, created_at) SELECT project_id, ${toId}, added_by, created_at FROM project_members WHERE user_id = ${fromId}`); tx.run(sql`DELETE FROM project_members WHERE user_id = ${fromId}`); tx.run(sql`DELETE FROM project_members WHERE user_id = ${toId} AND project_id IN (SELECT id FROM projects WHERE owner_id = ${toId})`); tx.run(sql`UPDATE project_invites SET invited_by = ${toId} WHERE invited_by = ${fromId}`); tx.run(sql`UPDATE subscriptions SET created_by = ${toId} WHERE created_by = ${fromId}`); tx.run(sql`UPDATE subscription_pools SET created_by = ${toId} WHERE created_by = ${fromId}`); tx.run(sql`UPDATE chats SET user_id = ${toId} WHERE user_id = ${fromId} AND NOT EXISTS (SELECT 1 FROM chats WHERE user_id = ${toId})`); if (moveBot) tx.run(sql`UPDATE chats SET tg_bot_user_id = ${toId} WHERE tg_bot_user_id = ${fromId}`); // The old account lets go first: Telegram chat and Google identity are unique. tx.update(users) .set({ googleSub: null, googleEmail: null, googleLinkedAt: null, ...(moveEgov ? { egovIin: null, egovLinkedAt: null } : {}), telegramChatId: null, telegramUserId: null, telegramUsername: null, telegramPairedAt: null, telegramBotTokenEnc: null, telegramBotUsername: null, disabledAt: new Date(), }) .where(eq(users.id, fromId)) .run(); if (moveTelegram || moveBot || moveEgov || moveGoogle) { tx.update(users) .set({ ...(moveEgov ? { egovIin: from.egovIin, egovLinkedAt: from.egovLinkedAt } : {}), ...(moveGoogle ? { googleSub: from.googleSub, googleEmail: from.googleEmail, googleLinkedAt: from.googleLinkedAt } : {}), ...(moveTelegram ? { telegramChatId: from.telegramChatId, telegramUserId: from.telegramUserId, telegramUsername: from.telegramUsername, telegramPairedAt: from.telegramPairedAt } : {}), ...(moveBot ? { telegramBotTokenEnc: from.telegramBotTokenEnc, telegramBotUsername: from.telegramBotUsername } : {}), }) .where(eq(users.id, toId)) .run(); } }); }