2024-06-24 18:16:55 -04:00
import { db } from "@/lib/db"
import { NextResponse } from "next/server" ;
import fetchUserWithImpersonation from "@/lib/fetch-user-impersonation" ;
export async function GET ( req : Request ) {
try {
const user = await fetchUserWithImpersonation ( req )
if ( ! user ) {
2024-08-25 17:35:46 -04:00
return NextResponse . json ( { message : 'Unauthorized.' , error : null , value : null } , { status : 401 } ) ;
2024-06-24 18:16:55 -04:00
}
const redemptions = await db . redemption . findMany ( {
where : {
userId : user.id
}
} )
return NextResponse . json ( redemptions . map ( ( { userId , . . . attrs } ) = > attrs ) ) ;
} catch ( error ) {
2024-08-25 17:35:46 -04:00
return NextResponse . json ( { message : 'Something went wrong' , error : error , value : null } , { status : 500 } )
2024-06-24 18:16:55 -04:00
}
}
export async function POST ( req : Request ) {
try {
const user = await fetchUserWithImpersonation ( req )
if ( ! user ) {
2024-08-25 17:35:46 -04:00
return NextResponse . json ( { message : 'Unauthorized.' , error : null , value : null } , { status : 401 } ) ;
2024-06-24 18:16:55 -04:00
}
const { actionName , redemptionId , order , state } : { actionName : string , redemptionId : string , order : number , state : boolean } = await req . json ( ) ;
2024-08-25 17:35:46 -04:00
if ( ! redemptionId && ! actionName && ! order )
return NextResponse . json ( { message : 'One of the following fields must exist: redemptionId, actionName, order, state.' , error : null , value : null } , { status : 400 } )
if ( actionName && actionName . length > 32 )
return NextResponse . json ( { message : 'actionName cannot be longer than 32 characters.' , error : null , value : null } , { status : 400 } )
2024-06-24 18:16:55 -04:00
const action = await db . action . findFirst ( {
where : {
name : actionName
}
} )
if ( ! action )
2024-08-25 17:35:46 -04:00
return NextResponse . json ( { message : 'Action does not exist.' , error : null , value : null } , { status : 400 } )
2024-06-24 18:16:55 -04:00
let data :any = {
actionName ,
order ,
state
}
if ( ! data . actionName )
data = { actionName , . . . data }
if ( ! data . order )
data = { order , . . . data }
if ( ! data . state )
data = { state , . . . data }
const res = await db . redemption . create ( {
data : {
userId : user.id ,
redemptionId ,
order ,
state : true ,
. . . data
}
} ) ;
return NextResponse . json ( res , { status : 200 } ) ;
} catch ( error ) {
2024-08-25 17:35:46 -04:00
return NextResponse . json ( { message : 'Something went wrong' , error : error , value : null } , { status : 500 } )
2024-06-24 18:16:55 -04:00
}
}
export async function PUT ( req : Request ) {
try {
const user = await fetchUserWithImpersonation ( req )
if ( ! user ) {
2024-08-25 17:35:46 -04:00
return NextResponse . json ( { message : 'Unauthorized.' , error : null , value : null } , { status : 401 } ) ;
2024-06-24 18:16:55 -04:00
}
const { id , actionName , redemptionId , order , state } : { id : string , actionName : string , redemptionId : string , order : number , state : boolean } = await req . json ( ) ;
2024-08-25 17:35:46 -04:00
if ( ! redemptionId && ! actionName && ! order )
return NextResponse . json ( { message : 'One of the following fields must exist: redemptionId, actionName, order, state.' , error : null , value : null } , { status : 400 } )
if ( actionName && actionName . length > 32 )
return NextResponse . json ( { message : 'actionName cannot be longer than 32 characters.' , error : null , value : null } , { status : 400 } )
2024-06-24 18:16:55 -04:00
const action = await db . action . findFirst ( {
where : {
name : actionName
}
} )
if ( ! action )
2024-08-25 17:35:46 -04:00
return NextResponse . json ( { message : 'Action does not exist.' , error : null , value : null } , { status : 400 } )
2024-06-24 18:16:55 -04:00
let data :any = {
actionName ,
redemptionId ,
order ,
state
}
if ( ! data . actionName )
data = { actionName , . . . data }
if ( ! data . order )
data = { order , . . . data }
if ( ! data . state )
data = { state , . . . data }
if ( ! data . redemptionId )
data = { redemptionId , . . . data }
const res = await db . redemption . update ( {
where : {
id
} ,
data : data
} ) ;
return NextResponse . json ( res , { status : 200 } ) ;
} catch ( error ) {
2024-08-25 17:35:46 -04:00
return NextResponse . json ( { message : 'Something went wrong' , error : error , value : null } , { status : 500 } )
2024-06-24 18:16:55 -04:00
}
}
export async function DELETE ( req : Request ) {
try {
const user = await fetchUserWithImpersonation ( req )
if ( ! user ) {
2024-08-25 17:35:46 -04:00
return NextResponse . json ( { message : 'Unauthorized.' , error : null , value : null } , { status : 401 } ) ;
2024-06-24 18:16:55 -04:00
}
const { searchParams } = new URL ( req . url )
const id = searchParams . get ( 'id' ) as string
const redemptions = await db . redemption . delete ( {
where : {
id
}
} )
return NextResponse . json ( redemptions ) ;
} catch ( error ) {
2024-08-25 17:35:46 -04:00
return NextResponse . json ( { message : 'Something went wrong' , error : error , value : null } , { status : 500 } )
2024-06-24 18:16:55 -04:00
}
}