2022-11-11 22:17:44 -05:00
|
|
|
declare global {
|
|
|
|
var prisma: PrismaClient | undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
import config from "@lib/config"
|
2022-11-14 02:02:31 -05:00
|
|
|
import { Post, PrismaClient, File, User, Prisma } from "@prisma/client"
|
2022-11-14 04:28:40 -05:00
|
|
|
export type { User, File, Post } from "@prisma/client"
|
2022-11-11 22:17:44 -05:00
|
|
|
|
2022-11-12 19:06:23 -05:00
|
|
|
export const prisma =
|
|
|
|
global.prisma ||
|
|
|
|
new PrismaClient({
|
|
|
|
log: ["query"]
|
|
|
|
})
|
2022-11-11 22:17:44 -05:00
|
|
|
|
2022-11-14 21:49:58 -05:00
|
|
|
if (config.enable_admin) {
|
|
|
|
// a prisma middleware for capturing the first user and making them an admin
|
|
|
|
prisma.$use(async (params, next) => {
|
|
|
|
const result = await next(params)
|
|
|
|
if (params.model === "User" && params.action === "create") {
|
|
|
|
const users = await prisma.user.findMany()
|
|
|
|
if (users.length === 1) {
|
|
|
|
await prisma.user.update({
|
|
|
|
where: { id: users[0].id },
|
|
|
|
data: { role: "admin" }
|
|
|
|
})
|
|
|
|
}
|
2022-11-14 04:28:40 -05:00
|
|
|
}
|
2022-11-14 21:49:58 -05:00
|
|
|
return result
|
|
|
|
})
|
|
|
|
}
|
2022-11-14 04:28:40 -05:00
|
|
|
|
2022-11-14 02:02:31 -05:00
|
|
|
// prisma.$use(async (params, next) => {
|
|
|
|
// const result = await next(params)
|
|
|
|
// return updateDates(result)
|
|
|
|
// })
|
|
|
|
|
2022-11-12 19:06:23 -05:00
|
|
|
if (process.env.NODE_ENV !== "production") global.prisma = prisma
|
2022-11-11 22:17:44 -05:00
|
|
|
|
|
|
|
export type PostWithFiles = Post & {
|
|
|
|
files: File[]
|
|
|
|
}
|
|
|
|
|
2022-11-14 04:28:40 -05:00
|
|
|
export type PostWithFilesAndAuthor = PostWithFiles & {
|
|
|
|
author: User
|
|
|
|
}
|
|
|
|
|
2022-11-11 22:17:44 -05:00
|
|
|
export const getFilesForPost = async (postId: string) => {
|
|
|
|
const files = await prisma.file.findMany({
|
|
|
|
where: {
|
|
|
|
postId
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return files
|
|
|
|
}
|
|
|
|
|
2022-11-12 02:59:33 -05:00
|
|
|
export async function getFilesByPost(postId: string) {
|
|
|
|
const files = await prisma.file.findMany({
|
|
|
|
where: {
|
|
|
|
postId
|
|
|
|
}
|
|
|
|
})
|
2022-11-11 22:17:44 -05:00
|
|
|
|
2022-11-12 02:59:33 -05:00
|
|
|
return files
|
2022-11-11 22:17:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function getPostsByUser(userId: string): Promise<Post[]>
|
|
|
|
export async function getPostsByUser(
|
|
|
|
userId: string,
|
|
|
|
includeFiles: true
|
|
|
|
): Promise<PostWithFiles[]>
|
|
|
|
export async function getPostsByUser(userId: User["id"], withFiles?: boolean) {
|
|
|
|
const posts = await prisma.post.findMany({
|
|
|
|
where: {
|
|
|
|
authorId: userId
|
2022-11-12 02:59:33 -05:00
|
|
|
},
|
|
|
|
include: {
|
|
|
|
files: withFiles
|
2022-11-12 21:39:03 -05:00
|
|
|
},
|
|
|
|
orderBy: {
|
|
|
|
createdAt: "desc"
|
2022-11-11 22:17:44 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return posts
|
|
|
|
}
|
|
|
|
|
|
|
|
export const getUserById = async (userId: User["id"]) => {
|
|
|
|
const user = await prisma.user.findUnique({
|
|
|
|
where: {
|
|
|
|
id: userId
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
email: true,
|
|
|
|
// displayName: true,
|
|
|
|
role: true,
|
2022-11-14 21:49:58 -05:00
|
|
|
displayName: true
|
2022-11-11 22:17:44 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return user
|
|
|
|
}
|
|
|
|
|
|
|
|
export const isUserAdmin = async (userId: User["id"]) => {
|
|
|
|
const user = await prisma.user.findUnique({
|
|
|
|
where: {
|
|
|
|
id: userId
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
role: true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return user?.role?.toLowerCase() === "admin"
|
|
|
|
}
|
|
|
|
|
2022-11-12 02:59:33 -05:00
|
|
|
export const createUser = async (
|
|
|
|
username: string,
|
|
|
|
password: string,
|
|
|
|
serverPassword?: string
|
|
|
|
) => {
|
2022-11-11 22:17:44 -05:00
|
|
|
if (!username || !password) {
|
|
|
|
throw new Error("Missing param")
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
config.registration_password &&
|
|
|
|
serverPassword !== config.registration_password
|
|
|
|
) {
|
|
|
|
console.log("Registration password mismatch")
|
|
|
|
throw new Error("Wrong registration password")
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
// user,
|
|
|
|
// token
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-14 04:28:40 -05:00
|
|
|
type GetPostByIdOptions = {
|
|
|
|
withFiles: boolean
|
|
|
|
withAuthor: boolean
|
|
|
|
}
|
|
|
|
|
|
|
|
export const getPostById = async (
|
|
|
|
postId: Post["id"],
|
|
|
|
options?: GetPostByIdOptions
|
2022-12-04 04:31:51 -05:00
|
|
|
): Promise<Post | PostWithFiles | PostWithFilesAndAuthor | null> => {
|
2022-11-11 22:17:44 -05:00
|
|
|
const post = await prisma.post.findUnique({
|
|
|
|
where: {
|
|
|
|
id: postId
|
2022-11-12 02:59:33 -05:00
|
|
|
},
|
|
|
|
include: {
|
2022-11-14 04:28:40 -05:00
|
|
|
files: options?.withFiles,
|
|
|
|
author: options?.withAuthor
|
|
|
|
? {
|
|
|
|
select: {
|
|
|
|
id: true,
|
2022-11-14 21:49:58 -05:00
|
|
|
displayName: true
|
2022-11-14 04:28:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
: false
|
2022-11-11 22:17:44 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-12-04 04:31:51 -05:00
|
|
|
return post
|
2022-11-11 22:17:44 -05:00
|
|
|
}
|
2022-11-12 21:39:03 -05:00
|
|
|
|
2022-11-14 02:02:31 -05:00
|
|
|
export const getAllPosts = async ({
|
|
|
|
withFiles = false,
|
2022-12-04 04:31:51 -05:00
|
|
|
withAuthor = false,
|
2022-11-14 02:02:31 -05:00
|
|
|
take = 100,
|
|
|
|
...rest
|
|
|
|
}: {
|
|
|
|
withFiles?: boolean
|
2022-12-04 04:31:51 -05:00
|
|
|
withAuthor?: boolean
|
|
|
|
} & Prisma.PostFindManyArgs = {}): Promise<
|
|
|
|
Post[] | PostWithFiles[] | PostWithFilesAndAuthor[]
|
|
|
|
> => {
|
2022-11-12 21:39:03 -05:00
|
|
|
const posts = await prisma.post.findMany({
|
|
|
|
include: {
|
2022-12-04 04:31:51 -05:00
|
|
|
files: withFiles,
|
|
|
|
author: withAuthor
|
2022-11-12 21:39:03 -05:00
|
|
|
},
|
|
|
|
// TODO: optimize which to grab
|
2022-11-14 02:02:31 -05:00
|
|
|
take,
|
|
|
|
...rest
|
|
|
|
})
|
|
|
|
|
2022-12-04 04:31:51 -05:00
|
|
|
return posts as typeof withFiles extends true
|
|
|
|
? typeof withAuthor extends true
|
|
|
|
? PostWithFilesAndAuthor[]
|
|
|
|
: PostWithFiles[]
|
|
|
|
: Post[]
|
2022-11-14 02:02:31 -05:00
|
|
|
}
|
|
|
|
|
2022-11-14 04:28:40 -05:00
|
|
|
export type UserWithPosts = User & {
|
|
|
|
posts: Post[]
|
|
|
|
}
|
|
|
|
|
|
|
|
export const getAllUsers = async () => {
|
|
|
|
const users = await prisma.user.findMany({
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
email: true,
|
|
|
|
role: true,
|
2022-11-14 20:32:32 -05:00
|
|
|
displayName: true,
|
2022-11-14 04:28:40 -05:00
|
|
|
posts: true,
|
2022-11-14 21:49:58 -05:00
|
|
|
createdAt: true
|
|
|
|
}
|
2022-11-14 04:28:40 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
return users as UserWithPosts[]
|
|
|
|
}
|
|
|
|
|
2022-11-14 02:02:31 -05:00
|
|
|
export const searchPosts = async (
|
|
|
|
query: string,
|
|
|
|
{
|
|
|
|
withFiles = false,
|
2022-12-04 04:55:20 -05:00
|
|
|
userId,
|
|
|
|
publicOnly
|
2022-11-14 02:02:31 -05:00
|
|
|
}: {
|
|
|
|
withFiles?: boolean
|
|
|
|
userId?: User["id"]
|
2022-12-04 04:55:20 -05:00
|
|
|
publicOnly?: boolean
|
2022-11-14 02:02:31 -05:00
|
|
|
} = {}
|
|
|
|
): Promise<PostWithFiles[]> => {
|
|
|
|
const posts = await prisma.post.findMany({
|
|
|
|
where: {
|
|
|
|
OR: [
|
|
|
|
{
|
|
|
|
title: {
|
|
|
|
search: query
|
|
|
|
},
|
2022-12-04 04:55:20 -05:00
|
|
|
authorId: userId,
|
|
|
|
visibility: publicOnly ? "public" : undefined
|
2022-11-14 02:02:31 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
files: {
|
|
|
|
some: {
|
|
|
|
content: {
|
|
|
|
search: query
|
|
|
|
},
|
|
|
|
userId: userId
|
|
|
|
}
|
2022-12-04 04:55:20 -05:00
|
|
|
},
|
|
|
|
visibility: publicOnly ? "public" : undefined
|
2022-11-14 02:02:31 -05:00
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
include: {
|
|
|
|
files: withFiles
|
|
|
|
}
|
2022-11-12 21:39:03 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
return posts as PostWithFiles[]
|
|
|
|
}
|