New and Improved Screens (#1151)

This commit is contained in:
Luke Vella 2024-06-19 11:14:18 +01:00 committed by GitHub
parent 5461c57228
commit 997a1eec78
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
75 changed files with 1517 additions and 743 deletions

View file

@ -10,6 +10,7 @@
"types": "src/index.ts",
"dependencies": {
"@rallly/database": "*",
"@rallly/features": "*",
"@rallly/emails": "*",
"@rallly/utils": "*",
"@trpc/server": "^10.13.0",

View file

@ -0,0 +1,17 @@
import { prisma } from "@rallly/database";
import { possiblyPublicProcedure, router } from "../trpc";
export const dashboard = router({
info: possiblyPublicProcedure.query(async ({ ctx }) => {
const activePollCount = await prisma.poll.count({
where: {
userId: ctx.user.id,
status: "live",
deleted: false, // TODO (Luke Vella) [2024-06-16]: We should add deleted/cancelled to the status enum
},
});
return { activePollCount };
}),
});

View file

@ -1,13 +1,26 @@
import dayjs from "dayjs";
import timezone from "dayjs/plugin/timezone";
import toArray from "dayjs/plugin/toArray";
import utc from "dayjs/plugin/utc";
import { mergeRouters, router } from "../trpc";
import { auth } from "./auth";
import { dashboard } from "./dashboard";
import { polls } from "./polls";
import { scheduledEvents } from "./scheduled-events";
import { user } from "./user";
dayjs.extend(toArray); // used for creating ics
dayjs.extend(timezone);
dayjs.extend(utc);
export const appRouter = mergeRouters(
router({
scheduledEvents,
auth,
polls,
user,
dashboard,
}),
);

View file

@ -1,10 +1,7 @@
import { prisma } from "@rallly/database";
import { PollStatus, prisma } from "@rallly/database";
import { TRPCError } from "@trpc/server";
import { waitUntil } from "@vercel/functions";
import dayjs from "dayjs";
import timezone from "dayjs/plugin/timezone";
import toArray from "dayjs/plugin/toArray";
import utc from "dayjs/plugin/utc";
import * as ics from "ics";
import { z } from "zod";
@ -19,10 +16,6 @@ import {
import { comments } from "./polls/comments";
import { participants } from "./polls/participants";
dayjs.extend(toArray);
dayjs.extend(timezone);
dayjs.extend(utc);
const getPollIdFromAdminUrlId = async (urlId: string) => {
const res = await prisma.poll.findUnique({
select: {
@ -43,6 +36,64 @@ const getPollIdFromAdminUrlId = async (urlId: string) => {
export const polls = router({
participants,
comments,
getCountByStatus: possiblyPublicProcedure.query(async ({ ctx }) => {
const res = await prisma.poll.groupBy({
by: ["status"],
where: {
userId: ctx.user.id,
deleted: false,
},
_count: {
status: true,
},
});
return res.reduce(
(acc, { status, _count }) => {
acc[status] = _count.status;
return acc;
},
{} 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,
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,
},
},
},
});
}),
// START LEGACY ROUTES
create: possiblyPublicProcedure
.input(

View file

@ -0,0 +1,33 @@
import { listScheduledEvents } from "@rallly/features/scheduled-events/api";
import dayjs from "dayjs";
import timezone from "dayjs/plugin/timezone";
import toArray from "dayjs/plugin/toArray";
import utc from "dayjs/plugin/utc";
import { z } from "zod";
import { possiblyPublicProcedure, router } from "../trpc";
dayjs.extend(toArray);
dayjs.extend(timezone);
dayjs.extend(utc);
export const scheduledEvents = router({
list: possiblyPublicProcedure
.input(
z.object({
period: z.enum(["upcoming", "past"]).default("upcoming"),
}),
)
.query(async ({ input, ctx }) => {
const events = await listScheduledEvents({
userId: ctx.user.id,
period: input.period,
});
return events.map(({ poll, ...event }) => ({
...event,
timeZone: poll?.timeZone || null,
participants: poll?.participants ?? [],
}));
}),
});