From c5bc3497d6197882bf1b3861431a0d622e7f69b1 Mon Sep 17 00:00:00 2001 From: Marek Date: Fri, 9 Dec 2022 19:58:43 -0500 Subject: [PATCH] Add generated team page --- src/lib/fetchTeam.ts | 65 ++++++++++++++++++++++++++++++++++++ src/pages/generated-team.tsx | 38 +++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 src/lib/fetchTeam.ts create mode 100644 src/pages/generated-team.tsx diff --git a/src/lib/fetchTeam.ts b/src/lib/fetchTeam.ts new file mode 100644 index 000000000000..bb32062e0b74 --- /dev/null +++ b/src/lib/fetchTeam.ts @@ -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 { + 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 { + const employees = await fetchTeam() + const rootEmployee = employees.find(employee => employee.supervisor === '') + + if (!rootEmployee) { + throw new Error('No root employee found') + } + + const supervisorMap = new Map() + 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) +} diff --git a/src/pages/generated-team.tsx b/src/pages/generated-team.tsx new file mode 100644 index 000000000000..632832b06546 --- /dev/null +++ b/src/pages/generated-team.tsx @@ -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 ( +
  • + {teamMember.name} +
      + {teamMember.reports.map(t => ( + + ))} +
    +
  • + ) +} + +export default function GeneratedPage({ teamMemberTree }: { teamMemberTree: TeamMember }): JSX.Element { + return ( +
    +
    +

    Team

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