adbcc4b9af
feat: implement conversations API with message retrieval and posting feat: add avatar URL handling for users and update user role definitions feat: create chat system tables in the database with initial seed data fix: update user roles from "sales_user" to "sales" for consistency chore: add middleware for system API route access fixed fuckups on john's side, added a benchmark Made Graphs actualy load from database and realtime data, graphs will update and percentages will be mathematically made, and made realtime added chatcart edits, made graphs glow. added in some little small home feeling fuctions added in a slide show, in login with qoutes from creators because i want to leave a print on it saying we made this shit, we built it brick for brick login page added qoutes some random, and made them shuffle from left to right one dissapears other come in adding shape to the textbox for the qoutes Fixed my chat fuckups when it comes to chats
63 lines
2.1 KiB
TypeScript
63 lines
2.1 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server"
|
|
import { getSessionUser } from "@/lib/auth"
|
|
import { query } from "@/lib/db"
|
|
|
|
export async function POST(request: NextRequest, { params }: { params: Promise<{ id: string }> }) {
|
|
try {
|
|
const user = await getSessionUser()
|
|
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
|
|
|
|
const { id } = await params
|
|
const { content } = await request.json()
|
|
if (!content?.trim()) {
|
|
return NextResponse.json({ error: "Content is required" }, { status: 400 })
|
|
}
|
|
|
|
await query(
|
|
`INSERT INTO customer_notes (customer_id, author_id, content)
|
|
VALUES ($1, $2, $3)`,
|
|
[id, user.id, content.trim()]
|
|
)
|
|
|
|
return NextResponse.json({ success: true })
|
|
} catch (error) {
|
|
console.error("Create note error:", error)
|
|
return NextResponse.json({ error: "Failed to create note" }, { status: 500 })
|
|
}
|
|
}
|
|
|
|
export async function GET(request: NextRequest, { params }: { params: Promise<{ id: string }> }) {
|
|
try {
|
|
const user = await getSessionUser()
|
|
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
|
|
|
|
const { id } = await params
|
|
|
|
const result = await query(
|
|
`SELECT cn.id, cn.created_at, cn.updated_at, cn.content,
|
|
u.id AS user_id, u.first_name, u.last_name, u.avatar_url
|
|
FROM customer_notes cn
|
|
JOIN users u ON u.id = cn.author_id
|
|
WHERE cn.customer_id = $1 AND cn.deleted_at IS NULL
|
|
ORDER BY cn.created_at DESC`,
|
|
[id]
|
|
)
|
|
|
|
const notes = result.rows.map((r: any) => ({
|
|
id: r.id,
|
|
leadId: id,
|
|
userId: r.user_id,
|
|
authorName: `${r.first_name} ${r.last_name}`,
|
|
authorAvatar: r.avatar_url || `https://ui-avatars.com/api/?name=${encodeURIComponent(`${r.first_name} ${r.last_name}`)}&background=1d4ed8&color=fff&size=128`,
|
|
note: r.content,
|
|
createdAt: r.created_at,
|
|
updatedAt: r.updated_at,
|
|
}))
|
|
|
|
return NextResponse.json(notes)
|
|
} catch (error) {
|
|
console.error("Lead notes API error:", error)
|
|
return NextResponse.json({ error: "Failed to load notes" }, { status: 500 })
|
|
}
|
|
}
|