mirror of
https://github.com/marcogll/TaxHacker_s23.git
synced 2026-01-13 13:25:18 +00:00
BREAKING: postgres + saas
This commit is contained in:
@@ -3,32 +3,44 @@ import { codeFromName } from "@/lib/utils"
|
||||
import { Prisma } from "@prisma/client"
|
||||
import { cache } from "react"
|
||||
|
||||
export const getFields = cache(async () => {
|
||||
export type FieldData = {
|
||||
[key: string]: unknown
|
||||
}
|
||||
|
||||
export const getFields = cache(async (userId: string) => {
|
||||
return await prisma.field.findMany({
|
||||
where: { userId },
|
||||
orderBy: {
|
||||
createdAt: "asc",
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
export const createField = async (field: Prisma.FieldCreateInput) => {
|
||||
export const createField = async (userId: string, field: FieldData) => {
|
||||
if (!field.code) {
|
||||
field.code = codeFromName(field.name as string)
|
||||
}
|
||||
return await prisma.field.create({
|
||||
data: field,
|
||||
data: {
|
||||
...field,
|
||||
user: {
|
||||
connect: {
|
||||
id: userId,
|
||||
},
|
||||
},
|
||||
} as Prisma.FieldCreateInput,
|
||||
})
|
||||
}
|
||||
|
||||
export const updateField = async (code: string, field: Prisma.FieldUpdateInput) => {
|
||||
export const updateField = async (userId: string, code: string, field: FieldData) => {
|
||||
return await prisma.field.update({
|
||||
where: { code },
|
||||
where: { userId_code: { code, userId } },
|
||||
data: field,
|
||||
})
|
||||
}
|
||||
|
||||
export const deleteField = async (code: string) => {
|
||||
export const deleteField = async (userId: string, code: string) => {
|
||||
return await prisma.field.delete({
|
||||
where: { code },
|
||||
where: { userId_code: { code, userId } },
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user