138 lines
4.0 KiB
TypeScript
138 lines
4.0 KiB
TypeScript
import newRechnungenIdMap from "./new-rechnungen-id-map.json" with { type: "json" };
|
|
import newUserIdMap from "./new-user-id-map.json" with { type: "json" };
|
|
import { Enums, prisma } from "#lib/server/prisma.js";
|
|
import Papa from "papaparse"
|
|
import * as fs from "fs";
|
|
import { fileURLToPath } from "url";
|
|
import { generatePrefixedId } from "#lib/db.js";
|
|
import { VALID_UUID_PREFIXES } from "#lib/constants.js";
|
|
import moment from "moment";
|
|
|
|
const saveRechnungenMap = () => {
|
|
fs.writeFileSync(
|
|
fileURLToPath(new URL("./new-rechnungen-id-map.json", import.meta.url)),
|
|
JSON.stringify(newRechnungenIdMap)
|
|
);
|
|
};
|
|
|
|
const path = fileURLToPath(new URL("./rechnungen.csv", import.meta.url));
|
|
|
|
if (!fs.existsSync(path)) {
|
|
throw new Error(`${path} existiert nicht.`)
|
|
}
|
|
|
|
const file = fs.createReadStream(path, "utf8");
|
|
Papa.parse(file, {
|
|
header: true,
|
|
async complete(results, file) {
|
|
let i = 0;
|
|
for (const rechnung of results.data as any) {
|
|
if (rechnung.id in newRechnungenIdMap) {
|
|
continue;
|
|
}
|
|
|
|
if (!(rechnung.user_id in newUserIdMap)) {
|
|
console.log(`User ${rechnung.user_id} not found.`);
|
|
continue;
|
|
}
|
|
|
|
try {
|
|
const id = generatePrefixedId(6, VALID_UUID_PREFIXES.Rechnung)
|
|
|
|
const services: Enums.Service[] = []
|
|
|
|
if (rechnung.service_1) {
|
|
services.push(Enums.Service.Qualitaetsdruck)
|
|
}
|
|
|
|
if (rechnung.service_2) {
|
|
services.push(Enums.Service.Aushang)
|
|
}
|
|
|
|
if (rechnung.service_3) {
|
|
services.push(Enums.Service.SameDay)
|
|
}
|
|
|
|
if (rechnung.service_4) {
|
|
services.push(Enums.Service.Telefonberatung)
|
|
}
|
|
|
|
const paymentTypeMap = {
|
|
"UEBERWIESEN": Enums.Bezahlmethoden.rechnung,
|
|
"ueberweisung": Enums.Bezahlmethoden.rechnung,
|
|
"RECHNUNG": Enums.Bezahlmethoden.rechnung,
|
|
"-FAIL": Enums.Bezahlmethoden.rechnung,
|
|
"ELV-FAIL": Enums.Bezahlmethoden.sofort,
|
|
"ELV-OK": Enums.Bezahlmethoden.sofort,
|
|
"giropay": Enums.Bezahlmethoden.giropay,
|
|
"KK-FAIL": Enums.Bezahlmethoden.creditcard,
|
|
"KK-OK": Enums.Bezahlmethoden.creditcard,
|
|
"kreditkarte": Enums.Bezahlmethoden.creditcard,
|
|
"PAYP-FAIL": Enums.Bezahlmethoden.paypal,
|
|
"PAYP-OK": Enums.Bezahlmethoden.paypal,
|
|
"paypal": Enums.Bezahlmethoden.paypal,
|
|
"rechnung": Enums.Bezahlmethoden.rechnung,
|
|
"SEPA": Enums.Bezahlmethoden.rechnung,
|
|
"sepa": Enums.Bezahlmethoden.rechnung,
|
|
"sofort": Enums.Bezahlmethoden.sofort,
|
|
"SUE-FAIL": Enums.Bezahlmethoden.sofort,
|
|
"SUE-OK": Enums.Bezahlmethoden.sofort,
|
|
"": Enums.Bezahlmethoden.rechnung
|
|
}
|
|
await prisma.rechnung.create({
|
|
data: {
|
|
id,
|
|
betrag: parseFloat(rechnung.amount),
|
|
bezahlmethode: paymentTypeMap[rechnung.payment_type],
|
|
// bedarfsausweis_gewerbe,
|
|
// bedarfsausweis_wohnen,
|
|
// benutzer,
|
|
// benutzer_id,
|
|
status: rechnung.status === "storniert" ? Enums.Rechnungsstatus.canceled : rechnung.status,
|
|
bezahlt_am: moment(rechnung.date_created).toDate(),
|
|
email: rechnung.email,
|
|
empfaenger: `${rechnung.vorname} ${rechnung.name}`,
|
|
erstellt_am: moment(rechnung.date_created).toDate(),
|
|
// geg_nachweis_gewerbe,
|
|
// geg_nachweis_wohnen,
|
|
ort: rechnung.ort,
|
|
partner_code: rechnung.resellercode,
|
|
plz: rechnung.ort,
|
|
services,
|
|
strasse: rechnung.strasse,
|
|
transaktions_referenz: rechnung.transaction_id,
|
|
// verbrauchsausweis_gewerbe,
|
|
// verbrauchsausweis_wohnen,
|
|
versand_empfaenger: `${rechnung.vorname} ${rechnung.name}`,
|
|
versand_ort: rechnung.ort,
|
|
versand_plz: rechnung.plz,
|
|
versand_strasse: rechnung.strasse,
|
|
lex_office_id: rechnung.lex_office_id,
|
|
benutzer_id: newUserIdMap[rechnung.user_id]
|
|
}
|
|
});
|
|
|
|
newRechnungenIdMap[rechnung.id] = id;
|
|
} catch (e) {
|
|
saveRechnungenMap();
|
|
continue;
|
|
}
|
|
|
|
// Alle 50 werden gespeichert.
|
|
if (i % 50 == 0) {
|
|
console.log(`Saved ${i} - ${results.data.length - i} left`);
|
|
saveRechnungenMap();
|
|
}
|
|
|
|
i++;
|
|
}
|
|
}
|
|
});
|
|
|
|
process.on("SIGINT", () => {
|
|
console.log("Shutting down.");
|
|
|
|
saveRechnungenMap()
|
|
|
|
process.exit(0)
|
|
}) |