Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions atmn/source/commands/nuke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> {
interface NukeOptions {
backup: boolean;
deleteStripeCustomers: boolean;
}

async function promptAndConfirmNuke(orgName: string): Promise<NukeOptions> {
console.log('\n' + chalk.bgRed.white.bold(' DANGER: SANDBOX NUKE '));
console.log(
chalk.red(
Expand All @@ -25,8 +30,13 @@ async function promptAndConfirmNuke(orgName: string): Promise<boolean> {
),
);

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,
});

Expand All @@ -51,7 +61,10 @@ async function promptAndConfirmNuke(orgName: string): Promise<boolean> {
default: true,
});

return backupConfirm;
return {
backup: backupConfirm,
deleteStripeCustomers,
};
}

export default async function Nuke() {
Expand All @@ -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!'));
}
Expand All @@ -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) {
Expand Down
10 changes: 7 additions & 3 deletions atmn/source/core/nuke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export async function nukeCustomers(
id: string;
text: string;
}[],
deleteInStripe: boolean = false,
) {
const s = initSpinner('Deleting customers');
const total = customers.length;
Expand All @@ -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();
Expand All @@ -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,
});
}

Expand Down