Skip to content
Snippets Groups Projects
Commit 48511da7 authored by Sebastian Bergandy's avatar Sebastian Bergandy :keyboard:
Browse files

Merge branch 'OZG-5977-clear-failued-upload-on-upload' into 'main'

OZG-5977 clear failed uploads on next upload

See merge request !87
parents 66ceceae 5d3b9374
No related branches found
No related tags found
1 merge request!87OZG-5977 clear failed uploads on next upload
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
* Die sprachspezifischen Genehmigungen und Beschränkungen * Die sprachspezifischen Genehmigungen und Beschränkungen
* unter der Lizenz sind dem Lizenztext zu entnehmen. * 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 { Mock, mock, useFromMock } from '@alfa-client/test-utils';
import { SnackBarService } from '@alfa-client/ui'; import { SnackBarService } from '@alfa-client/ui';
import { HttpErrorResponse, HttpResponse } from '@angular/common/http'; import { HttpErrorResponse, HttpResponse } from '@angular/common/http';
...@@ -36,6 +36,7 @@ import { DummyLinkRel } from 'libs/tech-shared/test/dummy'; ...@@ -36,6 +36,7 @@ import { DummyLinkRel } from 'libs/tech-shared/test/dummy';
import { createDummyResource } from 'libs/tech-shared/test/resource'; import { createDummyResource } from 'libs/tech-shared/test/resource';
import { uniqueId } from 'lodash-es'; import { uniqueId } from 'lodash-es';
import { Observable, of, throwError } from 'rxjs'; import { Observable, of, throwError } from 'rxjs';
import { createApiError } from '../../../tech-shared/test/error';
import { createHttpErrorResponse } from '../../../tech-shared/test/http'; import { createHttpErrorResponse } from '../../../tech-shared/test/http';
import { multipleCold, singleCold, singleHot } from '../../../tech-shared/test/marbles'; import { multipleCold, singleCold, singleHot } from '../../../tech-shared/test/marbles';
import { BinaryFileResource, FileUploadType, ToUploadFile, UploadFile, UploadFileByIdentifier } from './binary-file.model'; import { BinaryFileResource, FileUploadType, ToUploadFile, UploadFile, UploadFileByIdentifier } from './binary-file.model';
...@@ -98,11 +99,18 @@ describe('BinaryFileService', () => { ...@@ -98,11 +99,18 @@ describe('BinaryFileService', () => {
const toUploadFile: ToUploadFile = { type, file, uploadUrl: faker.internet.url() }; const toUploadFile: ToUploadFile = { type, file, uploadUrl: faker.internet.url() };
beforeEach(() => { beforeEach(() => {
service._clearFailedUploads = jest.fn();
service._generateUniqueId = jest.fn().mockReturnValue(uniqId); service._generateUniqueId = jest.fn().mockReturnValue(uniqId);
service._addUploadFileLoading = jest.fn(); service._addUploadFileLoading = jest.fn();
service._doUploadFile = 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', () => { it('should create an empty map if type key not exists', () => {
service._uploadFiles$.next({}); service._uploadFiles$.next({});
...@@ -124,6 +132,39 @@ describe('BinaryFileService', () => { ...@@ -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', () => { describe('add upload file loading', () => {
const uniqId: string = uniqueId(); const uniqId: string = uniqueId();
const type: string = 'dummyType'; const type: string = 'dummyType';
......
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
* Die sprachspezifischen Genehmigungen und Beschränkungen * Die sprachspezifischen Genehmigungen und Beschränkungen
* unter der Lizenz sind dem Lizenztext zu entnehmen. * 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 { SnackBarService } from '@alfa-client/ui';
import { HttpErrorResponse, HttpResponse } from '@angular/common/http'; import { HttpErrorResponse, HttpResponse } from '@angular/common/http';
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
...@@ -50,12 +50,25 @@ export class BinaryFileService { ...@@ -50,12 +50,25 @@ export class BinaryFileService {
} }
public uploadFileNew(toUploadFile: ToUploadFile): void { public uploadFileNew(toUploadFile: ToUploadFile): void {
this._clearFailedUploads(toUploadFile.type);
this.createEmptyMapIfTypeNotExists(toUploadFile.type); this.createEmptyMapIfTypeNotExists(toUploadFile.type);
const uniqId: string = this._generateUniqueId(); const uniqId: string = this._generateUniqueId();
this._addUploadFileLoading(uniqId, toUploadFile); this._addUploadFileLoading(uniqId, toUploadFile);
this._doUploadFile(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 { _generateUniqueId(): string {
return uniqueId(); return uniqueId();
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment