Refactor unsafeGetAndComputeAgeAssurance to expose flags as well

This commit is contained in:
Eric Bailey
2026-06-08 14:26:32 -05:00
parent a4eda59f48
commit bef23cc9f6
3 changed files with 34 additions and 18 deletions
+31 -15
View File
@@ -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 {
+1 -1
View File
@@ -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[]) {
+2 -2
View File
@@ -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})
} }