/* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/no-var-requires */ import { Table } from 'console-table-printer'; const inquirer = require('inquirer'); import SevenNotification from '../enums/SevenNotification'; import SevenAuthorizedUser from './SevenAuthorizedUser'; import { showNotification } from '../helpers/notificationHandler'; import { getAuthorizedUsers, removeUserFromAuthorizedUsers, saveCurrentUser, getCurrentUser } from './keytarHandler'; /** * Shows prompt which allows selecting user/account from list of authorized users * @param {SevenAuthorizedUser[]} allUsers * @returns {Promise} */ const selectUserFromPrompt = (allUsers: SevenAuthorizedUser[]): Promise => { const userChoices = allUsers.map((item: SevenAuthorizedUser, index) => { return { name: `[${index}] ${item.username} ${item.environment} ${item.dataCenter}`, value: item }; }); return inquirer.prompt([ { name: 'selected_user', type: 'list', message: 'Select user (account):', choices: userChoices } ]).then((answers: any) => { return answers.selected_user; }).catch((error: any) => { if (error.isTtyError) { showNotification(SevenNotification.ERROR, 'Prompt couldn\'t be rendered in the current terminal environment'); } else { showNotification(SevenNotification.ERROR, error.message); } return null; }); }; /** * Filters and returns user from list of authorized users. Filtering can be done by * index, keyId or selecting from command prompt. * @param {number|null} index * @param {string|null} keyId * @returns {Promise} */ const selectUserFromList = (index: number | null, keyId: string | null): Promise => { return getAuthorizedUsers().then(async (allUsers: SevenAuthorizedUser[]) => { if (!allUsers || !allUsers.length) { showNotification(SevenNotification.WARNING, 'No saved authorized users found.'); return null; } if (index || index === 0) { // index proceeded as option if (index < 0 || index >= allUsers.length) { showNotification(SevenNotification.ERROR, 'Index not valid.'); return null; } return allUsers[index]; } else if (keyId) { const foundUser = allUsers.find((item) => item.keyId === keyId); if (foundUser) { return foundUser; } showNotification(SevenNotification.ERROR, `User with keyId ${keyId} not found.`); return null; } return selectUserFromPrompt(allUsers).then((selectedUser: SevenAuthorizedUser) => { return selectedUser; }); }); }; /** * Sets current user that's selected * @param {number} index */ const setCurrentUser = (index: number | any): void => { selectUserFromList(index, null).then((selectedUser: SevenAuthorizedUser | null) => { if (selectedUser) saveCurrentUser(selectedUser); }).catch((err: any) => { showNotification(SevenNotification.ERROR, err.message); }); }; /** * Renders saved authorized users as list on command line * @param {SevenAuthorizedUser[]} users */ const displayUsers = (users: SevenAuthorizedUser[]): void => { if (!users?.length) { showNotification(SevenNotification.INFO, 'No saved authorized users found.'); return; } getCurrentUser().then((currentUser: SevenAuthorizedUser | null) => { const table = new Table({ columns: [ { name: 'Index', alignment: 'center' }, { name: 'Username', alignment: 'center' }, { name: 'User uuid', alignment: 'center' }, { name: 'Environment', alignment: 'center' }, { name: 'DC', alignment: 'center' } ], }); let count = 0; users.forEach((item: SevenAuthorizedUser) => { table.addRow( { Index: count++, Username: item.username, 'User uuid': item.identity, Environment: item.environment, DC: item.dataCenter }, { color: currentUser?.identity === item.identity ? 'green' : 'cyan' }); }); table.printTable(); }).catch(); }; /** * Loads all authorized users and renders them as list in terminal */ const loadAuthorizedUsers = (): void => { getAuthorizedUsers().then((result: SevenAuthorizedUser[]) => { displayUsers(result); }).catch((err) => { showNotification(SevenNotification.ERROR, err.message); }); }; /** * Removes user (account) from list of authorized users. Also, if that user is currently active, * it removes it too. * @param {number | null} index * @param {string | null} keyId */ const logoutUser = (index: number | any, keyId: string | any): void => { selectUserFromList(index, keyId).then((selectedUser: SevenAuthorizedUser | null) => { if (selectedUser) removeUserFromAuthorizedUsers(selectedUser); }).catch((err: any) => { showNotification(SevenNotification.ERROR, err.message); }); }; export { loadAuthorizedUsers, setCurrentUser, logoutUser, getAuthorizedUsers, selectUserFromPrompt };