Skip to content

Commit

Permalink
add bulk dni
Browse files Browse the repository at this point in the history
  • Loading branch information
xtian7489 committed Jul 2, 2024
1 parent d193f43 commit d66eed2
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 74 deletions.
4 changes: 2 additions & 2 deletions lib/admin/admin-padron/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ export default class AdminPadron extends Component {
<BuscarDNI forum={forum} facultades={facultades} />
<hr />
<AgregarDNI forum={forum} facultades={facultades} />
{/* <hr /> */}
{/* <BulkDNI forum={forum} facultades={facultades} /> */}
<hr />
<BulkDNI forum={forum} facultades={facultades} />
</div>
)
}
Expand Down
141 changes: 70 additions & 71 deletions lib/api-v2/padron/csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
}
}
)
5 changes: 5 additions & 0 deletions lib/db-api/padron.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion lib/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d66eed2

Please sign in to comment.