public async run()

in src/commands/orgs/collaborators/update.ts [81:190]


  public async run(client: AppCenterClient): Promise<CommandResult> {
    // validate that string and file properties are not specified simultaneously
    this.validateParameters();

    // loading user lists and lists of org users and org invitations
    const collaboratorsToAddPromise = getUsersList(this.collaboratorsToAdd, this.collaboratorsToAddFile, debug);
    const collaboratorsToDeletePromise = getUsersList(this.collaboratorsToDelete, this.collaboratorsToDeleteFile, debug);
    const collaboratorsToMakeAdminsPromise = getUsersList(this.collaboratorsToMakeAdmins, this.collaboratorsToMakeAdminsFile, debug);
    const adminsToMakeCollaboratorsPromise = getUsersList(this.adminsToMakeCollaborators, this.adminsToMakeCollaboratorsFile, debug);
    const usersInvitedToOrgPromise = this.getUsersInvitedToOrg(client);
    const usersJoinedOrgPromise = getOrgUsers(client, this.name, debug);

    // showing spinner while prerequisites are being loaded
    const [
      collaboratorsToAdd,
      collaboratorsToDelete,
      collaboratorsToMakeAdmins,
      adminsToMakeCollaborators,
      usersInvitedToOrg,
      usersJoinedOrg,
    ] = await out.progress(
      "Loading prerequisites...",
      Promise.all([
        collaboratorsToAddPromise,
        collaboratorsToDeletePromise,
        collaboratorsToMakeAdminsPromise,
        adminsToMakeCollaboratorsPromise,
        usersInvitedToOrgPromise,
        usersJoinedOrgPromise,
      ])
    );

    let addedCollaborators: string[];
    let deletedCollaborators: string[];
    if (collaboratorsToAdd.length || collaboratorsToDelete.length) {
      const joinedUserEmailsToUserObject = this.toUserEmailMap(usersJoinedOrg);
      const userJoinedOrgEmails = Array.from(joinedUserEmailsToUserObject.keys());

      addedCollaborators = await out.progress(
        "Adding collaborators...",
        this.addCollaborators(client, collaboratorsToAdd, usersInvitedToOrg, userJoinedOrgEmails)
      );

      // updating list of invited users
      addedCollaborators.forEach((collaborator) => {
        if (usersInvitedToOrg.indexOf(collaborator) === -1) {
          usersInvitedToOrg.push(collaborator);
        }
      });

      deletedCollaborators = await out.progress(
        "Deleting collaborators...",
        this.deleteCollaborators(client, collaboratorsToDelete, usersInvitedToOrg, joinedUserEmailsToUserObject)
      );
    } else {
      addedCollaborators = [];
      deletedCollaborators = [];
    }

    let toAdmins: string[];
    let toCollaborators: string[];
    if (collaboratorsToMakeAdmins.length || adminsToMakeCollaborators.length) {
      // just deleted org users should be excluded from role changing
      const joinedUserEmailsToUserObject = this.toUserEmailMap(
        usersJoinedOrg.filter((user) => deletedCollaborators.indexOf(user.email) === -1)
      );

      toAdmins = await out.progress(
        "Changing role to admins...",
        this.changeUsersRole(client, collaboratorsToMakeAdmins, joinedUserEmailsToUserObject, "admin")
      );

      // updating roles after setting admins
      Array.from(joinedUserEmailsToUserObject.values())
        .filter((user) => collaboratorsToMakeAdmins.indexOf(user.email) > -1)
        .forEach((user) => (user.role = "admin"));

      toCollaborators = await out.progress(
        "Changing role to collaborator...",
        this.changeUsersRole(client, adminsToMakeCollaborators, joinedUserEmailsToUserObject, "collaborator")
      );
    } else {
      toAdmins = [];
      toCollaborators = [];
    }

    out.text(
      (result) => {
        const stringArray: string[] = [];

        if (result.addedCollaborators.length) {
          stringArray.push(`Successfully added ${result.addedCollaborators.length} collaborators to organization`);
        }
        if (result.deletedCollaborators.length) {
          stringArray.push(`Successfully deleted ${result.deletedCollaborators.length} collaborators from organization`);
        }
        if (result.toAdmins.length) {
          stringArray.push(`Successfully changed roles for ${result.toAdmins.length} collaborators to "admin"`);
        }
        if (result.toCollaborators.length) {
          stringArray.push(`Successfully changed roles for ${result.toCollaborators.length} admins to "collaborator"`);
        }

        return stringArray.join(Os.EOL);
      },
      { addedCollaborators, deletedCollaborators, toAdmins, toCollaborators }
    );

    return success();
  }