From 4a712896c6d0c6772caa528c90429850399c14f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=8Dvar=20Oddsson?= Date: Fri, 17 Jan 2025 09:52:03 +0000 Subject: [PATCH] chore(j-s): Use lawyer registry from api (#17434) * Refactor lawyer registry * Simplify code * Handle errors from API * Improve type safety * Add error handling to get single lawyer * Refactor --------- Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> --- .../web/messages/Core/errors.ts | 6 +++ .../pages/api/lawyers/getLawyer/getLawyer.ts | 46 ---------------- .../web/pages/api/lawyers/getLawyer/index.ts | 3 -- .../api/lawyers/getLawyers/getLawyers.ts | 36 ------------- .../web/pages/api/lawyers/getLawyers/index.ts | 3 -- .../src/utils/hooks/useLawyers/useLawyers.ts | 52 +++++++++++++------ 6 files changed, 43 insertions(+), 103 deletions(-) delete mode 100644 apps/judicial-system/web/pages/api/lawyers/getLawyer/getLawyer.ts delete mode 100644 apps/judicial-system/web/pages/api/lawyers/getLawyer/index.ts delete mode 100644 apps/judicial-system/web/pages/api/lawyers/getLawyers/getLawyers.ts delete mode 100644 apps/judicial-system/web/pages/api/lawyers/getLawyers/index.ts diff --git a/apps/judicial-system/web/messages/Core/errors.ts b/apps/judicial-system/web/messages/Core/errors.ts index 02927f00f07f..b2f48b364cdd 100644 --- a/apps/judicial-system/web/messages/Core/errors.ts +++ b/apps/judicial-system/web/messages/Core/errors.ts @@ -94,6 +94,12 @@ export const errors = defineMessages({ description: 'Notaður sem villuskilaboð þegar ekki gengur að sækja lögmanna skrá', }, + fetchLawyer: { + id: 'judicial.system.core:errors.fetch_lawyer', + defaultMessage: 'Upp kom villa við að sækja lögmann úr lögmanna skrá', + description: + 'Notaður sem villuskilaboð þegar ekki gengur að sækja lögmann úr lögmanna skrá', + }, copyLink: { id: 'judicial.system.core:errors.copy_link', defaultMessage: 'Ekki tókst að afrita hlekk', diff --git a/apps/judicial-system/web/pages/api/lawyers/getLawyer/getLawyer.ts b/apps/judicial-system/web/pages/api/lawyers/getLawyer/getLawyer.ts deleted file mode 100644 index 925dc364551f..000000000000 --- a/apps/judicial-system/web/pages/api/lawyers/getLawyer/getLawyer.ts +++ /dev/null @@ -1,46 +0,0 @@ -import { NextApiRequest, NextApiResponse } from 'next' - -import { type Lawyer, mapToLawyer } from '@island.is/judicial-system/types' -import { validate } from '@island.is/judicial-system-web/src/utils/validate' - -const getLawyer = async (nationalId: string): Promise => { - const isValid = validate([[nationalId, ['empty', 'national-id']]]).isValid - if (!isValid) { - throw new Error('Invalid national id') - } - - const response = await fetch(`https://lmfi.is/api/lawyer/${nationalId}`, { - headers: { - Authorization: `Basic ${process.env.LAWYERS_ICELAND_API_KEY}`, - Accept: 'application/json', - }, - }) - - if (response.ok) { - const lawyer = await response.json() - const lawyerMapped = { - ...mapToLawyer(lawyer), - } - return lawyerMapped - } - - const reason = await response.text() - console.error('Failed to get lawyer:', reason) - throw new Error(reason) -} - -export default async function handler( - req: NextApiRequest, - res: NextApiResponse, -) { - const { nationalId } = req.query - - const laywer = await getLawyer(nationalId as string) - - /* Max age is 30 minutes, revalided if repeated within 30 sec*/ - res.setHeader( - 'Cache-Control', - 'public, s-maxage=1800, stale-while-revalidate=30', - ) - res.status(200).json(laywer) -} diff --git a/apps/judicial-system/web/pages/api/lawyers/getLawyer/index.ts b/apps/judicial-system/web/pages/api/lawyers/getLawyer/index.ts deleted file mode 100644 index 1a85508093d8..000000000000 --- a/apps/judicial-system/web/pages/api/lawyers/getLawyer/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -import handler from './getLawyer' - -export default handler diff --git a/apps/judicial-system/web/pages/api/lawyers/getLawyers/getLawyers.ts b/apps/judicial-system/web/pages/api/lawyers/getLawyers/getLawyers.ts deleted file mode 100644 index 5487b0755c1a..000000000000 --- a/apps/judicial-system/web/pages/api/lawyers/getLawyers/getLawyers.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { NextApiRequest, NextApiResponse } from 'next' - -import { type Lawyer, mapToLawyer } from '@island.is/judicial-system/types' - -const getLawyers = async (): Promise => { - const response = await fetch('https://lmfi.is/api/lawyers', { - headers: { - Authorization: `Basic ${process.env.LAWYERS_ICELAND_API_KEY}`, - Accept: 'application/json', - }, - }) - - if (response.ok) { - const lawyers = await response.json() - const lawyersMapped = (lawyers || []).map(mapToLawyer) - return lawyersMapped - } - - const reason = await response.text() - console.error('Failed to get lawyers:', reason) - throw new Error(reason) -} - -export default async function handler( - _req: NextApiRequest, - res: NextApiResponse, -) { - const laywers = await getLawyers() - - /* Max age is 30 minutes, revalided if repeated within 30 sec*/ - res.setHeader( - 'Cache-Control', - 'public, s-maxage=1800, stale-while-revalidate=30', - ) - res.status(200).json(laywers) -} diff --git a/apps/judicial-system/web/pages/api/lawyers/getLawyers/index.ts b/apps/judicial-system/web/pages/api/lawyers/getLawyers/index.ts deleted file mode 100644 index 357cf2171450..000000000000 --- a/apps/judicial-system/web/pages/api/lawyers/getLawyers/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -import handler from './getLawyers' - -export default handler diff --git a/apps/judicial-system/web/src/utils/hooks/useLawyers/useLawyers.ts b/apps/judicial-system/web/src/utils/hooks/useLawyers/useLawyers.ts index 57f8727e9122..aa14c8a97225 100644 --- a/apps/judicial-system/web/src/utils/hooks/useLawyers/useLawyers.ts +++ b/apps/judicial-system/web/src/utils/hooks/useLawyers/useLawyers.ts @@ -5,18 +5,25 @@ import { toast } from '@island.is/island-ui/core' import { type Lawyer } from '@island.is/judicial-system/types' import { errors as errorMessages } from '@island.is/judicial-system-web/messages' +const LAWYER_REGISTRY_URL = '/api/defender/lawyerRegistry' + export const useGetLawyers = (): Lawyer[] => { const { formatMessage } = useIntl() - const { data, error } = useSWR( - '/api/lawyers/getLawyers', - (url: string) => fetch(url).then((res) => res.json()), - { - revalidateIfStale: false, - revalidateOnFocus: false, - revalidateOnReconnect: false, - errorRetryCount: 2, - }, - ) + const fetcher = (url: string): Promise => + fetch(url).then((res) => { + if (!res.ok) { + throw new Error('Failed to get lawyers from lawyer registry') + } + + return res.json() + }) + + const { data, error } = useSWR(LAWYER_REGISTRY_URL, fetcher, { + revalidateIfStale: false, + revalidateOnFocus: false, + revalidateOnReconnect: false, + errorRetryCount: 2, + }) if (error) { toast.error(formatMessage(errorMessages.fetchLawyers)) @@ -30,12 +37,22 @@ export const useGetLawyer = ( nationalId?: string | null, shouldFetch?: boolean, ): Lawyer | undefined => { - const fetchWithNationalId = (url: string, nationalId: string) => - fetch(`${url}?nationalId=${nationalId}`).then((res) => res.json()) + const { formatMessage } = useIntl() + + const fetcher = (url: string): Promise => + fetch(url).then((res) => { + if (!res.ok) { + throw new Error( + `Failed to get lawyer with nationalId ${nationalId} from lawyer registry`, + ) + } - const { data } = useSWR( - nationalId && shouldFetch ? [`/api/lawyers/getLawyer`, nationalId] : null, - fetchWithNationalId, + return res.json() + }) + + const { data, error } = useSWR( + nationalId && shouldFetch ? `${LAWYER_REGISTRY_URL}/${nationalId}` : null, + fetcher, { revalidateIfStale: false, revalidateOnFocus: false, @@ -44,5 +61,10 @@ export const useGetLawyer = ( }, ) + if (error) { + toast.error(formatMessage(errorMessages.fetchLawyer)) + return undefined + } + return data }