Skip to content

Commit

Permalink
feat(be): implement suggested users api
Browse files Browse the repository at this point in the history
  • Loading branch information
acatzk committed Jan 30, 2024
1 parent 4cab2ef commit 0e7a9a3
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions server/api/routers/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,42 @@ export const userRouter = createTRPCRouter({
username: input.username
}
})
}),

getSuggestedUsers: protectedProcedure
.input(
z.object({
limit: z.number().min(1).max(100).nullish(),
cursor: z.number().nullish() // <-- "cursor" needs to exist, but can be any type
})
)
.query(async (opts) => {
const { ctx, input } = opts
const limit = input.limit ?? 15
const { cursor } = input

const users = await ctx.db.user.findMany({
take: limit + 1, // get an extra item at the end which we'll use as next cursor
cursor: cursor ? { id: cursor } : undefined,
where: {
externalId: {
not: ctx.auth.userId
}
},
orderBy: {
createdAt: 'desc'
}
})

let nextCursor: typeof cursor | undefined = undefined
if (users.length > limit) {
const nextItem = users.pop()
nextCursor = nextItem!.id
}

return {
users,
nextCursor
}
})
})

0 comments on commit 0e7a9a3

Please sign in to comment.