From 0ea5298d77162e03140b1625deb84e1aac9f273f Mon Sep 17 00:00:00 2001 From: Anand Chavan Date: Fri, 13 Nov 2026 13:41:46 +0530 Subject: [PATCH] fix(request-copy): prevent duplicate submissions on request-a-copy and grant/deny flows Repeated clicks on the "Request copy" button and on the "Send" button of the grant and deny email forms fired one REST request per click, creating duplicate item requests and duplicate notification emails. Guard each submit with an in-flight flag, disable the button and show a spinner while the request is pending, and re-enable it when the request fails so the user can retry. Fixes #6541 --- ...tstream-request-a-copy-page.component.html | 10 ++++-- ...ream-request-a-copy-page.component.spec.ts | 32 ++++++++++++++++++- ...bitstream-request-a-copy-page.component.ts | 10 ++++++ .../deny-request-copy.component.html | 2 +- .../deny-request-copy.component.spec.ts | 23 ++++++++++++- .../deny-request-copy.component.ts | 10 ++++++ .../email-request-copy.component.html | 9 ++++-- .../email-request-copy.component.spec.ts | 8 +++++ .../email-request-copy.component.ts | 8 +++++ .../themed-email-request-copy.component.ts | 7 +++- .../grant-request-copy.component.html | 1 + .../grant-request-copy.component.spec.ts | 23 ++++++++++++- .../grant-request-copy.component.ts | 10 ++++++ src/assets/i18n/en.json5 | 4 +++ 14 files changed, 148 insertions(+), 9 deletions(-) diff --git a/src/app/item-page/bitstreams/request-a-copy/bitstream-request-a-copy-page.component.html b/src/app/item-page/bitstreams/request-a-copy/bitstream-request-a-copy-page.component.html index 3ba6d00fc2c..582fc7d681b 100644 --- a/src/app/item-page/bitstreams/request-a-copy/bitstream-request-a-copy-page.component.html +++ b/src/app/item-page/bitstreams/request-a-copy/bitstream-request-a-copy-page.component.html @@ -101,9 +101,15 @@

{{'bitstream-request-a-copy.header' | translate}}

