134 lines
3.4 KiB
TypeScript
134 lines
3.4 KiB
TypeScript
import { createClient } from '@/lib/supabase/server'
|
|
import { NextResponse } from 'next/server'
|
|
import type { CreateDeadlineInput } from '@/types'
|
|
|
|
/**
|
|
* GET /api/deadlines/[id]
|
|
* Get a single deadline by ID
|
|
*/
|
|
export async function GET(
|
|
request: Request,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
const supabase = await createClient()
|
|
const { id } = await params
|
|
|
|
const {
|
|
data: { user },
|
|
} = await supabase.auth.getUser()
|
|
|
|
if (!user) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const { data, error } = await supabase
|
|
.from('deadlines')
|
|
.select('*, customer:customers(id, name)')
|
|
.eq('id', id)
|
|
.eq('user_id', user.id)
|
|
.single()
|
|
|
|
if (error) {
|
|
if (error.code === 'PGRST116') {
|
|
return NextResponse.json({ error: 'Deadline not found' }, { status: 404 })
|
|
}
|
|
return NextResponse.json({ error: error.message }, { status: 500 })
|
|
}
|
|
|
|
return NextResponse.json({ data })
|
|
}
|
|
|
|
/**
|
|
* PUT /api/deadlines/[id]
|
|
* Update a deadline
|
|
*/
|
|
export async function PUT(
|
|
request: Request,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
const supabase = await createClient()
|
|
const { id } = await params
|
|
|
|
const {
|
|
data: { user },
|
|
} = await supabase.auth.getUser()
|
|
|
|
if (!user) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const body: Partial<CreateDeadlineInput> = await request.json()
|
|
|
|
// First, get existing deadline to verify ownership
|
|
const { data: existing, error: fetchError } = await supabase
|
|
.from('deadlines')
|
|
.select('*')
|
|
.eq('id', id)
|
|
.eq('user_id', user.id)
|
|
.single()
|
|
|
|
if (fetchError) {
|
|
if (fetchError.code === 'PGRST116') {
|
|
return NextResponse.json({ error: 'Deadline not found' }, { status: 404 })
|
|
}
|
|
return NextResponse.json({ error: fetchError.message }, { status: 500 })
|
|
}
|
|
|
|
// Build update object
|
|
const updateData: Record<string, unknown> = {}
|
|
if (body.title !== undefined) updateData.title = body.title
|
|
if (body.due_date !== undefined) updateData.due_date = body.due_date
|
|
if (body.due_time !== undefined) updateData.due_time = body.due_time
|
|
if (body.deadline_type !== undefined) updateData.deadline_type = body.deadline_type
|
|
if (body.priority !== undefined) updateData.priority = body.priority
|
|
if (body.customer_id !== undefined) updateData.customer_id = body.customer_id || null
|
|
if (body.notes !== undefined) updateData.notes = body.notes
|
|
|
|
// Update the deadline
|
|
const { data, error } = await supabase
|
|
.from('deadlines')
|
|
.update(updateData)
|
|
.eq('id', id)
|
|
.eq('user_id', user.id)
|
|
.select('*, customer:customers(id, name)')
|
|
.single()
|
|
|
|
if (error) {
|
|
return NextResponse.json({ error: error.message }, { status: 500 })
|
|
}
|
|
|
|
return NextResponse.json({ data })
|
|
}
|
|
|
|
/**
|
|
* DELETE /api/deadlines/[id]
|
|
* Delete a deadline
|
|
*/
|
|
export async function DELETE(
|
|
request: Request,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
const supabase = await createClient()
|
|
const { id } = await params
|
|
|
|
const {
|
|
data: { user },
|
|
} = await supabase.auth.getUser()
|
|
|
|
if (!user) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const { error } = await supabase
|
|
.from('deadlines')
|
|
.delete()
|
|
.eq('id', id)
|
|
.eq('user_id', user.id)
|
|
|
|
if (error) {
|
|
return NextResponse.json({ error: error.message }, { status: 500 })
|
|
}
|
|
|
|
return NextResponse.json({ success: true })
|
|
}
|