mirror of
https://github.com/vas3k/TaxHacker.git
synced 2026-01-06 13:41:05 -06:00
63 lines
1.4 KiB
TypeScript
63 lines
1.4 KiB
TypeScript
import { prisma } from "@/lib/db"
|
|
import { codeFromName } from "@/lib/utils"
|
|
import { Prisma } from "@/prisma/client"
|
|
import { cache } from "react"
|
|
|
|
export type ProjectData = {
|
|
[key: string]: unknown
|
|
}
|
|
|
|
export const getProjects = cache(async (userId: string) => {
|
|
return await prisma.project.findMany({
|
|
where: { userId },
|
|
orderBy: {
|
|
name: "asc",
|
|
},
|
|
})
|
|
})
|
|
|
|
export const getProjectByCode = cache(async (userId: string, code: string) => {
|
|
return await prisma.project.findUnique({
|
|
where: { userId_code: { code, userId } },
|
|
})
|
|
})
|
|
|
|
export const createProject = async (userId: string, project: ProjectData) => {
|
|
if (!project.code) {
|
|
project.code = codeFromName(project.name as string)
|
|
}
|
|
return await prisma.project.create({
|
|
data: {
|
|
...project,
|
|
user: {
|
|
connect: {
|
|
id: userId,
|
|
},
|
|
},
|
|
} as Prisma.ProjectCreateInput,
|
|
})
|
|
}
|
|
|
|
export const updateProject = async (userId: string, code: string, project: ProjectData) => {
|
|
return await prisma.project.update({
|
|
where: { userId_code: { code, userId } },
|
|
data: project,
|
|
})
|
|
}
|
|
|
|
export const deleteProject = async (userId: string, code: string) => {
|
|
await prisma.transaction.updateMany({
|
|
where: {
|
|
userId,
|
|
projectCode: code,
|
|
},
|
|
data: {
|
|
projectCode: null,
|
|
},
|
|
})
|
|
|
|
return await prisma.project.delete({
|
|
where: { userId_code: { code, userId } },
|
|
})
|
|
}
|