128 lines
4.0 KiB
JavaScript
128 lines
4.0 KiB
JavaScript
const xlsx = require('node-xlsx');
|
|
|
|
const UResponsable = require('../../models/tablas/UResponsable');
|
|
const Ubicacion = require('../../models/tablas/Ubicacion');
|
|
const Uso = require('../../models/tablas/Uso');
|
|
const Marca = require('../../models/tablas/Marca');
|
|
const Tipo = require('../../models/tablas/Tipo');
|
|
const Inventario = require('../../models/tablas/Inventario');
|
|
const Printers = require('../../models/tablas/Printers');
|
|
|
|
const post = async(req, res) => {
|
|
const file = req.files.printers;
|
|
|
|
let printers = [];
|
|
|
|
let data = await xlsx.parse(file.tempFilePath);
|
|
data = Object.entries(data[0]);
|
|
data.shift();
|
|
data = data[0][1]
|
|
data.shift();
|
|
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: tipo[0]['idTipo'],
|
|
modelo: row[2],
|
|
idMarca: marca[0]['idMarca'],
|
|
noSerie: row[4],
|
|
descripcion: row[5],
|
|
idActivoFijo: 6,
|
|
noResguardo: row[6],
|
|
idUnidadResponsable: uResponsable.dataValues.idUnidadResponsable,
|
|
idUbicacion: ubicacion[0]['idUbicacion'],
|
|
idUso: uso[0]['idUso']
|
|
}
|
|
printers.push(printer);
|
|
};
|
|
let count = 0;
|
|
for (printer of printers) {
|
|
count++;
|
|
await Inventario.create({
|
|
noInventario: printer.noInventario,
|
|
noResguardo: printer.noResguardo,
|
|
idActivoFijo: printer.idActivoFijo,
|
|
idUnidadResponsable: printer.idUnidadResponsable,
|
|
idUbicacion: printer.idUbicacion,
|
|
idUso: printer.idUso
|
|
})
|
|
.then(async() => {
|
|
await Printers.create({
|
|
noInventario: printer.noInventario,
|
|
idMarca: printer.idMarca,
|
|
modelo: printer.modelo,
|
|
noSerie: printer.noSerie,
|
|
idTipo: printer.idTipo,
|
|
descripcion: printer.descripcion,
|
|
idActivoFijo: printer.idActivoFijo
|
|
})
|
|
.catch(async(err) => {
|
|
await Inventario.destroy({
|
|
where: {
|
|
noInventario: printer.noInventario
|
|
}
|
|
});
|
|
return res.status(500).json({
|
|
ok: false,
|
|
err
|
|
})
|
|
})
|
|
})
|
|
.catch((e) => res.status(500).json({
|
|
ok: false,
|
|
e
|
|
}))
|
|
|
|
}
|
|
|
|
res.json({
|
|
ok: true,
|
|
message: `${ count } records created successfully.`
|
|
})
|
|
}
|
|
|
|
module.exports = post |