v0.1.17 - 목표 패널 및 D-DAY 선택 추가

This commit is contained in:
2026-04-21 18:37:06 +09:00
parent 440f0f46a1
commit 4355185203
11 changed files with 429 additions and 21 deletions

103
backend/src/routes/goals.js Normal file
View File

@@ -0,0 +1,103 @@
import { and, asc, eq, like } from 'drizzle-orm'
import { z } from 'zod'
import { db } from '../db/client.js'
import { goals } from '../db/schema.js'
import { findAuthenticatedUser } from '../lib/authSession.js'
const goalSchema = z.object({
title: z.string().trim().min(1).max(80),
targetDate: z.string().regex(/^\d{4}-\d{2}-\d{2}$/),
color: z.string().trim().min(4).max(32).optional(),
})
const goalQuerySchema = z.object({
query: z.string().trim().optional(),
status: z.enum(['active', 'done', 'archived', 'all']).optional(),
})
async function requireAuthenticatedUser(request, reply) {
const user = await findAuthenticatedUser(request)
if (!user) {
reply.code(401).send({
message: '인증이 필요합니다.',
})
return null
}
return user
}
export async function registerGoalRoutes(app) {
app.get('/api/goals', async (request, reply) => {
const user = await requireAuthenticatedUser(request, reply)
if (!user) {
return
}
const query = goalQuerySchema.safeParse(request.query ?? {})
if (!query.success) {
return reply.code(400).send({
message: '목표 조회 조건이 올바르지 않습니다.',
issues: query.error.flatten(),
})
}
const filters = [eq(goals.userId, user.id)]
if (query.data.status && query.data.status !== 'all') {
filters.push(eq(goals.status, query.data.status))
}
if (query.data.query) {
filters.push(like(goals.title, `%${query.data.query}%`))
}
const items = await db
.select()
.from(goals)
.where(and(...filters))
.orderBy(asc(goals.targetDate), asc(goals.id))
return { goals: items }
})
app.post('/api/goals', async (request, reply) => {
const user = await requireAuthenticatedUser(request, reply)
if (!user) {
return
}
const payload = goalSchema.safeParse(request.body)
if (!payload.success) {
return reply.code(400).send({
message: '목표 입력값이 올바르지 않습니다.',
issues: payload.error.flatten(),
})
}
const now = new Date()
const [goal] = await db
.insert(goals)
.values({
userId: user.id,
title: payload.data.title,
targetDate: payload.data.targetDate,
color: payload.data.color ?? '#1c1917',
status: 'active',
createdAt: now,
updatedAt: now,
})
.returning()
return reply.code(201).send({
message: '목표가 추가되었습니다.',
goal,
})
})
}