@@ -20,69 +20,81 @@ function getOrganisationLinkHref(id, redirectUrl) {
2020}
2121
2222export async function fetchWithAuthInterception ( url , request , h ) {
23- const { data , response } = await fetchWithAuthHeader ( url , request )
23+ request . logger . info ( { url } , 'auth: fetchWithAuthInterception' )
2424
25- if ( ! response . ok ) {
26- throw Boom . unauthorized ( )
27- }
28-
29- if ( data . action === 'link-organisations' ) {
30- const { path : redirectUrl } = request
31- const { defraId, isCurrentOrganisationLinked, organisations } = data
32- const hasManyOrganisations = organisations . length > 1
33- const entityName = hasManyOrganisations ? 'organisations' : 'organisation'
34- const shouldShowTableOfUnlinkedOrganisations =
35- hasManyOrganisations && ! isCurrentOrganisationLinked
36- const otherRelationships = defraId . otherRelationships . map (
37- ( { defraIdOrgName, defraIdRelationshipId } ) => [
38- { text : defraIdOrgName } ,
39- {
40- html : `<a href="http://localhost:3200/cdp-defra-id-stub/register/${ defraId . userId } /relationship/${ defraIdRelationshipId } /current">Switch</a>`
41- }
42- ]
43- )
25+ try {
26+ const { data, response } = await fetchWithAuthHeader ( url , request )
4427
45- const commonTemplateData = {
46- defraIdOrgName : defraId . orgName ,
47- entityName,
48- isCurrentOrganisationLinked,
49- otherRelationships,
50- shouldShowTableOfUnlinkedOrganisations
28+ if ( ! response . ok ) {
29+ throw Boom . unauthorized ( )
5130 }
5231
53- const templateData = shouldShowTableOfUnlinkedOrganisations
54- ? {
55- ...commonTemplateData ,
56- organisations : organisations . map ( ( { name, orgId, id } ) => [
57- { text : name ?? 'data missing' } ,
58- { text : orgId ?? 'data missing' } ,
59- { text : id ?? 'data missing' } ,
60- {
61- html : `<a href="${ getOrganisationLinkHref ( id , redirectUrl ) } ">Link</a>`
32+ if ( data . action === 'link-organisations' ) {
33+ const { path : redirectUrl } = request
34+ const { defraId, isCurrentOrganisationLinked, organisations } = data
35+ const hasManyOrganisations = organisations . length > 1
36+ const entityName = hasManyOrganisations ? 'organisations' : 'organisation'
37+ const shouldShowTableOfUnlinkedOrganisations =
38+ hasManyOrganisations && ! isCurrentOrganisationLinked
39+ const otherRelationships = defraId . otherRelationships . map (
40+ ( { defraIdOrgName, defraIdRelationshipId } ) => [
41+ { text : defraIdOrgName } ,
42+ {
43+ html : `<a href="http://localhost:3200/cdp-defra-id-stub/register/${ defraId . userId } /relationship/${ defraIdRelationshipId } /current">Switch</a>`
44+ }
45+ ]
46+ )
47+
48+ const commonTemplateData = {
49+ defraIdOrgName : defraId . orgName ,
50+ entityName,
51+ isCurrentOrganisationLinked,
52+ otherRelationships,
53+ shouldShowTableOfUnlinkedOrganisations
54+ }
55+
56+ const templateData = shouldShowTableOfUnlinkedOrganisations
57+ ? {
58+ ...commonTemplateData ,
59+ organisations : organisations . map ( ( { name, orgId, id } ) => [
60+ { text : name ?? 'data missing' } ,
61+ { text : orgId ?? 'data missing' } ,
62+ { text : id ?? 'data missing' } ,
63+ {
64+ html : `<a href="${ getOrganisationLinkHref ( id , redirectUrl ) } ">Link</a>`
65+ }
66+ ] )
67+ }
68+ : {
69+ ...commonTemplateData ,
70+ organisation : {
71+ name : organisations [ 0 ] . name ,
72+ orgId : organisations [ 0 ] . orgId ,
73+ id : organisations [ 0 ] . id ,
74+ linkHref : getOrganisationLinkHref (
75+ organisations [ 0 ] . id ,
76+ redirectUrl
77+ ) ,
78+ addHref : `http://localhost:3200/cdp-defra-id-stub/register/${ defraId . userId } /relationship`
6279 }
63- ] )
64- }
65- : {
66- ...commonTemplateData ,
67- organisation : {
68- name : organisations [ 0 ] . name ,
69- orgId : organisations [ 0 ] . orgId ,
70- id : organisations [ 0 ] . id ,
71- linkHref : getOrganisationLinkHref ( organisations [ 0 ] . id , redirectUrl ) ,
72- addHref : `http://localhost:3200/cdp-defra-id-stub/register/${ defraId . userId } /relationship`
7380 }
74- }
7581
76- return {
77- data,
78- response,
79- view : h . view ( 'organisation/link-options' , {
80- pageTitle : 'Link Organisation' ,
81- heading : 'Link Organisation' ,
82- ...templateData
83- } )
82+ return {
83+ data,
84+ response,
85+ view : h . view ( 'organisation/link-options' , {
86+ pageTitle : 'Link Organisation' ,
87+ heading : 'Link Organisation' ,
88+ defraId : request . server . app . defraId ,
89+ ...templateData
90+ } )
91+ }
8492 }
85- }
8693
87- return { data, response }
94+ return { data, response }
95+ } catch ( error ) {
96+ request . logger . error ( error , 'auth: fetchWithAuthInterception' )
97+
98+ throw error
99+ }
88100}
0 commit comments