fix(bot): clear stale reminder draft buttons

This commit is contained in:
2026-03-24 04:32:15 +04:00
parent 2888438260
commit 322e1bfd33
2 changed files with 41 additions and 13 deletions

View File

@@ -378,11 +378,19 @@ describe('registerAdHocNotifications', () => {
expect(calls[1]?.payload).toMatchObject({ expect(calls[1]?.payload).toMatchObject({
text: `Окей, ${updatedWhen} напомню.` text: `Окей, ${updatedWhen} напомню.`
}) })
expect(calls[2]?.method).toBe('editMessageReplyMarkup')
expect(calls[2]?.payload).toMatchObject({
chat_id: -10012345,
message_id: 1,
reply_markup: {
inline_keyboard: []
}
})
await bot.handleUpdate(reminderMessageUpdate('А вообще, я не буду кушать') as never) await bot.handleUpdate(reminderMessageUpdate('А вообще, я не буду кушать') as never)
expect(draftEditCalls).toBe(2) expect(draftEditCalls).toBe(2)
expect(calls[2]?.payload).toMatchObject({ expect(calls[3]?.payload).toMatchObject({
text: 'Окей, тогда не напоминаю.' text: 'Окей, тогда не напоминаю.'
}) })
expect(await promptRepository.getPendingAction('-10012345', '10002')).toBeNull() expect(await promptRepository.getPendingAction('-10012345', '10002')).toBeNull()
@@ -403,9 +411,9 @@ describe('registerAdHocNotifications', () => {
reminderCallbackUpdate(`adhocnotif:confirm:${renewedProposalId}`) as never reminderCallbackUpdate(`adhocnotif:confirm:${renewedProposalId}`) as never
) )
expect(calls[4]?.method).toBe('answerCallbackQuery') expect(calls[5]?.method).toBe('answerCallbackQuery')
expect(calls[5]?.method).toBe('editMessageText') expect(calls[6]?.method).toBe('editMessageText')
expect(calls[5]?.payload).toMatchObject({ expect(calls[6]?.payload).toMatchObject({
text: `Окей, ${initialWhen} напомню.` text: `Окей, ${initialWhen} напомню.`
}) })

View File

@@ -45,6 +45,7 @@ type NotificationDraftPayload =
| { | {
stage: 'confirm' stage: 'confirm'
proposalId: string proposalId: string
confirmationMessageId: number | null
householdId: string householdId: string
threadId: string threadId: string
creatorMemberId: string creatorMemberId: string
@@ -418,10 +419,10 @@ async function replyInTopic(
options?: { options?: {
parseMode?: 'HTML' parseMode?: 'HTML'
} }
): Promise<void> { ): Promise<number | null> {
const message = ctx.msg const message = ctx.msg
if (!ctx.chat || !message) { if (!ctx.chat || !message) {
return return null
} }
const threadId = const threadId =
@@ -429,7 +430,7 @@ async function replyInTopic(
? message.message_thread_id ? message.message_thread_id
: undefined : undefined
await ctx.api.sendMessage(ctx.chat.id, text, { const sentMessage = await ctx.api.sendMessage(ctx.chat.id, text, {
...(threadId !== undefined ...(threadId !== undefined
? { ? {
message_thread_id: threadId message_thread_id: threadId
@@ -449,6 +450,8 @@ async function replyInTopic(
} }
: {}) : {})
}) })
return sentMessage.message_id
} }
async function resolveReminderTopicContext( async function resolveReminderTopicContext(
@@ -593,7 +596,8 @@ export function registerAdHocNotifications(options: {
async function showDraftConfirmation( async function showDraftConfirmation(
ctx: Context, ctx: Context,
draft: Extract<NotificationDraftPayload, { stage: 'confirm' }> draft: Extract<NotificationDraftPayload, { stage: 'confirm' }>,
previousConfirmationMessageId?: number | null
) { ) {
const reminderContext = await resolveReminderTopicContext( const reminderContext = await resolveReminderTopicContext(
ctx, ctx,
@@ -603,7 +607,7 @@ export function registerAdHocNotifications(options: {
return return
} }
await replyInTopic( const confirmationMessageId = await replyInTopic(
ctx, ctx,
notificationSummaryText({ notificationSummaryText({
locale: reminderContext.locale, locale: reminderContext.locale,
@@ -612,6 +616,23 @@ export function registerAdHocNotifications(options: {
}), }),
notificationDraftReplyMarkup(reminderContext.locale, draft, reminderContext.members) notificationDraftReplyMarkup(reminderContext.locale, draft, reminderContext.members)
) )
if (
previousConfirmationMessageId &&
ctx.chat &&
previousConfirmationMessageId !== confirmationMessageId
) {
await ctx.api.editMessageReplyMarkup(ctx.chat.id, previousConfirmationMessageId, {
reply_markup: {
inline_keyboard: []
}
})
}
await saveDraft(options.promptRepository, ctx, {
...draft,
confirmationMessageId
})
} }
async function refreshConfirmationMessage( async function refreshConfirmationMessage(
@@ -791,12 +812,12 @@ export function registerAdHocNotifications(options: {
const confirmPayload: Extract<NotificationDraftPayload, { stage: 'confirm' }> = { const confirmPayload: Extract<NotificationDraftPayload, { stage: 'confirm' }> = {
...existingDraft, ...existingDraft,
stage: 'confirm', stage: 'confirm',
confirmationMessageId: null,
renderedNotificationText, renderedNotificationText,
scheduledForIso: schedule.scheduledFor!.toString(), scheduledForIso: schedule.scheduledFor!.toString(),
timePrecision: schedule.timePrecision!, timePrecision: schedule.timePrecision!,
viewMode: 'compact' viewMode: 'compact'
} }
await saveDraft(options.promptRepository, ctx, confirmPayload)
await showDraftConfirmation(ctx, confirmPayload) await showDraftConfirmation(ctx, confirmPayload)
return return
} }
@@ -932,8 +953,7 @@ export function registerAdHocNotifications(options: {
viewMode: 'compact' viewMode: 'compact'
} }
await saveDraft(options.promptRepository, ctx, nextPayload) await showDraftConfirmation(ctx, nextPayload, existingDraft.confirmationMessageId)
await showDraftConfirmation(ctx, nextPayload)
return return
} }
@@ -1043,6 +1063,7 @@ export function registerAdHocNotifications(options: {
const draft: Extract<NotificationDraftPayload, { stage: 'confirm' }> = { const draft: Extract<NotificationDraftPayload, { stage: 'confirm' }> = {
stage: 'confirm', stage: 'confirm',
proposalId: createProposalId(), proposalId: createProposalId(),
confirmationMessageId: null,
householdId: reminderContext.householdId, householdId: reminderContext.householdId,
threadId: reminderContext.threadId, threadId: reminderContext.threadId,
creatorMemberId: reminderContext.member.id, creatorMemberId: reminderContext.member.id,
@@ -1058,7 +1079,6 @@ export function registerAdHocNotifications(options: {
viewMode: 'compact' viewMode: 'compact'
} }
await saveDraft(options.promptRepository, ctx, draft)
await showDraftConfirmation(ctx, draft) await showDraftConfirmation(ctx, draft)
}) })