.
This commit is contained in:
@@ -12,99 +12,73 @@ const post = async(req, res) => {
|
||||
const file = req.files.printers;
|
||||
|
||||
let printers = [];
|
||||
let ubicaciones = [];
|
||||
let unidades = [];
|
||||
let usos = [];
|
||||
let tipos = [];
|
||||
let marcas = [];
|
||||
|
||||
await Ubicacion.findAll({
|
||||
attributes: ['ubicacion'],
|
||||
order: ['idUbicacion']
|
||||
})
|
||||
.then(resultado => {
|
||||
resultado.forEach(el => ubicaciones.push(Object.entries(el.dataValues)));
|
||||
resultado = ubicaciones.flat(2);
|
||||
ubicaciones = resultado.filter(ubicacion => ubicacion != 'ubicacion')
|
||||
})
|
||||
.catch(err => res.status(500).json({
|
||||
ok: false,
|
||||
error: err
|
||||
}));
|
||||
|
||||
await UResponsable.findAll({
|
||||
attributes: ['uResponsable'],
|
||||
order: ['idUnidadResponsable']
|
||||
})
|
||||
.then((result) => {
|
||||
result.forEach(el => unidades.push(Object.entries(el.dataValues)));
|
||||
unidades = unidades.flat(2);
|
||||
unidades = unidades.filter(filtro => filtro != 'uResponsable')
|
||||
}).catch((err) => res.status(500).json({
|
||||
ok: false,
|
||||
error: err
|
||||
}));
|
||||
|
||||
await Uso.findAll({
|
||||
attributes: ['uso'],
|
||||
order: ['idUso']
|
||||
})
|
||||
.then((result) => {
|
||||
result.forEach(el => usos.push(Object.entries(el.dataValues)));
|
||||
usos = usos.flat(2);
|
||||
usos = usos.filter(filtro => filtro != 'uso')
|
||||
}).catch((err) => res.status(500).json({
|
||||
ok: false,
|
||||
error: err
|
||||
}));
|
||||
|
||||
await Marca.findAll({
|
||||
attributes: ['marca'],
|
||||
order: ['idMarca']
|
||||
})
|
||||
.then((result) => {
|
||||
result.forEach(el => marcas.push(Object.entries(el.dataValues)));
|
||||
marcas = marcas.flat(2);
|
||||
marcas = marcas.filter(filtro => filtro != 'marca')
|
||||
}).catch((err) => res.status(500).json({
|
||||
ok: false,
|
||||
error: err
|
||||
}));
|
||||
|
||||
await Tipo.findAll({
|
||||
attributes: ['tipo'],
|
||||
order: ['idTipo']
|
||||
})
|
||||
.then((result) => {
|
||||
result.forEach(el => tipos.push(Object.entries(el.dataValues)));
|
||||
tipos = tipos.flat(2);
|
||||
tipos = tipos.filter(filtro => filtro != 'tipo')
|
||||
}).catch((err) => res.status(500).json({
|
||||
ok: false,
|
||||
error: err
|
||||
}));
|
||||
|
||||
let data = await xlsx.parse(file.tempFilePath);
|
||||
data = Object.entries(data[0]);
|
||||
data.shift();
|
||||
data = data[0][1]
|
||||
data.shift();
|
||||
data.forEach(row => {
|
||||
for (row of data) {
|
||||
if (row[1]) {
|
||||
var tipo = await Tipo.findOrCreate({
|
||||
where: {
|
||||
tipo: row[1] || 'No declarado'
|
||||
}
|
||||
})
|
||||
.catch((e) => { throw new Error(e); });
|
||||
}
|
||||
|
||||
if (row[3]) {
|
||||
var marca = await Marca.findOrCreate({
|
||||
where: {
|
||||
marca: row[3] || 'No declarado'
|
||||
}
|
||||
})
|
||||
.catch((e) => { throw new Error(e); });
|
||||
}
|
||||
|
||||
if (row[7]) {
|
||||
var uResponsable = await UResponsable.findOne({
|
||||
where: {
|
||||
uResponsable: row[7] || 'No declarado'
|
||||
}
|
||||
})
|
||||
.catch((e) => { throw new Error(e); });
|
||||
}
|
||||
|
||||
if (row[9]) {
|
||||
var ubicacion = await Ubicacion.findOrCreate({
|
||||
where: {
|
||||
ubicacion: row[9] || 'No declarado'
|
||||
}
|
||||
})
|
||||
.catch((e) => { throw new Error(e); });
|
||||
}
|
||||
|
||||
if (row[10]) {
|
||||
var uso = await Uso.findOrCreate({
|
||||
where: {
|
||||
uso: row[10] || 'No declarado'
|
||||
}
|
||||
})
|
||||
.catch((e) => { throw new Error(e); });
|
||||
}
|
||||
|
||||
let printer = {
|
||||
noInventario: row[0],
|
||||
idTipo: tipos.indexOf(row[1]) + 1,
|
||||
idTipo: tipo[0]['idTipo'],
|
||||
modelo: row[2],
|
||||
idMarca: marcas.indexOf(row[3]) + 1,
|
||||
idMarca: marca[0]['idMarca'],
|
||||
noSerie: row[4],
|
||||
descripcion: row[5],
|
||||
idActivoFijo: 6,
|
||||
noResguardo: row[6],
|
||||
idUnidadResponsable: unidades.indexOf(row[7]) + 1,
|
||||
idUbicacion: ubicaciones.indexOf(row[9]) + 1,
|
||||
idUso: usos.indexOf(row[10]) + 1
|
||||
idUnidadResponsable: uResponsable.dataValues.idUnidadResponsable,
|
||||
idUbicacion: ubicacion[0]['idUbicacion'],
|
||||
idUso: uso[0]['idUso']
|
||||
}
|
||||
printers.push(printer);
|
||||
});
|
||||
};
|
||||
let count = 0;
|
||||
for (printer of printers) {
|
||||
count++;
|
||||
|
||||
Reference in New Issue
Block a user