Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

Add generated team page from BambooHR API #5772

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/lib/fetchTeam.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
export interface TeamMember {
name: string
reports: TeamMember[]
}

interface BambooEmployee {
displayName: string
preferredName: string
firstName: string
lastName: string
supervisor: string
}

interface BambooEmployeeWithReports extends BambooEmployee {
reports: BambooEmployeeWithReports[]
}

interface BambooDirectoryResponse {
employees: BambooEmployee[]
}

export async function fetchTeam(): Promise<BambooEmployee[]> {
const authorizationHeader = Buffer.from(`${process.env.BAMBOO_API_KEY ?? ''}:x`).toString('base64')
const response = await fetch('https://api.bamboohr.com/api/gateway.php/sourcegraph/v1/employees/directory', {
method: 'GET',
headers: {
accept: 'application/json',
authorization: `Basic ${authorizationHeader}`,
},
})

const body = (await response.json()) as BambooDirectoryResponse

return body.employees
}

export async function getTeamMemberTree(): Promise<TeamMember> {
const employees = await fetchTeam()
const rootEmployee = employees.find(employee => employee.supervisor === '')

if (!rootEmployee) {
throw new Error('No root employee found')
}

const supervisorMap = new Map<string, BambooEmployee>()
for (const employee of employees) {
supervisorMap.set(employee.displayName, employee)
}

function addReports(employee: BambooEmployee): BambooEmployeeWithReports {
const reports = employees.filter(e => e.supervisor === employee.displayName)
return { ...employee, reports: reports.map(addReports) }
}

const employeeTreeWithReports = addReports(rootEmployee)

function convertEmployeeTreeToTeamMembers(employee: BambooEmployeeWithReports): TeamMember {
return {
name: `${employee.preferredName ?? employee.firstName} ${employee.lastName}`,
reports: employee.reports.map(convertEmployeeTreeToTeamMembers),
}
}

return convertEmployeeTreeToTeamMembers(employeeTreeWithReports)
}
38 changes: 38 additions & 0 deletions src/pages/generated-team.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React from 'react'

import { getTeamMemberTree, TeamMember } from '../lib/fetchTeam'

interface Props {
teamMemberTree: TeamMember
}

function TeamMemberListItem({ teamMember }: { teamMember: TeamMember }): JSX.Element {
return (
<li>
{teamMember.name}
<ul>
{teamMember.reports.map(t => (
<TeamMemberListItem key={t.name} teamMember={t} />
))}
</ul>
</li>
)
}

export default function GeneratedPage({ teamMemberTree }: { teamMemberTree: TeamMember }): JSX.Element {
return (
<div className="container">
<section id="content">
<h1>Team</h1>
<ul>
<TeamMemberListItem teamMember={teamMemberTree} />
</ul>
</section>
</div>
)
}

export async function getStaticProps(): Promise<{ props: Props }> {
const teamMemberTree = await getTeamMemberTree()
return { props: { teamMemberTree } }
}