import type { NextApiRequest , NextApiResponse } from 'next' ; import { getDevToArticles } from '~/lib/devto' ; import prisma from '~/lib/prisma' ; import { NextApiFunc } from '~/types' ; export default async ( req , res ) : Promise < NextApiFunc > => { try { const totalViews = await prisma . counters . aggregate ( { _sum : { views : true , } , } ) ; let devToViews = 0 ; const devArticlesRequest = await getDevToArticles ( ) ; if ( devArticlesRequest . ok ) { const devArticles = await devArticlesRequest . json ( ) . catch ( ( ) => [ ] ) ; devToViews = devArticles . filter ( ( it ) => it . published ) . reduce ( ( accumulator , article ) => { const { page_views_count : views = 0 } = article ; return accumulator + views ; } , 0 ) ; } return res . status ( 200 ) . send ( { success : true , total : ( ( totalViews ?. _sum ?. views || BigInt ( 0 ) ) + BigInt ( devToViews ) ) . toString ( ) , } ) ; } catch ( err ) { return res . status ( 500 ) . send ( { success : false , error : err ?. message || err ?. stackTrace . toString ( ) || 'Unexpected error' , } ) ; } } ;