Use Prisma type utils

This commit is contained in:
Max Leiter 2022-12-17 17:30:17 -08:00
parent 69c482a165
commit 82aadd94f2
6 changed files with 20 additions and 29 deletions

View file

@ -40,7 +40,7 @@ const Post = ({
}) => {
const session = useSession()
const parsedPost = JSON.parse(stringifiedInitialPost || "{}")
const parsedPost = JSON.parse(stringifiedInitialPost || "{}") as PostWithFiles
const initialPost = parsedPost?.id ? parsedPost : null
const { setToast } = useToasts()
const router = useRouter()
@ -51,7 +51,7 @@ const Post = ({
const [expiresAt, setExpiresAt] = useState<Date>()
const defaultDocs: Document[] = initialPost
? initialPost.files?.map((doc: PostWithFiles["files"][0]) => ({
? initialPost.files?.map((doc) => ({
title: doc.title,
content: doc.content,
id: doc.id

View file

@ -69,12 +69,7 @@ const PostFiles = ({
}, [isAuthor, post.expiresAt, router])
if (isLoading) {
return (
<DocumentComponent
skeleton={true}
initialTab={"preview"}
/>
)
return <DocumentComponent skeleton={true} initialTab={"preview"} />
}
if (isProtected) {
@ -83,7 +78,7 @@ const PostFiles = ({
return (
<>
{post.files?.map(({ id, content, title, html }: File) => (
{post.files?.map(({ id, content, title, html }) => (
<DocumentComponent
skeleton={false}
key={id}

View file

@ -60,20 +60,20 @@ export type PostWithAuthor = Prisma.PostGetPayload<typeof postWithAuthor>
export type ServerPostWithFilesAndAuthor = Prisma.PostGetPayload<typeof postWithFilesAndAuthor>
export type PostWithFiles = Omit<ServerPostWithFiles, "files"> & {
files: Omit<ServerPostWithFiles["files"][number], "content" | "html"> & {
files: (Omit<ServerPostWithFiles["files"][number], "content" | "html"> & {
content: string
html: string
}[]
})[]
}
export type PostWithFilesAndAuthor = Omit<
ServerPostWithFilesAndAuthor,
"files"
> & {
files: Omit<ServerPostWithFilesAndAuthor["files"][number], "content" | "html"> & {
files: (Omit<ServerPostWithFilesAndAuthor["files"][number], "content" | "html"> & {
content: string
html: string
}[]
})[]
}
export const getFilesForPost = async (postId: string) => {
@ -247,7 +247,7 @@ export const getAllUsers = async () => {
}
})
return users as UserWithPosts[]
return users
}
export const searchPosts = async (
@ -267,7 +267,7 @@ export const searchPosts = async (
OR: [
{
title: {
search: query
search: query,
},
authorId: userId,
visibility: publicOnly ? "public" : undefined
@ -276,8 +276,8 @@ export const searchPosts = async (
files: {
some: {
content: {
in: Buffer.from(query)
}
in: [Buffer.from(query)]
},
}
},
visibility: publicOnly ? "public" : undefined

View file

@ -25,7 +25,7 @@ async function handlePost(req: NextApiRequest, res: NextApiResponse<any>) {
return res.status(401).json({ error: "Unauthorized" })
}
const files = req.body.files as Omit<File, 'content' | 'html'> & { content: string; html: string; }[]
const files = req.body.files as (Omit<File, 'content' | 'html'> & { content: string; html: string; })[]
const fileTitles = files.map((file) => file.title)
const missingTitles = fileTitles.filter((title) => title === "")
if (missingTitles.length > 0) {
@ -65,7 +65,6 @@ async function handlePost(req: NextApiRequest, res: NextApiResponse<any>) {
authorId: session.user.id,
files: {
create: files.map((file) => {
console.log(file)
return {
title: file.title,
content: Buffer.from(file.content, "utf-8"),
@ -85,7 +84,6 @@ async function handlePost(req: NextApiRequest, res: NextApiResponse<any>) {
}
})
.catch((error) => {
console.log(error)
return res.status(500).json(error)
})

View file

@ -1,6 +1,6 @@
import { withMethods } from "@lib/api-middleware/with-methods"
import { parseQueryParam } from "@lib/server/parse-query-param"
import { PostWithFiles, searchPosts } from "@lib/server/prisma"
import { searchPosts, ServerPostWithFiles } from "@lib/server/prisma"
import { NextApiRequest, NextApiResponse } from "next"
import { getSession } from "next-auth/react"
@ -10,21 +10,21 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const session = await getSession()
const query = parseQueryParam(q)
const user = parseQueryParam(userId)
if (!query) {
res.status(400).json({ error: "Invalid query" })
return
}
try {
let posts: PostWithFiles[]
if (session?.user.id === userId || session?.user.role === "admin") {
let posts: ServerPostWithFiles[]
if (session?.user.id === user || session?.user.role === "admin") {
posts = await searchPosts(query, {
userId: parseQueryParam(userId),
publicOnly: true
userId: user,
})
} else {
posts = await searchPosts(query, {
userId: parseQueryParam(userId),
userId: user,
publicOnly: true
})
}

View file

@ -1,5 +1,3 @@
// api/user/[id].ts
import { parseQueryParam } from "@lib/server/parse-query-param"
import { getUserById } from "@lib/server/prisma"
import { NextApiRequest, NextApiResponse } from "next"