49 lines
1.7 KiB
TypeScript
49 lines
1.7 KiB
TypeScript
import type { Prisma, PrismaClient } from '@prisma/client'
|
|
|
|
type MoneyDataClient = PrismaClient | Prisma.TransactionClient
|
|
|
|
export async function createDefaultMoneyData(prisma: MoneyDataClient, userId: string) {
|
|
const cash = await prisma.asset.create({
|
|
data: {
|
|
userId,
|
|
name: '기본자산',
|
|
balanceType: 'cumulative',
|
|
initialBalance: 0,
|
|
sortOrder: 0
|
|
}
|
|
})
|
|
|
|
const incomeParent = await prisma.category.create({
|
|
data: { userId, type: 'income', name: '근로', icon: 'work', color: '#2563eb', sortOrder: 0 }
|
|
})
|
|
await prisma.category.create({
|
|
data: { userId, type: 'income', name: '월급', icon: 'salary', color: '#2563eb', parentId: incomeParent.id, sortOrder: 0 }
|
|
})
|
|
|
|
const foodParent = await prisma.category.create({
|
|
data: { userId, type: 'expense', name: '식비', icon: 'food', color: '#f97316', sortOrder: 0 }
|
|
})
|
|
await prisma.category.create({
|
|
data: { userId, type: 'expense', name: '외식', icon: 'food', color: '#f97316', parentId: foodParent.id, sortOrder: 0 }
|
|
})
|
|
|
|
const livingParent = await prisma.category.create({
|
|
data: { userId, type: 'expense', name: '생활', icon: 'shopping', color: '#ef4444', sortOrder: 1 }
|
|
})
|
|
await prisma.category.create({
|
|
data: { userId, type: 'expense', name: '마트', icon: 'shopping', color: '#ef4444', parentId: livingParent.id, sortOrder: 0 }
|
|
})
|
|
|
|
return { cash }
|
|
}
|
|
|
|
export async function ensureDefaultMoneyData(prisma: MoneyDataClient, userId: string) {
|
|
const [assetCount, categoryCount] = await Promise.all([
|
|
prisma.asset.count({ where: { userId } }),
|
|
prisma.category.count({ where: { userId } })
|
|
])
|
|
|
|
if (assetCount || categoryCount) return
|
|
await createDefaultMoneyData(prisma, userId)
|
|
}
|