@@ -4,7 +4,9 @@ import { revalidatePath } from 'next/cache'
44import { auth } from '@/auth'
55import { prisma } from '@/lib/db'
66import { assertCan , can } from '@/lib/acl'
7+ import { dueDateFromYmd } from '@/lib/due-date'
78import { createTaskSchema , updateTaskSchema } from '@/lib/validations/task'
9+ import type { TaskPriority } from '@/types/task'
810
911function revalidateTaskViews ( projectId ?: string | null ) {
1012 revalidatePath ( '/tasks' )
@@ -15,6 +17,17 @@ function revalidateTaskViews(projectId?: string | null) {
1517 }
1618}
1719
20+ function parseDueForUpdate (
21+ raw : FormDataEntryValue | null
22+ ) : Date | null | undefined {
23+ if ( raw === null ) return undefined
24+ if ( typeof raw !== 'string' ) return undefined
25+ const t = raw . trim ( )
26+ if ( t === '' ) return null
27+ const d = dueDateFromYmd ( t )
28+ return d === null ? undefined : d
29+ }
30+
1831export async function createTaskAction ( formData : FormData ) {
1932 const session = await auth ( )
2033 const userId = session ?. user ?. id
@@ -26,6 +39,8 @@ export async function createTaskAction(formData: FormData) {
2639 title : formData . get ( 'title' ) ,
2740 description : formData . get ( 'description' ) ?? '' ,
2841 status : formData . get ( 'status' ) ?? undefined ,
42+ priority : formData . get ( 'priority' ) ?? undefined ,
43+ dueDate : formData . get ( 'dueDate' ) ?? '' ,
2944 } )
3045
3146 if ( ! parsed . success ) {
@@ -35,7 +50,9 @@ export async function createTaskAction(formData: FormData) {
3550 }
3651 }
3752
38- const { title, description, status } = parsed . data
53+ const { title, description, status, priority, dueDate : dueYmd } = parsed . data
54+ const dueDateResolved =
55+ dueYmd && dueYmd . trim ( ) ? ( dueDateFromYmd ( dueYmd ) ?? null ) : null
3956 const projectIdRaw = formData . get ( 'projectId' )
4057 const projectId =
4158 typeof projectIdRaw === 'string' && projectIdRaw . length > 0
@@ -60,6 +77,8 @@ export async function createTaskAction(formData: FormData) {
6077 title,
6178 description : description ?. trim ( ) ? description . trim ( ) : null ,
6279 status : status ?? 'todo' ,
80+ priority : priority ?? ( 'none' as TaskPriority ) ,
81+ dueDate : dueDateResolved ,
6382 userId : userId ! ,
6483 projectId,
6584 } ,
@@ -114,13 +133,15 @@ export async function updateTaskAction(formData: FormData) {
114133 title : formData . get ( 'title' ) ?? undefined ,
115134 description : formData . get ( 'description' ) ?? '' ,
116135 status : formData . get ( 'status' ) ?? undefined ,
136+ priority : formData . get ( 'priority' ) ?? undefined ,
137+ dueDate : formData . get ( 'dueDate' ) ?? '' ,
117138 } )
118139
119140 if ( ! parsed . success ) {
120141 return { ok : false as const , error : '校验失败' }
121142 }
122143
123- const { id, title, description, status } = parsed . data
144+ const { id, title, description, status, priority } = parsed . data
124145
125146 const existing = await prisma . task . findUnique ( {
126147 where : { id } ,
@@ -136,16 +157,24 @@ export async function updateTaskAction(formData: FormData) {
136157 } )
137158 if ( ! gate . ok ) return { ok : false as const , error : gate . error }
138159
160+ const dueParsed = parseDueForUpdate ( formData . get ( 'dueDate' ) )
161+
139162 const data : {
140163 title ?: string
141164 description ?: string | null
142165 status ?: 'todo' | 'doing' | 'done'
166+ priority ?: TaskPriority
167+ dueDate ?: Date | null
143168 } = { }
144169 if ( title !== undefined ) data . title = title
145170 if ( description !== undefined ) {
146171 data . description = description . trim ( ) ? description . trim ( ) : null
147172 }
148173 if ( status !== undefined ) data . status = status
174+ if ( priority !== undefined ) data . priority = priority
175+ if ( dueParsed !== undefined ) {
176+ data . dueDate = dueParsed
177+ }
149178
150179 await prisma . task . update ( {
151180 where : { id } ,
0 commit comments