Skip to content

Commit 63ae784

Browse files
committed
fix: include backfill_operations in deleteaccount existence check and deletion
- Add hasUserBackfillOperations() and hasUserActiveBackfill() to BackfillManager - Refuse /deleteaccount while an in_progress backfill exists for the user - Delete backfill_operations rows as part of account deletion flow - Update confirmation and success embeds to list backfill history - Update api-reference.md and full-documentation.md to document the deletion Signed-off-by: Michael Cramer <michael@bigmichi1.de>
1 parent c693734 commit 63ae784

4 files changed

Lines changed: 42 additions & 5 deletions

File tree

docs/api-reference.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -720,21 +720,24 @@ Permanently delete all data the bot holds about the invoking user.
720720
**Parameters**: None
721721

722722
**Flow**:
723-
1. Bot checks whether credentials exist for the user. If none are found, responds with a warning and exits.
724-
2. Bot sends an ephemeral embed with Yes / Cancel buttons (30-second timeout).
725-
3. If the user clicks **Yes, delete everything**: credentials, all redeemed code records, and audit log entries are permanently removed. A summary is shown.
726-
4. If the user clicks **Cancel** or the timeout elapses: no data is changed.
723+
1. Bot checks whether credentials or backfill history exist for the user. If none are found, responds with a warning and exits.
724+
2. Bot checks whether a backfill the user initiated is currently in progress; if so, refuses deletion until it completes.
725+
3. Bot sends an ephemeral embed with Yes / Cancel buttons (30-second timeout).
726+
4. If the user clicks **Yes, delete everything**: credentials, all redeemed code records, audit log entries, and backfill operation history are permanently removed. A summary is shown.
727+
5. If the user clicks **Cancel** or the timeout elapses: no data is changed.
727728

728729
**Data removed on confirmation**:
729730
- `users` row (credentials, server, autoredeem preference)
730731
- All `redeemed_codes` rows for the user
731732
- All `audit_log` rows for the user
733+
- All `backfill_operations` rows for the user
732734

733735
**Error Codes**:
734736

735737
| Code | Meaning | Resolution |
736738
|------|---------|------------|
737-
| `NO_ACCOUNT` | No credentials stored for this user | Nothing to delete |
739+
| `NO_ACCOUNT` | No credentials or backfill history stored for this user | Nothing to delete |
740+
| `BACKFILL_IN_PROGRESS` | A backfill the user initiated is still running | Wait for the backfill to complete, then retry |
738741

739742
**Example**:
740743
```

docs/full-documentation.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ Permanently and irreversibly delete all data the bot holds about you. Requires a
215215
- Your Idle Champions credentials (user ID + hash)
216216
- Your full code redemption history
217217
- Your audit log entries
218+
- Your backfill operation history
218219
- **Confirmation:** A Yes / Cancel button prompt appears with a 30-second timeout — no action is taken unless you click **Yes, delete everything**
219220
- **After deletion:** You will need to run `/setup` again to use the bot
220221
- **Example:** `/deleteaccount`

src/bot/commands/deleteaccount.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,24 @@ export async function execute(interaction: ChatInputCommandInteraction) {
109109
return;
110110
}
111111

112+
// Refuse deletion if the user has an active backfill — completing it would
113+
// try to write to rows that no longer exist after account deletion.
114+
if (await backfillManager.hasUserActiveBackfill(interaction.user.id)) {
115+
await interaction.editReply({
116+
embeds: [
117+
new EmbedBuilder()
118+
.setColor(0xffaa00)
119+
.setTitle('⚠️ Backfill In Progress')
120+
.setDescription(
121+
'A backfill operation you initiated is currently running. ' +
122+
'Please wait for it to complete before deleting your account.'
123+
),
124+
],
125+
components: [],
126+
});
127+
return;
128+
}
129+
112130
// Perform deletion — order matters for FK constraints:
113131
// pending_codes.discord_id → users.discord_id (no cascade), so clear it first
114132
await codeManager.clearPendingCodes(interaction.user.id);

src/bot/database/backfillManager.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,21 @@ class BackfillManager {
109109
return row !== undefined;
110110
}
111111

112+
async hasUserActiveBackfill(discordId: string): Promise<boolean> {
113+
const row = db
114+
.select({ id: backfillOperations.id })
115+
.from(backfillOperations)
116+
.where(
117+
and(
118+
eq(backfillOperations.initiatedBy, discordId),
119+
eq(backfillOperations.status, 'in_progress')
120+
)
121+
)
122+
.limit(1)
123+
.get();
124+
return row !== undefined;
125+
}
126+
112127
async deleteUserBackfillOperations(discordId: string): Promise<number> {
113128
const rows = db
114129
.select({ id: backfillOperations.id })

0 commit comments

Comments
 (0)