import type { NextApiRequest , NextApiResponse } from 'next' ; import prisma from '~/lib/prisma' ; import { NextApiFunc } from '~/types' ; BigInt . prototype . toJSON = function ( ) { return this . toString ( ) ; } ; export default async ( req , res ) : Promise < NextApiFunc > => { try { const totalViews = await prisma . counters . aggregate ( { _sum : { likes : true , loves : true , awards : true , bookmarks : true , } , } ) ; const { _sum : counters } = totalViews ; return res . status ( 200 ) . send ( { success : true , counters , total : Object . keys ( counters ) . reduce ( ( accumulator , key ) : string => { return ( ( BigInt ( accumulator ) + BigInt ( counters [ key ] || 0 ) ) . toString ( ) ) ; } , '0' ) , } ) ; } catch ( err ) { return res . status ( 500 ) . send ( { success : false , error : err ?. message || err ?. stackTrace . toString ( ) || 'Unexpected error' , } ) ; } } ;