import { NextApiRequest , NextApiResponse } from 'next' ; import got from 'got' ; import { sendAbuseReport } from '../../lib/slack-notifier' ; const notify = async ( { playbackId , reason , comment } ) => { if ( process . env . AIRTABLE_KEY && process . env . AIRTABLE_BASE_ID ) { try { await got . post ( `https://api.airtable.com/v0/${ process . env . AIRTABLE_BASE_ID }/Reported` , { headers : { Authorization : `Bearer ${ process . env . AIRTABLE_KEY }` } , json : { records : [ { fields : { playbackId , reason , comment , status : "Pending" } } , ] } } ) ; } catch ( e ) { console . error ( 'Error reporting to airtable' , e . response . body ) ; } } try { await sendAbuseReport ( { playbackId , reason , comment } ) ; } catch ( e ) { console . error ( 'Error reporting to slack' , e ) ; } } ; export default async ( req , res ) : Promise < void > => { const { method } = req ; switch ( method ) { case 'POST' : await notify ( req . body ) ; res . json ( { message : 'thank you' } ) ; break ; default : res . setHeader ( 'Allow' , [ 'POST' ] ) ; res . status ( 405 ) . end ( `Method ${ method } Not Allowed` ) ; } } ;