2024-09-25 10:23:16 +00:00
|
|
|
import { z } from "zod";
|
|
|
|
import { storySchema } from "app/ui/forms/schemas";
|
2024-09-26 10:37:52 +00:00
|
|
|
import { Pub, Story } from "@prisma/client";
|
|
|
|
import { pubSchema } from "app/ui/forms/schemas";
|
2024-09-27 10:17:37 +00:00
|
|
|
import { StoryWithGenres } from "app/story/page";
|
2024-09-25 10:23:16 +00:00
|
|
|
|
|
|
|
//schemas
|
|
|
|
|
2024-09-26 10:37:52 +00:00
|
|
|
const storySchemaTrimmed = storySchema.omit({ genres: true })
|
|
|
|
const pubSchemaTrimmed = pubSchema.omit({ genres: true })
|
2024-09-25 10:23:16 +00:00
|
|
|
const genreSchema = z.object({ id: z.number() })
|
|
|
|
const genresSchema = z.array(genreSchema)
|
|
|
|
|
2024-09-30 12:44:47 +00:00
|
|
|
export async function prepStoryData(data: Story): Promise<{ title: string, word_count: number }> {
|
2024-09-26 10:37:52 +00:00
|
|
|
const storyData = structuredClone(data)
|
2024-09-25 10:23:16 +00:00
|
|
|
//throw an error if validation fails
|
2024-09-26 10:37:52 +00:00
|
|
|
storySchemaTrimmed.safeParse(storyData)
|
2024-09-25 10:23:16 +00:00
|
|
|
return storyData
|
|
|
|
}
|
|
|
|
|
2024-09-30 12:44:47 +00:00
|
|
|
export async function prepPubData(data: Pub): Promise<Pub> {
|
2024-09-26 10:37:52 +00:00
|
|
|
const pubData = structuredClone(data)
|
|
|
|
pubSchemaTrimmed.safeParse(pubData)
|
|
|
|
return pubData
|
|
|
|
}
|
|
|
|
|
2024-09-25 10:23:16 +00:00
|
|
|
export async function prepGenreData(data: number[]): Promise<{ id: number }[]> {
|
|
|
|
"use server"
|
|
|
|
|
|
|
|
//prepare data
|
|
|
|
const genresArray = data.map((e) => { return { id: e } })
|
|
|
|
|
|
|
|
//prepare schemas
|
|
|
|
|
|
|
|
//throws error if validation fails
|
|
|
|
genresSchema.safeParse(genresArray)
|
|
|
|
return genresArray
|
|
|
|
|
|
|
|
}
|