61 lines
1.5 KiB
TypeScript
61 lines
1.5 KiB
TypeScript
import { prisma } from "@/lib/prisma"
|
|
import type { Category } from "@/lib/types"
|
|
|
|
export const categoryService = {
|
|
async create(data: Omit<Category, "id">): Promise<Category> {
|
|
const created = await prisma.category.create({
|
|
data: {
|
|
name: data.name,
|
|
color: data.color,
|
|
icon: data.icon,
|
|
keywords: JSON.stringify(data.keywords),
|
|
parentId: data.parentId,
|
|
},
|
|
})
|
|
|
|
return {
|
|
id: created.id,
|
|
name: created.name,
|
|
color: created.color,
|
|
icon: created.icon,
|
|
keywords: JSON.parse(created.keywords) as string[],
|
|
parentId: created.parentId,
|
|
}
|
|
},
|
|
|
|
async update(id: string, data: Partial<Omit<Category, "id">>): Promise<Category> {
|
|
const updated = await prisma.category.update({
|
|
where: { id },
|
|
data: {
|
|
name: data.name,
|
|
color: data.color,
|
|
icon: data.icon,
|
|
keywords: data.keywords ? JSON.stringify(data.keywords) : undefined,
|
|
parentId: data.parentId,
|
|
},
|
|
})
|
|
|
|
return {
|
|
id: updated.id,
|
|
name: updated.name,
|
|
color: updated.color,
|
|
icon: updated.icon,
|
|
keywords: JSON.parse(updated.keywords) as string[],
|
|
parentId: updated.parentId,
|
|
}
|
|
},
|
|
|
|
async delete(id: string): Promise<void> {
|
|
// Business rule: Remove category from transactions (set to null)
|
|
await prisma.transaction.updateMany({
|
|
where: { categoryId: id },
|
|
data: { categoryId: null },
|
|
})
|
|
|
|
await prisma.category.delete({
|
|
where: { id },
|
|
})
|
|
},
|
|
}
|
|
|