Add scheduled events schema (#1679)

This commit is contained in:
Luke Vella 2025-04-22 14:28:15 +01:00 committed by GitHub
parent 22f32f9314
commit 56bd684c55
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
35 changed files with 1412 additions and 659 deletions

View file

@ -0,0 +1,80 @@
import { prisma } from "@rallly/database";
import dayjs from "dayjs";
import timezone from "dayjs/plugin/timezone";
import utc from "dayjs/plugin/utc";
import type { Status } from "../schema";
dayjs.extend(utc);
dayjs.extend(timezone);
const mapStatus = {
upcoming: "confirmed",
unconfirmed: "unconfirmed",
past: undefined,
canceled: "canceled",
} as const;
export async function getScheduledEvents({
userId,
status,
search,
}: {
userId: string;
status: Status;
search?: string;
}) {
const now = new Date();
const rawEvents = await prisma.scheduledEvent.findMany({
where: {
userId,
deletedAt: null,
...(status != "past" && { start: { gte: now } }),
...(status === "past" && { start: { lt: now } }),
...(search && { title: { contains: search, mode: "insensitive" } }),
status: mapStatus[status],
},
orderBy: {
start: status === "past" ? "desc" : "asc",
},
select: {
id: true,
title: true,
description: true,
location: true,
start: true,
end: true,
allDay: true,
timeZone: true,
status: true,
invites: {
select: {
id: true,
inviteeName: true,
user: {
select: {
image: true,
},
},
},
},
},
});
const events = rawEvents.map((event) => ({
...event,
status:
event.status === "confirmed"
? // If the event is confirmed, it's either past or upcoming
((event.start < now ? "past" : "upcoming") as Status)
: event.status,
invites: event.invites.map((invite) => ({
id: invite.id,
inviteeName: invite.inviteeName,
inviteeImage: invite.user?.image ?? undefined,
})),
}));
return events;
}