@cto.ai/ops
Version:
💻 CTO.ai Ops - The CLI built for Teams 🚀
126 lines (125 loc) • 6.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const sdk_1 = require("@cto.ai/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 } = 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 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 => 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 => !utils_1.validateEmail(invitee))
.forEach(invalidEmailInvitee => {
this.log(`❗ The format of ${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 = (config) => (inputs) => {
const { inviteesArray } = inputs;
const { user: { email, username }, team: { id: teamId }, } = config;
this.services.analytics.track({
userId: email,
teamId,
cliEvent: 'Ops CLI team:invite',
event: 'Ops CLI team:invite',
properties: {
email,
username,
invitees: inviteesArray,
},
}, this.accessToken);
};
}
async run() {
const { flags: { invitees }, argv, } = this.parse(TeamInvite);
try {
await this.isLoggedIn();
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 = utils_1.asyncPipe(this.getInvitesPrompt, this.splitInvitees, this.inviteUserToTeam, this.printInviteResponses, this.sendAnalytics(this.state.config));
await invitePipeline({ invitees, config: this.state.config });
}
catch (err) {
this.debug('%O', err);
this.config.runHook('error', { err, accessToken: this.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")',
}),
};