mirror of
https://github.com/marcogll/TaxHacker_s23.git
synced 2026-01-13 21:35:19 +00:00
BREAKING: postgres + saas
This commit is contained in:
@@ -3,38 +3,50 @@ import { codeFromName } from "@/lib/utils"
|
||||
import { Prisma } from "@prisma/client"
|
||||
import { cache } from "react"
|
||||
|
||||
export const getCategories = cache(async () => {
|
||||
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 (code: string) => {
|
||||
export const getCategoryByCode = cache(async (userId: string, code: string) => {
|
||||
return await prisma.category.findUnique({
|
||||
where: { code },
|
||||
where: { userId_code: { code, userId } },
|
||||
})
|
||||
})
|
||||
|
||||
export const createCategory = async (category: Prisma.CategoryCreateInput) => {
|
||||
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,
|
||||
data: {
|
||||
...category,
|
||||
user: {
|
||||
connect: {
|
||||
id: userId,
|
||||
},
|
||||
},
|
||||
} as Prisma.CategoryCreateInput,
|
||||
})
|
||||
}
|
||||
|
||||
export const updateCategory = async (code: string, category: Prisma.CategoryUpdateInput) => {
|
||||
export const updateCategory = async (userId: string, code: string, category: CategoryData) => {
|
||||
return await prisma.category.update({
|
||||
where: { code },
|
||||
where: { userId_code: { code, userId } },
|
||||
data: category,
|
||||
})
|
||||
}
|
||||
|
||||
export const deleteCategory = async (code: string) => {
|
||||
export const deleteCategory = async (userId: string, code: string) => {
|
||||
return await prisma.category.delete({
|
||||
where: { code },
|
||||
where: { userId_code: { code, userId } },
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user