Add user preference settings
This commit is contained in:
34
server/api/user-settings/index.patch.ts
Normal file
34
server/api/user-settings/index.patch.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { createError, readBody } from 'h3'
|
||||
import { prisma } from '../../utils/db'
|
||||
import { requireUser } from '../../utils/auth'
|
||||
|
||||
const currencies = new Set(['KRW', 'USD', 'JPY'])
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const user = await requireUser(event)
|
||||
const body = await readBody<{
|
||||
primaryCurrency?: string
|
||||
monthStartDay?: number
|
||||
weekStartDay?: number
|
||||
}>(event)
|
||||
|
||||
const primaryCurrency = body.primaryCurrency || 'KRW'
|
||||
const monthStartDay = Number(body.monthStartDay || 1)
|
||||
const weekStartDay = Number(body.weekStartDay ?? 0)
|
||||
|
||||
if (!currencies.has(primaryCurrency)) {
|
||||
throw createError({ statusCode: 400, statusMessage: 'Invalid currency' })
|
||||
}
|
||||
if (!Number.isInteger(monthStartDay) || monthStartDay < 1 || monthStartDay > 28) {
|
||||
throw createError({ statusCode: 400, statusMessage: 'Invalid month start day' })
|
||||
}
|
||||
if (!Number.isInteger(weekStartDay) || weekStartDay < 0 || weekStartDay > 6) {
|
||||
throw createError({ statusCode: 400, statusMessage: 'Invalid week start day' })
|
||||
}
|
||||
|
||||
return prisma.userSetting.upsert({
|
||||
where: { userId: user.id },
|
||||
update: { primaryCurrency, monthStartDay, weekStartDay },
|
||||
create: { userId: user.id, primaryCurrency, monthStartDay, weekStartDay }
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user