Files
Newbie_CRM/src/app/api/proposals/route.ts
T
Rene 3fe32d923e
Build & Auto-Repair / build (push) Has been cancelled
Add descriptive error messages across all API routes and toast notifications on client pages
- 86 catch blocks in 49 API route files now return the actual error message via error.message
- 14 campaign/lead/config catch blocks that lacked console.error now log errors
- 17 client pages/components now show toast.error notifications on API failures
- Silent .catch(() => {}) and finally-only try blocks now surface errors to users
2026-07-01 15:20:30 +02:00

88 lines
3.3 KiB
TypeScript

import { NextRequest, NextResponse } from "next/server"
import { getSessionUser } from "@/lib/auth"
import { query } from "@/lib/db"
export async function GET(request: NextRequest) {
try {
const user = await getSessionUser()
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
const { searchParams } = new URL(request.url)
const search = searchParams.get("search") || ""
const statusId = searchParams.get("status") || ""
let sql = `SELECT p.id, p.title, p.proposal_number, p.subtotal, p.total_amount, p.valid_until, p.created_at,
ps.name AS status_name, ps.color AS status_color,
u.first_name || ' ' || u.last_name AS created_by_name
FROM proposals p
JOIN proposal_statuses ps ON ps.id = p.status_id
JOIN users u ON u.id = p.created_by
WHERE p.deleted_at IS NULL`
const params: any[] = []
let idx = 1
if (search) {
sql += ` AND (p.title ILIKE $${idx} OR p.proposal_number ILIKE $${idx})`
params.push(`%${search}%`)
idx++
}
if (statusId) {
sql += ` AND p.status_id = $${idx}`
params.push(statusId)
idx++
}
sql += ` ORDER BY p.created_at DESC LIMIT 50`
const result = await query(sql, params)
return NextResponse.json(result.rows)
} catch (error) {
console.error("Proposals API error:", error)
const message = error instanceof Error ? error.message : "Failed to load proposals"
return NextResponse.json({ error: message }, { status: 500 })
}
}
export async function POST(request: NextRequest) {
try {
const user = await getSessionUser()
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
const body = await request.json()
const draftStatus = await query("SELECT id FROM proposal_statuses WHERE name = 'Draft'")
const statusId = draftStatus.rows[0]?.id
const numResult = await query("SELECT COALESCE(MAX(SUBSTRING(proposal_number FROM 'Q-(\\d+)')::INT), 0) + 1 AS next_num FROM proposals WHERE deleted_at IS NULL")
const nextNum = numResult.rows[0]?.next_num || 1
const proposalNumber = `Q-${String(nextNum).padStart(4, "0")}`
const result = await query(
`INSERT INTO proposals (lead_id, customer_id, title, proposal_number, status_id, content, subtotal, tax_percent, tax_amount, total_amount, valid_until, notes, terms, created_by)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14) RETURNING id`,
[
body.leadId || null,
body.customerId || null,
body.title,
proposalNumber,
statusId,
body.content ? JSON.stringify(body.content) : null,
body.subtotal || 0,
body.taxPercent !== undefined ? body.taxPercent : 15,
body.taxAmount || 0,
body.totalAmount || 0,
body.validUntil || null,
body.notes || null,
body.terms || null,
user.id,
]
)
return NextResponse.json({ success: true, id: result.rows[0].id, proposalNumber }, { status: 201 })
} catch (error) {
console.error("Proposals POST error:", error)
const message = error instanceof Error ? error.message : "Failed to create proposal"
return NextResponse.json({ error: message }, { status: 500 })
}
}