export recommendations with extra labels

This commit is contained in:
Maximilian Krambach 2019-04-03 11:31:08 +02:00
parent aa9af9db8c
commit e3c673b55e
3 changed files with 48 additions and 10 deletions

View File

@ -29,7 +29,7 @@ function isPropertyDefinition<T>(obj: any): obj is CsvColumnDefinitionProperty<T
* all the models. This map function is called for every model and the user should return a string that is
* put into the csv. Also a column label must be given, that is capitalized and translated.
*/
interface CsvColumnDefinitionMap<T> {
export interface CsvColumnDefinitionMap<T> {
label: string;
map: (model: T) => string;
}

View File

@ -2,8 +2,13 @@ import { Injectable } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { CsvExportService, CsvColumnDefinitionProperty } from 'app/core/ui-services/csv-export.service';
import {
CsvExportService,
CsvColumnDefinitionProperty,
CsvColumnDefinitionMap
} from 'app/core/ui-services/csv-export.service';
import { InfoToExport } from './motion-pdf.service';
import { MotionRepositoryService } from 'app/core/repositories/motions/motion-repository.service';
import { ViewMotion } from '../models/view-motion';
/**
@ -19,7 +24,11 @@ export class MotionCsvExportService {
* @param csvExport CsvExportService
* @param translate TranslateService
*/
public constructor(private csvExport: CsvExportService, private translate: TranslateService) {}
public constructor(
private csvExport: CsvExportService,
private translate: TranslateService,
private motionRepo: MotionRepositoryService
) {}
/**
* Export all motions as CSV
@ -30,8 +39,22 @@ export class MotionCsvExportService {
*/
public exportMotionList(motions: ViewMotion[], contentToExport: string[], infoToExport: InfoToExport[]): void {
const propertyList = ['identifier', 'title'].concat(contentToExport, infoToExport);
const exportProperties: CsvColumnDefinitionProperty<ViewMotion>[] = propertyList.map(option => {
const exportProperties: (
| CsvColumnDefinitionProperty<ViewMotion>
| CsvColumnDefinitionMap<ViewMotion>)[] = propertyList.map(option => {
if (option === 'recommendation') {
return {
label: 'recommendation',
map: motion => this.motionRepo.getExtendedRecommendationLabel(motion)
};
} else if (option === 'state') {
return {
label: 'state',
map: motion => this.motionRepo.getExtendedStateLabel(motion)
};
} else {
return { property: option } as CsvColumnDefinitionProperty<ViewMotion>;
}
});
this.csvExport.export(motions, exportProperties, this.translate.instant('Motions') + '.csv');
@ -52,8 +75,7 @@ export class MotionCsvExportService {
{ property: 'title' },
{
label: 'recommendation',
map: motion =>
motion.recommendation ? this.translate.instant(motion.recommendation.recommendation_label) : ''
map: motion => (motion.recommendation ? this.motionRepo.getExtendedRecommendationLabel(motion) : '')
},
{ property: 'motion_block', label: 'Motion block' }
],

View File

@ -3,9 +3,10 @@ import { Injectable } from '@angular/core';
import { Workbook } from 'exceljs';
import { InfoToExport } from './motion-pdf.service';
import { MotionRepositoryService } from 'app/core/repositories/motions/motion-repository.service';
import { TranslateService } from '@ngx-translate/core';
import { ViewMotion } from '../models/view-motion';
import { XlsxExportServiceService } from 'app/core/ui-services/xlsx-export-service.service';
import { TranslateService } from '@ngx-translate/core';
/**
* Service to export motion elements to XLSX
@ -16,8 +17,16 @@ import { TranslateService } from '@ngx-translate/core';
export class MotionXlsxExportService {
/**
* Constructor
*
* @param xlsx XlsxExportServiceService
* @param translate translationService
* @param motionRepo MotionRepositoryService
*/
public constructor(private xlsx: XlsxExportServiceService, private translate: TranslateService) {}
public constructor(
private xlsx: XlsxExportServiceService,
private translate: TranslateService,
private motionRepo: MotionRepositoryService
) {}
/**
* Export motions as XLSX
@ -53,7 +62,14 @@ export class MotionXlsxExportService {
properties.map(property => {
const motionProp = motion[property];
if (motionProp) {
switch (property) {
case 'state':
return this.motionRepo.getExtendedStateLabel(motion);
case 'recommendation':
return this.motionRepo.getExtendedRecommendationLabel(motion);
default:
return this.translate.instant(motionProp.toString());
}
} else {
return null;
}