-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathdelete.ts
More file actions
115 lines (98 loc) · 3.33 KB
/
delete.ts
File metadata and controls
115 lines (98 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
* Copyright (c) 2025, Salesforce, Inc.
* SPDX-License-Identifier: Apache-2
* For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0
*/
import * as readline from 'node:readline';
import {Args, Flags} from '@oclif/core';
import {MrtCommand} from '@salesforce/b2c-tooling-sdk/cli';
import {deleteNotification} from '@salesforce/b2c-tooling-sdk/operations/mrt';
import {t, withDocs} from '../../../../i18n/index.js';
/**
* Prompt for confirmation.
*/
async function confirm(message: string): Promise<boolean> {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
return new Promise((resolve) => {
rl.question(`${message} (y/N): `, (answer) => {
rl.close();
resolve(answer.toLowerCase() === 'y' || answer.toLowerCase() === 'yes');
});
});
}
/**
* Delete a notification from an MRT project.
*/
export default class MrtNotificationDelete extends MrtCommand<typeof MrtNotificationDelete> {
static args = {
id: Args.string({
description: 'Notification ID to delete',
required: true,
}),
};
static description = withDocs(
t('commands.mrt.notification.delete.description', 'Delete a notification from a Managed Runtime project'),
'/cli/mrt.html#b2c-mrt-project-notification-delete',
);
static enableJsonFlag = true;
static examples = [
'<%= config.bin %> <%= command.id %> abc-123 --project my-storefront',
'<%= config.bin %> <%= command.id %> abc-123 -p my-storefront --force',
];
static flags = {
...MrtCommand.baseFlags,
force: Flags.boolean({
char: 'f',
description: 'Skip confirmation prompt',
default: false,
}),
};
async run(): Promise<{id: string; deleted: boolean}> {
// Prevent deletion in safe mode
this.assertDestructiveOperationAllowed('delete notification');
this.requireMrtCredentials();
const {id} = this.args;
const {mrtProject: project} = this.resolvedConfig.values;
if (!project) {
this.error('MRT project is required. Provide --project flag, set MRT_PROJECT, or set mrtProject in dw.json.');
}
const {force} = this.flags;
// Confirm deletion unless --force is specified
if (!force && !this.jsonEnabled()) {
const confirmed = await confirm(
t('commands.mrt.notification.delete.confirm', 'Are you sure you want to delete notification {{id}}?', {id}),
);
if (!confirmed) {
this.log(t('commands.mrt.notification.delete.cancelled', 'Deletion cancelled.'));
return {id, deleted: false};
}
}
this.log(t('commands.mrt.notification.delete.deleting', 'Deleting notification {{id}}...', {id}));
try {
await deleteNotification(
{
projectSlug: project,
notificationId: id,
origin: this.resolvedConfig.values.mrtOrigin,
},
this.getMrtAuth(),
);
if (!this.jsonEnabled()) {
this.log(t('commands.mrt.notification.delete.success', 'Notification {{id}} deleted.', {id}));
}
return {id, deleted: true};
} catch (error) {
if (error instanceof Error) {
this.error(
t('commands.mrt.notification.delete.failed', 'Failed to delete notification: {{message}}', {
message: error.message,
}),
);
}
throw error;
}
}
}