Skip to content

Commit

Permalink
馃搱 Include pollId when capturing events (#555)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukevella committed Mar 13, 2023
1 parent e77ed26 commit d14a88e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 58 deletions.
1 change: 1 addition & 0 deletions apps/web/src/components/create-poll.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ const Page: React.FunctionComponent<CreatePollPageProps> = ({
onSuccess: (res) => {
setIsRedirecting(true);
posthog.capture("created poll", {
pollId: res.id,
numberOfOptions: formData.options?.options?.length,
optionsView: formData?.options?.view,
});
Expand Down
6 changes: 4 additions & 2 deletions apps/web/src/components/poll/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ export const normalizeVotes = (

export const useAddParticipantMutation = () => {
return trpc.polls.participants.add.useMutation({
onSuccess: (_, { name }) => {
onSuccess: (_, { pollId, name }) => {
posthog.capture("add participant", {
pollId,
name,
});
},
Expand Down Expand Up @@ -61,8 +62,9 @@ export const useDeleteParticipantMutation = () => {
},
);
},
onSuccess: (_, { participantId }) => {
onSuccess: (_, { pollId, participantId }) => {
posthog.capture("remove participant", {
pollId,
participantId,
});
},
Expand Down
119 changes: 63 additions & 56 deletions apps/web/src/server/routers/polls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,73 +94,80 @@ export const polls = router({
demo: z.boolean().optional(),
}),
)
.mutation(async ({ ctx, input }): Promise<{ urlId: string }> => {
const adminUrlId = await nanoid();
const participantUrlId = await nanoid();
let verified = false;
.mutation(
async ({ ctx, input }): Promise<{ id: string; urlId: string }> => {
const adminUrlId = await nanoid();
const participantUrlId = await nanoid();
let verified = false;

if (ctx.session.user.isGuest === false) {
const user = await prisma.user.findUnique({
where: { id: ctx.session.user.id },
});
if (ctx.session.user.isGuest === false) {
const user = await prisma.user.findUnique({
where: { id: ctx.session.user.id },
});

// If user is logged in with the same email address
if (user?.email === input.user.email) {
verified = true;
// If user is logged in with the same email address
if (user?.email === input.user.email) {
verified = true;
}
}
}

const poll = await prisma.poll.create({
data: {
id: await nanoid(),
title: input.title,
type: input.type,
timeZone: input.timeZone,
location: input.location,
description: input.description,
authorName: input.user.name,
demo: input.demo,
verified: verified,
notifications: verified,
adminUrlId,
participantUrlId,
user: {
connectOrCreate: {
where: {
email: input.user.email,
},
create: {
id: await nanoid(),
...input.user,
const poll = await prisma.poll.create({
select: {
adminUrlId: true,
id: true,
title: true,
},
data: {
id: await nanoid(),
title: input.title,
type: input.type,
timeZone: input.timeZone,
location: input.location,
description: input.description,
authorName: input.user.name,
demo: input.demo,
verified: verified,
notifications: verified,
adminUrlId,
participantUrlId,
user: {
connectOrCreate: {
where: {
email: input.user.email,
},
create: {
id: await nanoid(),
...input.user,
},
},
},
},
options: {
createMany: {
data: input.options.map((value) => ({
value,
})),
options: {
createMany: {
data: input.options.map((value) => ({
value,
})),
},
},
},
},
});
});

const adminLink = absoluteUrl(`/admin/${adminUrlId}`);
const participantLink = absoluteUrl(`/p/${participantUrlId}`);
const adminLink = absoluteUrl(`/admin/${adminUrlId}`);
const participantLink = absoluteUrl(`/p/${participantUrlId}`);

await sendEmail("NewPollEmail", {
to: input.user.email,
subject: `Let's find a date for ${poll.title}`,
props: {
title: poll.title,
name: input.user.name,
adminLink,
participantLink,
},
});
await sendEmail("NewPollEmail", {
to: input.user.email,
subject: `Let's find a date for ${poll.title}`,
props: {
title: poll.title,
name: input.user.name,
adminLink,
participantLink,
},
});

return { urlId: adminUrlId };
}),
return { id: poll.id, urlId: adminUrlId };
},
),
update: publicProcedure
.input(
z.object({
Expand Down

0 comments on commit d14a88e

Please sign in to comment.