Create guest user in user table when creating a participant

This commit is contained in:
Luke Vella 2024-12-19 15:26:43 +00:00
parent 346ef136f0
commit 65192f95c9
No known key found for this signature in database
GPG key ID: 469CAD687F0D784C
5 changed files with 20 additions and 7 deletions

View file

@ -92,9 +92,14 @@ export const participants = router({
const participant = await prisma.participant.create({ const participant = await prisma.participant.create({
data: { data: {
pollId: pollId,
name: name, name: name,
email, email,
locale: user.locale ?? undefined,
poll: {
connect: {
id: pollId,
},
},
user: { user: {
connectOrCreate: { connectOrCreate: {
where: { where: {
@ -102,12 +107,15 @@ export const participants = router({
}, },
create: { create: {
id: user.id, id: user.id,
isGuest: true,
}, },
}, },
}, },
locale: user.locale ?? undefined,
}, },
include: { select: {
id: true,
name: true,
email: true,
poll: { poll: {
select: { select: {
id: true, id: true,

View file

@ -9,8 +9,9 @@ BEGIN
WHERE user_id IS NULL WHERE user_id IS NULL
LOOP LOOP
WITH new_user AS ( WITH new_user AS (
INSERT INTO users (is_guest, created_at) INSERT INTO users (id, is_guest, created_at)
VALUES ( VALUES (
gen_random_uuid()::text,
TRUE, TRUE,
NOW() NOW()
) )

View file

@ -4,7 +4,6 @@
- Made the column `user_id` on table `participants` required. This step will fail if there are existing NULL values in that column. - Made the column `user_id` on table `participants` required. This step will fail if there are existing NULL values in that column.
*/ */
-- AlterTable -- AlterTable
ALTER TABLE "participants" ALTER COLUMN "user_id" SET NOT NULL; ALTER TABLE "participants" ALTER COLUMN "user_id" SET NOT NULL;

View file

@ -0,0 +1,5 @@
-- DropForeignKey
ALTER TABLE "participants" DROP CONSTRAINT "participants_user_id_fkey";
-- AddForeignKey
ALTER TABLE "participants" ADD CONSTRAINT "participants_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

View file

@ -194,7 +194,7 @@ model Participant {
votes Vote[] @relation("ParticipantToVotes") votes Vote[] @relation("ParticipantToVotes")
poll Poll @relation("PollToParticipants", fields: [pollId], references: [id], onDelete: Cascade) poll Poll @relation("PollToParticipants", fields: [pollId], references: [id], onDelete: Cascade)
user User @relation(fields: [userId], references: [id]) user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@index([pollId], type: Hash) @@index([pollId], type: Hash)
@@map("participants") @@map("participants")