Skip to content

Commit d515ba3

Browse files
committed
Update members api
1 parent 5f9f258 commit d515ba3

File tree

2 files changed

+4
-28
lines changed

2 files changed

+4
-28
lines changed

src/pages/api/fauna/members.ts

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getUserFromUserRef } from 'src/utils/fauna'
12
import { NextApiHandler, NextApiRequest, NextApiResponse } from 'next'
23

34
import 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

src/utils/fauna.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ export function getUserFromUserRef({
3333
username: q.Select(['data', 'username'], userDoc, null),
3434
account: {
3535
firstName: q.Select(['data', 'account', 'firstName'], userDoc, null),
36+
bio: q.Select(['data', 'account', 'bio'], userDoc, null),
3637
},
38+
socials: q.Select(['data', 'socials'], userDoc, null),
3739
isFollowing,
3840
}
3941
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy