Skip to content

Commit fae2c0b

Browse files
authored
feat(deepLinks): make deeplinking work with multi org (#5344)
1 parent e2d62ce commit fae2c0b

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

src/identity/apis/auth.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Functions calling API
22
import {
33
getAccount,
4+
getAccounts,
45
getAccountsOrgs,
56
getIdentity,
67
getMe as getMeQuartz,
@@ -282,3 +283,31 @@ export const updateDefaultOrgByAccountID = async ({
282283

283284
// success status code is 204; no response body expected.
284285
}
286+
287+
// fetch user default account's default org
288+
export const getDefaultAccountDefaultOrg = async (): Promise<OrganizationSummaries[number]> => {
289+
const response = await getAccounts({})
290+
291+
if (response.status === 401) {
292+
throw new UnauthorizedError(response.data.message)
293+
}
294+
295+
if (response.status === 500) {
296+
throw new ServerError(response.data.message)
297+
}
298+
const {data} = response
299+
300+
if (Array.isArray(data) && data.length) {
301+
const defaultAccount = data.find(account => account.isDefault)
302+
303+
// fetch default org
304+
if (defaultAccount) {
305+
const quartzOrg = await fetchOrgsByAccountID(defaultAccount.id)
306+
const defaultQuartzOrg =
307+
quartzOrg.find(org => org.isDefault) || quartzOrg[0]
308+
309+
return defaultQuartzOrg
310+
}
311+
throw new GenericError('No default account found')
312+
}
313+
}

src/shared/components/NotFound.tsx

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {getOrg as fetchOrg} from 'src/organizations/apis'
2121
// Utils
2222
import {buildDeepLinkingMap} from 'src/utils/deepLinks'
2323
import {event} from 'src/cloud/utils/reporting'
24+
import {shouldUseQuartzIdentity} from 'src/identity/utils/shouldUseQuartzIdentity'
2425

2526
// Components
2627
import LogoWithCubo from 'src/checkout/LogoWithCubo'
@@ -30,6 +31,9 @@ import {Organization} from 'src/types'
3031
// Constants
3132
import {CLOUD} from 'src/shared/constants'
3233

34+
// API
35+
import {getDefaultAccountDefaultOrg} from 'src/identity/apis/auth'
36+
3337
const NotFoundNew: FC = () => (
3438
<AppWrapper type="funnel" className="page-not-found" testID="not-found">
3539
<FunnelPage enableGraphic={true} className="page-not-found-funnel">
@@ -136,10 +140,20 @@ const NotFound: FC = () => {
136140

137141
const handleDeepLink = useCallback(async () => {
138142
if (!org.current) {
139-
setIsFetchingOrg(true)
140-
org.current = await fetchOrg()
143+
if (shouldUseQuartzIdentity()) {
144+
try {
145+
setIsFetchingOrg(true)
146+
const defaultQuartzOrg = await getDefaultAccountDefaultOrg()
147+
org.current = defaultQuartzOrg
148+
} catch {
149+
history.push(`/no-orgs`)
150+
return
151+
}
152+
} else {
153+
setIsFetchingOrg(true)
154+
org.current = await fetchOrg()
155+
}
141156
}
142-
143157
const deepLinkingMap = buildDeepLinkingMap(org.current)
144158

145159
if (deepLinkingMap.hasOwnProperty(location.pathname)) {

src/utils/deepLinks.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import {Organization} from 'src/client'
2+
import {OrganizationSummaries} from 'src/client/unityRoutes'
23
import {PROJECT_NAME_PLURAL} from 'src/flows'
34

4-
export const buildDeepLinkingMap = (org: Organization) => ({
5+
export const buildDeepLinkingMap = (
6+
org: Organization | OrganizationSummaries[number]
7+
) => ({
58
'/me/about': `/orgs/${org.id}/about`,
69
'/me/alerts': `/orgs/${org.id}/alerting`,
710
'/me/billing': `/orgs/${org.id}/billing`,

0 commit comments

Comments
 (0)