Skip to content

Commit

Permalink
fix(boards): get all boards for a user
Browse files Browse the repository at this point in the history
  • Loading branch information
emilielr committed Jun 10, 2024
1 parent def541f commit 1946000
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 19 deletions.
34 changes: 28 additions & 6 deletions next-tavla/app/(admin)/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,35 @@ export async function getPrivateBoardsForUser() {
.flat()
}

export async function getBoardsForUser() {
const user = await getUserFromSessionCookie()
if (!user) return redirect('/')
export async function getAllBoardsForUser() {
const user = await getUser()
if (!user)
throw new TavlaError({
code: 'NOT_FOUND',
message: `Found no user`,
})

const privateBoardIDs = concat(user.owner ?? [], user.editor ?? [])

const organizations = await getOrganizationsForUser()
const organizationBoardIDs = organizations.map((o) => o.boards)

const boardRef = await firestore().collection('boards').get()
const boardIDs = concat(privateBoardIDs, organizationBoardIDs.flat())

return boardRef.docs
.map((doc) => ({ id: doc.id, ...doc.data() } as TBoard))
const batchedBoardIDs = chunk(boardIDs, 20)

const boardQueries = batchedBoardIDs.map((batch) =>
firestore()
.collection('boards')
.where(firestore.FieldPath.documentId(), 'in', batch)
.get(),
)

const boardRefs = await Promise.all(boardQueries)

return boardRefs
.map((ref) =>
ref.docs.map((doc) => ({ id: doc.id, ...doc.data() } as TBoard)),
)
.flat()
}
37 changes: 24 additions & 13 deletions next-tavla/app/(admin)/boards/[[...id]]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import { FilterButton } from '../components/FilterButton'
import { BoardTable } from '../components/BoardTable'
import { Metadata } from 'next'
import React from 'react'
import { getBoardsForUser, getOrganizationsForUser } from 'app/(admin)/actions'
import {
getAllBoardsForUser,
getOrganizationsForUser,
} from 'app/(admin)/actions'
import { initializeAdminApp } from 'app/(admin)/utils/firebase'
import { getUserFromSessionCookie } from 'app/(admin)/utils/server'
import { Heading1 } from '@entur/typography'
import { TBoardWithOrganization } from 'types/settings'
import { TBoard, TBoardWithOrganization } from 'types/settings'

initializeAdminApp()

Expand All @@ -20,18 +23,9 @@ async function OrganizationsBoardsPage() {
const user = await getUserFromSessionCookie()
if (!user) permanentRedirect('/')

const organizations = await getOrganizationsForUser()
const boards = await getBoardsForUser()
const boards = await getAllBoardsForUser()

const boardsWithOrganization = boards.map((board) => {
const org = organizations.find((org) =>
org.boards?.includes(board.id ?? ''),
)
return {
board: { ...board },
organization: { ...org },
} as TBoardWithOrganization
})
const boardsWithOrganization = await getBoardsWithOrganization(boards)

return (
<div className="flex flex-col gap-8">
Expand All @@ -46,3 +40,20 @@ async function OrganizationsBoardsPage() {
}

export default OrganizationsBoardsPage

async function getBoardsWithOrganization(boards: TBoard[]) {
const organizations = await getOrganizationsForUser()

const boardsWithOrganization = boards.map((board: TBoard) => {
const org = organizations.find((org) =>
org.boards?.includes(board.id ?? ''),
)

return {
board: { ...board },
organization: { ...org },
} as TBoardWithOrganization
})

return boardsWithOrganization
}

0 comments on commit 1946000

Please sign in to comment.