46 lines
1.4 KiB
TypeScript
46 lines
1.4 KiB
TypeScript
import { FastifyInstance } from "fastify"
|
|
import { eq } from "drizzle-orm"
|
|
import { z } from "zod"
|
|
import { db } from "../db"
|
|
import { appSettings } from "../db/schema"
|
|
|
|
const SettingsUpdateSchema = z.object({
|
|
workspaceName: z.string().min(1).optional(),
|
|
defaultBillable: z.boolean().optional(),
|
|
weekStart: z.number().int().min(0).max(6).optional(),
|
|
})
|
|
|
|
export default async function settingsRoutes(fastify: FastifyInstance) {
|
|
fastify.addHook("preHandler", async (request, reply) => {
|
|
try {
|
|
await request.jwtVerify()
|
|
} catch {
|
|
return reply.code(401).send({ message: "Unauthorized" })
|
|
}
|
|
})
|
|
|
|
async function getOrCreate() {
|
|
const rows = await db.select().from(appSettings).limit(1)
|
|
if (rows.length > 0) return rows[0]
|
|
const [created] = await db.insert(appSettings).values({}).returning()
|
|
return created
|
|
}
|
|
|
|
fastify.get("/", async () => getOrCreate())
|
|
|
|
fastify.patch("/", async (request, reply) => {
|
|
const payload = request.user as { sub: string; role: string }
|
|
if (payload.role !== "admin") {
|
|
return reply.code(403).send({ message: "Admin only" })
|
|
}
|
|
const body = SettingsUpdateSchema.parse(request.body)
|
|
const current = await getOrCreate()
|
|
const [updated] = await db
|
|
.update(appSettings)
|
|
.set({ ...body, updatedAt: new Date() })
|
|
.where(eq(appSettings.id, current.id))
|
|
.returning()
|
|
return updated
|
|
})
|
|
}
|