feat: Add -y flag to delete-user script for non-interactive use

Adds support for `-y` flags to skip interactive confirmation prompts,
enabling use in CI/CD pipelines and automation scripts.

- First `-y`: skips the "Really delete user?" confirmation
- Second `-y`: also skips the "Delete transaction history?" prompt (defaults to yes)
- Without `-y`, the script behaves exactly as before (interactive prompts)

Usage: `npm run delete-user -- -y -y user@example.com`
Made-with: Cursor
This commit is contained in:
Danilo Pejakovic 2026-03-15 14:13:09 +01:00
parent cbdc6f6060
commit 8ad2fbed1b

View file

@ -44,12 +44,24 @@ async function gracefulExit(code = 0) {
(async () => {
await connect();
const args = process.argv.slice(2);
let yesCount = 0;
const filteredArgs = args.filter((arg) => {
if (arg === '-y') {
yesCount++;
return false;
}
return true;
});
const confirmDelete = yesCount >= 1;
const confirmTxDelete = yesCount >= 2;
console.purple('---------------');
console.purple('Deleting a user and all related data');
console.purple('---------------');
// 1) Get email
let email = process.argv[2]?.trim();
let email = filteredArgs[0]?.trim();
if (!email) {
email = (await askQuestion('Email:')).trim();
}
@ -62,17 +74,22 @@ async function gracefulExit(code = 0) {
}
// 3) Confirm full deletion
const confirmAll = await askQuestion(
`Really delete user ${user.email} (${user._id}) and ALL their data? (y/N)`,
);
if (confirmAll.toLowerCase() !== 'y') {
console.yellow('Aborted.');
return gracefulExit(0);
if (!confirmDelete) {
const confirmAll = await askQuestion(
`Really delete user ${user.email} (${user._id}) and ALL their data? (y/N)`,
);
if (confirmAll.toLowerCase() !== 'y') {
console.yellow('Aborted.');
return gracefulExit(0);
}
}
// 4) Ask specifically about transactions
const confirmTx = await askQuestion('Also delete all transaction history for this user? (y/N)');
const deleteTx = confirmTx.toLowerCase() === 'y';
let deleteTx = confirmTxDelete;
if (!confirmTxDelete) {
const confirmTx = await askQuestion('Also delete all transaction history for this user? (y/N)');
deleteTx = confirmTx.toLowerCase() === 'y';
}
const uid = user._id.toString();