diff --git a/lib/validators/salmonRunRecord.ts b/lib/validators/salmonRunRecord.ts index 539639b84..e8a2d17ba 100644 --- a/lib/validators/salmonRunRecord.ts +++ b/lib/validators/salmonRunRecord.ts @@ -5,8 +5,28 @@ const urlRegex = /^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9( export const salmonRunRecordSchema = z.object({ rotationId: z.number(), // check on db level - goldenEggCount: z.number().min(0).max(300), - category: z.string(), // check on db level + goldenEggCount: z.number().min(10).max(300), + category: z.enum([ + "TOTAL", + "TOTAL_NO_NIGHT", + "PRINCESS", + "NT_NORMAL", + "HT_NORMAL", + "LT_NORMAL", + "NT_RUSH", + "HT_RUSH", + "NT_FOG", + "HT_FOG", + "LT_FOG", + "NT_GOLDIE", + "HT_GOLDIE", + "NT_GRILLERS", + "HT_GRILLERS", + "NT_MOTHERSHIP", + "HT_MOTHERSHIP", + "LT_MOTHERSHIP", + "LT_COHOCK", + ]), userIds: z.array(z.number()), links: z .string() diff --git a/pages/api/sr/records.ts b/pages/api/sr/records.ts new file mode 100644 index 000000000..a964a327e --- /dev/null +++ b/pages/api/sr/records.ts @@ -0,0 +1,70 @@ +import { ADMIN_DISCORD_ID, SALMON_RUN_ADMIN_DISCORD_IDS } from "lib/constants"; +import { getMySession } from "lib/getMySession"; +import { salmonRunRecordSchema } from "lib/validators/salmonRunRecord"; +import { NextApiRequest, NextApiResponse } from "next"; +import prisma from "prisma/client"; +import { getAllSalmonRunRecords } from "prisma/queries/getAllSalmonRunRecords"; + +const getHandler = async (req: NextApiRequest, res: NextApiResponse) => { + const user = await getMySession(req); + + const records = await getAllSalmonRunRecords(user?.id); + + res.status(200).json(records); +}; + +const postHandler = async (req: NextApiRequest, res: NextApiResponse) => { + const user = await getMySession(req); + + if (!user) return res.status(401).end(); + if ( + !SALMON_RUN_ADMIN_DISCORD_IDS.includes(user.discordId) && + user.discordId !== ADMIN_DISCORD_ID + ) { + return res.status(401).end(); + } + + const parsed = salmonRunRecordSchema.safeParse(req.body); + if (!parsed.success) { + return res.status(400).end(); + } + + await prisma.salmonRunRecord.create({ + data: { + goldenEggCount: parsed.data.goldenEggCount, + category: parsed.data.category, + links: parsed.data.links.trim().split("\n"), + approved: false, + // approved: SALMON_RUN_ADMIN_DISCORD_IDS.includes(user.discordId) || user.discordId === ADMIN_DISCORD_ID + submitter: { + connect: { id: user.id }, + }, + rotation: { + connect: { id: parsed.data.rotationId }, + }, + roster: { + connect: [{ id: user.id }].concat( + parsed.data.userIds.map((id) => ({ id })) + ), + }, + }, + }); + + res.status(200).end(); +}; + +const salmonRunRecordsHandler = async ( + req: NextApiRequest, + res: NextApiResponse +) => { + switch (req.method) { + case "GET": + await getHandler(req, res); + break; + case "POST": + await postHandler(req, res); + break; + } +}; + +export default salmonRunRecordsHandler; diff --git a/pages/sr/leaderboards/new.tsx b/pages/sr/leaderboards/new.tsx index 5b86a79c7..5e4d71c87 100644 --- a/pages/sr/leaderboards/new.tsx +++ b/pages/sr/leaderboards/new.tsx @@ -20,6 +20,8 @@ import Breadcrumbs from "components/common/Breadcrumbs"; import MyContainer from "components/common/MyContainer"; import UserSelector from "components/common/UserSelector"; import RotationSelector from "components/sr/RotationSelector"; +import { sendData } from "lib/postData"; +import useUser from "lib/useUser"; import { salmonRunRecordSchema } from "lib/validators/salmonRunRecord"; import Image from "next/image"; import { useState } from "react"; @@ -53,14 +55,28 @@ type FormData = z.infer; const AddRecordModal = () => { const { i18n } = useLingui(); const [sending, setSending] = useState(false); + const [loggedInUser] = useUser(); const { handleSubmit, errors, register, control, watch } = useForm({ resolver: zodResolver(salmonRunRecordSchema), }); const watchRotationId = watch("rotationId", undefined); - const onSubmit = async (data: FormData) => { - console.log("data", data); + const onSubmit = async (formData: FormData) => { + if (!loggedInUser) { + console.error("Unexpected no logged in user"); + return; + } + setSending(true); + const mutationData = { ...formData }; + + const success = await sendData("POST", "/api/sr/records", mutationData); + setSending(false); + if (!success) return; + + //mutate(`/api/users/${loggedInUser.id}/builds`); + + //redirect() }; return ( @@ -76,7 +92,7 @@ const AddRecordModal = () => { ( )} diff --git a/prisma/queries/getAllSalmonRunRecords.ts b/prisma/queries/getAllSalmonRunRecords.ts new file mode 100644 index 000000000..4070c82ff --- /dev/null +++ b/prisma/queries/getAllSalmonRunRecords.ts @@ -0,0 +1,15 @@ +import { Prisma } from "@prisma/client"; +import prisma from "prisma/client"; + +export type GetAllSalmonRunRecordsData = Prisma.PromiseReturnType< + typeof getAllSalmonRunRecords +>; + +export const getAllSalmonRunRecords = async (userId?: number) => + prisma.salmonRunRecord.findMany({ + where: { OR: [{ approved: true }, { submitterId: userId ?? -1 }] }, + include: { + rotation: true, + roster: true, + }, + });