| 1 |
import { get } from "../../api"; |
| 2 |
import { todayAsString } from "../../format"; |
| 3 |
import { _ } from "../../i18n"; |
| 4 |
import { Route } from "../route"; |
| 5 |
import ImportSvelte from "./Import.svelte"; |
| 6 |
|
| 7 |
/** |
| 8 |
* Construct the filename from date and basename. |
| 9 |
* |
| 10 |
* If it already starts with a date use it, otherwise prepend the date |
| 11 |
*/ |
| 12 |
function newFilename(date: string, basename: string): string { |
| 13 |
return /^\d{4}-\d{2}-\d{2}/.test(basename) ? basename : `${date} ${basename}`; |
| 14 |
} |
| 15 |
|
| 16 |
export interface ProcessedImportableFile { |
| 17 |
/** Full filename of this file. */ |
| 18 |
readonly name: string; |
| 19 |
/** Basename of this file. */ |
| 20 |
readonly basename: string; |
| 21 |
/** Whether at least one importer identified this file. */ |
| 22 |
readonly identified_by_importers: boolean; |
| 23 |
readonly importers: { |
| 24 |
readonly account: string; |
| 25 |
readonly importer_name: string; |
| 26 |
readonly newName: string; |
| 27 |
}[]; |
| 28 |
} |
| 29 |
|
| 30 |
export interface ImportReportProps { |
| 31 |
files: ProcessedImportableFile[]; |
| 32 |
} |
| 33 |
|
| 34 |
export const import_report = new Route<ImportReportProps>( |
| 35 |
"import", |
| 36 |
ImportSvelte, |
| 37 |
async () => |
| 38 |
get("imports", undefined) |
| 39 |
.then((files) => { |
| 40 |
// Initially set the file names for all importable files. |
| 41 |
const today = todayAsString(); |
| 42 |
return files.map((file) => { |
| 43 |
const importers = file.importers.map( |
| 44 |
({ account, importer_name, date, name }) => ({ |
| 45 |
account, |
| 46 |
importer_name, |
| 47 |
newName: newFilename(date, name), |
| 48 |
}), |
| 49 |
); |
| 50 |
const identified_by_importers = importers.length > 0; |
| 51 |
if (!identified_by_importers) { |
| 52 |
const newName = newFilename(today, file.basename); |
| 53 |
importers.push({ account: "", newName, importer_name: "" }); |
| 54 |
} |
| 55 |
return { ...file, identified_by_importers, importers }; |
| 56 |
}); |
| 57 |
}) |
| 58 |
.then((files) => ({ files })), |
| 59 |
() => _("Import"), |
| 60 |
); |