This commit is contained in:
46
server/routes/user.ts
Normal file
46
server/routes/user.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import type { FastifyInstance } from 'fastify'
|
||||
import { z } from 'zod'
|
||||
import prisma from '../prisma/client.ts'
|
||||
|
||||
export default function (fastify: FastifyInstance) {
|
||||
fastify.get('/preferences', async (req, reply) => {
|
||||
if (req.user) {
|
||||
return prisma.userPreferences.findFirst({ where: { userId: req.user.id } })
|
||||
}
|
||||
|
||||
reply.code(401).send(false)
|
||||
})
|
||||
|
||||
fastify.patch('/preferences', async (req, reply) => {
|
||||
if (!req.user) {
|
||||
reply.code(401).send(false)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const schema = z.object({
|
||||
toggleInputHotkey: z.string(),
|
||||
toggleOutputHotkey: z.string(),
|
||||
volumes: z.record(z.string(), z.number()),
|
||||
})
|
||||
const input = schema.parse(req.body)
|
||||
|
||||
return prisma.userPreferences.update({
|
||||
where: { userId: req.user.id },
|
||||
data: input,
|
||||
})
|
||||
}
|
||||
catch (err) {
|
||||
fastify.log.error(err)
|
||||
reply.code(400)
|
||||
|
||||
if (err instanceof z.ZodError) {
|
||||
reply.send({ error: z.prettifyError(err) })
|
||||
}
|
||||
else {
|
||||
reply.send({ error: err.message })
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user