rallly/apps/web/src/auth/helpers/merge-user.ts
2025-03-10 10:48:20 +00:00

64 lines
1.3 KiB
TypeScript

import { prisma } from "@rallly/database";
import { posthog } from "@rallly/posthog/server";
import * as Sentry from "@sentry/nextjs";
export const mergeGuestsIntoUser = async (
userId: string,
guestIds: string[],
) => {
const count = await prisma.user.count({
where: {
id: userId,
},
});
if (count === 0) {
console.warn(`User ${userId} not found`);
return;
}
try {
await prisma.$transaction(async (tx) => {
await Promise.all([
tx.poll.updateMany({
where: {
guestId: {
in: guestIds,
},
},
data: {
guestId: null,
userId: userId,
},
}),
tx.participant.updateMany({
where: {
guestId: {
in: guestIds,
},
},
data: {
guestId: null,
userId: userId,
},
}),
tx.comment.updateMany({
where: {
guestId: {
in: guestIds,
},
},
data: {
guestId: null,
userId: userId,
},
}),
]);
});
posthog?.alias({ distinctId: userId, alias: guestIds[0] });
} catch (error) {
Sentry.captureException(error);
}
};