Refactor unsafeGetAndComputeAgeAssurance to expose flags as well
This commit is contained in:
+31
-15
@@ -18,7 +18,10 @@ import {
|
|||||||
parseAccessFromString,
|
parseAccessFromString,
|
||||||
parseStatusFromString,
|
parseStatusFromString,
|
||||||
} from '#/ageAssurance/types'
|
} from '#/ageAssurance/types'
|
||||||
import {getAgeAssuranceRegionConfigWithFallback} from '#/ageAssurance/util'
|
import {
|
||||||
|
computeAgeAssuranceFlags,
|
||||||
|
getAgeAssuranceRegionConfigWithFallback,
|
||||||
|
} from '#/ageAssurance/util'
|
||||||
import {type Geolocation, useGeolocation} from '#/geolocation'
|
import {type Geolocation, useGeolocation} from '#/geolocation'
|
||||||
import {device} from '#/storage'
|
import {device} from '#/storage'
|
||||||
|
|
||||||
@@ -27,7 +30,7 @@ import {device} from '#/storage'
|
|||||||
* server state before computing access based on AA config from the server +
|
* server state before computing access based on AA config from the server +
|
||||||
* geolocation and other data.
|
* geolocation and other data.
|
||||||
*/
|
*/
|
||||||
export function computeAgeAssuranceState({
|
function computeAgeAssuranceState({
|
||||||
hasSession,
|
hasSession,
|
||||||
config,
|
config,
|
||||||
geolocation,
|
geolocation,
|
||||||
@@ -113,32 +116,45 @@ export function computeAgeAssuranceState({
|
|||||||
* This is a last-ditch helper for out-of-band reads of the AA state, such as
|
* This is a last-ditch helper for out-of-band reads of the AA state, such as
|
||||||
* during account creation. Don't use it for anything else.
|
* during account creation. Don't use it for anything else.
|
||||||
*/
|
*/
|
||||||
export function getAndComputeAgeAssuranceState({did}: {did: string}) {
|
export function unsafeGetAndComputeAgeAssurance({did}: {did: string}) {
|
||||||
const config = getConfigFromCache()
|
const config = getConfigFromCache()
|
||||||
const state = getServerStateFromCache({did})
|
const state = getServerStateFromCache({did})
|
||||||
const data = getOtherRequiredDataFromCache({did})
|
const requiredData = getOtherRequiredDataFromCache({did})
|
||||||
const geolocation = device.get(['mergedGeolocation'])
|
const geolocation = device.get(['mergedGeolocation'])
|
||||||
|
|
||||||
if (!geolocation || !config || !state || !data) {
|
if (!geolocation || !config || !state || !requiredData) {
|
||||||
return {
|
return {
|
||||||
status: AgeAssuranceStatus.Unknown,
|
state: {
|
||||||
access: AgeAssuranceAccess.Safe,
|
status: AgeAssuranceStatus.Unknown,
|
||||||
|
access: AgeAssuranceAccess.Safe,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return computeAgeAssuranceState({
|
const region = getAgeAssuranceRegionConfigWithFallback(config, geolocation)
|
||||||
|
const data = {
|
||||||
|
accountCreatedAt: state.metadata?.accountCreatedAt,
|
||||||
|
declaredAge: requiredData?.birthdate
|
||||||
|
? getAge(new Date(requiredData.birthdate))
|
||||||
|
: undefined,
|
||||||
|
birthdate: requiredData?.birthdate,
|
||||||
|
}
|
||||||
|
const computed = computeAgeAssuranceState({
|
||||||
hasSession: true,
|
hasSession: true,
|
||||||
config,
|
config,
|
||||||
geolocation,
|
geolocation,
|
||||||
state: state.state,
|
state: state.state,
|
||||||
data: {
|
data,
|
||||||
accountCreatedAt: state.metadata?.accountCreatedAt,
|
|
||||||
declaredAge: data?.birthdate
|
|
||||||
? getAge(new Date(data.birthdate))
|
|
||||||
: undefined,
|
|
||||||
birthdate: data?.birthdate,
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
return {
|
||||||
|
state: computed,
|
||||||
|
flags: computeAgeAssuranceFlags({
|
||||||
|
state: computed,
|
||||||
|
config: region,
|
||||||
|
data,
|
||||||
|
}),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useAgeAssuranceState(): AgeAssuranceState {
|
export function useAgeAssuranceState(): AgeAssuranceState {
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ jest.mock('jwt-decode', () => ({
|
|||||||
jest.mock('../../birthdate')
|
jest.mock('../../birthdate')
|
||||||
jest.mock('../../../ageAssurance/data')
|
jest.mock('../../../ageAssurance/data')
|
||||||
jest.mock('../../../ageAssurance/state', () => ({
|
jest.mock('../../../ageAssurance/state', () => ({
|
||||||
getAndComputeAgeAssuranceState: () => ({}),
|
unsafeGetAndComputeAgeAssurance: () => ({state: {}}),
|
||||||
}))
|
}))
|
||||||
jest.mock('#/lib/notifications/notifications', () => ({
|
jest.mock('#/lib/notifications/notifications', () => ({
|
||||||
unregisterPushToken(_agents: BskyAgent[]) {
|
unregisterPushToken(_agents: BskyAgent[]) {
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ import {
|
|||||||
setBirthdateForDid,
|
setBirthdateForDid,
|
||||||
setCreatedAtForDid,
|
setCreatedAtForDid,
|
||||||
} from '#/ageAssurance/data'
|
} from '#/ageAssurance/data'
|
||||||
import {getAndComputeAgeAssuranceState} from '#/ageAssurance/state'
|
import {unsafeGetAndComputeAgeAssurance} from '#/ageAssurance/state'
|
||||||
import {AgeAssuranceAccess} from '#/ageAssurance/types'
|
import {AgeAssuranceAccess} from '#/ageAssurance/types'
|
||||||
import {features} from '#/analytics'
|
import {features} from '#/analytics'
|
||||||
import {emitNetworkConfirmed, emitNetworkLost} from '../events'
|
import {emitNetworkConfirmed, emitNetworkLost} from '../events'
|
||||||
@@ -219,7 +219,7 @@ export async function createAgentAndCreateAccount(
|
|||||||
}),
|
}),
|
||||||
// wait for AA data to load first, then check state
|
// wait for AA data to load first, then check state
|
||||||
aa.then(async () => {
|
aa.then(async () => {
|
||||||
const state = getAndComputeAgeAssuranceState({did: account.did})
|
const {state} = unsafeGetAndComputeAgeAssurance({did: account.did})
|
||||||
if (state.access !== AgeAssuranceAccess.Full) {
|
if (state.access !== AgeAssuranceAccess.Full) {
|
||||||
restrictChatSettings({agent, did: account.did})
|
restrictChatSettings({agent, did: account.did})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user