export async function validateIdentityIdForPhoneNumberInclusion()

in src/libs/identity.ts [72:113]


export async function validateIdentityIdForPhoneNumberInclusion(
    stage: string,
    identityId: string,
    identityAPIBearerToken: string,
): Promise<boolean> {
    if (stage === 'CODE') {
        return true; // The ids in Saleforce CODE might not validate against idAPI PROD, but we want the phone numbers in CODE
    }
    let userDetailsFromIdAPI;
    try {
        userDetailsFromIdAPI = await queryUserDetailsFromIdAPI(
            identityId,
            identityAPIBearerToken,
        );
        if (userDetailsFromIdAPI.status !== 'ok') {
            return false;
        }
        const consent = userDetailsFromIdAPI.user.consents.find(
            (consent) => consent.id == 'phone_optout',
        );
        /*
            consent is now either underfined or 
            {
                "actor": "user",
                "id": "phone_optout",
                "version": 0,
                "consented": false,
                "timestamp": "2018-12-03T10:22:45Z",
                "privacyPolicyVersion": 1
            }
        */
        if (consent !== undefined) {
            return consent.consented === false; // `false` is the value that we are looking for
            // Means that the user has not consented out of the phone use
        } else {
            return false;
        }
    } catch (err) {
        console.error(err);
        return false; // We isolate any caller from any failure of connecting to the identityAPI, by returning a proper boolean, `false` in this case
    }
}