@mondaycom/apps-cli
Version:
A cli tool to manage apps (and monday-code projects) in monday.com
78 lines (77 loc) • 3.38 kB
JavaScript
import { Flags } from '@oclif/core';
import chalk from 'chalk';
import { AuthenticatedCommand } from '../../commands-base/authenticated-command.js';
import { APP_ID_TO_ENTER } from '../../consts/messages.js';
import { removeAppStorageDataForAccount } from '../../services/apps-service.js';
import { DynamicChoicesService } from '../../services/dynamic-choices-service.js';
import { PromptService } from '../../services/prompt-service.js';
import { HttpError } from '../../types/errors/index.js';
import logger from '../../utils/logger.js';
const MESSAGES = {
clientAccountNumber: 'Client account id (number)',
appId: APP_ID_TO_ENTER,
areYouSurePrompt: 'Are you sure you want to remove this account data? -= this operation cannot be undone =- (yes/no)',
force: 'Skip the confirmation step',
operationAborted: 'Operation aborted',
removingData: 'Removing data...',
operationAccepted: 'We got your request, it will take a few minutes to complete',
operationSuccess: 'Operation completed successfully',
};
export default class RemoveData extends AuthenticatedCommand {
static description = 'Completely remove all the storage data for specific customer account.';
static examples = ['<%= config.bin %> <%= command.id %> -a APP_ID -c CLIENT_ACCOUNT_ID'];
static flags = RemoveData.serializeFlags({
appId: Flags.integer({
char: 'a',
description: 'Select the app that you wish to remove account data for',
}),
clientAccountId: Flags.integer({
char: 'c',
description: MESSAGES.clientAccountNumber,
}),
force: Flags.boolean({
char: 'f',
description: MESSAGES.force,
}),
});
async run() {
const { flags } = await this.parse(RemoveData);
let { appId, clientAccountId } = flags;
const { force } = flags;
try {
if (!appId) {
appId = await DynamicChoicesService.chooseApp();
}
if (!clientAccountId) {
clientAccountId = await PromptService.promptInputNumber(`${MESSAGES.clientAccountNumber}:`, true);
}
if (!force) {
const answer = await PromptService.promptInput(MESSAGES.areYouSurePrompt, true);
if (`${answer}`.toLowerCase().includes('no')) {
this.preparePrintCommand(this, { appId, clientAccountId, force });
logger.log(MESSAGES.operationAborted);
return;
}
}
logger.log(MESSAGES.removingData);
const response = await removeAppStorageDataForAccount(appId, clientAccountId);
if (response.statusCode === 202) {
logger.log(MESSAGES.operationAccepted);
}
else {
logger.log(MESSAGES.operationSuccess);
}
this.preparePrintCommand(this, { appId, clientAccountId, force });
}
catch (error) {
logger.debug(error);
if (error instanceof HttpError) {
logger.error(`\n ${chalk.italic(chalk.red(error.message))}`);
}
else {
logger.error(`An unknown error happened while removing storage data for a client account"`);
}
process.exit(1);
}
}
}