From d66eed252473c5b3a7d30a5a7034b805a710024e Mon Sep 17 00:00:00 2001 From: xtian7489 Date: Tue, 2 Jul 2024 16:10:41 -0300 Subject: [PATCH] add bulk dni --- lib/admin/admin-padron/component.js | 4 +- lib/api-v2/padron/csv.js | 141 ++++++++++++++-------------- lib/db-api/padron.js | 5 + lib/setup/index.js | 2 +- 4 files changed, 78 insertions(+), 74 deletions(-) diff --git a/lib/admin/admin-padron/component.js b/lib/admin/admin-padron/component.js index 0ab8539..6ed989b 100644 --- a/lib/admin/admin-padron/component.js +++ b/lib/admin/admin-padron/component.js @@ -56,8 +56,8 @@ export default class AdminPadron extends Component {
- {/*
*/} - {/* */} +
+ ) } diff --git a/lib/api-v2/padron/csv.js b/lib/api-v2/padron/csv.js index 00487e8..fe73791 100644 --- a/lib/api-v2/padron/csv.js +++ b/lib/api-v2/padron/csv.js @@ -84,74 +84,73 @@ app.get('/padron/all/csv', } ) -// app.post('/padron/bulk/csv', -// restrict, -// staff, -// function readCSV (req, res, next) { -// log('Reading CSV') -// console.log(req.body) -// var csv = req.body.csv -// if(!csv){ -// return res.status(500).end() -// } -// csv2json(csv, function(err, json){ -// if (err){ -// log('csv to array error', err) -// return res.status(500).end() -// } -// req.listDocuments = json.map(d => Object.values(d)[0].trim()) -// console.log(req.listDocuments) -// next() -// }) -// }, -// function deleteDuplicates (req, res, next) { -// log('Deleting duplicates from listDocuments') -// // delete from req.listDocuments array any duplicate -// req.listDocuments = req.listDocuments.filter((v, i, a) => a.indexOf(v) === i) -// next() -// }, -// async function checkDNI(req, res, next){ -// // Checking every document is not in the padron -// log('Checking every document is not in the padron') -// let inPadron = [] -// for(var i = 0; i < req.listDocuments.length; i++){ -// var dni = req.listDocuments[i] -// var padron = await dbApi.padron.isDNIPadron(dni) -// if (padron) { -// log('DNI in padron', dni) -// inPadron.push(dni) -// } -// // if(padron){ -// // return a 400 with a message -// // return res.status(400).json({ -// // message: `El documento ${dni} ya está en el padrón` -// // }) -// // } -// } -// if(inPadron.length > 0){ -// return res.status(400).json({ -// message: `Los documentos ${inPadron.join(', ')} ya están en el padrón`, -// status: 400 -// }) -// } -// next() -// }, -// async function insertDocuments (req, res, next) { -// let newDocuments = req.listDocuments.map(d => { -// return { -// dni: d -// } -// }) -// log('Inserting documents') -// let insertedDocuments = await dbApi.padron.insertMany(newDocuments) -// if (insertedDocuments){ -// log('Documents inserted') -// // return a 200 -// return res.status(200).json({ -// message: `${ insertedDocuments.length } Documentos insertados correctamente`, -// status: 200, -// insertedDocuments -// }) -// } -// } -// ) +app.post('/padron/bulk/csv', + restrict, + staff, + function readCSV(req, res, next) { + log('Reading CSV') + var csv = req.body.csv + if (!csv) { + return res.status(500).end() + } + csv2json(csv, function (err, json) { + if (err) { + log('csv to array error', err) + return res.status(500).end() + } + req.listDocuments = json.map(d => Object.values(d)[0].trim()) + console.log(req.listDocuments) + next() + }) + }, + function deleteDuplicates(req, res, next) { + log('Deleting duplicates from listDocuments') + // delete from req.listDocuments array any duplicate + req.listDocuments = req.listDocuments.filter((v, i, a) => a.indexOf(v) === i) + next() + }, + async function checkDNI(req, res, next) { + // Checking every document is not in the padron + log('Checking every document is not in the padron') + let inPadron = [] + for (var i = 0; i < req.listDocuments.length; i++) { + var dni = req.listDocuments[i] + var padron = await dbApi.padron.isDNIPadron(dni) + if (padron) { + log('DNI in padron', dni) + inPadron.push(dni) + } + // if(padron){ + // return a 400 with a message + // return res.status(400).json({ + // message: `El documento ${dni} ya está en el padrón` + // }) + // } + } + if (inPadron.length > 0) { + return res.status(400).json({ + message: `Los documentos ${inPadron.join(', ')} ya están en el padrón`, + status: 400 + }) + } + next() + }, + async function insertDocuments(req, res, next) { + let newDocuments = req.listDocuments.map(d => { + return { + dni: d + } + }) + log('Inserting documents') + let insertedDocuments = await dbApi.padron.insertMany(newDocuments) + if (insertedDocuments) { + log('Documents inserted') + // return a 200 + return res.status(200).json({ + message: `${insertedDocuments.length} Documentos insertados correctamente`, + status: 200, + insertedDocuments + }) + } + } +) diff --git a/lib/db-api/padron.js b/lib/db-api/padron.js index 11f758b..807a818 100644 --- a/lib/db-api/padron.js +++ b/lib/db-api/padron.js @@ -87,3 +87,8 @@ exports.getAll = () => { log('Getting all the padron') return Padron.find({}).populate(['user']) } + +exports.insertMany = (dataArray) => { + log('Getting agenda sorted by datetime') + return Padron.insertMany(dataArray) +} \ No newline at end of file diff --git a/lib/setup/index.js b/lib/setup/index.js index 6f8a24d..4015242 100644 --- a/lib/setup/index.js +++ b/lib/setup/index.js @@ -97,7 +97,7 @@ function Setup (app) { app.use(express.static(path.resolve('public'))) app.use(bodyParser.urlencoded({ extended: true })) - app.use(bodyParser.json()) + app.use(bodyParser.json({ limit: '5mb' })) /** * Cross Origin Resource Sharing