From 47dda68f077d0abe3e1d157dd0334bfed5c2b35d Mon Sep 17 00:00:00 2001 From: Dominik Natter Date: Thu, 27 Mar 2025 14:35:44 +0100 Subject: [PATCH] add: working JSON data insert --- src/app/(payload)/admin/importMap.js | 46 ---------------- src/app/(payload)/api/insert-papers/route.ts | 40 ++++++++++++++ src/collections/Media.ts | 4 +- src/collections/Papers.ts | 55 ++++++++++---------- src/collections/Users.ts | 40 ++++++++++++-- src/payload-types.ts | 42 ++++++++------- 6 files changed, 131 insertions(+), 96 deletions(-) create mode 100644 src/app/(payload)/api/insert-papers/route.ts diff --git a/src/app/(payload)/admin/importMap.js b/src/app/(payload)/admin/importMap.js index 84d96b1..07fb7c2 100644 --- a/src/app/(payload)/admin/importMap.js +++ b/src/app/(payload)/admin/importMap.js @@ -1,51 +1,5 @@ -import { RscEntryLexicalCell as RscEntryLexicalCell_44fe37237e0ebf4470c9990d8cb7b07e } from '@payloadcms/richtext-lexical/rsc' -import { RscEntryLexicalField as RscEntryLexicalField_44fe37237e0ebf4470c9990d8cb7b07e } from '@payloadcms/richtext-lexical/rsc' -import { InlineToolbarFeatureClient as InlineToolbarFeatureClient_e70f5e05f09f93e00b997edb1ef0c864 } from '@payloadcms/richtext-lexical/client' -import { FixedToolbarFeatureClient as FixedToolbarFeatureClient_e70f5e05f09f93e00b997edb1ef0c864 } from '@payloadcms/richtext-lexical/client' -import { BoldFeatureClient as BoldFeatureClient_e70f5e05f09f93e00b997edb1ef0c864 } from '@payloadcms/richtext-lexical/client' -import { ItalicFeatureClient as ItalicFeatureClient_e70f5e05f09f93e00b997edb1ef0c864 } from '@payloadcms/richtext-lexical/client' -import { UnderlineFeatureClient as UnderlineFeatureClient_e70f5e05f09f93e00b997edb1ef0c864 } from '@payloadcms/richtext-lexical/client' -import { StrikethroughFeatureClient as StrikethroughFeatureClient_e70f5e05f09f93e00b997edb1ef0c864 } from '@payloadcms/richtext-lexical/client' -import { SubscriptFeatureClient as SubscriptFeatureClient_e70f5e05f09f93e00b997edb1ef0c864 } from '@payloadcms/richtext-lexical/client' -import { SuperscriptFeatureClient as SuperscriptFeatureClient_e70f5e05f09f93e00b997edb1ef0c864 } from '@payloadcms/richtext-lexical/client' -import { InlineCodeFeatureClient as InlineCodeFeatureClient_e70f5e05f09f93e00b997edb1ef0c864 } from '@payloadcms/richtext-lexical/client' -import { ParagraphFeatureClient as ParagraphFeatureClient_e70f5e05f09f93e00b997edb1ef0c864 } from '@payloadcms/richtext-lexical/client' -import { HeadingFeatureClient as HeadingFeatureClient_e70f5e05f09f93e00b997edb1ef0c864 } from '@payloadcms/richtext-lexical/client' -import { AlignFeatureClient as AlignFeatureClient_e70f5e05f09f93e00b997edb1ef0c864 } from '@payloadcms/richtext-lexical/client' -import { IndentFeatureClient as IndentFeatureClient_e70f5e05f09f93e00b997edb1ef0c864 } from '@payloadcms/richtext-lexical/client' -import { UnorderedListFeatureClient as UnorderedListFeatureClient_e70f5e05f09f93e00b997edb1ef0c864 } from '@payloadcms/richtext-lexical/client' -import { OrderedListFeatureClient as OrderedListFeatureClient_e70f5e05f09f93e00b997edb1ef0c864 } from '@payloadcms/richtext-lexical/client' -import { ChecklistFeatureClient as ChecklistFeatureClient_e70f5e05f09f93e00b997edb1ef0c864 } from '@payloadcms/richtext-lexical/client' -import { LinkFeatureClient as LinkFeatureClient_e70f5e05f09f93e00b997edb1ef0c864 } from '@payloadcms/richtext-lexical/client' -import { RelationshipFeatureClient as RelationshipFeatureClient_e70f5e05f09f93e00b997edb1ef0c864 } from '@payloadcms/richtext-lexical/client' -import { BlockquoteFeatureClient as BlockquoteFeatureClient_e70f5e05f09f93e00b997edb1ef0c864 } from '@payloadcms/richtext-lexical/client' -import { UploadFeatureClient as UploadFeatureClient_e70f5e05f09f93e00b997edb1ef0c864 } from '@payloadcms/richtext-lexical/client' -import { HorizontalRuleFeatureClient as HorizontalRuleFeatureClient_e70f5e05f09f93e00b997edb1ef0c864 } from '@payloadcms/richtext-lexical/client' import { SignInWithAuthjsButton as SignInWithAuthjsButton_06d0cb594d8f6ba2ac35015f930c882e } from 'payload-authjs/components' export const importMap = { - "@payloadcms/richtext-lexical/rsc#RscEntryLexicalCell": RscEntryLexicalCell_44fe37237e0ebf4470c9990d8cb7b07e, - "@payloadcms/richtext-lexical/rsc#RscEntryLexicalField": RscEntryLexicalField_44fe37237e0ebf4470c9990d8cb7b07e, - "@payloadcms/richtext-lexical/client#InlineToolbarFeatureClient": InlineToolbarFeatureClient_e70f5e05f09f93e00b997edb1ef0c864, - "@payloadcms/richtext-lexical/client#FixedToolbarFeatureClient": FixedToolbarFeatureClient_e70f5e05f09f93e00b997edb1ef0c864, - "@payloadcms/richtext-lexical/client#BoldFeatureClient": BoldFeatureClient_e70f5e05f09f93e00b997edb1ef0c864, - "@payloadcms/richtext-lexical/client#ItalicFeatureClient": ItalicFeatureClient_e70f5e05f09f93e00b997edb1ef0c864, - "@payloadcms/richtext-lexical/client#UnderlineFeatureClient": UnderlineFeatureClient_e70f5e05f09f93e00b997edb1ef0c864, - "@payloadcms/richtext-lexical/client#StrikethroughFeatureClient": StrikethroughFeatureClient_e70f5e05f09f93e00b997edb1ef0c864, - "@payloadcms/richtext-lexical/client#SubscriptFeatureClient": SubscriptFeatureClient_e70f5e05f09f93e00b997edb1ef0c864, - "@payloadcms/richtext-lexical/client#SuperscriptFeatureClient": SuperscriptFeatureClient_e70f5e05f09f93e00b997edb1ef0c864, - "@payloadcms/richtext-lexical/client#InlineCodeFeatureClient": InlineCodeFeatureClient_e70f5e05f09f93e00b997edb1ef0c864, - "@payloadcms/richtext-lexical/client#ParagraphFeatureClient": ParagraphFeatureClient_e70f5e05f09f93e00b997edb1ef0c864, - "@payloadcms/richtext-lexical/client#HeadingFeatureClient": HeadingFeatureClient_e70f5e05f09f93e00b997edb1ef0c864, - "@payloadcms/richtext-lexical/client#AlignFeatureClient": AlignFeatureClient_e70f5e05f09f93e00b997edb1ef0c864, - "@payloadcms/richtext-lexical/client#IndentFeatureClient": IndentFeatureClient_e70f5e05f09f93e00b997edb1ef0c864, - "@payloadcms/richtext-lexical/client#UnorderedListFeatureClient": UnorderedListFeatureClient_e70f5e05f09f93e00b997edb1ef0c864, - "@payloadcms/richtext-lexical/client#OrderedListFeatureClient": OrderedListFeatureClient_e70f5e05f09f93e00b997edb1ef0c864, - "@payloadcms/richtext-lexical/client#ChecklistFeatureClient": ChecklistFeatureClient_e70f5e05f09f93e00b997edb1ef0c864, - "@payloadcms/richtext-lexical/client#LinkFeatureClient": LinkFeatureClient_e70f5e05f09f93e00b997edb1ef0c864, - "@payloadcms/richtext-lexical/client#RelationshipFeatureClient": RelationshipFeatureClient_e70f5e05f09f93e00b997edb1ef0c864, - "@payloadcms/richtext-lexical/client#BlockquoteFeatureClient": BlockquoteFeatureClient_e70f5e05f09f93e00b997edb1ef0c864, - "@payloadcms/richtext-lexical/client#UploadFeatureClient": UploadFeatureClient_e70f5e05f09f93e00b997edb1ef0c864, - "@payloadcms/richtext-lexical/client#HorizontalRuleFeatureClient": HorizontalRuleFeatureClient_e70f5e05f09f93e00b997edb1ef0c864, "payload-authjs/components#SignInWithAuthjsButton": SignInWithAuthjsButton_06d0cb594d8f6ba2ac35015f930c882e } diff --git a/src/app/(payload)/api/insert-papers/route.ts b/src/app/(payload)/api/insert-papers/route.ts new file mode 100644 index 0000000..8cba21e --- /dev/null +++ b/src/app/(payload)/api/insert-papers/route.ts @@ -0,0 +1,40 @@ +import { NextRequest, NextResponse } from 'next/server'; +import { getPayload } from 'payload'; +import config from '@payload-config'; + +const payload = await getPayload({ config }); + +export async function POST(req: NextRequest) { + try { + const papersJson = await req.json(); + + const mapPaperData = (paper) => ({ + title: paper.title, + department: paper.Abteilung.includes('WI') ? 'WI' : paper.Abteilung.includes('CI') ? 'CI' : 'MD', + year: paper.id.toString().slice(0, 4), + issue: paper.Ausgangslage, + goal: paper.Zielsetzung, + result: paper["Geplantes Ergebnis der Prüfungskandidatin/des Prüfungskandidaten"], + mentor: paper["Betreuer/innen"].replace("Hauptverantwortlich: ", ""), + authors: paper.Projektteam.split(",").map((author) => { + const isLeader = author.includes("(Hauptverantwortlich)"); + return { + name: author.replace("(Hauptverantwortlich)", "").trim(), + position: isLeader ? "leader" : "member", + }; + }).filter(author => author.name.length > 0), + }); + + for (const paper of papersJson) { + await payload.create({ + collection: "papers", + data: mapPaperData(paper), + }); + } + + return NextResponse.json({ message: "Papers successfully inserted!" }); + } catch (err) { + console.error("Insertion error:", err); + return NextResponse.json({ error: "Failed to insert papers." }, { status: 500 }); + } +} diff --git a/src/collections/Media.ts b/src/collections/Media.ts index 89972a2..5de7722 100644 --- a/src/collections/Media.ts +++ b/src/collections/Media.ts @@ -15,7 +15,9 @@ const dirname = path.dirname(filename) export const Media: CollectionConfig = { slug: 'media', access: { - + create: ({ req: { user } }) => Boolean(user), + update: ({ req: { user } }) => Boolean(user && user.role === 'admin'), + delete: ({ req: { user } }) => Boolean(user && user.role === 'admin'), }, fields: [ { diff --git a/src/collections/Papers.ts b/src/collections/Papers.ts index a6e635a..2678dcb 100644 --- a/src/collections/Papers.ts +++ b/src/collections/Papers.ts @@ -1,8 +1,5 @@ import type { CollectionConfig } from "payload"; - - - export const Papers: CollectionConfig = { slug: "papers", labels: { @@ -18,49 +15,61 @@ export const Papers: CollectionConfig = { if (!user || !id) return false; // Explicitly handle missing ID const paper = await payload.findByID({ - collection: 'papers', + collection: "papers", id, depth: 1, }); if (!paper) return false; - return paper.authors.some((author: any) => author.user.id === user.id); + return paper.authors.some((author: any) => author.user.name === user.name); }, - - }, admin: { useAsTitle: "title", }, fields: [ + { + name: "published", + type: "checkbox", + defaultValue: false, + label: "Veröffentlicht", + access: { + update: ({ req: { user } }) => Boolean(user?.type === "admin"), + } + }, { name: "title", type: "text", - required: true, + unique: true, + }, + { + name: "department", + type: "select", + options: [ + { label: "WI", value: "WI" }, + { label: "CI", value: "CI" }, + { label: "MD", value: "MD" }, + ], }, { name: "year", type: "text", - required: true, }, { name: "issue", label: "Problemstellung", type: "textarea", - required: true, }, { name: "goal", label: "Zielsetzung", type: "textarea", - required: true, }, { name: "result", label: "Ergebnis", type: "textarea", - required: true, }, { name: "technologies", @@ -75,7 +84,6 @@ export const Papers: CollectionConfig = { { name: "description", type: "text", - required: true, admin: { placeholder: "... wurde für das Frontend verwendet", }, @@ -90,32 +98,26 @@ export const Papers: CollectionConfig = { name: "image", type: "upload", relationTo: "media", - required: true, }, { name: "description", type: "text", - required: true, }, ], }, - + { + name: "mentor", + type: "text", + label: "Betreuer", + }, { name: "authors", type: "array", label: "Projektmitglieder", - required: true, fields: [ { - name: "profilePicture", - type: "upload", - relationTo: "media", - required: true, - }, - { - name: "user", - type: "relationship", - relationTo: "users", + name: "name", + type: "text", required: true, }, { @@ -136,7 +138,6 @@ export const Papers: CollectionConfig = { { name: "description", type: "text", - required: true, }, ], validate: (authors) => { diff --git a/src/collections/Users.ts b/src/collections/Users.ts index 1bb6011..dd5ce06 100644 --- a/src/collections/Users.ts +++ b/src/collections/Users.ts @@ -1,20 +1,52 @@ -import type { CollectionConfig } from 'payload' +import type { CollectionConfig } from 'payload'; +import path from 'path'; +import { fileURLToPath } from 'url'; + +const filename = fileURLToPath(import.meta.url); +const dirname = path.dirname(filename); export const Users: CollectionConfig = { slug: 'users', admin: { useAsTitle: 'email', }, + access: { + create: ({ req: { user } }) => Boolean(user && user.type === 'admin'), + update: ({ req: { user } }) => { + if (!user) return false; + if(user.type === 'admin') return true; + return false; + }, + delete: ({ req: { user } }) => Boolean(user && user.type === 'admin'), + }, fields: [ { - name: 'type', + name: 'type', type: 'select', options: [ { label: 'Admin', value: 'admin' }, { label: 'Schüler', value: 'student' }, ], - } + defaultValue: 'student', + }, + { + name: 'email', + type: 'email', + required: true, + unique: true, + + }, + { + name: 'emailVerified', + type: 'date', + required: false, + }, + { + name: 'image', + type: 'text', + required: false, + }, // Email added by default // Add more fields as needed ], -} +}; diff --git a/src/payload-types.ts b/src/payload-types.ts index 3ebb1b3..6462fe4 100644 --- a/src/payload-types.ts +++ b/src/payload-types.ts @@ -123,29 +123,33 @@ export interface UserAuthOperations { */ export interface Paper { id: number; - title: string; - year: string; - issue: string; - goal: string; - result: string; + published?: boolean | null; + title?: string | null; + department?: ('WI' | 'CI' | 'MD') | null; + year?: string | null; + issue?: string | null; + goal?: string | null; + result?: string | null; technologies?: | { technology: number | Technology; - description: string; + description?: string | null; id?: string | null; }[] | null; - prototype: { - image: number | Media; - description: string; + prototype?: { + image?: (number | null) | Media; + description?: string | null; }; - authors: { - profilePicture: number | Media; - user: string | User; - position: 'leader' | 'member'; - description: string; - id?: string | null; - }[]; + mentor?: string | null; + authors?: + | { + name: string; + position: 'leader' | 'member'; + description?: string | null; + id?: string | null; + }[] + | null; updatedAt: string; createdAt: string; } @@ -356,7 +360,9 @@ export interface PayloadMigration { * via the `definition` "papers_select". */ export interface PapersSelect { + published?: T; title?: T; + department?: T; year?: T; issue?: T; goal?: T; @@ -374,11 +380,11 @@ export interface PapersSelect { image?: T; description?: T; }; + mentor?: T; authors?: | T | { - profilePicture?: T; - user?: T; + name?: T; position?: T; description?: T; id?: T;