+ (click)="onSubmit()"> + @if (!submitting) { + {{'bitstream-request-a-copy.submit' | translate}} + } @else { + {{'bitstream-request-a-copy.submitting' | translate}} + } + diff --git a/src/app/item-page/bitstreams/request-a-copy/bitstream-request-a-copy-page.component.spec.ts b/src/app/item-page/bitstreams/request-a-copy/bitstream-request-a-copy-page.component.spec.ts index 53736134084..1be7d424055 100644 --- a/src/app/item-page/bitstreams/request-a-copy/bitstream-request-a-copy-page.component.spec.ts +++ b/src/app/item-page/bitstreams/request-a-copy/bitstream-request-a-copy-page.component.spec.ts @@ -44,7 +44,10 @@ import { import { Store } from '@ngrx/store'; import { provideMockStore } from '@ngrx/store/testing'; import { TranslateModule } from '@ngx-translate/core'; -import { of } from 'rxjs'; +import { + NEVER, + of, +} from 'rxjs'; import { BitstreamRequestACopyPageComponent } from './bitstream-request-a-copy-page.component'; @@ -326,6 +329,33 @@ describe('BitstreamRequestACopyPageComponent', () => { expect(itemRequestDataService.requestACopy).toHaveBeenCalledWith(itemRequest, 'payload'); expect(notificationsService.error).toHaveBeenCalled(); expect(location.back).not.toHaveBeenCalled(); + expect(component.submitting).toBeFalse(); + }); + }); + + describe('while the request is in flight', () => { + beforeEach(waitForAsync(() => { + init(); + (itemRequestDataService.requestACopy as jasmine.Spy).and.returnValue(NEVER); + initTestbed(); + })); + beforeEach(() => { + fixture = TestBed.createComponent(BitstreamRequestACopyPageComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + it('should only submit the request once', () => { + component.name.patchValue('User Name'); + component.email.patchValue('user@name.org'); + component.allfiles.patchValue('false'); + component.message.patchValue('I would like to request a copy'); + component.captchaPayload.patchValue('payload'); + + component.onSubmit(); + component.onSubmit(); + + expect(itemRequestDataService.requestACopy).toHaveBeenCalledTimes(1); + expect(component.submitting).toBeTrue(); }); }); }); diff --git a/src/app/item-page/bitstreams/request-a-copy/bitstream-request-a-copy-page.component.ts b/src/app/item-page/bitstreams/request-a-copy/bitstream-request-a-copy-page.component.ts index bc503bfcf85..d4c3bb21310 100644 --- a/src/app/item-page/bitstreams/request-a-copy/bitstream-request-a-copy-page.component.ts +++ b/src/app/item-page/bitstreams/request-a-copy/bitstream-request-a-copy-page.component.ts @@ -101,6 +101,11 @@ export class BitstreamRequestACopyPageComponent implements OnInit, OnDestroy { captchaEnabled$: Observable; challengeHref$: Observable; + /** + * Whether the request is currently being sent; disables the submit button to prevent duplicate requests/emails + */ + submitting = false; + constructor(private location: Location, private translateService: TranslateService, private route: ActivatedRoute, @@ -242,6 +247,10 @@ export class BitstreamRequestACopyPageComponent implements OnInit, OnDestroy { * When the submission fails, the user will stay on the page and an error notification will be shown */ onSubmit() { + if (this.submitting || this.requestCopyForm.invalid) { + return; + } + this.submitting = true; const itemRequest = new ItemRequest(); if (hasValue(this.bitstream)) { itemRequest.bitstreamId = this.bitstream.uuid; @@ -260,6 +269,7 @@ export class BitstreamRequestACopyPageComponent implements OnInit, OnDestroy { this.notificationsService.success(this.translateService.get('bitstream-request-a-copy.submit.success')); this.navigateBack(); } else { + this.submitting = false; this.notificationsService.error(this.translateService.get('bitstream-request-a-copy.submit.error')); } }); diff --git a/src/app/request-copy/deny-request-copy/deny-request-copy.component.html b/src/app/request-copy/deny-request-copy/deny-request-copy.component.html index 8fa823d455f..84014b07db9 100644 --- a/src/app/request-copy/deny-request-copy/deny-request-copy.component.html +++ b/src/app/request-copy/deny-request-copy/deny-request-copy.component.html @@ -3,7 +3,7 @@

{{'deny-request-copy.header' | translate}}

@if (itemRequestRD && itemRequestRD.hasSucceeded) {

{{'deny-request-copy.intro' | translate}}

- +
} @if (!itemRequestRD || itemRequestRD?.isLoading) { diff --git a/src/app/request-copy/deny-request-copy/deny-request-copy.component.spec.ts b/src/app/request-copy/deny-request-copy/deny-request-copy.component.spec.ts index ec1f13aefdd..89f6c24ac47 100644 --- a/src/app/request-copy/deny-request-copy/deny-request-copy.component.spec.ts +++ b/src/app/request-copy/deny-request-copy/deny-request-copy.component.spec.ts @@ -28,7 +28,10 @@ import { TranslateModule, TranslateService, } from '@ngx-translate/core'; -import { of } from 'rxjs'; +import { + NEVER, + of, +} from 'rxjs'; import { getMockThemeService } from '../../shared/theme-support/test/theme-service.mock'; import { ThemeService } from '../../shared/theme-support/theme.service'; @@ -182,6 +185,24 @@ describe('DenyRequestCopyComponent', () => { it('should not navigate', () => { expect(router.navigateByUrl).not.toHaveBeenCalled(); }); + + it('should allow sending again', () => { + expect(component.sending).toBeFalse(); + }); + }); + + describe('when the request is still in flight', () => { + beforeEach(() => { + email = new RequestCopyEmail('subject', 'message'); + (itemRequestService.deny as jasmine.Spy).and.returnValue(NEVER); + component.deny(email); + component.deny(email); + }); + + it('should only send the request once', () => { + expect(itemRequestService.deny).toHaveBeenCalledTimes(1); + expect(component.sending).toBeTrue(); + }); }); }); }); diff --git a/src/app/request-copy/deny-request-copy/deny-request-copy.component.ts b/src/app/request-copy/deny-request-copy/deny-request-copy.component.ts index 2187ea61dca..e05ad8ed866 100644 --- a/src/app/request-copy/deny-request-copy/deny-request-copy.component.ts +++ b/src/app/request-copy/deny-request-copy/deny-request-copy.component.ts @@ -70,6 +70,11 @@ export class DenyRequestCopyComponent implements OnInit { */ message$: Observable; + /** + * Whether the email is currently being sent, used to prevent duplicate submissions + */ + sending = false; + constructor( private router: Router, private route: ActivatedRoute, @@ -122,6 +127,10 @@ export class DenyRequestCopyComponent implements OnInit { * @param email Subject and contents of the message to send back to the user requesting the item */ deny(email: RequestCopyEmail) { + if (this.sending) { + return; + } + this.sending = true; this.itemRequestRD$.pipe( getFirstSucceededRemoteDataPayload(), switchMap((itemRequest: ItemRequest) => this.itemRequestService.deny(itemRequest.token, email)), @@ -131,6 +140,7 @@ export class DenyRequestCopyComponent implements OnInit { this.notificationsService.success(this.translateService.get('deny-request-copy.success')); this.router.navigateByUrl('/'); } else { + this.sending = false; this.notificationsService.error(this.translateService.get('deny-request-copy.error'), rd.errorMessage); } }); diff --git a/src/app/request-copy/email-request-copy/email-request-copy.component.html b/src/app/request-copy/email-request-copy/email-request-copy.component.html index 0b009b8d2b3..20a05a56dd4 100644 --- a/src/app/request-copy/email-request-copy/email-request-copy.component.html +++ b/src/app/request-copy/email-request-copy/email-request-copy.component.html @@ -44,12 +44,17 @@