Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a function to get team from group for Shared Bucket Detail to re… #230

Merged
merged 2 commits into from
Apr 15, 2024
Merged
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
4 changes: 2 additions & 2 deletions src/pages/SharedBucketDetail/SharedBucketDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { DaplaCtrlContext } from '../../provider/DaplaCtrlProvider'
import { useState, useEffect, useContext } from 'react'
import { useParams, useNavigate } from 'react-router-dom'
import { Dialog, LeadParagraph, Text, Link } from '@statisticsnorway/ssb-component-library'
import { formatDisplayName, getGroupType, stripSuffixes } from '../../utils/utils'
import { formatDisplayName, getGroupType, getTeamFromGroup } from '../../utils/utils'

const SharedBucketDetail = () => {
const { setBreadcrumbTeamDetailDisplayName, setBreadcrumbBucketDetailDisplayName } = useContext(DaplaCtrlContext)
Expand Down Expand Up @@ -46,7 +46,7 @@ const SharedBucketDetail = () => {

;((response['sharedBucket'] as SharedBucket).groups ?? []).forEach(({ uniform_name, users }) => {
;(users ?? []).forEach((user) => {
const team_name = stripSuffixes(uniform_name)
const team_name = getTeamFromGroup(response['allTeams'] as Team[], uniform_name)
const group_type = getGroupType(team_name, uniform_name)
const key = `${user.principal_name}-${team_name}`
if (!usersMap[key]) {
Expand Down
35 changes: 31 additions & 4 deletions src/services/sharedBucketDetail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { flattenEmbedded, DAPLA_TEAM_API_URL } from '../utils/utils'
const TEAMS_URL = `${DAPLA_TEAM_API_URL}/teams`

export interface SharedBucketDetail {
[key: string]: Team | SharedBucket
[key: string]: Team | SharedBucket | Team[]
}

export interface SharedBucket {
Expand All @@ -16,7 +16,7 @@ export interface SharedBucket {
export interface Team {
uniform_name: string
display_name?: string
section_name: string
section_name?: string
}

export interface User {
Expand Down Expand Up @@ -90,14 +90,41 @@ export const fetchSharedBucketDetailData = async (teamId: string, shortName: str
}
}

export const fetchAllTeams = async (): Promise<Team[]> => {
const teamsUrl = new URL(`${TEAMS_URL}`, window.location.origin)

const selects = ['uniform_name']

teamsUrl.searchParams.append('select', selects.join(','))

try {
const teams = await fetchAPIData(teamsUrl.toString())
if (!teams) throw new ApiError(500, 'No json data returned')
if (!teams._embedded?.teams) return {} as Team[]

const flattedTeams = flattenEmbedded({ ...teams })
return flattedTeams.teams
} catch (error) {
if (error instanceof ApiError) {
console.error('Failed to fetch all teams:', error)
throw error
} else {
const apiError = new ApiError(500, 'An unexpected error occurred')
console.error('Failed to fetch all teams:', apiError)
throw apiError
}
}
}

export const getSharedBucketDetailData = async (teamId: string, shortName: string): Promise<SharedBucketDetail> => {
try {
const [team, sharedBucket] = await Promise.all([
const [team, sharedBucket, allTeams] = await Promise.all([
fetchTeamDetail(teamId),
fetchSharedBucketDetailData(teamId, shortName),
fetchAllTeams(),
])

return { team, sharedBucket }
return { team, sharedBucket, allTeams }
} catch (error) {
if (error instanceof ApiError) {
console.error('Failed to fetch shared bucket detail:', error)
Expand Down
19 changes: 9 additions & 10 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import { DropdownItems } from '../@types/pageTypes'
import { JobResponse } from '../services/teamDetail'
import { Team } from '../services/sharedBucketDetail'

export const DAPLA_TEAM_API_URL = `/api`

Expand All @@ -11,16 +12,14 @@ export const getGroupType = (teamName: string, groupName: string): string => {
return groupName.slice(teamName.length + 1)
}

export const stripSuffixes = (inputString: string) => {
// Regular expression to match the specified suffixes

// TODO: Get suffixes from function parameters (which is passed from query response), this because we dont know
// all the custom groups, and they can be created at any time
// this may require a re-write of how we fetch and aggregate data in SharedBucketDetail.tsx
const suffixesPattern = /-(data-admins|managers|developers|consumers|support|editor|admin)$/

// Replace matched suffix with an empty string
return inputString.replace(suffixesPattern, '')
export const getTeamFromGroup = (allTeams: Team[], groupName: string): string => {
if (allTeams?.length) {
const teamName = allTeams.filter(({ uniform_name }) => {
if (groupName.includes(uniform_name)) return uniform_name
})
return teamName[0].uniform_name
}
return ''
}

export const formatDisplayName = (displayName: string) => {
Expand Down