Files
money.sori.studio/server/utils/defaults.ts

57 lines
1.6 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
}
})
await prisma.category.create({
data: { userId, type: 'income', name: '기본 수입', icon: 'salary', color: '#2563eb', sortOrder: 0 }
})
await prisma.category.create({
data: { userId, type: 'expense', name: '기본 지출', icon: 'shopping', color: '#ef4444', sortOrder: 0 }
})
return { cash }
}
export async function ensureDefaultMoneyData(prisma: MoneyDataClient, userId: string) {
const [assetCount, incomeTopCount, expenseTopCount] = await Promise.all([
prisma.asset.count({ where: { userId } }),
prisma.category.count({ where: { userId, type: 'income', parentId: null } }),
prisma.category.count({ where: { userId, type: 'expense', parentId: null } })
])
if (!assetCount) {
await prisma.asset.create({
data: {
userId,
name: '기본자산',
balanceType: 'cumulative',
initialBalance: 0,
sortOrder: 0
}
})
}
if (!incomeTopCount) {
await prisma.category.create({
data: { userId, type: 'income', name: '기본 수입', icon: 'salary', color: '#2563eb', sortOrder: 0 }
})
}
if (!expenseTopCount) {
await prisma.category.create({
data: { userId, type: 'expense', name: '기본 지출', icon: 'shopping', color: '#ef4444', sortOrder: 0 }
})
}
}