async function ensureCustomerItem (
  cognitoIdentityId,
  cognitoUserId,
  keyId,
  errorCallback,
  successCallback
) {
  let customerItem
  let preLoginItem
  try {
    const customerPromise = dynamoDb
      .get({
        TableName: getCustomersTableName(),
        Key: {
          Id: cognitoIdentityId
        }
      })
      .promise()
    const preLoginAccountPromise = dynamoDb
      .get({
        TableName: getEnv('PreLoginAccountsTableName'),
        Key: {
          UserId: cognitoUserId
        }
      })
      .promise()

    const [customerResponse, preLoginAccountResponse] = await Promise.all([
      customerPromise,
      preLoginAccountPromise
    ])

    customerItem = customerResponse.Item
    preLoginItem = preLoginAccountResponse.Item
  } catch (error) {
    console.error(error)
    errorCallback(error)
    return
  }

  // We assume that if customerItem is defined (i.e. present in
  // CustomersTable), then the following are true:
  //  - the item was migrated from pre-account management, or has logged in
  //    since signing up (and thus contains any relevant data that was
  //    previously or currently stored in PreLoginAccountsTable)
  //  - the customerItem's RegistrationStatus, Id (identity ID), UserPoolId
  //    (user ID), and ApiKeyId are all present
  if (customerItem !== undefined) {
    console.log(
      `Found CustomersTable item with identity ID [${cognitoIdentityId}]`
    )

    if (successCallback) {
      successCallback(customerItem)
      return
    } else {
      return customerItem
    }
  }

  if (preLoginItem === undefined) {
    const errorMessage =
      'Account not found in CustomersTable or PreLoginAccountsTable!'
    console.error(errorMessage)
    errorCallback(new Error(errorMessage))
  }

  let newCustomerItem
  try {
    newCustomerItem = await transferPreLoginAccountToCustomersTable({
      identityId: cognitoIdentityId,
      apiKeyId: keyId,
      preLoginItem
    })
    console.log(
      `Created new customer in CustomersTable with identity ID [${newCustomerItem.Id}]`
    )
  } catch (error) {
    console.error(error)
    errorCallback(error)
    return
  }

  if (successCallback) {
    successCallback(newCustomerItem)
  } else {
    return newCustomerItem
  }
}
Ensure user is tracked in customer table, and pop item from
PreLoginAccountsTable if present (transferring its fields to the customer
table item).

Note to future maintainers: This function contains old code which may be
over-engineered.