UNPKG

@cto.ai/ops

Version:

💻 CTO.ai - The CLI built for Teams 🚀

108 lines (107 loc) • 4.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); // tslint:disable:no-implicit-dependencies no-submodule-imports const CustomErrors_1 = require("../../errors/CustomErrors"); const utils_1 = require("../../utils"); const base_1 = tslib_1.__importStar(require("./../../base")); class TeamLeave extends base_1.default { constructor() { super(...arguments); this.getActiveTeamCreator = async (inputs) => { try { const { tokens: { accessToken }, team: { name }, } = inputs.config; const { data: creator } = await this.services.api.find(`/private/teams/${name}/creator`, { headers: { Authorization: accessToken }, }); return Object.assign(Object.assign({}, inputs), { creator }); } catch (err) { this.debug('%O', err); throw new CustomErrors_1.APIError(err); } }; this.checkIfYouAreOwner = async (inputs) => { const { creator: { userId: creatorId } } = inputs, _a = inputs.config.user, { id: activeUserId } = _a, props = tslib_1.__rest(_a, ["id"]); const isCurrentUserOwner = creatorId === activeUserId; if (isCurrentUserOwner) { throw new CustomErrors_1.UnableToLeaveTeamError(); } return Object.assign(Object.assign({}, inputs), { memberToRemove: Object.assign(Object.assign({ userId: activeUserId }, props), { createdAt: '', firstName: '', lastName: '', teamId: '' }) }); }; this.confirmMemberRemove = async (inputs) => { const { config: { team: { name: activeTeamName }, }, } = inputs; const { actionBlue } = this.ux.colors; const { confirmRemove } = await this.ux.prompt({ message: `Are you sure you want to leave ${actionBlue(activeTeamName)} team?`, name: 'confirmRemove', suffix: false, type: 'confirm', }); if (!confirmRemove) { process.exit(); } return inputs; }; this.removeMemberFromTeam = async (inputs) => { const { memberToRemove: { userId, username }, config: { team: { name: activeTeamName }, tokens: { accessToken }, }, } = inputs; try { await this.services.api.remove(`/private/teams/${activeTeamName}/members`, userId, { headers: { Authorization: accessToken }, }); return inputs; } catch (err) { throw new CustomErrors_1.FailedToRemoveMemberFromTeam(err, username, activeTeamName); } }; this.successRemoveMessage = async (inputs) => { const { config: { team: { name: activeTeamName }, }, } = inputs; const { white, callOutCyan } = this.ux.colors; this.log(white(`You were successfully removed from ${callOutCyan(activeTeamName)}`)); return inputs; }; this.sendAnalytics = async (inputs) => { const { config, leftTeam } = inputs; this.services.analytics.track('Ops CLI Team:Leave', { leftTeam, username: config.user.username, }, config); }; this.updateToDefaultTeam = async (inputs) => { try { const { config: { user: { id, username: name }, }, } = inputs; await this.writeConfig(inputs.config, { team: { name, id }, }); const { white, callOutCyan } = this.ux.colors; this.log(white(`Selected team is now: ${callOutCyan(name)}`)); } catch (err) { this.debug('%O', err); throw new CustomErrors_1.ConfigError(err); } return inputs; }; } async run() { const { args: { member: memberArg }, } = this.parse(TeamLeave); const config = await this.isLoggedIn(); try { const leaveTeamPipe = (0, utils_1.asyncPipe)(this.getActiveTeamCreator, this.checkIfYouAreOwner, this.confirmMemberRemove, this.removeMemberFromTeam, this.successRemoveMessage, this.updateToDefaultTeam, this.sendAnalytics); await leaveTeamPipe({ config, memberArg }); } catch (err) { this.debug('%O', err); this.config.runHook('error', { accessToken: config.tokens.accessToken, err, }); } } } exports.default = TeamLeave; TeamLeave.description = 'Leave current team.'; TeamLeave.flags = { help: base_1.flags.help({ char: 'h' }), };