158 lines
5.0 KiB
JavaScript
158 lines
5.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 ActivoFijo = require('../../models/tablas/ActivoFijo');
|
|
const OS = require('../../models/tablas/OS');
|
|
const Marca = require('../../models/tablas/Marca');
|
|
const Inventario = require('../../models/tablas/Inventario');
|
|
const Equipo = require('../../models/tablas/Equipo');
|
|
|
|
|
|
const post = async(req, res) => {
|
|
const file = req.files.equipos;
|
|
|
|
let equipos = [];
|
|
|
|
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 activoFijo = await ActivoFijo.findOrCreate({
|
|
where: {
|
|
activoFijo: row[1] || 'No declarado'
|
|
}
|
|
})
|
|
.catch((res) => console.log(res));
|
|
}
|
|
|
|
if (row[3]) {
|
|
var marca = await Marca.findOrCreate({
|
|
where: {
|
|
marca: row[3] || 'No declarado'
|
|
}
|
|
})
|
|
.catch((e) => { throw new Error(e); });
|
|
}
|
|
|
|
if (row[12]) {
|
|
var os = await OS.findOrCreate({
|
|
where: {
|
|
osname: row[12] || 'No declarado'
|
|
}
|
|
})
|
|
.catch((e) => { throw new Error(e); });
|
|
}
|
|
|
|
if (row[14]) {
|
|
var uResponsable = await UResponsable.findOrCreate({
|
|
where: {
|
|
uResponsable: row[14] || 'No declarado'
|
|
}
|
|
})
|
|
.catch((e) => { throw new Error(e); });
|
|
}
|
|
|
|
if (row[16]) {
|
|
var ubicacion = await Ubicacion.findOrCreate({
|
|
where: {
|
|
ubicacion: row[16] || 'No declarado'
|
|
}
|
|
})
|
|
.catch((e) => { throw new Error(e); });
|
|
}
|
|
|
|
if (row[17]) {
|
|
var uso = await Uso.findOrCreate({
|
|
where: {
|
|
uso: row[17] || 'No declarado'
|
|
}
|
|
})
|
|
.catch((e) => { throw new Error(e); });
|
|
}
|
|
|
|
let equipo = {
|
|
noInventario: row[0],
|
|
idActivoFijo: activoFijo[0]['idActivoFijo'],
|
|
modelo: row[2],
|
|
marca: marca[0]['idMarca'],
|
|
noSerie: row[4],
|
|
procesador: row[5],
|
|
vProcesador: row[6],
|
|
hdd: row[7],
|
|
memoriasz: row[8],
|
|
memoria: row[9],
|
|
vMemoria: row[10],
|
|
graficos: row[11],
|
|
osname: os[0]['idOS'],
|
|
noResguardo: row[13],
|
|
uResponsable: uResponsable[0]['idUnidadResponsable'],
|
|
ubicacion: ubicacion[0]['idUbicacion'],
|
|
uso: uso[0]['idUso']
|
|
}
|
|
equipos.push(equipo);
|
|
}
|
|
|
|
|
|
var count = 0;
|
|
for (equipo of equipos) {
|
|
count++;
|
|
await Inventario.create({
|
|
noInventario: equipo.noInventario,
|
|
noResguardo: equipo.noResguardo,
|
|
idActivoFijo: equipo.idActivoFijo,
|
|
idUnidadResponsable: equipo.uResponsable,
|
|
idUbicacion: equipo.ubicacion,
|
|
idUso: equipo.uso
|
|
})
|
|
.then(async() => {
|
|
await Equipo.create({
|
|
noInventario: equipo.noInventario,
|
|
idActivoFijo: equipo.activoFijo,
|
|
idMarca: equipo.marca,
|
|
idOS: equipo.osname,
|
|
modelo: equipo.modelo,
|
|
noSerie: equipo.noSerie,
|
|
procesador: equipo.procesador,
|
|
vProcesador: equipo.vProcesador,
|
|
memoria: equipo.memoria,
|
|
vMemoria: equipo.vMemoria,
|
|
memoriasz: equipo.memoriasz,
|
|
graficos: equipo.graficos,
|
|
hdd: equipo.hdd
|
|
})
|
|
.catch(async(err) => {
|
|
await Inventario.destroy({
|
|
where: {
|
|
noInventario: equipo.noInventario
|
|
}
|
|
});
|
|
res.status(500).json({
|
|
ok: false,
|
|
err
|
|
});
|
|
throw new Error(e);
|
|
})
|
|
})
|
|
.catch((e) => {
|
|
res.status(500).json({
|
|
ok: false,
|
|
e
|
|
});
|
|
throw new Error(e);
|
|
})
|
|
}
|
|
|
|
res.json({
|
|
ok: true,
|
|
message: `${ count } records created successfully.`
|
|
// equipos
|
|
// activoF
|
|
})
|
|
}
|
|
|
|
module.exports = post |