1
+ import { getUserFromUserRef } from 'src/utils/fauna'
1
2
import { NextApiHandler , NextApiRequest , NextApiResponse } from 'next'
2
3
3
4
import faunadb from 'faunadb'
@@ -21,34 +22,7 @@ const FaunaCreateHandler: NextApiHandler = async (
21
22
try {
22
23
const response : any = await client . query (
23
24
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 } )
52
26
} )
53
27
)
54
28
const users : User [ ] = response . data
0 commit comments