126 lines
3.8 KiB
TypeScript
126 lines
3.8 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { initDB, createContact, getContactById, getContactByPhone, getContactsByStage, getAllContacts, updateContact, deleteContact, searchContacts, getContactsByPaymentStatus, getTaskCountByContactId } from '@/lib/db'
|
|
import { STAGES } from '@/lib/types'
|
|
|
|
export async function GET(request: NextRequest) {
|
|
const searchParams = request.nextUrl.searchParams
|
|
const id = searchParams.get('id')
|
|
const phone = searchParams.get('phone')
|
|
const stage = searchParams.get('stage')
|
|
const search = searchParams.get('search')
|
|
const payment_status = searchParams.get('payment_status')
|
|
|
|
try {
|
|
await initDB()
|
|
|
|
let contacts: any[] = []
|
|
|
|
if (id) {
|
|
const contact = getContactById(parseInt(id))
|
|
if (!contact) {
|
|
return NextResponse.json({ error: 'Contact not found' }, { status: 404 })
|
|
}
|
|
const taskCount = getTaskCountByContactId(contact.id)
|
|
return NextResponse.json({ ...contact, taskCount })
|
|
}
|
|
|
|
if (phone) {
|
|
const contact = getContactByPhone(phone)
|
|
if (!contact) {
|
|
return NextResponse.json({ error: 'Contact not found' }, { status: 404 })
|
|
}
|
|
return NextResponse.json(contact)
|
|
}
|
|
|
|
if (stage) {
|
|
contacts = getContactsByStage(stage as any)
|
|
} else if (search) {
|
|
contacts = searchContacts(search)
|
|
} else if (payment_status) {
|
|
contacts = getContactsByPaymentStatus(payment_status as any)
|
|
} else {
|
|
contacts = getAllContacts()
|
|
}
|
|
|
|
if (!Array.isArray(contacts)) {
|
|
contacts = []
|
|
}
|
|
|
|
return NextResponse.json(contacts)
|
|
} catch (error: any) {
|
|
console.error('API Error:', error)
|
|
return NextResponse.json({ error: error.message }, { status: 500 })
|
|
}
|
|
}
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
await initDB()
|
|
const body = await request.json()
|
|
|
|
const { name, phone, notes, schedule_date, follow_up_date } = body
|
|
|
|
if (!name || !phone) {
|
|
return NextResponse.json(
|
|
{ error: 'Name and phone are required' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
const existingContact = getContactByPhone(phone)
|
|
if (existingContact) {
|
|
return NextResponse.json(
|
|
{ error: 'Contact with this phone already exists' },
|
|
{ status: 409 }
|
|
)
|
|
}
|
|
|
|
const contact = createContact({ name, phone, notes, schedule_date, follow_up_date })
|
|
return NextResponse.json(contact, { status: 201 })
|
|
} catch (error: any) {
|
|
return NextResponse.json({ error: error.message }, { status: 500 })
|
|
}
|
|
}
|
|
|
|
export async function PUT(request: NextRequest) {
|
|
try {
|
|
await initDB()
|
|
const body = await request.json()
|
|
|
|
const { id, name, stage, notes, schedule_date, follow_up_date, payment_status } = body
|
|
|
|
if (!id) {
|
|
return NextResponse.json({ error: 'Contact ID is required' }, { status: 400 })
|
|
}
|
|
|
|
if (stage && !Object.values(STAGES).includes(stage)) {
|
|
return NextResponse.json({ error: 'Invalid stage' }, { status: 400 })
|
|
}
|
|
|
|
const updated = updateContact(id, { name, stage, notes, schedule_date, follow_up_date, payment_status })
|
|
if (!updated) {
|
|
return NextResponse.json({ error: 'Contact not found' }, { status: 404 })
|
|
}
|
|
|
|
return NextResponse.json(updated)
|
|
} catch (error: any) {
|
|
return NextResponse.json({ error: error.message }, { status: 500 })
|
|
}
|
|
}
|
|
|
|
export async function DELETE(request: NextRequest) {
|
|
try {
|
|
await initDB()
|
|
const searchParams = request.nextUrl.searchParams
|
|
const id = searchParams.get('id')
|
|
|
|
if (!id) {
|
|
return NextResponse.json({ error: 'Contact ID is required' }, { status: 400 })
|
|
}
|
|
|
|
deleteContact(parseInt(id))
|
|
return NextResponse.json({ success: true })
|
|
} catch (error: any) {
|
|
return NextResponse.json({ error: error.message }, { status: 500 })
|
|
}
|
|
} |