async function batchWriteUsers()

in source/workflow-export/export-users.js [161:239]


async function batchWriteUsers(users, userLastConfirmedInUserPoolDate, cognitoTPS, oneSecondFromNow, cognitoApiCallCount) {
  const batchWriteMax = 25;
  try {
    while (users.length > 0) {
      const batchWriteParams = {
        RequestItems: { [TABLE_NAME]: [] }
      };

      let usersToWrite = users.splice(0, batchWriteMax);

      for (let i = 0; i < usersToWrite.length; i++) {
        let user = usersToWrite[i];

        let subValue;
        const subAttribute = user.Attributes.find(attr => attr.Name === 'sub');
        if (subAttribute) {
          subValue = subAttribute.Value;
        }

        if (!subValue) {
          throw new Error('Unable to determine the sub attribute for user');
        }

        // The pseudoUsername  will be used as the username for the user import
        // CSV when importing users during the import workflow
        let pseudoUsername;
        if (poolUsernameAttributes.length === 0) {
          pseudoUsername = user.Username;
        } else if (poolUsernameAttributes.length === 1) {
          const pseudoUsernameAttribute = user.Attributes.find(attr => attr.Name === poolUsernameAttributes[0]);
          pseudoUsername = pseudoUsernameAttribute.Value;
        } else {
          // Narrow down which attribute to use as pseudoUsername
          const possibleAttributes = user.Attributes.filter(attr => poolUsernameAttributes.includes(attr.Name) && attr.Value.trim() !== '');
          if (possibleAttributes.length === 1) {
            pseudoUsername = possibleAttributes[0].Value;
          }
        }

        if (!pseudoUsername) {
          throw new Error('Unable to determine the pseudoUsername for the user');
        }

        batchWriteParams.RequestItems[TABLE_NAME].push({
          PutRequest: {
            Item: {
              id: `USER-${subValue}`,
              type: TYPE_USER,
              username: user.Username,
              pseudoUsername: pseudoUsername,
              userAttributes: user.Attributes,
              userEnabled: user.Enabled,
              userStatus: user.UserStatus,
              lastConfirmedInUserPool: userLastConfirmedInUserPoolDate,
              lastUpdatedRegion: AWS_REGION
            }
          }
        });
      }

      let batchResult;
      do {
        console.log(`Writing ${batchWriteParams.RequestItems[TABLE_NAME].length} item(s) to ${TABLE_NAME}`);
        batchResult = await documentClient.batchWrite(batchWriteParams).promise();

        if (batchResult.UnprocessedItems[TABLE_NAME] !== undefined && batchResult.UnprocessedItems[TABLE_NAME].length > 0) {
          console.log(`Detected ${batchResult.UnprocessedItems[TABLE_NAME].length} unprocessed item(s). Waiting 100 ms then processing again`);
          batchWriteParams.RequestItems[TABLE_NAME] = batchResult.UnprocessedItems[TABLE_NAME];
          await sleep(0, 100, false);
        }
      } while (batchResult.UnprocessedItems[TABLE_NAME] !== undefined && batchResult.UnprocessedItems[TABLE_NAME].length > 0);
    }
  } catch (error) {
    console.error('Error occurred while batch writing items into dynamodb.');
    throw error;
  }

  return { cognitoApiCallCount, oneSecondFromNow };
}