11import { NextResponse } from 'next/server' ;
2- import { promises as fs } from 'fs' ;
3- import path from 'path' ;
2+ import { promises as fs } from 'fs' ;
3+ import path from 'path' ;
44
5- const complaintsFile = path . join ( process . cwd ( ) , 'complaints.json' ) ;
5+ const isDev = process . env . NODE_ENV === 'development' ;
6+ const complaintsFile = isDev ? path . join ( process . cwd ( ) , 'complaints.json' ) : null ;
67
7- async function initializeComplaintsFile ( ) {
8- try {
9- await fs . access ( complaintsFile ) ;
10- } catch {
11- await fs . writeFile ( complaintsFile , JSON . stringify ( [ ] ) ) ;
12- }
8+ async function initializeComplaintsFile ( ) {
9+ if ( ! isDev ) return ;
10+ try {
11+ await fs . access ( complaintsFile ! ) ;
12+ } catch {
13+ await fs . writeFile ( complaintsFile ! , JSON . stringify ( [ ] ) ) ;
1314 }
15+ }
1416
15- export async function GET ( ) {
16- try {
17- await initializeComplaintsFile ( ) ;
18- const data = await fs . readFile ( complaintsFile , 'utf8' ) ;
19- const complaints = JSON . parse ( data ) ;
20- return NextResponse . json ( complaints ) ;
21- } catch ( err ) {
22- console . error ( 'Error reading complaints:' , err ) ;
23- return NextResponse . json ( { error : 'Failed to load complaints' } , { status : 500 } ) ;
24- }
17+ export async function GET ( ) {
18+ if ( ! isDev ) {
19+ return NextResponse . json ( { error : 'Complaints API is not available in production.' } , { status : 501 } ) ;
2520 }
21+ try {
22+ await initializeComplaintsFile ( ) ;
23+ const data = await fs . readFile ( complaintsFile ! , 'utf8' ) ;
24+ const complaints = JSON . parse ( data ) ;
25+ return NextResponse . json ( complaints ) ;
26+ } catch ( err ) {
27+ console . error ( 'Error reading complaints:' , err ) ;
28+ return NextResponse . json ( { error : 'Failed to load complaints' } , { status : 500 } ) ;
29+ }
30+ }
2631
27- export async function POST ( req : Request ) {
28- try {
29- const { title, description, place, imageUrl } = await req . json ( ) ;
30- if ( ! title || ! description || ! place ) {
31- return NextResponse . json ( { error : 'Title, description, and place are required' } , { status : 400 } ) ;
32- }
33-
34- await initializeComplaintsFile ( ) ;
35- const data = await fs . readFile ( complaintsFile , 'utf8' ) ;
36- const complaints = JSON . parse ( data ) ;
37- const newId = complaints . length ? complaints [ complaints . length - 1 ] . id + 1 : 1 ;
38- const complaint = {
39- id : newId ,
40- title,
41- description,
42- place,
43- date : new Date ( ) . toLocaleDateString ( 'en-GB' ) . split ( '/' ) . reverse ( ) . join ( '-' ) ,
44- solved : false ,
45- imageUrl : imageUrl || null ,
46- comments : [ ]
47- } ;
48- complaints . push ( complaint ) ;
49- await fs . writeFile ( complaintsFile , JSON . stringify ( complaints , null , 2 ) ) ;
50- return NextResponse . json ( { message : 'Complaint added successfully' , complaint } ) ;
51- } catch ( err ) {
52- console . error ( 'Error saving complaint:' , err ) ;
53- return NextResponse . json ( { error : 'Failed to save complaint' } , { status : 500 } ) ;
32+ export async function POST ( req : Request ) {
33+ if ( ! isDev ) {
34+ return NextResponse . json ( { error : 'Complaints API is not available in production.' } , { status : 501 } ) ;
35+ }
36+ try {
37+ const { title, description, place, imageUrl } = await req . json ( ) ;
38+ if ( ! title || ! description || ! place ) {
39+ return NextResponse . json ( { error : 'Title, description, and place are required' } , { status : 400 } ) ;
5440 }
41+
42+ await initializeComplaintsFile ( ) ;
43+ const data = await fs . readFile ( complaintsFile ! , 'utf8' ) ;
44+ const complaints = JSON . parse ( data ) ;
45+ const newId = complaints . length ? complaints [ complaints . length - 1 ] . id + 1 : 1 ;
46+ const complaint = {
47+ id : newId ,
48+ title,
49+ description,
50+ place,
51+ date : new Date ( ) . toLocaleDateString ( 'en-GB' ) . split ( '/' ) . reverse ( ) . join ( '-' ) ,
52+ solved : false ,
53+ imageUrl : imageUrl || null ,
54+ comments : [ ]
55+ } ;
56+ complaints . push ( complaint ) ;
57+ await fs . writeFile ( complaintsFile ! , JSON . stringify ( complaints , null , 2 ) ) ;
58+ return NextResponse . json ( { message : 'Complaint added successfully' , complaint } ) ;
59+ } catch ( err ) {
60+ console . error ( 'Error saving complaint:' , err ) ;
61+ return NextResponse . json ( { error : 'Failed to save complaint' } , { status : 500 } ) ;
5562 }
63+ }
5664
5765
0 commit comments