Skip to content

feat/AB#106234_EIS-improve-messages-for-file-download #2708

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: alpha
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions apps/back-office/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
AppAbility,
FormService,
DatePipe,
EISDocsInterceptorService,
} from '@oort-front/shared';
import { registerLocaleData } from '@angular/common';
import localeFr from '@angular/common/locales/fr';
Expand Down Expand Up @@ -154,6 +155,11 @@ export const httpTranslateLoader = (http: HttpClient) =>
useClass: AuthInterceptorService,
multi: true,
},
{
provide: HTTP_INTERCEPTORS,
useClass: EISDocsInterceptorService,
multi: true,
},
{
provide: AppAbility,
useValue: new AppAbility(),
Expand Down
6 changes: 6 additions & 0 deletions apps/front-office/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
AuthInterceptorService,
FormService,
DatePipe,
EISDocsInterceptorService,
} from '@oort-front/shared';
import { registerLocaleData } from '@angular/common';
import localeFr from '@angular/common/locales/fr';
Expand Down Expand Up @@ -150,6 +151,11 @@ export const httpTranslateLoader = (http: HttpClient) =>
useClass: AuthInterceptorService,
multi: true,
},
{
provide: HTTP_INTERCEPTORS,
useClass: EISDocsInterceptorService,
multi: true,
},
{
provide: AppAbility,
useValue: new AppAbility(),
Expand Down
6 changes: 6 additions & 0 deletions apps/web-widgets/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
AuthInterceptorService,
FormService,
DatePipe,
EISDocsInterceptorService,
} from '@oort-front/shared';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { OverlayContainer, OverlayModule } from '@angular/cdk/overlay';
Expand Down Expand Up @@ -198,6 +199,11 @@ export const getBaseHref = () => {
useClass: AuthInterceptorService,
multi: true,
},
{
provide: HTTP_INTERCEPTORS,
useClass: EISDocsInterceptorService,
multi: true,
},
{
provide: AppAbility,
useValue: new AppAbility(),
Expand Down
5 changes: 5 additions & 0 deletions libs/shared/src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,11 @@
},
"fetchingGroups": "Fetching groups from service...",
"file": {
"csapi": {
"invalid": "File not valid",
"notFound": "File not found",
"unauthorized": "Unauthorized"
},
"download": {
"error": "Something went wrong during the download",
"ongoing": "Your file export is ongoing. You will receive an email once ready.",
Expand Down
5 changes: 5 additions & 0 deletions libs/shared/src/i18n/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,11 @@
},
"fetchingGroups": "Fetching groups from service...",
"file": {
"csapi": {
"invalid": "**************************",
"notFound": "**************************",
"unauthorized": "**************************"
},
"download": {
"error": "Un problème est survenu lors du téléchargement",
"ongoing": "Votre téléchargement est en cours. Vous recevrez bientôt un e-mail contenant votre fichier.",
Expand Down
5 changes: 5 additions & 0 deletions libs/shared/src/i18n/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,11 @@
},
"fetchingGroups": "******",
"file": {
"csapi": {
"invalid": "******",
"notFound": "******",
"unauthorized": "******"
},
"download": {
"error": "******",
"ongoing": "******",
Expand Down
7 changes: 4 additions & 3 deletions libs/shared/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export * from './lib/services/date-translate/date-translate.service';
export * from './lib/services/document-management/document-management.service';
export * from './lib/services/download/download.service';
export * from './lib/services/editor/editor.service';
export * from './lib/services/eis-docs-interceptor/eis-docs-interceptor.service';
export * from './lib/services/form-builder/form-builder.service';
export * from './lib/services/form/form.service';
export * from './lib/services/grid-data-formatter/grid-data-formatter.service';
Expand All @@ -27,8 +28,8 @@ export * from './lib/services/grid/grid.service';
export * from './lib/services/html-parser/html-parser.service';
export * from './lib/services/kendo-translation/kendo-translation.service';
export * from './lib/services/map/map-layers.service';
export * from './lib/services/reference-data/reference-data.service';
export * from './lib/services/query-builder/query-builder.service';
export * from './lib/services/reference-data/reference-data.service';
export * from './lib/services/rest/rest.service';
export * from './lib/services/workflow/workflow.service';

Expand Down Expand Up @@ -66,6 +67,7 @@ export * from './lib/models/workflow.model';

// === COMPONENTS ===
export * from './lib/components/access/public-api';
export * from './lib/components/action-buttons/public-api';
export * from './lib/components/aggregation/edit-aggregation-modal/edit-aggregation-modal.component';
export * from './lib/components/applications-summary/public-api';
export * from './lib/components/confirm-modal/public-api';
Expand All @@ -85,6 +87,7 @@ export * from './lib/components/layout/public-api';
export * from './lib/components/mapping/public-api';
export * from './lib/components/navbar/public-api';
export * from './lib/components/payload-modal/payload-modal.component';
export * from './lib/components/query-builder/public-api';
export * from './lib/components/record-history/public-api';
export * from './lib/components/record-modal/public-api';
export * from './lib/components/role-summary/public-api';
Expand All @@ -106,8 +109,6 @@ export * from './lib/components/widgets/map-settings/public-api';
export * from './lib/components/widgets/map/public-api';
export * from './lib/components/widgets/summary-card-settings/public-api';
export * from './lib/components/workflow-stepper/public-api';
export * from './lib/components/query-builder/public-api';
export * from './lib/components/action-buttons/public-api';

// Export of controls
export * from './lib/components/applications-archive/public-api';
Expand Down
29 changes: 15 additions & 14 deletions libs/shared/src/lib/components/form/form.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Apollo } from 'apollo-angular';
import { Dialog } from '@angular/cdk/dialog';
import { HttpErrorResponse } from '@angular/common/http';
import {
Component,
ElementRef,
Expand All @@ -9,25 +10,25 @@ import {
Output,
ViewChild,
} from '@angular/core';
import { Dialog } from '@angular/cdk/dialog';
import { TranslateService } from '@ngx-translate/core';
import { SnackbarService, UILayoutService } from '@oort-front/ui';
import { Apollo } from 'apollo-angular';
import { isNil } from 'lodash';
import { BehaviorSubject, takeUntil } from 'rxjs';
import { SurveyModel } from 'survey-core';
import { ADD_RECORD, EDIT_RECORD } from './graphql/mutations';
import { Form } from '../../models/form.model';
import {
AddRecordMutationResponse,
EditRecordMutationResponse,
Record as RecordModel,
} from '../../models/record.model';
import { BehaviorSubject, takeUntil } from 'rxjs';
import addCustomFunctions from '../../utils/custom-functions';
import { AuthService } from '../../services/auth/auth.service';
import { FormBuilderService } from '../../services/form-builder/form-builder.service';
import { FormHelpersService } from '../../services/form-helper/form-helper.service';
import addCustomFunctions from '../../utils/custom-functions';
import { RecordHistoryComponent } from '../record-history/record-history.component';
import { TranslateService } from '@ngx-translate/core';
import { UnsubscribeComponent } from '../utils/unsubscribe/unsubscribe.component';
import { FormHelpersService } from '../../services/form-helper/form-helper.service';
import { SnackbarService, UILayoutService } from '@oort-front/ui';
import { isNil } from 'lodash';
import { ADD_RECORD, EDIT_RECORD } from './graphql/mutations';

/**
* This component is used to display forms
Expand Down Expand Up @@ -272,11 +273,11 @@ export class FormComponent
);
} catch (errors) {
/** If there is any upload errors, save them for display */
const uploadErrors = (errors as { question: string; file: File }[]).map(
(error) => {
return `${error.question}: ${error.file.name}`;
}
);
const uploadErrors = (
errors as { question: string; file: File; error: HttpErrorResponse }[]
).map((error) => {
return `- Error ${error.error.status}, ${error.error.message} for ${error.question}: ${error.file.name}`;
});
this.snackBar.openSnackBar(
this.translate.instant('models.form.notifications.savingFailed') +
(!isNil(uploadErrors) ? '\n' + uploadErrors?.join('\n') : ''),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import { DOCUMENT } from '@angular/common';
import { HttpHeaders } from '@angular/common/http';
import { HttpHeaders, HttpStatusCode } from '@angular/common/http';
import { Inject, Injectable } from '@angular/core';
import { InMemoryCache } from '@apollo/client';
import { setContext } from '@apollo/client/link/context';
import { TranslateService } from '@ngx-translate/core';
import { SnackbarService } from '@oort-front/ui';
import { Apollo } from 'apollo-angular';
import { HttpLink } from 'apollo-angular/http';
import { isNil, set } from 'lodash';
import { catchError, firstValueFrom, throwError } from 'rxjs';
import { Question } from 'survey-core';
import { SnackbarSpinnerComponent } from '../../components/snackbar-spinner/snackbar-spinner.component';
import { RestService } from '../rest/rest.service';
import { Apollo } from 'apollo-angular';
import {
DriveQueryResponse,
GET_DRIVE_ID,
GET_OCCURRENCE_BY_ID,
OccurrenceQueryResponse,
} from './graphql/queries';
import { firstValueFrom } from 'rxjs';
import { InMemoryCache } from '@apollo/client';
import { HttpLink } from 'apollo-angular/http';
import { setContext } from '@apollo/client/link/context';

/**
* Available properties from the CS API Documentation
Expand Down Expand Up @@ -160,6 +160,11 @@ export class DocumentManagementService {
const url = `${this.environment.csApiUrl}/documents/drives/${file.content.driveId}/items/${file.content.itemId}/content`;
this.restService
.get(url, { ...options, responseType: 'blob', headers })
.pipe(
catchError((err) => {
return throwError(() => this.buildCSApiError(err));
})
)
.subscribe({
next: (res) => {
const blob = new Blob([res]);
Expand All @@ -170,13 +175,12 @@ export class DocumentManagementService {
snackBarSpinner.instance.loading = false;
snackBarRef.instance.triggerSnackBar(SNACKBAR_DURATION);
},
error: () => {
snackBarSpinner.instance.message = this.translate.instant(
'common.notifications.file.download.error'
error: async (error) => {
snackBarRef.instance.dismiss();
this.snackBar.openSnackBar(
`Error ${error.status}, ${error.message}:\n${file.name}`,
{ error: true }
);
snackBarSpinner.instance.loading = false;
snackBarSpinner.instance.error = true;
snackBarRef.instance.triggerSnackBar(SNACKBAR_DURATION);
},
});
}
Expand All @@ -197,6 +201,30 @@ export class DocumentManagementService {
link.remove();
}

/**
* Update the given error instance from the eis-docs-interceptor to be displayed with correct message based on it's status
*
* @param error Error formatted from the eis-docs-interceptor file
* @returns Error instance with correct messages per status, or default if different status
*/
private buildCSApiError(error: any) {
const parseError = JSON.parse(error.message || '{}');
if (parseError.status === HttpStatusCode.Unauthorized) {
parseError.message = this.translate.instant(
'common.notifications.file.csapi.unauthorized'
);
} else if (parseError.status === HttpStatusCode.NotFound) {
parseError.message = this.translate.instant(
'common.notifications.file.csapi.notFound'
);
} else if (parseError.status === HttpStatusCode.BadRequest) {
parseError.message = this.translate.instant(
'common.notifications.file.csapi.invalid'
);
}
return parseError;
}

/**
* Uploads a file
*
Expand Down Expand Up @@ -270,6 +298,11 @@ export class DocumentManagementService {
headers,
}
)
.pipe(
catchError((err) => {
return throwError(() => this.buildCSApiError(err));
})
)
.subscribe({
next: (data) => {
const { itemId, driveId } = data;
Expand All @@ -283,14 +316,9 @@ export class DocumentManagementService {
itemId,
});
},
error: () => {
snackBarSpinner.instance.message = this.translate.instant(
'common.notifications.file.upload.error'
);
snackBarSpinner.instance.loading = false;
snackBarSpinner.instance.error = true;
snackBarRef.instance.triggerSnackBar(SNACKBAR_DURATION);
reject(null);
error: (error) => {
snackBarRef.instance.dismiss();
reject(error);
},
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { TestBed } from '@angular/core/testing';
import { EISDocsInterceptorService } from './eis-docs-interceptor.service';
import { ApolloTestingModule } from 'apollo-angular/testing';
import { HttpClientModule } from '@angular/common/http';

describe('EISDocsInterceptorService', () => {
let service: EISDocsInterceptorService;

beforeEach(() => {
TestBed.configureTestingModule({
providers: [
{ provide: 'environment', useValue: { csApiUrl: 'csApiUrl' } },
],
imports: [ApolloTestingModule, HttpClientModule],
});
service = TestBed.inject(EISDocsInterceptorService);
});

it('should be created', () => {
expect(service).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import {
HttpErrorResponse,
HttpEvent,
HttpHandler,
HttpInterceptor,
HttpRequest,
} from '@angular/common/http';
import { Inject, Injectable } from '@angular/core';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';

/**
* Shared EIS docs interceptor service
*/
@Injectable({
providedIn: 'root',
})
export class EISDocsInterceptorService implements HttpInterceptor {
/**
* Shared EIS docs interceptor service
*
* @param environment environment
*/
constructor(@Inject('environment') private environment: any) {}

/**
* Intercept request to format given errors from CS API accordingly to our frontend
*
* @param request http request
* @param next next interceptor in chain
* @returns request with format error if needed
*/
intercept(
request: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
if (request.url.startsWith(this.environment.csApiUrl)) {
return next.handle(request).pipe(
catchError((err) => {
return throwError(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@unai-reliefapp

indeed, it's due to the responseType

Not sure an interceptor is a good solution, as it's just one request
I'll check that

() =>
new HttpErrorResponse({
...err,
error: JSON.stringify(err),
})
);
})
);
}
return next.handle(request);
}
}
Loading