This commit is contained in:
Cat /dev/Nulo 2024-09-07 12:28:06 -03:00
parent 1644d8b207
commit c24c931d46
2 changed files with 15 additions and 5 deletions

View file

@ -78,13 +78,23 @@ await sql`
CREATE INDEX IF NOT EXISTS idx_sucursales_composite ON sucursales (id_dataset, id_comercio, id_bandera, id_sucursal); CREATE INDEX IF NOT EXISTS idx_sucursales_composite ON sucursales (id_dataset, id_comercio, id_bandera, id_sucursal);
`; `;
async function readFile(path: string) {
// XXX: DORINKA SRL a veces envía archivos con UTF-16.
const buffer = await fs.readFile(path, { encoding: null });
if (buffer[0] === 0xff && buffer[1] === 0xfe) {
return buffer.toString("utf16le");
} else {
return buffer.toString("utf8");
}
}
async function importSucursales( async function importSucursales(
sql: postgres.Sql, sql: postgres.Sql,
datasetId: number, datasetId: number,
dir: string dir: string
) { ) {
const sucursales: Papa.ParseResult<any> = Papa.parse( const sucursales: Papa.ParseResult<any> = Papa.parse(
await fs.readFile(join(dir, "sucursales.csv"), "utf-8"), await readFile(join(dir, "sucursales.csv")),
{ {
header: true, header: true,
} }
@ -131,7 +141,7 @@ async function importDataset(dir: string) {
datasetId = res[0].id; datasetId = res[0].id;
const comercios: Papa.ParseResult<{ comercio_cuit: string }> = Papa.parse( const comercios: Papa.ParseResult<{ comercio_cuit: string }> = Papa.parse(
await fs.readFile(join(dir, "comercio.csv"), "utf-8"), await readFile(join(dir, "comercio.csv")),
{ header: true } { header: true }
); );
const comercioCuit = comercios.data[0].comercio_cuit; const comercioCuit = comercios.data[0].comercio_cuit;
@ -139,7 +149,7 @@ async function importDataset(dir: string) {
await importSucursales(sql, datasetId, dir); await importSucursales(sql, datasetId, dir);
let file = await fs.readFile(join(dir, "productos.csv"), "utf-8"); let file = await readFile(join(dir, "productos.csv"));
// WALL OF SHAME: estos proveedores no saben producir CSVs correctos // WALL OF SHAME: estos proveedores no saben producir CSVs correctos
if (comercioCuit == "30612929455") { if (comercioCuit == "30612929455") {
// Libertad S.A. // Libertad S.A.

View file

@ -10,9 +10,9 @@
"typescript": "^5.0.0" "typescript": "^5.0.0"
}, },
"dependencies": { "dependencies": {
"ckan": "workspace:*",
"p-queue": "^8.0.1", "p-queue": "^8.0.1",
"papaparse": "^5.4.1", "papaparse": "^5.4.1",
"postgres": "^3.4.4", "postgres": "^3.4.4"
"ckan": "workspace:*"
} }
} }