modified csv separator in ut
reordered StatisticService
MODIFY
src/server/rest/v1/service/StatisticService.ts
src/server/rest/v1/service/StatisticService.ts
@@ -470,26 +470,33 @@ export default class StatisticService {
return transactions;
}
- static convertToCSV(transactionStats: any[], dataCategory: string, dataType: string, year: number | string, dataScope?: string): string {
+ static getPricingCell(transaction: any, numberOfTransaction: number): string[] {
+ if (transaction._id.unit) {
+ return [numberOfTransaction.toString(), transaction._id.unit];
+ }
+ return [numberOfTransaction.toString(), ' '];
+ }
+
// Build header row
- const getYearAndMonthCells = (year: number | string, dataScope?: string) : string => {
+ static getYearAndMonthCells(year: number | string, dataScope?: string) : string {
if (year && year !== '0') {
- const year = 'year';
+ const yearHeader = 'year';
if (dataScope && dataScope === 'month') {
- return [year, 'month'].join(Constants.CSV_SEPARATOR);
+ return [yearHeader, 'month'].join(Constants.CSV_SEPARATOR);
}
- return year;
+ return yearHeader;
}
}
- const getdataTypeCells = (dataType: string) : string => {
+ // Build dataType cells
+ static getdataTypeCells = (dataType: string) : string => {
switch (dataType) {
case 'Consumption':
return 'consumption';
case 'Usage':
return 'usage';
case 'Inactivity':
- return 'inactivity'
+ return 'inactivity';
case 'Transactions':
return 'numberOfSessions';
case 'Pricing':
@@ -497,12 +504,14 @@ export default class StatisticService {
default:
return '';
}
- }
+ };
+
+ static convertToCSV(transactionStats: any[], dataCategory: string, dataType: string, year: number | string, dataScope?: string): string {
const headers = [
dataCategory === 'C' ? 'chargingStation' : 'user',
- getYearAndMonthCells(year, dataScope),
- getdataTypeCells(dataType)
- ]
+ StatisticService.getYearAndMonthCells(year, dataScope),
+ StatisticService.getdataTypeCells(dataType)
+ ];
let index: number;
const transactions = [];
if (transactionStats && transactionStats.length > 0) {
@@ -619,9 +628,9 @@ export default class StatisticService {
});
}
// Now build the export file
- let number: number;
+ let numberOfTransaction: number;
const rows = transactions.map((transaction) => {
- number = Utils.truncTo(transaction.total, 2);
+ numberOfTransaction = Utils.truncTo(transaction.total, 2);
// Use raw numbers - it makes no sense to format numbers here,
// anyway only locale 'en-US' is supported here as could be seen by:
// const supportedLocales = Intl.NumberFormat.supportedLocalesOf(['fr-FR', 'en-US', 'de-DE']);
@@ -629,17 +638,10 @@ export default class StatisticService {
dataCategory === 'C' ? transaction._id.chargeBox : Utils.buildUserFullName(transaction.user, false),
year && year !== '0' ? year : '',
transaction._id.month > 0 ? transaction._id.month : '',
- dataType === 'Pricing' ? getPricingCell(transaction) : number.toString()
+ dataType === 'Pricing' ? StatisticService.getPricingCell(transaction, numberOfTransaction) : numberOfTransaction.toString()
].map((value) => typeof value === 'string' ? '"' + value.replace('"', '""') + '"' : value);
return row;
}).join(Constants.CR_LF);
- // Build pricing cell
- const getPricingCell = (transaction: any) => {
- if (transaction._id.unit) {
- return [number.toString(), transaction._id.unit];
- }
- return [number.toString(), ' '];
- };
return [headers, rows].join(Constants.CR_LF);
}
}

MODIFY
test/api/TestUtils.ts
test/api/TestUtils.ts
@@ -16,15 +16,15 @@ export default class TestUtils {
const objectArray = [];
const fileArray = TestUtils.convertExportFileToRawArray(fileData);
if (Array.isArray(fileArray) && fileArray.length > 0) {
- const columns = fileArray[0].split('\t');
+ const columns = fileArray[0].split(Constants.CSV_SEPARATOR);
for (let i = 1; i < fileArray.length; i++) {
- const values = fileArray[i].split('\t');
+ const values = fileArray[i].split(Constants.CSV_SEPARATOR);
jsonString = '{';
for (let j = 0; j < columns.length; j++) {
if (j > 0) {
jsonString += ',';
}
- jsonString += `"${columns[j]}":"${values[j]}"`;
+ jsonString += `"${columns[j].replace(/^"|"$/g, '')}":"${values[j].replace(/^"|"$/g, '')}"`;
}
jsonString += '}';
objectArray.push(JSON.parse(jsonString));

modified csv separator in ut
reordered StatisticService