Skip to content

Instantly share code, notes, and snippets.

@agustin107
Created May 24, 2024 14:57
Show Gist options
  • Save agustin107/b0e33e7540d4ecac62d1434101a3c8f6 to your computer and use it in GitHub Desktop.
Save agustin107/b0e33e7540d4ecac62d1434101a3c8f6 to your computer and use it in GitHub Desktop.
const { get, set, isUndefined, noop, identity } = require('lodash');
const { InvalidInputFileError } = require('../errors');
const { processCsv, parseCsvValue, filterEmpty, entityWithImages } = require('../utils/CsvUtils');
const { entityHeaders } = require('../utils/CsvEntitiesHeaders');
function parseCsv(entity, parseRow = identity) {
return (req, res, next) => {
const { fields = {}, files } = req;
const [file] = Object.values(files);
if (!file) {
throw new InvalidInputFileError();
}
res.locals = {
candidates: [],
mapHeadersSchema: new Map(),
headersSchema: [],
};
const entityMap = entityHeaders.get(entity || fields.entity);
const onHeaders = noop;
const onData = onDataFn(res, fields, entityMap, parseRow);
const onEnd = onEndFn(res, next);
processCsv(file.path, onHeaders, onData, onEnd);
};
}
function onDataFn(res, fields, entityMap, parseRow) {
return (row) => {
createCandidate(res, row, fields, entityMap, parseRow);
};
}
function onEndFn(res, next) {
return () => {
const headersSchema = [];
for (const [key, label] of res.locals.mapHeadersSchema) {
headersSchema.push({ key, label });
}
res.locals.headersSchema = headersSchema;
next();
};
}
function createCandidate(res, row, fields, entityMap, parseRow) {
const { headers = '' } = fields;
const entity = {};
let parsedEntity = {};
for (const [key, value] of entityMap) {
let rawValue = get(row, value);
if (!isUndefined(rawValue)) {
rawValue = rawValue.trim();
set(entity, key, rawValue);
set(parsedEntity, key, parseCsvValue(rawValue));
res.locals.mapHeadersSchema.set(key, value);
}
}
for (const header of headers.split(',')) {
if (!Object.keys(entity).includes(header)) {
let rawValue = get(row, header);
if (!isUndefined(rawValue)) {
rawValue = rawValue.trim();
set(entity, header, rawValue);
set(parsedEntity, header, parseCsvValue(rawValue));
res.locals.mapHeadersSchema.set(header, header);
}
}
}
parsedEntity = parseRow(entityWithImages(filterEmpty(parsedEntity)));
res.locals.candidates.push({ entity, parsedEntity });
}
module.exports = {
parseCsv,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment