All files / commands team.js

0% Statements 0/141
0% Branches 0/48
0% Functions 0/10
0% Lines 0/138

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
/**
 * Team Command - Team collaboration and management
 */
 
const { Command } = require('commander');
const chalk = require('chalk');
const ora = require('ora');
const inquirer = require('inquirer');
const { table } = require('table');
const logger = require('../utils/logger');
const APIClient = require('../services/api-client');
 
const teamCommand = new Command('team')
  .description('šŸ‘„ Team collaboration and management');
 
// List team members
teamCommand
  .command('list')
  .description('List team members')
  .option('--org <id>', 'organization ID')
  .option('--role <role>', 'filter by role (admin|member|viewer)')
  .action(async (options) => {
    const spinner = ora('Fetching team members...').start();
 
    try {
      logger.command('team list', options, true);
 
      const apiClient = new APIClient();
      const response = await apiClient.get('/collaboration/team', {
        params: {
          organization_id: options.org,
          role: options.role
        }
      });
 
      spinner.succeed('Team members retrieved');
 
      const members = response.data.members || [];
 
      console.log(chalk.bold.blue('\nšŸ‘„ Team Members\n'));
 
      if (members.length === 0) {
        console.log(chalk.gray('No team members found'));
        console.log(chalk.blue('Invite members with: vaultace team invite <email>'));
        return;
      }
 
      const memberData = [
        ['Name', 'Email', 'Role', 'Status', 'Last Active']
      ];
 
      members.forEach(member => {
        memberData.push([
          member.name || 'N/A',
          member.email,
          member.role,
          member.status === 'active' ? '🟢 Active' : 'šŸ”“ Inactive',
          member.last_active || 'Never'
        ]);
      });
 
      console.log(table(memberData));
 
      console.log(chalk.gray(`\nTotal: ${members.length} members`));
 
    } catch (error) {
      spinner.fail('Failed to fetch team members');
      logger.error('Team list error', { error: error.message });
 
      if (error.response?.status === 401) {
        console.log(chalk.yellow('\nAuthentication required. Run: vaultace auth login'));
      } else if (error.response?.status === 403) {
        console.log(chalk.red('\nInsufficient permissions. Admin access required.'));
      } else {
        console.error(chalk.red(`Error: ${error.message}`));
      }
      process.exit(1);
    }
  });
 
// Invite team member
teamCommand
  .command('invite')
  .description('Invite new team member')
  .argument('<email>', 'email address to invite')
  .option('--role <role>', 'member role (admin|member|viewer)', 'member')
  .option('--message <text>', 'custom invitation message')
  .action(async (email, options) => {
    try {
      logger.command('team invite', { email, role: options.role }, true);
 
      const { confirm } = await inquirer.prompt([
        {
          type: 'confirm',
          name: 'confirm',
          message: `Invite ${email} as ${options.role}?`,
          default: true
        }
      ]);
 
      if (!confirm) {
        console.log(chalk.yellow('Invitation cancelled'));
        return;
      }
 
      const spinner = ora('Sending invitation...').start();
 
      const apiClient = new APIClient();
      const response = await apiClient.post('/collaboration/invite', {
        email: email,
        role: options.role,
        message: options.message
      });
 
      spinner.succeed('Invitation sent successfully');
 
      console.log(chalk.green(`\nāœ… Invitation sent to ${email}`));
      console.log(`Role: ${options.role}`);
      console.log(`Invitation ID: ${response.data.invitation_id}`);
 
    } catch (error) {
      if (error.spinner) error.spinner.fail('Failed to send invitation');
      logger.error('Team invite error', { error: error.message, email });
 
      if (error.response?.status === 409) {
        console.error(chalk.red('User is already a team member'));
      } else {
        console.error(chalk.red(`Error: ${error.message}`));
      }
      process.exit(1);
    }
  });
 
// Remove team member
teamCommand
  .command('remove')
  .description('Remove team member')
  .argument('<email>', 'email address to remove')
  .option('--force', 'skip confirmation prompt')
  .action(async (email, options) => {
    try {
      logger.command('team remove', { email }, true);
 
      if (!options.force) {
        const { confirm } = await inquirer.prompt([
          {
            type: 'confirm',
            name: 'confirm',
            message: `Remove ${email} from the team?`,
            default: false
          }
        ]);
 
        if (!confirm) {
          console.log(chalk.yellow('Removal cancelled'));
          return;
        }
      }
 
      const spinner = ora('Removing team member...').start();
 
      const apiClient = new APIClient();
      await apiClient.delete(`/collaboration/members/${encodeURIComponent(email)}`);
 
      spinner.succeed('Team member removed');
 
      console.log(chalk.green(`\nāœ… ${email} removed from team`));
 
    } catch (error) {
      if (error.spinner) error.spinner.fail('Failed to remove team member');
      logger.error('Team remove error', { error: error.message, email });
 
      if (error.response?.status === 404) {
        console.error(chalk.red('Team member not found'));
      } else {
        console.error(chalk.red(`Error: ${error.message}`));
      }
      process.exit(1);
    }
  });
 
