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";
|
2022-06-11 03:08:49 +00:00
|
|
|
import { Event, Prisma } from "@prisma/client";
|
|
|
|
import { festaAPI } from "../../../utils/api";
|
|
|
|
import { festaNoConfig } from "../../../utils/api/configurator";
|
|
|
|
import { festaBearerAuthOptional, FestaToken } from "../../../utils/api/authenticator";
|
|
|
|
import { festaJsonSchemaBody } from "../../../utils/api/bodyValidator";
|
|
|
|
import { festaRESTSpecific } from "../../../utils/api/executor";
|
|
|
|
import { festaJsonSchemaQuery } from "../../../utils/api/queryValidator";
|
|
|
|
import { Response } from "../../../utils/api/throwables";
|
2022-05-31 03:03:48 +00:00
|
|
|
|
2022-05-30 03:19:49 +00:00
|
|
|
|
2022-06-11 03:08:49 +00:00
|
|
|
type Config = {}
|
2022-05-31 03:03:48 +00:00
|
|
|
|
2022-06-11 03:08:49 +00:00
|
|
|
type Auth = FestaToken | undefined
|
|
|
|
|
|
|
|
type Query = {
|
|
|
|
slug: string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Body = {
|
|
|
|
name: string,
|
|
|
|
postcard?: string,
|
|
|
|
description: string,
|
|
|
|
startingAt?: string,
|
|
|
|
endingAt?: string,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
await festaAPI(req, res, {
|
|
|
|
|
|
|
|
configurator: festaNoConfig,
|
|
|
|
|
|
|
|
authenticator: festaBearerAuthOptional,
|
|
|
|
|
|
|
|
queryValidator: festaJsonSchemaQuery<Query>({
|
|
|
|
type: "object",
|
|
|
|
properties: {
|
|
|
|
slug: { type: "string" }
|
|
|
|
},
|
|
|
|
required: [
|
|
|
|
"slug",
|
|
|
|
]
|
|
|
|
}),
|
|
|
|
|
|
|
|
bodyValidator: festaJsonSchemaBody<Body>({
|
|
|
|
type: "object",
|
|
|
|
properties: {
|
|
|
|
name: { type: "string" },
|
|
|
|
postcard: { type: "string", nullable: true },
|
|
|
|
description: { type: "string" },
|
|
|
|
startingAt: { type: "string", nullable: true },
|
|
|
|
endingAt: { type: "string", nullable: true },
|
|
|
|
},
|
|
|
|
required: [
|
|
|
|
"slug",
|
|
|
|
"name",
|
|
|
|
"description",
|
|
|
|
]
|
|
|
|
}),
|
|
|
|
|
|
|
|
executor: festaRESTSpecific<Config, Auth, Query, Body, Event, Prisma.EventDelegate<any>, Prisma.EventFindUniqueArgs, Prisma.EventUpdateArgs>({
|
|
|
|
delegate: database.event,
|
|
|
|
|
|
|
|
getFindArgs: async ({ query }) => {
|
|
|
|
return {
|
|
|
|
where: {
|
|
|
|
slug: query.slug,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
beforeUpdate: async ({ auth }, item) => {
|
|
|
|
if (!auth) {
|
|
|
|
throw Response.error({ status: 403, message: "Authentication is required to edit events" })
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item && item.creatorId !== auth.user.id) {
|
|
|
|
throw Response.error({ status: 403, message: "Only the creator can edit an event" })
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
getUpdateArgs: async ({ body, query }) => {
|
|
|
|
return {
|
|
|
|
where: {
|
|
|
|
slug: query.slug,
|
|
|
|
},
|
|
|
|
data: body!,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
beforeDestruction: async ({ auth }, item) => {
|
|
|
|
if (!auth) {
|
|
|
|
throw Response.error({ status: 403, message: "Authentication is required to destroy events" })
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item && item.creatorId !== auth.user.id) {
|
|
|
|
throw Response.error({ status: 403, message: "Only the creator can destroy an event" })
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
getDestroyArgs: async ({ query }) => {
|
|
|
|
return {
|
|
|
|
where: {
|
|
|
|
slug: query.slug,
|
|
|
|
}
|
|
|
|
}
|
2022-05-31 10:00:55 +00:00
|
|
|
}
|
2022-06-11 03:08:49 +00:00
|
|
|
}),
|
2022-05-30 03:19:49 +00:00
|
|
|
})
|
2022-06-11 03:08:49 +00:00
|
|
|
}
|