-
Notifications
You must be signed in to change notification settings - Fork 4
NAE-2100 - Case view export button as NAE feature #279
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0a5f795
NAE-2100 - Case view export button as NAE feature
MartinSiran 6e8e2bb
NAE-2100 - Case view export button as NAE feature
MartinSiran b176578
NAE-2100 - Case view export button as NAE feature
MartinSiran 6596fb0
NAE-2100 - Case view export button as NAE feature
MartinSiran 2366a6b
NAE-2100 - Case view export button as NAE feature
MartinSiran c4aafc2
Merge branch 'release/6.4.2' into NAE-2100
mazarijuraj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
projects/netgrif-components-core/src/lib/export/public-api.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/* SERVICES */ | ||
export * from './services/export.service'; |
104 changes: 104 additions & 0 deletions
104
projects/netgrif-components-core/src/lib/export/services/export.service.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' } | ||
}); | ||
}); | ||
|
||
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' } }); | ||
}); | ||
}); | ||
}); |
71 changes: 71 additions & 0 deletions
71
projects/netgrif-components-core/src/lib/export/services/export.service.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
return this._httpClient.post(AbstractResourceProvider.sanitizeUrl(`/export/filteredCases`, this.SERVER_URL), { | ||
query: activeFilter.getRequestBody(), | ||
selectedDataFieldNames: currentHeaders.filter(header => header).map(header => | ||
header.type === HeaderColumnType.IMMEDIATE ? header.title : this._translate.instant(header.title)), | ||
selectedDataFieldIds: currentHeaders.filter(header => header).map( | ||
header => header.type === HeaderColumnType.IMMEDIATE ? header.fieldIdentifier : (header.fieldIdentifier === 'mongoId' ? `meta-stringId` : `meta-${header.fieldIdentifier}`)), | ||
isIntersection: mergeOperation === MergeOperator.AND | ||
}, { | ||
responseType: 'arraybuffer', observe: 'response' | ||
}).pipe(switchMap((response: any) => { | ||
if (response && response.body) { | ||
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); | ||
return of(true); | ||
} else { | ||
return of(false); | ||
} | ||
})); | ||
} | ||
|
||
public getResourceAddress(name: string, resourcesArray: any): string { | ||
let URL = ''; | ||
if (resourcesArray instanceof Array) { | ||
resourcesArray.forEach(resource => { | ||
if (resource.name === name) { | ||
URL = resource.address; | ||
} | ||
}); | ||
} else { | ||
if (resourcesArray.name === name) { | ||
URL = resourcesArray.address; | ||
} | ||
} | ||
return URL; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.