// Update member role
teamCommand
  .command('role')
  .description('Update team member role')
  .argument('<email>', 'team member email')
  .argument('<role>', 'new role (admin|member|viewer)')
  .action(async (email, role) => {
    try {
      logger.command('team role', { email, role }, true);
 
      const { confirm } = await inquirer.prompt([
        {
          type: 'confirm',
          name: 'confirm',
          message: `Change ${email} role to ${role}?`,
          default: true
        }
      ]);
 
      if (!confirm) {
        console.log(chalk.yellow('Role change cancelled'));
        return;
      }
 
      const spinner = ora('Updating role...').start();
 
      const apiClient = new APIClient();
      await apiClient.patch(`/collaboration/members/${encodeURIComponent(email)}`, {
        role: role
      });
 
      spinner.succeed('Role updated successfully');
 
      console.log(chalk.green(`\nāœ… ${email} role updated to ${role}`));
 
    } catch (error) {
      if (error.spinner) error.spinner.fail('Failed to update role');
      logger.error('Team role error', { error: error.message, email, role });
 
      if (error.response?.status === 404) {
        console.error(chalk.red('Team member not found'));
      } else if (error.response?.status === 400) {
        console.error(chalk.red('Invalid role specified'));
      } else {
        console.error(chalk.red(`Error: ${error.message}`));
      }
      process.exit(1);
    }
  });
 
// Show team activity
teamCommand
  .command('activity')
  .description('Show recent team activity')
  .option('--limit <number>', 'number of activities to show', '10')
  .option('--user <email>', 'filter by user email')
  .action(async (options) => {
    const spinner = ora('Fetching team activity...').start();
 
    try {
      logger.command('team activity', options, true);
 
      const apiClient = new APIClient();
      const response = await apiClient.get('/collaboration/activity', {
        params: {
          limit: parseInt(options.limit),
          user: options.user
        }
      });
 
      spinner.succeed('Team activity retrieved');
 
      const activities = response.data.activities || [];
 
      console.log(chalk.bold.green('\nšŸ“ˆ Team Activity\n'));
 
      if (activities.length === 0) {
        console.log(chalk.gray('No recent activity'));
        return;
      }
 
      activities.forEach(activity => {
        const timestamp = new Date(activity.timestamp).toLocaleString();
        const icon = getActivityIcon(activity.type);
        console.log(`${icon} ${activity.user_name || activity.user_email} ${activity.description}`);
        console.log(chalk.gray(`   ${timestamp}\n`));
      });
 
    } catch (error) {
      spinner.fail('Failed to fetch team activity');
      logger.error('Team activity error', { error: error.message });
      console.error(chalk.red(`Error: ${error.message}`));
      process.exit(1);
    }
  });
 
// Show pending invitations
teamCommand
  .command('invitations')
  .description('Show pending team invitations')
  .action(async () => {
    const spinner = ora('Fetching pending invitations...').start();
 
    try {
      logger.command('team invitations', {}, true);
 
      const apiClient = new APIClient();
      const response = await apiClient.get('/collaboration/invitations');
 
      spinner.succeed('Invitations retrieved');
 
      const invitations = response.data.invitations || [];
 
      console.log(chalk.bold.yellow('\nšŸ“§ Pending Invitations\n'));
 
      if (invitations.length === 0) {
        console.log(chalk.gray('No pending invitations'));
        return;
      }
 
      const inviteData = [
        ['Email', 'Role', 'Sent', 'Expires']
      ];
 
      invitations.forEach(invite => {
        inviteData.push([
          invite.email,
          invite.role,
          new Date(invite.sent_at).toLocaleDateString(),
          new Date(invite.expires_at).toLocaleDateString()
        ]);
      });
 
      console.log(table(inviteData));
 
      console.log(chalk.gray('To resend or cancel invitations, use the web dashboard.'));
 
    } catch (error) {
      spinner.fail('Failed to fetch invitations');
      logger.error('Team invitations error', { error: error.message });
      console.error(chalk.red(`Error: ${error.message}`));
      process.exit(1);
    }
  });
 
function getActivityIcon(type) {
  const icons = {
    'scan_completed': 'šŸ”',
    'vulnerability_fixed': 'šŸ”§',
    'member_invited': 'šŸ“§',
    'member_joined': 'šŸ‘‹',
    'member_removed': 'šŸ‘‹',
    'role_changed': 'šŸ”„',
    'repository_added': 'šŸ“',
    'repository_removed': 'šŸ—‘ļø',
    'settings_changed': 'āš™ļø',
    'default': 'šŸ“'
  };
  return icons[type] || icons.default;
}
 
module.exports = teamCommand;