From d1e86ccffce6bef240856afb981179d42fa5b138 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=8Dvar=20Oddsson?= Date: Tue, 7 Jan 2025 15:19:27 +0000 Subject: [PATCH 1/6] Refactor lawyer registry --- .../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 | 10 ++-- 5 files changed, 6 insertions(+), 92 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/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..b060368786ff 100644 --- a/apps/judicial-system/web/src/utils/hooks/useLawyers/useLawyers.ts +++ b/apps/judicial-system/web/src/utils/hooks/useLawyers/useLawyers.ts @@ -8,7 +8,7 @@ import { errors as errorMessages } from '@island.is/judicial-system-web/messages export const useGetLawyers = (): Lawyer[] => { const { formatMessage } = useIntl() const { data, error } = useSWR( - '/api/lawyers/getLawyers', + '/api/defender/lawyerRegistry', (url: string) => fetch(url).then((res) => res.json()), { revalidateIfStale: false, @@ -30,11 +30,13 @@ 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 fetchWithNationalId = (url: string) => + fetch(url).then((res) => res.json()) const { data } = useSWR( - nationalId && shouldFetch ? [`/api/lawyers/getLawyer`, nationalId] : null, + nationalId && shouldFetch + ? [`/api/defender/lawyerRegistry/${nationalId}`, nationalId] + : null, fetchWithNationalId, { revalidateIfStale: false, From 2493599ad66d0adaa6c87bdfe5082bcb06a05ba4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=8Dvar=20Oddsson?= Date: Tue, 7 Jan 2025 15:27:46 +0000 Subject: [PATCH 2/6] Simplify code --- .../web/src/utils/hooks/useLawyers/useLawyers.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) 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 b060368786ff..b398489ceec1 100644 --- a/apps/judicial-system/web/src/utils/hooks/useLawyers/useLawyers.ts +++ b/apps/judicial-system/web/src/utils/hooks/useLawyers/useLawyers.ts @@ -7,9 +7,11 @@ import { errors as errorMessages } from '@island.is/judicial-system-web/messages export const useGetLawyers = (): Lawyer[] => { const { formatMessage } = useIntl() + const fetcher = (url: string) => fetch(url).then((res) => res.json()) + const { data, error } = useSWR( '/api/defender/lawyerRegistry', - (url: string) => fetch(url).then((res) => res.json()), + fetcher, { revalidateIfStale: false, revalidateOnFocus: false, @@ -30,14 +32,13 @@ export const useGetLawyer = ( nationalId?: string | null, shouldFetch?: boolean, ): Lawyer | undefined => { - const fetchWithNationalId = (url: string) => - fetch(url).then((res) => res.json()) + const fetcher = (url: string) => fetch(url).then((res) => res.json()) const { data } = useSWR( nationalId && shouldFetch - ? [`/api/defender/lawyerRegistry/${nationalId}`, nationalId] + ? `/api/defender/lawyerRegistry/${nationalId}` : null, - fetchWithNationalId, + fetcher, { revalidateIfStale: false, revalidateOnFocus: false, From 73ac9ed0f26306ae28bb916b220b7eec9f676343 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=8Dvar=20Oddsson?= Date: Tue, 7 Jan 2025 22:57:26 +0000 Subject: [PATCH 3/6] Handle errors from API --- .../src/utils/hooks/useLawyers/useLawyers.ts | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) 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 b398489ceec1..6f4b79b971ca 100644 --- a/apps/judicial-system/web/src/utils/hooks/useLawyers/useLawyers.ts +++ b/apps/judicial-system/web/src/utils/hooks/useLawyers/useLawyers.ts @@ -7,7 +7,14 @@ import { errors as errorMessages } from '@island.is/judicial-system-web/messages export const useGetLawyers = (): Lawyer[] => { const { formatMessage } = useIntl() - const fetcher = (url: string) => fetch(url).then((res) => res.json()) + const fetcher = (url: string) => + fetch(url).then((res) => { + if (!res.ok) { + throw new Error('Failed to get lawyers from lawyer registry') + } + + return res.json() + }) const { data, error } = useSWR( '/api/defender/lawyerRegistry', @@ -32,7 +39,16 @@ export const useGetLawyer = ( nationalId?: string | null, shouldFetch?: boolean, ): Lawyer | undefined => { - const fetcher = (url: string) => fetch(url).then((res) => res.json()) + const fetcher = (url: string) => + fetch(url).then((res) => { + if (!res.ok) { + throw new Error( + `Failed to get lawyer with nationalId ${nationalId} from lawyer registry`, + ) + } + + return res.json() + }) const { data } = useSWR( nationalId && shouldFetch From 303da940a9d759088bc0988dc7eda11421fc89d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=8Dvar=20Oddsson?= Date: Thu, 9 Jan 2025 14:42:32 +0000 Subject: [PATCH 4/6] Improve type safety --- .../web/src/utils/hooks/useLawyers/useLawyers.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 6f4b79b971ca..cb2d452f234c 100644 --- a/apps/judicial-system/web/src/utils/hooks/useLawyers/useLawyers.ts +++ b/apps/judicial-system/web/src/utils/hooks/useLawyers/useLawyers.ts @@ -7,7 +7,7 @@ import { errors as errorMessages } from '@island.is/judicial-system-web/messages export const useGetLawyers = (): Lawyer[] => { const { formatMessage } = useIntl() - const fetcher = (url: string) => + const fetcher = (url: string): Promise => fetch(url).then((res) => { if (!res.ok) { throw new Error('Failed to get lawyers from lawyer registry') @@ -39,7 +39,7 @@ export const useGetLawyer = ( nationalId?: string | null, shouldFetch?: boolean, ): Lawyer | undefined => { - const fetcher = (url: string) => + const fetcher = (url: string): Promise => fetch(url).then((res) => { if (!res.ok) { throw new Error( From fd6946a196974ae6f54a31b9b461f43442e413c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=8Dvar=20Oddsson?= Date: Thu, 9 Jan 2025 14:45:56 +0000 Subject: [PATCH 5/6] Add error handling to get single lawyer --- apps/judicial-system/web/messages/Core/errors.ts | 6 ++++++ .../web/src/utils/hooks/useLawyers/useLawyers.ts | 9 ++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) 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/src/utils/hooks/useLawyers/useLawyers.ts b/apps/judicial-system/web/src/utils/hooks/useLawyers/useLawyers.ts index cb2d452f234c..5d9a9bcdcf03 100644 --- a/apps/judicial-system/web/src/utils/hooks/useLawyers/useLawyers.ts +++ b/apps/judicial-system/web/src/utils/hooks/useLawyers/useLawyers.ts @@ -39,6 +39,8 @@ export const useGetLawyer = ( nationalId?: string | null, shouldFetch?: boolean, ): Lawyer | undefined => { + const { formatMessage } = useIntl() + const fetcher = (url: string): Promise => fetch(url).then((res) => { if (!res.ok) { @@ -50,7 +52,7 @@ export const useGetLawyer = ( return res.json() }) - const { data } = useSWR( + const { data, error } = useSWR( nationalId && shouldFetch ? `/api/defender/lawyerRegistry/${nationalId}` : null, @@ -63,5 +65,10 @@ export const useGetLawyer = ( }, ) + if (error) { + toast.error(formatMessage(errorMessages.fetchLawyer)) + return undefined + } + return data } From b22a8b49f5878b501c02af763b435480eadde5e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=8Dvar=20Oddsson?= Date: Mon, 13 Jan 2025 14:32:19 +0000 Subject: [PATCH 6/6] Refactor --- .../src/utils/hooks/useLawyers/useLawyers.ts | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) 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 5d9a9bcdcf03..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,6 +5,8 @@ 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 fetcher = (url: string): Promise => @@ -16,16 +18,12 @@ export const useGetLawyers = (): Lawyer[] => { return res.json() }) - const { data, error } = useSWR( - '/api/defender/lawyerRegistry', - fetcher, - { - revalidateIfStale: false, - revalidateOnFocus: false, - revalidateOnReconnect: false, - errorRetryCount: 2, - }, - ) + const { data, error } = useSWR(LAWYER_REGISTRY_URL, fetcher, { + revalidateIfStale: false, + revalidateOnFocus: false, + revalidateOnReconnect: false, + errorRetryCount: 2, + }) if (error) { toast.error(formatMessage(errorMessages.fetchLawyers)) @@ -53,9 +51,7 @@ export const useGetLawyer = ( }) const { data, error } = useSWR( - nationalId && shouldFetch - ? `/api/defender/lawyerRegistry/${nationalId}` - : null, + nationalId && shouldFetch ? `${LAWYER_REGISTRY_URL}/${nationalId}` : null, fetcher, { revalidateIfStale: false,