diff --git a/atmn/source/commands/nuke.ts b/atmn/source/commands/nuke.ts index a1dbb7fa..4a73ea6d 100644 --- a/atmn/source/commands/nuke.ts +++ b/atmn/source/commands/nuke.ts @@ -7,7 +7,12 @@ import {initSpinner, isSandboxKey, readFromEnv} from '../core/utils.js'; import {getOrg} from '../core/requests/orgRequests.js'; import {Feature} from '../compose/models/composeModels.js'; -async function promptAndConfirmNuke(orgName: string): Promise { +interface NukeOptions { + backup: boolean; + deleteStripeCustomers: boolean; +} + +async function promptAndConfirmNuke(orgName: string): Promise { console.log('\n' + chalk.bgRed.white.bold(' DANGER: SANDBOX NUKE ')); console.log( chalk.red( @@ -25,8 +30,13 @@ async function promptAndConfirmNuke(orgName: string): Promise { ), ); + const deleteStripeCustomers = await confirm({ + message: `Also delete customers from your ${chalk.blueBright.bold('Stripe')} test account?`, + default: false, + }); + const shouldProceed = await confirm({ - message: `Confirm to continue. This will delete ${chalk.redBright.bold('all')} your ${chalk.redBright.bold('products')}, ${chalk.redBright.bold('features')} and ${chalk.redBright.bold('customers')} from your sandbox environment. You will confirm twice.`, + message: `Confirm to continue. This will delete ${chalk.redBright.bold('all')} your ${chalk.redBright.bold('products')}, ${chalk.redBright.bold('features')} and ${chalk.redBright.bold('customers')}${deleteStripeCustomers ? chalk.blueBright.bold(' (including Stripe)') : ''} from your sandbox environment. You will confirm twice.`, default: false, }); @@ -51,7 +61,10 @@ async function promptAndConfirmNuke(orgName: string): Promise { default: true, }); - return backupConfirm; + return { + backup: backupConfirm, + deleteStripeCustomers, + }; } export default async function Nuke() { @@ -60,9 +73,9 @@ export default async function Nuke() { if (isSandbox) { const org = await getOrg(); - const backupConfirm = await promptAndConfirmNuke(org.name); + const {backup, deleteStripeCustomers} = await promptAndConfirmNuke(org.name); - if (backupConfirm) { + if (backup) { fs.copyFileSync('autumn.config.ts', 'autumn.config.ts.backup'); console.log(chalk.green('Backup created successfully!')); } @@ -88,7 +101,7 @@ export default async function Nuke() { }); try { - await nukeCustomers(customers); + await nukeCustomers(customers, deleteStripeCustomers); await nukeProducts(products.map((product: {id: string}) => product.id)); await nukeFeatures(features.map((feature: {id: string}) => feature.id)); } catch (e: unknown) { diff --git a/atmn/source/core/nuke.ts b/atmn/source/core/nuke.ts index e5530ad2..ec57895a 100644 --- a/atmn/source/core/nuke.ts +++ b/atmn/source/core/nuke.ts @@ -6,6 +6,7 @@ export async function nukeCustomers( id: string; text: string; }[], + deleteInStripe: boolean = false, ) { const s = initSpinner('Deleting customers'); const total = customers.length; @@ -32,7 +33,7 @@ export async function nukeCustomers( await Promise.all( batch.map(async customer => { try { - await deleteCustomer(customer.id); + await deleteCustomer(customer.id, deleteInStripe); } finally { completed++; updateSpinner(); @@ -44,10 +45,13 @@ export async function nukeCustomers( s.success('Customers deleted successfully!'); } -async function deleteCustomer(id: string) { +async function deleteCustomer(id: string, deleteInStripe: boolean) { + const path = deleteInStripe + ? `/customers/${id}?delete_in_stripe=true` + : `/customers/${id}`; await externalRequest({ method: 'DELETE', - path: `/customers/${id}`, + path, }); }