2023-07-27 23:23:46 +03:00
|
|
|
import type { ExportableShortUrl } from '../../short-urls/data';
|
|
|
|
import type { NormalizedVisit } from '../../visits/types';
|
2023-07-29 11:43:15 +03:00
|
|
|
import { saveCsv } from '../helpers/files';
|
|
|
|
import type { JsonToCsv } from '../helpers/json';
|
2023-07-27 23:23:46 +03:00
|
|
|
|
|
|
|
export class ReportExporter {
|
|
|
|
public constructor(private readonly window: Window, private readonly jsonToCsv: JsonToCsv) {
|
|
|
|
}
|
|
|
|
|
|
|
|
public readonly exportVisits = (filename: string, visits: NormalizedVisit[]) => {
|
|
|
|
if (!visits.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.exportCsv(filename, visits);
|
|
|
|
};
|
|
|
|
|
|
|
|
public readonly exportShortUrls = (shortUrls: ExportableShortUrl[]) => {
|
|
|
|
if (!shortUrls.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.exportCsv('short_urls.csv', shortUrls);
|
|
|
|
};
|
|
|
|
|
|
|
|
private readonly exportCsv = (filename: string, rows: object[]) => {
|
|
|
|
const csv = this.jsonToCsv(rows);
|
|
|
|
saveCsv(this.window, csv, filename);
|
|
|
|
};
|
|
|
|
}
|