mirror of
https://github.com/vas3k/TaxHacker.git
synced 2026-01-09 23:20:28 -06:00
53 lines
1.3 KiB
TypeScript
53 lines
1.3 KiB
TypeScript
import { prisma } from "@/lib/db"
|
|
import { codeFromName } from "@/lib/utils"
|
|
import { Prisma } from "@/prisma/client"
|
|
import { cache } from "react"
|
|
|
|
export type CategoryData = {
|
|
[key: string]: unknown
|
|
}
|
|
|
|
export const getCategories = cache(async (userId: string) => {
|
|
return await prisma.category.findMany({
|
|
where: { userId },
|
|
orderBy: {
|
|
name: "asc",
|
|
},
|
|
})
|
|
})
|
|
|
|
export const getCategoryByCode = cache(async (userId: string, code: string) => {
|
|
return await prisma.category.findUnique({
|
|
where: { userId_code: { userId, code } },
|
|
})
|
|
})
|
|
|
|
export const createCategory = async (userId: string, category: CategoryData) => {
|
|
if (!category.code) {
|
|
category.code = codeFromName(category.name as string)
|
|
}
|
|
return await prisma.category.create({
|
|
data: {
|
|
...category,
|
|
user: {
|
|
connect: {
|
|
id: userId,
|
|
},
|
|
},
|
|
} as Prisma.CategoryCreateInput,
|
|
})
|
|
}
|
|
|
|
export const updateCategory = async (userId: string, code: string, category: CategoryData) => {
|
|
return await prisma.category.update({
|
|
where: { userId_code: { userId, code } },
|
|
data: category,
|
|
})
|
|
}
|
|
|
|
export const deleteCategory = async (userId: string, code: string) => {
|
|
return await prisma.category.delete({
|
|
where: { userId_code: { userId, code } },
|
|
})
|
|
}
|