diff --git a/alfa-client/libs/binary-file-shared/src/lib/binary-file.service.spec.ts b/alfa-client/libs/binary-file-shared/src/lib/binary-file.service.spec.ts index 9d26d35e6e9f7a882e0bad43d87f69fcb7562cc3..f32b65dd32e10f8754cb8e138122312211bdac26 100644 --- a/alfa-client/libs/binary-file-shared/src/lib/binary-file.service.spec.ts +++ b/alfa-client/libs/binary-file-shared/src/lib/binary-file.service.spec.ts @@ -21,7 +21,7 @@ * Die sprachspezifischen Genehmigungen und Beschränkungen * unter der Lizenz sind dem Lizenztext zu entnehmen. */ -import { BlobWithFileName, createEmptyStateResource, createStateResource, StateResource } from '@alfa-client/tech-shared'; +import { BlobWithFileName, createEmptyStateResource, createErrorStateResource, createStateResource, StateResource, } from '@alfa-client/tech-shared'; import { Mock, mock, useFromMock } from '@alfa-client/test-utils'; import { SnackBarService } from '@alfa-client/ui'; import { HttpErrorResponse, HttpResponse } from '@angular/common/http'; @@ -36,6 +36,7 @@ import { DummyLinkRel } from 'libs/tech-shared/test/dummy'; import { createDummyResource } from 'libs/tech-shared/test/resource'; import { uniqueId } from 'lodash-es'; import { Observable, of, throwError } from 'rxjs'; +import { createApiError } from '../../../tech-shared/test/error'; import { createHttpErrorResponse } from '../../../tech-shared/test/http'; import { multipleCold, singleCold, singleHot } from '../../../tech-shared/test/marbles'; import { BinaryFileResource, FileUploadType, ToUploadFile, UploadFile, UploadFileByIdentifier } from './binary-file.model'; @@ -98,11 +99,18 @@ describe('BinaryFileService', () => { const toUploadFile: ToUploadFile = { type, file, uploadUrl: faker.internet.url() }; beforeEach(() => { + service._clearFailedUploads = jest.fn(); service._generateUniqueId = jest.fn().mockReturnValue(uniqId); service._addUploadFileLoading = jest.fn(); service._doUploadFile = jest.fn(); }); + it('should clear failed uploads', () => { + service.uploadFileNew(toUploadFile); + + expect(service._clearFailedUploads).toHaveBeenCalled(); + }); + it('should create an empty map if type key not exists', () => { service._uploadFiles$.next({}); @@ -124,6 +132,39 @@ describe('BinaryFileService', () => { }); }); + describe('clear failed uploads', () => { + const fileUploadType: string = faker.word.noun(); + const failedFileKey: string = faker.word.noun(); + const successFileKey: string = faker.word.noun(); + const successUploadFile: UploadFile = createUploadFile(); + const secondSuccessFileKey: string = faker.word.noun(); + const secondFileUploadType: string = faker.word.noun(); + const secondSuccessUploadFile: UploadFile = createUploadFile(); + + it('should remove failed uploads', () => { + service._uploadFiles$.next({ + [fileUploadType]: { + [failedFileKey]: { ...createUploadFile(), uploadedFile: createErrorStateResource(createApiError()) }, + [successFileKey]: successUploadFile, + }, + [secondFileUploadType]: { + [secondSuccessFileKey]: secondSuccessUploadFile, + }, + }); + + service._clearFailedUploads(fileUploadType); + + expect(service._uploadFiles$.value).toEqual({ + [fileUploadType]: { + [successFileKey]: successUploadFile, + }, + [secondFileUploadType]: { + [secondSuccessFileKey]: secondSuccessUploadFile, + }, + }); + }); + }); + describe('add upload file loading', () => { const uniqId: string = uniqueId(); const type: string = 'dummyType'; diff --git a/alfa-client/libs/binary-file-shared/src/lib/binary-file.service.ts b/alfa-client/libs/binary-file-shared/src/lib/binary-file.service.ts index 223db64a230da606052caaf48a90dc45cd60f6a8..b6a628e32f0ef01e8ab34a02fc44a486ce1e69e9 100644 --- a/alfa-client/libs/binary-file-shared/src/lib/binary-file.service.ts +++ b/alfa-client/libs/binary-file-shared/src/lib/binary-file.service.ts @@ -21,7 +21,7 @@ * Die sprachspezifischen Genehmigungen und Beschränkungen * unter der Lizenz sind dem Lizenztext zu entnehmen. */ -import { BlobWithFileName, createEmptyStateResource, createErrorStateResource, createStateResource, EMPTY_STRING, getMessageForInvalidParam, HttpHeader, isNotNil, isUnprocessableEntity, isValidationFieldFileSizeExceedError, sanitizeFileName, StateResource, } from '@alfa-client/tech-shared'; +import { BlobWithFileName, createEmptyStateResource, createErrorStateResource, createStateResource, EMPTY_STRING, getMessageForInvalidParam, hasStateResourceError, HttpHeader, isNotNil, isUnprocessableEntity, isValidationFieldFileSizeExceedError, sanitizeFileName, StateResource, } from '@alfa-client/tech-shared'; import { SnackBarService } from '@alfa-client/ui'; import { HttpErrorResponse, HttpResponse } from '@angular/common/http'; import { Injectable } from '@angular/core'; @@ -50,12 +50,25 @@ export class BinaryFileService { } public uploadFileNew(toUploadFile: ToUploadFile): void { + this._clearFailedUploads(toUploadFile.type); this.createEmptyMapIfTypeNotExists(toUploadFile.type); const uniqId: string = this._generateUniqueId(); this._addUploadFileLoading(uniqId, toUploadFile); this._doUploadFile(uniqId, toUploadFile); } + _clearFailedUploads(fileUploadType: FileUploadType): void { + const uploads: UploadFileByIdentifier = this._uploadFiles$.value[fileUploadType]; + const keys: string[] = Object.keys(uploads); + const successfulUploads: UploadFileByIdentifier = {}; + for (const key of keys) { + if (!hasStateResourceError(uploads[key].uploadedFile)) { + successfulUploads[key] = uploads[key]; + } + } + this._uploadFiles$.next({ ...this._uploadFiles$.value, [fileUploadType]: successfulUploads }); + } + _generateUniqueId(): string { return uniqueId(); }