Skip to content

Commit

Permalink
enhance demo seed script
Browse files Browse the repository at this point in the history
color based event on calendar
separate dev traces on logflare
  • Loading branch information
ettorepuccetti committed Nov 13, 2023
1 parent 52885f6 commit a8964fe
Show file tree
Hide file tree
Showing 7 changed files with 304 additions and 28 deletions.
242 changes: 222 additions & 20 deletions prisma/seed-demo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PrismaClient } from "@prisma/client";
import dayjs from "dayjs";
import {
allEnglandClubName,
centerCourtName,
Expand All @@ -11,6 +12,12 @@ import {

const prisma = new PrismaClient();
async function main() {
//clean up
await prisma.reservation.deleteMany();
await prisma.court.deleteMany();
await prisma.user.deleteMany();

//clubs
const foroItalico = await prisma.club.upsert({
where: { name: foroItalicoName },
update: {},
Expand All @@ -20,26 +27,6 @@ async function main() {
"https://pub-f960339d8fe045c9a40b730d5aff9632.r2.dev/bnl-logo.png",
imageSrc:
"https://pub-f960339d8fe045c9a40b730d5aff9632.r2.dev/bnl-image.jpg",
courts: {
create: [
{
name: centralCourtName,
surface: "Clay",
indoor: false,
},
{
name: pietrangeliCourtName,
surface: "Clay",
indoor: false,
},
{
name: court1ForoName,
surface: "Clay",
indoor: false,
beginTime: "HOUR",
},
],
},
clubSettings: {
create: {
daysInFutureVisible: 7,
Expand Down Expand Up @@ -81,9 +68,224 @@ async function main() {
},
});

//courts
const centraleForo = await prisma.court.create({
data: {
clubId: foroItalico.id,
name: centralCourtName,
surface: "Clay",
indoor: false,
},
});

const pietrangeli = await prisma.court.create({
data: {
clubId: foroItalico.id,
name: pietrangeliCourtName,
surface: "Clay",
indoor: false,
},
});

const court1foro = await prisma.court.create({
data: {
clubId: foroItalico.id,
name: court1ForoName,
surface: "Clay",
indoor: false,
},
});

//users
const adminUser = await prisma.user.upsert({
where: { email: "adminuser@terrarossa.app" },
update: {},
create: {
email: "adminuser@terrarossa.app",
name: "admin",
role: "ADMIN",
image: "https://pub-f960339d8fe045c9a40b730d5aff9632.r2.dev/bnl-logo.png",
clubId: foroItalico.id,
accounts: {
create: {
type: "oauth",
provider: "auth0",
providerAccountId: "auth0|654df6e87a8e265bbdabde5b",
scope: "openid profile email",
},
},
},
});

const demoUser = await prisma.user.upsert({
where: { email: "demouser@terrarossa.app" },
update: {},
create: {
email: "demouser@terrarossa.app",
name: "demo user",
role: "USER",
image:
"https://pub-f960339d8fe045c9a40b730d5aff9632.r2.dev/user_demo_avatar.png",
accounts: {
create: {
type: "oauth",
provider: "auth0",
providerAccountId: "auth0|654df6952dd07ff7320aa86e",
scope: "openid profile email",
},
},
},
});

const federerUser = await prisma.user.create({
data: {
email: "r.federer@atp.com",
name: "Roger Federer",
role: "USER",
image:
"https://pub-f960339d8fe045c9a40b730d5aff9632.r2.dev/federer_avatar.jpg",
},
});

const marioRossi = await prisma.user.create({
data: {
email: "mariorossi@personal.it",
name: "Mario Rossi",
role: "USER",
image:
"https://pub-f960339d8fe045c9a40b730d5aff9632.r2.dev/mario_avatar.jpg",
},
});

const giovanni = await prisma.user.create({
data: {
email: "giovanni@personal.it",
name: "Giovanni",
role: "USER",
image:
"https://pub-f960339d8fe045c9a40b730d5aff9632.r2.dev/giovane_avatar.jpg",
},
});

//recurrent reservations (by admin only)
await prisma.recurrentReservation.create({
data: {
startDate: dayjs().startOf("day").toDate(),
endDate: dayjs().add(2, "weeks").startOf("day").toDate(),
reservations: {
createMany: {
data: [
{
overwriteName: "Mini Tennis U10",
courtId: pietrangeli.id,
userId: adminUser.id,
startTime: dayjs().hour(14).startOf("hour").toDate(),
endTime: dayjs().hour(16).startOf("hour").toDate(),
},
{
overwriteName: "Mario Rossi",
courtId: pietrangeli.id,
userId: adminUser.id,
startTime: dayjs()
.add(1, "week")
.hour(14)
.startOf("hour")
.toDate(),
endTime: dayjs().add(1, "week").hour(16).startOf("hour").toDate(),
},
],
},
},
},
});

await prisma.recurrentReservation.create({
data: {
startDate: dayjs().startOf("day").toDate(),
endDate: dayjs().add(2, "weeks").startOf("day").toDate(),
reservations: {
createMany: {
data: [
{
overwriteName: "Ago U16",
courtId: court1foro.id,
userId: adminUser.id,
startTime: dayjs().hour(14).startOf("hour").toDate(),
endTime: dayjs().hour(16).startOf("hour").toDate(),
},
{
overwriteName: "Ago U16",
courtId: court1foro.id,
userId: adminUser.id,
startTime: dayjs()
.add(1, "week")
.hour(14)
.startOf("hour")
.toDate(),
endTime: dayjs().add(1, "week").hour(16).startOf("hour").toDate(),
},
],
},
},
},
});

//reservations
await prisma.reservation.createMany({
data: [
{
courtId: pietrangeli.id,
userId: marioRossi.id,
startTime: dayjs().hour(17).startOf("hour").toDate(),
endTime: dayjs().hour(19).startOf("hour").toDate(),
},
{
courtId: centraleForo.id,
userId: demoUser.id,
startTime: dayjs().hour(10).startOf("hour").toDate(),
endTime: dayjs().hour(11).startOf("hour").toDate(),
},
{
courtId: centraleForo.id,
userId: federerUser.id,
startTime: dayjs().hour(12).startOf("hour").toDate(),
endTime: dayjs().hour(13).startOf("hour").toDate(),
},
{
courtId: court1foro.id,
userId: giovanni.id,
startTime: dayjs().hour(19).minute(30).startOf("minute").toDate(),
endTime: dayjs().hour(20).minute(30).startOf("minute").toDate(),
},
],
});

//admin reservations
await prisma.reservation.createMany({
data: [
{
courtId: court1foro.id,
userId: adminUser.id,
startTime: dayjs().hour(14).startOf("hour").toDate(),
endTime: dayjs().hour(17).startOf("hour").toDate(),
overwriteName: "S.A.T.",
},
{
courtId: centraleForo.id,
userId: adminUser.id,
startTime: dayjs().hour(15).startOf("hour").toDate(),
endTime: dayjs().hour(16).startOf("hour").toDate(),
overwriteName: "Prenotazione esterno",
},
],
});
console.log("Seeding database...");
console.log(allEnglandClub);
console.log(foroItalico);
console.log(centraleForo);
console.log(pietrangeli);
console.log(adminUser);
console.log(demoUser);
}

main()
Expand Down
8 changes: 7 additions & 1 deletion src/components/CalendarEventCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@ export default function CalendarEventCard({
}) {
return (
<Box
// need to disable property `className={"fc-event-main"}` because it prevent applying the color text
display={"flex"}
gap={1}
className={"fc-event-main"}
paddingLeft={"10px!important"}
alignItems={"center"}
data-test="calendar-event"
data-id={eventInfo.event.id}
//to replicate .fc-event-main
position={"relative"}
zIndex={2}
height={"100%"}
padding={"1px 1px 0px"}
>
{eventInfo.event.extendedProps.userImg && (
<Avatar
Expand Down
22 changes: 20 additions & 2 deletions src/components/FullCalendarWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import FullCalendar from "@fullcalendar/react";
import { type ResourceInput } from "@fullcalendar/resource";
import resourceTimeGridPlugin from "@fullcalendar/resource-timegrid";
import ScrollGrid from "@fullcalendar/scrollgrid";
import { Box } from "@mui/material";
import { Box, useTheme, type PaletteColor } from "@mui/material";
import { type inferRouterOutputs } from "@trpc/server";
import { useSession } from "next-auth/react";
import { useEffect, useRef, type RefObject } from "react";
Expand All @@ -35,6 +35,7 @@ interface FullCalendarWrapperProps {
}

export default function FullCalendarWrapper(props: FullCalendarWrapperProps) {
const theme = useTheme();
const { data: sessionData } = useSession();
const logger = useLogger({
component: "FullCalendarWrapper",
Expand All @@ -56,6 +57,21 @@ export default function FullCalendarWrapper(props: FullCalendarWrapperProps) {
}, []);

const reservationToEvent = (reservation: ReservationFromDb): EventInput => {
const getColors = (): PaletteColor => {
if (reservation.recurrentReservationId) {
return theme.palette.orange;
}
if (reservation.user?.role === "ADMIN") {
return theme.palette.yellow;
}
if (reservation.user?.id === sessionData?.user.id) {
return theme.palette.blue;
}
return theme.palette.lightBlue;
};

const paletteColor = getColors();

return {
id: reservation.id.toString(),
title: reservation.overwriteName
Expand All @@ -64,7 +80,9 @@ export default function FullCalendarWrapper(props: FullCalendarWrapperProps) {
start: reservation.startTime,
end: reservation.endTime,
resourceId: reservation.courtId,
// color: reservation.user?.role === "ADMIN" ? "red" : "green",
color: paletteColor.main,
borderColor: paletteColor.dark,
textColor: paletteColor.contrastText,
extendedProps: {
userId: reservation.user?.id,
userImg: reservation.user?.image,
Expand Down
2 changes: 1 addition & 1 deletion src/components/ReserveDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default function ReserveDialog() {

const [overwriteName, setOverwriteName] = useState<string | undefined>(
undefined,
); //cannot set to undefined because of controlled component
);
const resource = dateClick?.resource;
const logger = useLogger({ component: "ReserveDialog" });

Expand Down
4 changes: 2 additions & 2 deletions src/server/api/routers/reservation-mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ export const reservationMutationRouter = createTRPCRouter({
...input,
userId: ctx.session.user.id,
});
// delete all reservations that refer to the recurrent reservation.
// Is enough to explicit delete only the recurrent reservation, because Prisma cascade delete the normal reservations too.
// DELETE all reservations that refer to the recurrent reservation.
// Is enough to explicit delete only the recurrent reservation, because Prisma CASCADE delete the normal reservations too.
return ctx.prisma.recurrentReservation.delete({
where: {
id: input.recurrentReservationId,
Expand Down
Loading

0 comments on commit a8964fe

Please sign in to comment.