1- import { NextResponse , NextRequest } from "next/server" ;
1+ import { NextResponse , NextRequest } from "next/server" ;
22import { supabaseAdmin } from "@/lib/supabase" ;
33import { getToken } from "next-auth/jwt" ;
44
5- export async function GET ( req : NextRequest ) {
6-
7- try {
5+ export async function GET ( req : NextRequest ) {
6+ try {
87 const token = await getToken ( {
98 req,
109 secret : process . env . NEXTAUTH_SECRET ,
1110 } ) ;
12- console . log ( token )
13-
11+
1412 const userId = token ?. githubId ;
1513
16- if ( ! userId ) {
17- return NextResponse . json (
18- { error : `Unauthorized` } ,
19- { status : 400 }
20- ) ;
14+ if ( ! userId ) {
15+ return NextResponse . json ( { error : "Unauthorized" } , { status : 401 } ) ;
2116 }
2217
2318 const today = new Date ( ) ;
2419 const todayDate = today . toISOString ( ) . split ( "T" ) [ 0 ] ;
2520
2621 const yesterday = new Date ( ) ;
27- yesterday . setDate ( yesterday . getDate ( ) - 1 ) ;
28-
22+ yesterday . setDate ( yesterday . getDate ( ) - 1 ) ;
2923 const yesterdayDate = yesterday . toISOString ( ) . split ( "T" ) [ 0 ] ;
3024
31- const { data : todayData } = await supabaseAdmin
32- . from ( "daily_notes" )
33- . select ( "*" )
34- . eq ( "user_id" , userId )
35- . eq ( "date" , todayDate )
36- . single ( ) ;
37-
38- const { data : yesterdayData } = await supabaseAdmin
39- . from ( "daily_notes" )
40- . select ( "*" )
41- . eq ( "user_id" , userId )
42- . eq ( "date" , yesterdayDate )
43- . single ( ) ;
44- console . log ( todayData , yesterdayData ) ;
25+ const { data : todayData } = await supabaseAdmin
26+ . from ( "daily_notes" )
27+ . select ( "*" )
28+ . eq ( "user_id" , userId )
29+ . eq ( "date" , todayDate )
30+ . single ( ) ;
31+
32+ const { data : yesterdayData } = await supabaseAdmin
33+ . from ( "daily_notes" )
34+ . select ( "*" )
35+ . eq ( "user_id" , userId )
36+ . eq ( "date" , yesterdayDate )
37+ . single ( ) ;
38+
4539 return NextResponse . json ( {
4640 todayNote : todayData ?. note || "" ,
4741 yesterdayNote : yesterdayData ?. note || "" ,
4842 } ) ;
49-
50- } catch ( error ) {
51- return NextResponse . json (
52- { error : `Something went wrong ` } ,
53- { status : 500 }
54- ) ;
55-
43+ } catch {
44+ return NextResponse . json ( { error : "Something went wrong" } , { status : 500 } ) ;
5645 }
5746}
5847
5948export async function POST ( req : NextRequest ) {
60- try {
61-
62- const token = await getToken ( {
63- req,
64- secret : process . env . NEXTAUTH_SECRET ,
65- } ) ;
66- console . log ( token )
67-
68- const userId = token ?. githubId ;
69- const body = await req . json ( ) ;
70- console . log ( body ) ;
71- const { note} = body ;
72- if ( ! userId ) {
73- console . log ( "no user id" ) ;
74- return NextResponse . json (
75- { error : "User id is requied " } ,
76- { status : 400 }
77- ) ;
78- }
79- if ( ! note || ! note . trim ( ) ) {
80- console . log ( "no note " ) ;
81- return NextResponse . json (
82- { error : "Note cannot be empty" } ,
83- { status : 400 }
84- ) ;
85- }
86- if ( note . length > 280 ) {
87- console . log ( "max len " ) ;
88- return NextResponse . json (
89- { error : "Maximum 280 characters allowed" } ,
90- { status : 400 }
91- ) ;
92- }
93- const today = new Date ( ) . toISOString ( ) . split ( "T" ) [ 0 ] ;
94- const { data, error} = await supabaseAdmin
95- . from ( "daily_notes" )
96- . upsert ( {
97- user_id : userId ,
98- date : today ,
99- note : note . trim ( ) ,
100- } , {
101- onConflict :"user_id,date"
102- } )
103- . select ( )
104- . single ( ) ;
105- if ( error ) {
106- console . log ( error . message ) ;
107- return NextResponse . json (
108- { error : error . message } ,
109- { status :500 }
110- ) ;
49+ try {
50+ const token = await getToken ( {
51+ req,
52+ secret : process . env . NEXTAUTH_SECRET ,
53+ } ) ;
54+
55+ const userId = token ?. githubId ;
56+
57+ if ( ! userId ) {
58+ return NextResponse . json ( { error : "Unauthorized" } , { status : 401 } ) ;
59+ }
60+
61+ const body = await req . json ( ) ;
62+ const { note } = body ;
63+
64+ if ( ! note || ! note . trim ( ) ) {
65+ return NextResponse . json ( { error : "Note cannot be empty" } , { status : 400 } ) ;
66+ }
67+
68+ if ( note . length > 280 ) {
69+ return NextResponse . json ( { error : "Maximum 280 characters allowed" } , { status : 400 } ) ;
70+ }
71+
72+ const today = new Date ( ) . toISOString ( ) . split ( "T" ) [ 0 ] ;
73+ const { data, error } = await supabaseAdmin
74+ . from ( "daily_notes" )
75+ . upsert (
76+ { user_id : userId , date : today , note : note . trim ( ) } ,
77+ { onConflict : "user_id,date" }
78+ )
79+ . select ( )
80+ . single ( ) ;
81+
82+ if ( error ) {
83+ return NextResponse . json ( { error : "Failed to save note" } , { status : 500 } ) ;
84+ }
85+
86+ return NextResponse . json ( data ) ;
87+ } catch {
88+ return NextResponse . json ( { error : "Something went wrong" } , { status : 500 } ) ;
11189 }
112- console . log ( data ) ;
113- return NextResponse . json ( data ) ;
114- } catch ( error ) {
115- console . log ( error ) ;
116- return NextResponse . json (
117- { error : "Something went wrong" } ,
118- { status :500 }
119- ) ;
120- }
121- }
90+ }
0 commit comments