Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { neon } from '@neondatabase/serverless';
- import { NextRequest, NextResponse } from 'next/server';
- const sql = neon(process.env.POSTGRES_URL!);
- interface TicketResponse {
- id: number;
- ticket_id: number;
- response_text: string;
- admin_id: string;
- created_at: string;
- }
- type ApiResponse<T> = NextResponse<T | { error: string }>;
- export async function GET(
- request: NextRequest,
- context: { params: { id: string } }
- ): Promise<ApiResponse<TicketResponse[]>> {
- try {
- const { id } = context.params;
- if (!id) {
- return NextResponse.json(
- { error: 'Missing ticket ID' },
- { status: 400 }
- );
- }
- const rows = await sql`
- SELECT id, ticket_id, response_text, admin_id, created_at
- FROM ticket_responses
- WHERE ticket_id = ${id}
- ORDER BY created_at DESC
- ` as TicketResponse[];
- return NextResponse.json(rows);
- } catch (error) {
- console.error('Error fetching responses:', error);
- return NextResponse.json(
- { error: 'Failed to fetch responses' },
- { status: 500 }
- );
- }
- }
- export async function POST(
- request: NextRequest,
- context: { params: { id: string } }
- ): Promise<ApiResponse<TicketResponse>> {
- try {
- const { id } = context.params;
- const { response_text, admin_id } = await request.json();
- if (!response_text || !admin_id) {
- return NextResponse.json(
- { error: 'Missing required fields' },
- { status: 400 }
- );
- }
- const newResponse = await sql`
- INSERT INTO ticket_responses (ticket_id, response_text, admin_id)
- VALUES (${id}, ${response_text}, ${admin_id})
- RETURNING id, ticket_id, response_text, admin_id, created_at
- ` as TicketResponse[];
- await sql`
- UPDATE tickets
- SET status = CASE
- WHEN status = 'open' THEN 'in-progress'
- ELSE status
- END,
- updated_at = CURRENT_TIMESTAMP
- WHERE id = ${id}
- `;
- return NextResponse.json(newResponse[0]);
- } catch (error) {
- console.error('Error creating response:', error);
- return NextResponse.json(
- { error: 'Failed to create response' },
- { status: 500 }
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment