mirror of
https://github.com/lukevella/rallly.git
synced 2025-08-06 09:59:00 +02:00
64 lines
1.3 KiB
TypeScript
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);
|
|
}
|
|
};
|