1- import { NextResponse } from 'next/server' ;
2- import { promises as fs } from 'fs' ;
3- import path from 'path' ;
4- export const dynamic = 'force-static' ; // Add this line
5- export const runtime = 'edge' ;
1+ import { NextResponse } from "next/server" ;
2+ export const runtime = "edge" ;
63
7- const isDev = process . env . NODE_ENV === 'development' ;
8- const complaintsFile = isDev ? path . join ( process . cwd ( ) , 'complaints.json' ) : null ;
9-
10- async function initializeComplaintsFile ( ) {
11- if ( ! isDev ) return ;
12- try {
13- await fs . access ( complaintsFile ! ) ;
14- } catch {
15- await fs . writeFile ( complaintsFile ! , JSON . stringify ( [ ] ) ) ;
16- }
17- }
4+ // In-memory storage for complaints (not persistent, resets on redeploy)
5+ let complaints : any [ ] = [ ] ;
186
197export async function GET ( ) {
20- if ( ! isDev ) {
21- return NextResponse . json ( { error : 'Complaints API is not available in production.' } , { status : 501 } ) ;
22- }
23- try {
24- await initializeComplaintsFile ( ) ;
25- const data = await fs . readFile ( complaintsFile ! , 'utf8' ) ;
26- const complaints = JSON . parse ( data ) ;
27- return NextResponse . json ( complaints ) ;
28- } catch ( err ) {
29- console . error ( 'Error reading complaints:' , err ) ;
30- return NextResponse . json ( { error : 'Failed to load complaints' } , { status : 500 } ) ;
31- }
8+ return NextResponse . json ( complaints ) ;
329}
3310
3411export async function POST ( req : Request ) {
35- if ( ! isDev ) {
36- return NextResponse . json ( { error : 'Complaints API is not available in production.' } , { status : 501 } ) ;
37- }
3812 try {
3913 const { title, description, place, imageUrl } = await req . json ( ) ;
4014 if ( ! title || ! description || ! place ) {
41- return NextResponse . json ( { error : 'Title, description, and place are required' } , { status : 400 } ) ;
15+ return NextResponse . json (
16+ { error : "Title, description, and place are required" } ,
17+ { status : 400 }
18+ ) ;
4219 }
43-
44- await initializeComplaintsFile ( ) ;
45- const data = await fs . readFile ( complaintsFile ! , 'utf8' ) ;
46- const complaints = JSON . parse ( data ) ;
47- const newId = complaints . length ? complaints [ complaints . length - 1 ] . id + 1 : 1 ;
20+ const newId = complaints . length
21+ ? complaints [ complaints . length - 1 ] . id + 1
22+ : 1 ;
4823 const complaint = {
4924 id : newId ,
5025 title,
5126 description,
5227 place,
53- date : new Date ( ) . toLocaleDateString ( 'en-GB' ) . split ( '/' ) . reverse ( ) . join ( '-' ) ,
28+ date : new Date ( )
29+ . toLocaleDateString ( "en-GB" )
30+ . split ( "/" )
31+ . reverse ( )
32+ . join ( "-" ) ,
5433 solved : false ,
5534 imageUrl : imageUrl || null ,
56- comments : [ ]
35+ comments : [ ] ,
5736 } ;
5837 complaints . push ( complaint ) ;
59- await fs . writeFile ( complaintsFile ! , JSON . stringify ( complaints , null , 2 ) ) ;
60- return NextResponse . json ( { message : 'Complaint added successfully' , complaint } ) ;
38+ return NextResponse . json ( {
39+ message : "Complaint added successfully" ,
40+ complaint,
41+ } ) ;
6142 } catch ( err ) {
62- console . error ( 'Error saving complaint:' , err ) ;
63- return NextResponse . json ( { error : 'Failed to save complaint' } , { status : 500 } ) ;
43+ console . error ( "Error saving complaint:" , err ) ;
44+ return NextResponse . json (
45+ { error : "Failed to save complaint" } ,
46+ { status : 500 }
47+ ) ;
6448 }
6549}
66-
67-
0 commit comments