UNPKG

@cto.ai/ops

Version:

💻 CTO.ai - The CLI built for Teams 🚀

123 lines (122 loc) 6.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const cli_sdk_1 = require("@cto.ai/cli-sdk"); const base_1 = tslib_1.__importStar(require("./../../base")); const utils_1 = require("./../../utils"); const CustomErrors_1 = require("./../../errors/CustomErrors"); const { white, red, callOutCyan, green, reset } = cli_sdk_1.ux.colors; class TeamInvite extends base_1.default { constructor() { super(...arguments); this.getInvitesPrompt = async (inputs) => { if (inputs.invitees) return inputs; const { config: { team }, } = inputs; const { invitees } = await cli_sdk_1.ux.prompt({ type: 'input', name: 'invitees', message: `\n${callOutCyan('Invite team members to')} ${reset.blue(team.name)} ${callOutCyan('and start sharing your Ops')} ${reset.green('→')}\n${white('Enter the emails of the team member(s) that you want to invite as a comma-separated list.')} \n\n${white('🎟 Invite User')}`, validate: (input) => !!input, }); return Object.assign(Object.assign({}, inputs), { invitees }); }; /** * Splits the invitees by either string or space * Handles the case of: * "username1,username2,username3" => ["username1", "username2", "username3"] * "username1, username2, username3" => ["username1", "username2", "username3"] * "username1 username2 username3" => ["username1", "username2", "username3"] * "username1,username2 username3" => ["username1", "username2", "username3"] * ", username1 , username2,,,,,, username3 ,," => ["username1", "username2", "username3"] */ this.splitInvitees = (inputs) => { const inviteesArray = inputs.invitees .replace(/ /g, ',') // Replaces all whitespaces with string .replace(/,+/g, ',') // Handles the case of nico@cto.ai, nico+1@cto.ai .replace(/^,+/g, '') // Handles the case of nico@cto.ai, nico+1@cto.ai .replace(/,+$/g, '') // Handles the case of nico@cto.ai, nico+1@cto.ai .split(','); return Object.assign(Object.assign({}, inputs), { inviteesArray }); }; this.inviteUserToTeam = async (inputs) => { const { config: { team: { id }, }, inviteesArray, } = inputs; try { const filteredInviteesArray = inviteesArray.filter(invitee => (0, utils_1.validateEmail)(invitee)); const { data: inviteResponses, } = await this.services.api.create(`/private/teams/${id}/invites`, { UserOrEmail: filteredInviteesArray }, { headers: { Authorization: this.accessToken } }); return Object.assign(Object.assign({}, inputs), { inviteResponses }); } catch (err) { this.debug('%O', err); throw new CustomErrors_1.InviteSendingInvite(err); } }; this.printInviteResponses = (inputs) => { const { inviteResponses, inviteesArray } = inputs; let numSuccess = 0; this.log(''); // Gives and empty line inviteResponses.forEach(inviteResponse => { // Logs succesful invite if (inviteResponse.sentStatus === 'sent successfully!') { numSuccess++; this.log(`${green('✔')} ${white(`Invite Sent! ${inviteResponse.email}`)}`); // Logs unsuccessful invite } else { this.log(`😞 Sorry, we weren't able to complete your invite to ${red(inviteResponse.email)}. Please try again.`); } }); inviteesArray .filter(invitee => !(0, utils_1.validateEmail)(invitee)) .forEach(invalidEmailInvitee => { this.log(`❗ The format of ${cli_sdk_1.ux.colors.red(invalidEmailInvitee)} is invalid, please check that it is correct and try again.`); }); // Logs the summary of invites if (!numSuccess) { this.log(`\n ${white(`❌ Invited ${numSuccess} team members`)}`); } else { this.log(`\n ${white(`🙌 Invited ${numSuccess} team member${numSuccess > 1 ? 's' : ''}!`)}`); } return inputs; }; this.sendAnalytics = (inputs) => { const { inviteesArray, config, inviteResponses } = inputs; const inviteIds = inviteResponses.map(invite => invite.inviteCode); this.services.analytics.track('Ops CLI Team:Invite', { username: config.user.username, invitees: inviteesArray, inviteIds, }, config); }; } async run() { const { flags: { invitees }, argv, } = this.parse(TeamInvite); const config = await this.isLoggedIn(); try { if (argv.length) { throw new Error('team:invite doesn\'t accept any arguments. Please use the -i flag like this: ops team:invite "user1, user2@gmail.com, user3@something"'); } const invitePipeline = (0, utils_1.asyncPipe)(this.getInvitesPrompt, this.splitInvitees, this.inviteUserToTeam, this.printInviteResponses, this.sendAnalytics); await invitePipeline({ invitees, config }); } catch (err) { this.debug('%O', err); this.config.runHook('error', { err, accessToken: config.tokens.accessToken, }); } } } exports.default = TeamInvite; TeamInvite.description = 'Invite your team members.'; TeamInvite.strict = false; TeamInvite.flags = { help: base_1.flags.help({ char: 'h' }), invitees: base_1.flags.string({ char: 'i', description: 'A comma-separated string of usernames/emails we want to invite. E.g. ("user1, user2@gmail.com, user3@something")', }), };