🐛 Fix participant count on home screen (#1420)

This commit is contained in:
Luke Vella 2024-10-31 22:07:52 +00:00 committed by GitHub
parent 641eb13cb6
commit eaab13b516
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -45,7 +45,7 @@ export const polls = router({
by: ["status"], by: ["status"],
where: { where: {
userId: ctx.user.id, userId: ctx.user.id,
deleted: false, deletedAt: null,
}, },
_count: { _count: {
status: true, status: true,
@ -60,44 +60,6 @@ export const polls = router({
{} as Record<PollStatus, number>, {} as Record<PollStatus, number>,
); );
}), }),
list: possiblyPublicProcedure
.input(
z.object({
status: z.enum(["all", "live", "paused", "finalized"]),
}),
)
.query(async ({ ctx, input }) => {
return await prisma.poll.findMany({
where: {
userId: ctx.user.id,
deleted: false,
status: input.status === "all" ? undefined : input.status,
},
orderBy: [
{
createdAt: "desc",
},
{
title: "asc",
},
],
select: {
id: true,
title: true,
location: true,
timeZone: true,
createdAt: true,
status: true,
userId: true,
participants: {
select: {
id: true,
name: true,
},
},
},
});
}),
infiniteList: possiblyPublicProcedure infiniteList: possiblyPublicProcedure
.input( .input(
z.object({ z.object({
@ -111,7 +73,7 @@ export const polls = router({
const polls = await prisma.poll.findMany({ const polls = await prisma.poll.findMany({
where: { where: {
userId: ctx.user.id, userId: ctx.user.id,
deleted: false, deletedAt: null,
status: status === "all" ? undefined : status, status: status === "all" ? undefined : status,
}, },
orderBy: [ orderBy: [
@ -133,6 +95,9 @@ export const polls = router({
status: true, status: true,
userId: true, userId: true,
participants: { participants: {
where: {
deletedAt: null,
},
select: { select: {
id: true, id: true,
name: true, name: true,
@ -535,118 +500,6 @@ export const polls = router({
}, },
}); });
}), }),
paginatedList: possiblyPublicProcedure
.input(
z.object({
list: z.string().optional(),
pagination: z.object({
pageIndex: z.number(),
pageSize: z.number(),
}),
sorting: z
.array(
z.object({
id: z.string(),
desc: z.boolean(),
}),
)
.optional(),
}),
)
.query(async ({ ctx, input }) => {
const whereParticipated = {
userId: {
not: ctx.user.id,
},
participants: {
some: {
userId: ctx.user.id,
},
},
};
const whereCreated = {
userId: ctx.user.id,
};
const where =
input.list === "all"
? {
OR: [whereCreated, whereParticipated],
}
: input.list === "other"
? whereParticipated
: input.list === "mine"
? whereCreated
: null;
if (!where) {
throw new TRPCError({
code: "BAD_REQUEST",
message: "Invalid list",
});
}
const [total, rows] = await prisma.$transaction([
prisma.poll.count({
where: { deleted: false, ...where },
}),
prisma.poll.findMany({
where: { deleted: false, ...where },
select: {
id: true,
title: true,
location: true,
userId: true,
user: {
select: {
id: true,
name: true,
},
},
createdAt: true,
timeZone: true,
adminUrlId: true,
participantUrlId: true,
status: true,
event: {
select: {
start: true,
duration: true,
},
},
closed: true,
participants: {
select: {
id: true,
name: true,
},
orderBy: [
{
createdAt: "desc",
},
{ name: "desc" },
],
},
},
orderBy:
input.sorting && input
? input.sorting?.map((s) => ({
[s.id]: s.desc ? "desc" : "asc",
}))
: [
{
createdAt: "desc",
},
{ title: "asc" },
],
skip: input.pagination.pageIndex * input.pagination.pageSize,
take: input.pagination.pageSize,
}),
]);
return { total, rows };
}),
getParticipating: possiblyPublicProcedure getParticipating: possiblyPublicProcedure
.input( .input(
z.object({ z.object({
@ -669,7 +522,7 @@ export const polls = router({
}), }),
prisma.poll.findMany({ prisma.poll.findMany({
where: { where: {
deleted: false, deletedAt: null,
participants: { participants: {
some: { some: {
userId: ctx.user.id, userId: ctx.user.id,