From 143df44e820fb1f9fa4df22a2e2e529a8932423a Mon Sep 17 00:00:00 2001 From: johnnadeluy Date: Mon, 15 Apr 2024 14:45:03 +0200 Subject: [PATCH 1/2] Adds a function to get team from group for Shared Bucket Detail to replace manually added suffixes to be stripped DPSTAT-922 --- .../SharedBucketDetail/SharedBucketDetail.tsx | 4 +-- src/services/sharedBucketDetail.ts | 35 ++++++++++++++++--- src/utils/utils.ts | 28 ++++++++++----- 3 files changed, 53 insertions(+), 14 deletions(-) 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..8b0408d8 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._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..ebec8b7f 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,27 @@ 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: Replaced by the getTeamFromGroup function; consider removing +// 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)$/ +// // 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, '') +// // 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) => { From 5ac5b754652812a9a438d992c4645b8b55ee9ff6 Mon Sep 17 00:00:00 2001 From: johnnadeluy Date: Mon, 15 Apr 2024 14:54:39 +0200 Subject: [PATCH 2/2] Minor code refactoring --- src/services/sharedBucketDetail.ts | 2 +- src/utils/utils.ts | 13 ------------- 2 files changed, 1 insertion(+), 14 deletions(-) diff --git a/src/services/sharedBucketDetail.ts b/src/services/sharedBucketDetail.ts index 8b0408d8..f462c33d 100644 --- a/src/services/sharedBucketDetail.ts +++ b/src/services/sharedBucketDetail.ts @@ -100,7 +100,7 @@ export const fetchAllTeams = async (): Promise => { try { const teams = await fetchAPIData(teamsUrl.toString()) if (!teams) throw new ApiError(500, 'No json data returned') - if (!teams._embedded || !teams._embedded.teams) return {} as Team[] + if (!teams._embedded?.teams) return {} as Team[] const flattedTeams = flattenEmbedded({ ...teams }) return flattedTeams.teams diff --git a/src/utils/utils.ts b/src/utils/utils.ts index ebec8b7f..4541594f 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -12,19 +12,6 @@ export const getGroupType = (teamName: string, groupName: string): string => { return groupName.slice(teamName.length + 1) } -// TODO: Replaced by the getTeamFromGroup function; consider removing -// 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 }) => {