-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactor of current tenant provider
- Loading branch information
Showing
2 changed files
with
56 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...pp-libs/webapp-tenants/src/providers/currentTenantProvider/currentTenantProvider.utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { CommonQueryTenantItemFragmentFragment } from '@sb/webapp-api-client'; | ||
import { TenantType } from '@sb/webapp-api-client/constants'; | ||
import { prop } from 'ramda'; | ||
|
||
export const parseStoredState = (storedState: string, userId = '') => { | ||
try { | ||
const parsedStoredState = JSON.parse(storedState); | ||
|
||
return { | ||
parsedStoredState, | ||
storedTenantId: parsedStoredState?.[userId] ?? null, | ||
}; | ||
} catch { | ||
return { | ||
parsedStoredState: {}, | ||
storedTenantId: null, | ||
}; | ||
} | ||
}; | ||
|
||
const getTenantId = ( | ||
tenantId = '', | ||
storedTenantId: string | null, | ||
tenants: (CommonQueryTenantItemFragmentFragment | null | undefined)[] | ||
) => { | ||
if (tenantId) return tenantId; | ||
|
||
if (storedTenantId && tenants.map(prop<string>('id')).includes(storedTenantId)) { | ||
return storedTenantId; | ||
} | ||
|
||
return ''; | ||
}; | ||
|
||
export const getCurrentTenant = ( | ||
paramsTenantId = '', | ||
storedTenantId: string | null, | ||
tenants: (CommonQueryTenantItemFragmentFragment | null | undefined)[] | ||
) => { | ||
const tenantId = getTenantId(paramsTenantId, storedTenantId, tenants); | ||
|
||
const currentTenant = tenants.find((t) => t?.id === tenantId && t.membership.invitationAccepted); | ||
if (currentTenant) return currentTenant; | ||
|
||
const firstDefaultTenant = tenants.find((t) => t?.type === TenantType.PERSONAL); | ||
if (firstDefaultTenant) return firstDefaultTenant; | ||
|
||
return tenants[0]; | ||
}; |