1+ import { getUserFromUserRef } from 'src/utils/fauna'
12import { NextApiHandler , NextApiRequest , NextApiResponse } from 'next'
23
34import faunadb from 'faunadb'
@@ -21,34 +22,7 @@ const FaunaCreateHandler: NextApiHandler = async (
2122 try {
2223 const response : any = await client . query (
2324 q . Map ( q . Paginate ( q . Documents ( q . Collection ( 'users' ) ) ) , ( userRef ) => {
24- const user = q . Get ( userRef )
25- const userId = q . Select ( [ 'ref' , 'id' ] , user )
26- let isFollowing = false
27-
28- if ( session ) {
29- const followerId = ( session . user as User ) . id
30- const ref = q . Match ( q . Index ( 'unique_user_and_follower' ) , [
31- q . Ref ( q . Collection ( 'users' ) , userId ) ,
32- q . Ref ( q . Collection ( 'users' ) , followerId ) ,
33- ] )
34- isFollowing = q . If (
35- q . Exists ( ref ) ,
36- q . Select ( [ 'data' , 'isFollowing' ] , q . Get ( ref ) ) ,
37- false
38- ) as boolean
39- }
40- return {
41- id : userId ,
42- name : q . Select ( [ 'data' , 'name' ] , user , null ) ,
43- username : q . Select ( [ 'data' , 'username' ] , user , null ) ,
44- image : q . Select ( [ 'data' , 'image' ] , user , null ) ,
45- account : {
46- firstName : q . Select ( [ 'data' , 'account' , 'firstName' ] , user , null ) ,
47- bio : q . Select ( [ 'data' , 'account' , 'bio' ] , user , null ) ,
48- } ,
49- socials : q . Select ( [ 'data' , 'socials' ] , user , null ) ,
50- isFollowing,
51- }
25+ return getUserFromUserRef ( { ref : userRef , session } )
5226 } )
5327 )
5428 const users : User [ ] = response . data
0 commit comments