-
Notifications
You must be signed in to change notification settings - Fork 4
Release/6.4.2 #323
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
Release/6.4.2 #323
Changes from all commits
0a5f795
6e8e2bb
b176578
6596fb0
2366a6b
d1c14ac
c4aafc2
f307847
a7eb4fd
055780f
c56661c
a7853f0
3e9c486
42949df
3b1f375
3c31c12
fb3587d
28679ae
851d55c
55fbd3f
6a9dee8
2c2d2f7
e980c6a
ae4c982
2c780a9
276bad3
0da9108
d87347f
3b66993
f1c7d3c
97a1148
85c598e
c8fd881
6a02e71
a033948
09f6e98
cb30dcc
acf3fcf
bb2a406
87ef587
c9d6ce0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| /* SERVICES */ | ||
| export * from './services/export.service'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import { TestBed } from '@angular/core/testing'; | ||
| import { ExportService } from './export.service'; | ||
| import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; | ||
| import { ConfigurationService } from '../../configuration/configuration.service'; | ||
| import { TranslateService } from '@ngx-translate/core'; | ||
| import { Filter } from '../../filter/models/filter'; | ||
| import { HeaderColumn, HeaderColumnType } from '../../header/models/header-column'; | ||
|
|
||
| describe('ExportService', () => { | ||
| let service: ExportService; | ||
| let httpMock: HttpTestingController; | ||
|
|
||
| const mockConfigService = { | ||
| get: () => ({ | ||
| providers: { | ||
| resources: [{ name: 'case', address: 'http://mock-api' }] | ||
| } | ||
| }) | ||
| }; | ||
|
|
||
| const mockTranslateService = { | ||
| instant: (key: string) => `translated-${key}` | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| TestBed.configureTestingModule({ | ||
| imports: [HttpClientTestingModule], | ||
| providers: [ | ||
| ExportService, | ||
| { provide: ConfigurationService, useValue: mockConfigService }, | ||
| { provide: TranslateService, useValue: mockTranslateService } | ||
| ] | ||
| }); | ||
|
|
||
| service = TestBed.inject(ExportService); | ||
| httpMock = TestBed.inject(HttpTestingController); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| httpMock.verify(); | ||
| }); | ||
|
|
||
| it('should be created', () => { | ||
| expect(service).toBeTruthy(); | ||
| }); | ||
|
|
||
| describe('getResourceAddress()', () => { | ||
| it('should return the address from an array', () => { | ||
| const result = service.getResourceAddress('case', [{ name: 'case', address: 'http://test' }]); | ||
| expect(result).toBe('http://test'); | ||
| }); | ||
|
|
||
| it('should return the address from a single object', () => { | ||
| const result = service.getResourceAddress('case', { name: 'case', address: 'http://test' }); | ||
| expect(result).toBe('http://test'); | ||
| }); | ||
|
|
||
| it('should return an empty string if not found', () => { | ||
| const result = service.getResourceAddress('other', [{ name: 'case', address: 'http://test' }]); | ||
| expect(result).toBe(''); | ||
| }); | ||
| }); | ||
|
|
||
| describe('downloadExcelFromCurrentSelection()', () => { | ||
| it('should return true and trigger file download on valid response', (done) => { | ||
| spyOn(document.body, 'appendChild'); | ||
| spyOn(document.body, 'removeChild'); | ||
|
|
||
| const mockFilter: Filter = { | ||
| getRequestBody: () => ({ some: 'query' }) | ||
| } as any; | ||
|
|
||
| const headers: HeaderColumn[] = [ | ||
| new HeaderColumn(HeaderColumnType.IMMEDIATE, 'name', 'Name', 'string', true, 'net-id'), | ||
| new HeaderColumn(HeaderColumnType.META, 'date', 'Date', 'date') | ||
| ]; | ||
|
|
||
| service.downloadExcelFromCurrentSelection(mockFilter, headers).subscribe((result) => { | ||
| expect(result).toBeTrue(); | ||
| done(); | ||
| }); | ||
|
|
||
| const req = httpMock.expectOne('http://mock-api/export/filteredCases'); | ||
| expect(req.request.method).toBe('POST'); | ||
| req.flush(new ArrayBuffer(10), { | ||
| headers: { 'Content-Type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' } | ||
| }); | ||
| }); | ||
Kovy95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| it('should return false when response body is missing', (done) => { | ||
| const mockFilter: Filter = { | ||
| getRequestBody: () => ({ some: 'query' }) | ||
| } as any; | ||
|
|
||
| service.downloadExcelFromCurrentSelection(mockFilter, []).subscribe((result) => { | ||
| expect(result).toBeFalse(); | ||
| done(); | ||
| }); | ||
|
|
||
| const req = httpMock.expectOne('http://mock-api/export/filteredCases'); | ||
| req.flush(null, { headers: { 'Content-Type': 'application/octet-stream' } }); | ||
| }); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import {Injectable} from '@angular/core'; | ||
| import {AbstractResourceProvider} from '../../resources/resource-provider.service'; | ||
| import {ConfigurationService} from '../../configuration/configuration.service'; | ||
| import {Filter} from '../../filter/models/filter'; | ||
| import {HeaderColumn, HeaderColumnType} from '../../header/models/header-column'; | ||
| import {MergedFilter} from '../../filter/models/merged-filter'; | ||
| import {MergeOperator} from '../../filter/models/merge-operator'; | ||
| import {HttpClient} from '@angular/common/http'; | ||
| import {TranslateService} from '@ngx-translate/core'; | ||
| import {switchMap} from 'rxjs/operators'; | ||
| import {Observable, of} from 'rxjs'; | ||
|
|
||
| @Injectable({ | ||
| providedIn: 'root' | ||
| }) | ||
| export class ExportService { | ||
|
|
||
| protected readonly SERVER_URL: string; | ||
|
|
||
| constructor(protected _httpClient: HttpClient, | ||
| protected _translate: TranslateService, | ||
| protected _configService: ConfigurationService) { | ||
| this.SERVER_URL = this.getResourceAddress('case', this._configService.get().providers.resources); | ||
| } | ||
|
|
||
| public downloadExcelFromCurrentSelection(activeFilter: Filter, currentHeaders: Array<HeaderColumn>): Observable<boolean> { | ||
| const mergeOperation = activeFilter instanceof MergedFilter ? (activeFilter as any)._operator : MergeOperator.AND; | ||
|
|
||
Kovy95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return this._httpClient.post(AbstractResourceProvider.sanitizeUrl(`/export/filteredCases`, this.SERVER_URL), { | ||
| query: activeFilter.getRequestBody(), | ||
| selectedDataFieldNames: currentHeaders.filter(header => header).map(header => | ||
|
Check warning on line 31 in projects/netgrif-components-core/src/lib/export/services/export.service.ts
|
||
| header.type === HeaderColumnType.IMMEDIATE ? header.title : this._translate.instant(header.title)), | ||
| selectedDataFieldIds: currentHeaders.filter(header => header).map( | ||
|
Check warning on line 33 in projects/netgrif-components-core/src/lib/export/services/export.service.ts
|
||
| header => header.type === HeaderColumnType.IMMEDIATE ? header.fieldIdentifier : (header.fieldIdentifier === 'mongoId' ? `meta-stringId` : `meta-${header.fieldIdentifier}`)), | ||
|
Check warning on line 34 in projects/netgrif-components-core/src/lib/export/services/export.service.ts
|
||
| isIntersection: mergeOperation === MergeOperator.AND | ||
| }, { | ||
| responseType: 'arraybuffer', observe: 'response' | ||
| }).pipe(switchMap((response: any) => { | ||
| if (response && response.body) { | ||
|
Check warning on line 39 in projects/netgrif-components-core/src/lib/export/services/export.service.ts
|
||
| const contentType = response.headers.get('Content-Type'); | ||
| const linkElement = document.createElement('a'); | ||
| const blob = new Blob([response.body], {type: contentType}); | ||
| const urlBlob = window.URL.createObjectURL(blob); | ||
| linkElement.setAttribute('href', urlBlob); | ||
| linkElement.setAttribute('download', 'export.xlsx'); | ||
| document.body.appendChild(linkElement); | ||
| linkElement.click(); | ||
| document.body.removeChild(linkElement); | ||
|
Check warning on line 48 in projects/netgrif-components-core/src/lib/export/services/export.service.ts
|
||
| return of(true); | ||
Kovy95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } else { | ||
| return of(false); | ||
| } | ||
| })); | ||
| } | ||
|
|
||
| public getResourceAddress(name: string, resourcesArray: any): string { | ||
| let URL = ''; | ||
| if (resourcesArray instanceof Array) { | ||
|
Check warning on line 58 in projects/netgrif-components-core/src/lib/export/services/export.service.ts
|
||
| resourcesArray.forEach(resource => { | ||
| if (resource.name === name) { | ||
| URL = resource.address; | ||
| } | ||
| }); | ||
| } else { | ||
| if (resourcesArray.name === name) { | ||
|
Check warning on line 65 in projects/netgrif-components-core/src/lib/export/services/export.service.ts
|
||
| URL = resourcesArray.address; | ||
| } | ||
| } | ||
| return URL; | ||
| } | ||
Kovy95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.