diff --git a/src/pages/SharedBucketDetail/SharedBucketDetail.tsx b/src/pages/SharedBucketDetail/SharedBucketDetail.tsx index 9b06c2d7..6ccd9bb7 100644 --- a/src/pages/SharedBucketDetail/SharedBucketDetail.tsx +++ b/src/pages/SharedBucketDetail/SharedBucketDetail.tsx @@ -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) @@ -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]) { diff --git a/src/services/sharedBucketDetail.ts b/src/services/sharedBucketDetail.ts index f677c2d2..f462c33d 100644 --- a/src/services/sharedBucketDetail.ts +++ b/src/services/sharedBucketDetail.ts @@ -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 { @@ -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 { @@ -90,14 +90,41 @@ export const fetchSharedBucketDetailData = async (teamId: string, shortName: str } } +export const fetchAllTeams = async (): Promise => { + 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 => { 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) diff --git a/src/utils/utils.ts b/src/utils/utils.ts index c1cef455..4541594f 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -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` @@ -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) => {