2022-05-31 14:46:06 +00:00
|
|
|
import { database } from "../../../utils/prismaClient";
|
2022-05-30 03:19:49 +00:00
|
|
|
import { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
import { ApiResult } from "../../../types/api";
|
2022-05-31 10:00:55 +00:00
|
|
|
import { Model, restInPeace } from "../../../utils/restInPeace";
|
2022-05-31 03:03:48 +00:00
|
|
|
import { default as cryptoRandomString} from "crypto-random-string";
|
2022-05-31 10:00:55 +00:00
|
|
|
import { handleInterrupts, Interrupt } from "../../../utils/interrupt";
|
2022-05-31 14:39:56 +00:00
|
|
|
import { authorizeUser } from "../../../utils/authorizeUser";
|
2022-05-31 10:00:55 +00:00
|
|
|
import { Event } from "@prisma/client";
|
2022-05-31 03:03:48 +00:00
|
|
|
|
2022-05-30 03:19:49 +00:00
|
|
|
|
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse<ApiResult<Event | Event[]>>) {
|
2022-05-31 03:03:48 +00:00
|
|
|
handleInterrupts(res, async () => {
|
2022-05-31 14:39:30 +00:00
|
|
|
const user = await authorizeUser(req)
|
2022-05-31 03:03:48 +00:00
|
|
|
|
2022-05-31 14:01:34 +00:00
|
|
|
const canEdit = async (_model: Model, obj?: Event) => {
|
2022-05-31 10:00:55 +00:00
|
|
|
if(obj && obj.creatorId !== user.id) {
|
|
|
|
throw new Interrupt(403, {error: "Only the creator can edit an event"})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-31 03:03:48 +00:00
|
|
|
const which = {
|
|
|
|
slug: req.query.slug
|
|
|
|
}
|
|
|
|
const update = {
|
|
|
|
name: req.body.name
|
|
|
|
}
|
|
|
|
|
2022-05-31 10:06:37 +00:00
|
|
|
await restInPeace(req, res, {
|
2022-05-31 14:46:06 +00:00
|
|
|
model: database.event,
|
2022-05-31 03:03:48 +00:00
|
|
|
retrieve: {which},
|
2022-05-31 13:44:58 +00:00
|
|
|
update: {which, update, before: canEdit},
|
|
|
|
destroy: {which, before: canEdit},
|
2022-05-31 03:03:48 +00:00
|
|
|
})
|
2022-05-30 03:19:49 +00:00
|
|
|
})
|
|
|
|
}
|