Files
diplomarbeitsportal/src/collections/Papers.ts
2025-03-27 23:14:48 +01:00

164 lines
3.4 KiB
TypeScript

import type { CollectionConfig } from "payload";
export const Papers: CollectionConfig = {
slug: "papers",
labels: {
singular: "Diplomarbeit",
plural: "Diplomarbeiten",
},
access: {
create: ({ req: { user } }) => Boolean(user?.type === "admin"),
delete: ({ req: { user } }) => Boolean(user?.type === "admin"),
read: () => true,
update: async ({ req: { user, payload }, id }) => {
if (!user || !id) return false; // Explicitly handle missing ID
if (user?.type === "admin") return true;
const paper = await payload.findByID({
collection: "papers",
id,
depth: 1,
});
if (!paper) return false;
if(paper.published) return false;
return paper.authors.some((author: any) => author.name === user.name);
},
},
admin: {
useAsTitle: "title",
},
fields: [
{
name: "published",
type: "checkbox",
defaultValue: false,
label: "Veröffentlicht (Auf der Website sichtbar)",
access: {
update: ({ req: { user } }) => Boolean(user?.type === "admin"),
}
},
{
name: "title",
type: "text",
unique: true,
},
{
name: "department",
type: "select",
options: [
{ label: "WI", value: "WI" },
{ label: "CI", value: "CI" },
{ label: "MD", value: "MD" },
],
},
{
name: "year",
type: "text",
},
{
name: "issue",
label: "Problemstellung",
type: "textarea",
},
{
name: "goal",
label: "Zielsetzung",
type: "textarea",
},
{
name: "result",
label: "Ergebnis",
type: "textarea",
},
{
name: "technologies",
type: "array",
fields: [
{
name: "technology",
type: "relationship",
relationTo: "technologies",
required: true,
},
{
name: "description",
type: "text",
admin: {
placeholder: "... wurde für das Frontend verwendet",
},
},
],
},
{
name: "prototype",
type: "group",
fields: [
{
name: "image",
type: "upload",
relationTo: "media",
},
{
name: "description",
type: "text",
},
],
},
{
name: "mentor",
type: "text",
label: "Betreuer",
},
{
name: "authors",
type: "array",
label: "Projektmitglieder",
fields: [
{
name: "image",
type: "upload",
relationTo: "media",
},
{
name: "name",
type: "text",
required: true,
},
{
name: "position",
type: "select",
required: true,
options: [
{
label: "Projektleiter (PL)",
value: "leader",
},
{
label: "Projektmitarbeiter (PM)",
value: "member",
},
],
},
{
name: "description",
type: "text",
},
],
validate: (authors) => {
// @ts-ignore
const leaders = authors.filter(
(author) => author.position === "leader",
);
if (leaders.length > 1) {
return "Only one author can be the project leader.";
}
return true;
},
},
],
};