Skip to content

Commit

Permalink
fix: Updated according to PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdigdir committed Oct 7, 2024
1 parent 7c784b3 commit accd02a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
21 changes: 10 additions & 11 deletions packages/bff/src/graphql/types/organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ interface TransformedOrganization {
environments: string[];
}

const organizationsRedisKey = 'transformedOrganizations';

async function fetchOrganizations() {
const { default: logger } = await import('@digdir/dialogporten-node-logger');
try {
const response = await fetch('https://altinncdn.no/orgs/altinn-orgs.json');
if (!response.ok) {
Expand All @@ -52,28 +55,28 @@ async function storeOrganizationsInRedis() {
const { default: redisClient } = await import('../../redisClient.ts');
const organizations = await fetchOrganizations();
const transformedOrganizations = organizations!.flatMap((org) => convertOrgsToJson(org));
await redisClient.set('transformedOrganizations', JSON.stringify(transformedOrganizations), 'EX', 86400);
await redisClient.set(organizationsRedisKey, JSON.stringify(transformedOrganizations), 'EX', 86400);
} catch (error) {
console.error('Error storing organizations in Redis:', error);
}
}
export async function deleteOrganizationsFromRedis() {
try {
const { default: redisClient } = await import('../../redisClient.ts');
await redisClient.del('transformedOrganizations');
await redisClient.del(organizationsRedisKey);
} catch (error) {
console.error('Error deleting organizations from Redis:', error);
}
}

deleteOrganizationsFromRedis();

export async function getOrganizationsFromRedis() {
try {
const { default: redisClient } = await import('../../redisClient.ts');
const data = await redisClient.get('transformedOrganizations');
const data = await redisClient.get(organizationsRedisKey);
if (data) {
const d = JSON.parse(data);
const transformedOrganizations = JSON.parse(data);
return transformedOrganizations;
return JSON.parse(data);
}
await storeOrganizationsInRedis();
return await getOrganizationsFromRedis();
Expand All @@ -89,11 +92,7 @@ function convertOrgsToJson(orgs: Orgs): TransformedOrganization[] {
const { name, logo, orgnr, homepage, environments } = details;
result.push({
id,
name: {
en: name.en,
nb: name.nb,
nn: name.nn,
},
name,
logo,
orgnr,
homepage,
Expand Down
6 changes: 3 additions & 3 deletions packages/frontend/src/api/organizations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import type { OrganizationFieldsFragment } from 'bff-types-generated';

interface OrganizationOutput {
name: string;
logo?: string;
logo: string;
}

export const getOrganization = (
organizations: OrganizationFieldsFragment[],
org: string,
locale: string,
): OrganizationOutput | undefined => {
const currentOrg = organizations?.find((o) => o.id?.includes(org as string));
const name = currentOrg?.name && ((currentOrg.name[locale as keyof typeof currentOrg.name] ?? '') as string);
const currentOrg = organizations?.find((o) => o.id === (org as string));
const name = currentOrg?.name && (currentOrg.name[locale as keyof typeof currentOrg.name] ?? '');
const logo = currentOrg?.logo ?? '';
if (name) {
return {
Expand Down
6 changes: 2 additions & 4 deletions packages/frontend/src/api/useDialogs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,7 @@ export const useSearchDialogs = ({ parties, searchString, org }: searchDialogsPr

// biome-ignore lint/correctness/useExhaustiveDependencies: Full control of what triggers this code is needed
useEffect(() => {
setSearchResults(
enabled ? mapDialogDtoToInboxItem(data?.searchDialogs?.items ?? [], parties, organizations || []) : [],
);
setSearchResults(enabled ? mapDialogDtoToInboxItem(data?.searchDialogs?.items ?? [], parties, organizations) : []);
}, [setSearchResults, data?.searchDialogs?.items, enabled]);

return {
Expand Down Expand Up @@ -160,7 +158,7 @@ export const useDialogs = (parties: PartyFieldsFragment[]): UseDialogsOutput =>
queryFn: () => getDialogs(partyURIs),
enabled: partyURIs.length > 0,
});
const dialogs = mapDialogDtoToInboxItem(data?.searchDialogs?.items ?? [], parties, organizations || []);
const dialogs = mapDialogDtoToInboxItem(data?.searchDialogs?.items ?? [], parties, organizations);
return {
isLoading,
isSuccess,
Expand Down

0 comments on commit accd02a

Please sign in to comment.