async function getUserEmailAddress()

in typescript/src/soft-opt-ins/processSubscription.ts [29:68]


async function getUserEmailAddress(
  identityId: string,
  identityApiKey: string,
): Promise<string> {
  const domain = await getIdentityUrl();
  const url = `${domain}/user/${identityId}`;

  const params = {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${identityApiKey}`,
    },
  };

  try {
    console.log(`url ${url}`);

    return fetch(url, params).then(async (response: Response) => {
      if (response.ok) {
        const json = await response.json();

        if (!json.user || !json.user.primaryEmailAddress) {
          return await handleError(
            `User or primaryEmailAddress is undefined for user ${identityId}`,
          );
        }

        return json.user.primaryEmailAddress;
      } else {
        return await handleError(
          `Could not fetch details from identity API for user ${identityId}`,
        );
      }
    });
  } catch (error) {
    return await handleError(
      `error while retrieving user data for identityId: ${identityId}: ${error}`,
    );
  }
}