98 lines
2.7 KiB
TypeScript
98 lines
2.7 KiB
TypeScript
import { query } from '../../utils/db'
|
|
import { getCurrentUserId } from '../../utils/user'
|
|
|
|
/**
|
|
* TODO 목록 조회
|
|
* GET /api/todo/list
|
|
*/
|
|
export default defineEventHandler(async (event) => {
|
|
const params = getQuery(event)
|
|
const userId = await getCurrentUserId(event)
|
|
|
|
const status = params.status as string | null
|
|
const assigneeId = params.assigneeId ? Number(params.assigneeId) : null
|
|
const projectId = params.projectId ? Number(params.projectId) : null
|
|
const meetingId = params.meetingId ? Number(params.meetingId) : null
|
|
const myOnly = params.myOnly === 'true'
|
|
|
|
const conditions: string[] = []
|
|
const values: any[] = []
|
|
let paramIndex = 1
|
|
|
|
if (status) {
|
|
conditions.push(`t.status = $${paramIndex++}`)
|
|
values.push(status)
|
|
}
|
|
|
|
if (assigneeId) {
|
|
conditions.push(`t.assignee_id = $${paramIndex++}`)
|
|
values.push(assigneeId)
|
|
}
|
|
|
|
if (projectId) {
|
|
conditions.push(`t.project_id = $${paramIndex++}`)
|
|
values.push(projectId)
|
|
}
|
|
|
|
if (meetingId) {
|
|
conditions.push(`t.meeting_id = $${paramIndex++}`)
|
|
values.push(meetingId)
|
|
}
|
|
|
|
if (myOnly && userId) {
|
|
conditions.push(`(t.assignee_id = $${paramIndex} OR t.reporter_id = $${paramIndex})`)
|
|
values.push(userId)
|
|
paramIndex++
|
|
}
|
|
|
|
const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : ''
|
|
|
|
const sql = `
|
|
SELECT
|
|
t.*,
|
|
p.project_name,
|
|
m.meeting_title,
|
|
a.employee_name as assignee_name,
|
|
r.employee_name as reporter_name
|
|
FROM wr_todo t
|
|
LEFT JOIN wr_project_info p ON t.project_id = p.project_id
|
|
LEFT JOIN wr_meeting m ON t.meeting_id = m.meeting_id
|
|
LEFT JOIN wr_employee_info a ON t.assignee_id = a.employee_id
|
|
LEFT JOIN wr_employee_info r ON t.reporter_id = r.employee_id
|
|
${whereClause}
|
|
ORDER BY
|
|
CASE t.status WHEN 'PENDING' THEN 1 WHEN 'IN_PROGRESS' THEN 2 WHEN 'COMPLETED' THEN 3 ELSE 4 END,
|
|
t.priority DESC,
|
|
t.due_date ASC NULLS LAST,
|
|
t.created_at DESC
|
|
LIMIT 100
|
|
`
|
|
|
|
const todos = await query(sql, values)
|
|
|
|
return {
|
|
todos: todos.map((t: any) => ({
|
|
todoId: t.todo_id,
|
|
todoTitle: t.todo_title,
|
|
todoContent: t.todo_content,
|
|
sourceType: t.source_type,
|
|
sourceId: t.source_id,
|
|
meetingId: t.meeting_id,
|
|
meetingTitle: t.meeting_title,
|
|
projectId: t.project_id,
|
|
projectName: t.project_name,
|
|
assigneeId: t.assignee_id,
|
|
assigneeName: t.assignee_name,
|
|
reporterId: t.reporter_id,
|
|
reporterName: t.reporter_name,
|
|
dueDate: t.due_date,
|
|
status: t.status,
|
|
priority: t.priority,
|
|
completedAt: t.completed_at,
|
|
weeklyReportId: t.weekly_report_id,
|
|
createdAt: t.created_at,
|
|
updatedAt: t.updated_at
|
|
}))
|
|
}
|
|
})
|