submitting records works

This commit is contained in:
Kalle (Sendou) 2020-12-19 15:00:24 +02:00
parent 2cb22fb5d8
commit 763f6c062c
4 changed files with 126 additions and 5 deletions

View File

@ -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()

70
pages/api/sr/records.ts Normal file
View File

@ -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;

View File

@ -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<typeof salmonRunRecordSchema>;
const AddRecordModal = () => {
const { i18n } = useLingui();
const [sending, setSending] = useState(false);
const [loggedInUser] = useUser();
const { handleSubmit, errors, register, control, watch } = useForm<FormData>({
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 = () => {
<Controller
name="rotationId"
control={control}
defaultValue={undefined}
defaultValue={null}
render={({ value, onChange }) => (
<RotationSelector rotationId={value} setRotationId={onChange} />
)}

View File

@ -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,
},
});