Skip to content

Commit

Permalink
Merge pull request #338 from pqv199x/fix-errors
Browse files Browse the repository at this point in the history
update getScanningResult api
  • Loading branch information
thanhson1085 authored Nov 27, 2018
2 parents 0197f13 + 60f69e0 commit d8bd9ba
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 90 deletions.
57 changes: 28 additions & 29 deletions apis/voters.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,37 +180,36 @@ router.post('/verifyTx', async (req, res, next) => {
}
})

router.post('/getScanningResult', async (req, res, next) => {
try {
const id = req.body.id
const voter = req.body.voter || ''
const action = req.body.action || ''

voter.toLowerCase()

const acc = await db.Voter.findOne({ voter: voter })

if (!acc) {
return res.status(404).send()
}
const signTx = await db.SignTransaction.findOne({ signedAddress: voter })
const checkTx = action === 'withdraw' ? true : await db.Transaction.findOne({ tx: signTx.tx })
if (id === signTx.signId && voter === signTx.signedAddress && checkTx) {
res.json({
tx: signTx.tx
})
} else {
res.send({
error: {
message: 'Not match'
router.get('/getScanningResult',
async (req, res, next) => {
try {
const id = req.query.id
const action = req.query.action || ''

const signTx = await db.SignTransaction.findOne({ signId: id })
const checkTx = (signTx.tx && action === 'withdraw')
? true : await db.Transaction.findOne({ tx: signTx.tx })
if (signTx && id === signTx.signId) {
if (checkTx) {
res.send({
tx: signTx.tx
})
} else {
res.send('Scanned')
}
})
} else {
res.send({
error: {
message: 'Not match'
}
})
}
} catch (e) {
console.trace(e)
console.log(e)
return res.status(500).send(e)
}
} catch (e) {
console.trace(e)
console.log(e)
return res.status(500).send(e)
}
})
)

module.exports = router
33 changes: 12 additions & 21 deletions app/components/candidates/Withdraw.vue
Original file line number Diff line number Diff line change
Expand Up @@ -211,32 +211,23 @@ export default {
},
async verifyScannedQR () {
let self = this
let body = {
action: 'withdraw'
}
if (self.id) {
body.id = self.id
}
body.voter = self.coinbase
let { data } = await axios.post('/api/voters/getScanningResult', body)
let { data } = await axios.get('/api/voters/getScanningResult?action=withdraw&id=' + self.id)
if (!data.error) {
self.loading = true
if (self.interval) {
if (data.tx) {
clearInterval(self.interval)
let toastMessage = data.tx ? 'You have successfully withdrawed!'
: 'An error occurred while voting, please try again'
self.$toasted.show(toastMessage)
setTimeout(() => {
if (data.tx) {
self.loading = false
self.processing = false
self.$router.push({ path: `/setting` })
}
}, 3000)
}
let toastMessage = data.tx ? 'You have successfully withdrawed!'
: 'An error occurred while withdrawing, please try again'
self.$toasted.show(toastMessage)
setTimeout(() => {
if (data.tx) {
self.loading = false
self.processing = false
self.$router.push({ path: `/setting` })
}
}, 2000)
}
}
}
Expand Down
33 changes: 13 additions & 20 deletions app/components/voters/Unvoting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -352,31 +352,24 @@ export default {
},
async verifyScannedQR () {
let self = this
let body = {}
if (self.id) {
body.id = self.id
}
body.voter = self.voter
let { data } = await axios.post('/api/voters/getScanningResult', body)
let { data } = await axios.get('/api/voters/getScanningResult?action=unvote&id=' + self.id)
if (!data.error) {
self.loading = true
if (self.interval) {
if (data.tx) {
clearInterval(self.interval)
let toastMessage = data.tx ? 'You have successfully unvoted!'
: 'An error occurred while voting, please try again'
self.$toasted.show(toastMessage)
setTimeout(() => {
if (data.tx) {
self.loading = false
self.processing = false
self.step = 0
self.$router.push({ path: `/confirm/${data.tx}` })
}
}, 2000)
}
let toastMessage = data.tx ? 'You have successfully voted!'
: 'An error occurred while voting, please try again'
self.$toasted.show(toastMessage)
setTimeout(() => {
if (data.tx) {
self.loading = false
self.processing = false
self.step = 0
self.$router.push({ path: `/confirm/${data.tx}` })
}
}, 2000)
}
}
}
Expand Down
33 changes: 13 additions & 20 deletions app/components/voters/Voting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -340,31 +340,24 @@ export default {
},
async verifyScannedQR () {
let self = this
let body = {}
if (self.id) {
body.id = self.id
}
body.voter = self.voter
let { data } = await axios.post('/api/voters/getScanningResult', body)
let { data } = await axios.get('/api/voters/getScanningResult?action=vote&id=' + self.id)
if (!data.error) {
self.loading = true
if (self.interval) {
if (data.tx) {
clearInterval(self.interval)
let toastMessage = data.tx ? 'You have successfully voted!'
: 'An error occurred while voting, please try again'
self.$toasted.show(toastMessage)
setTimeout(() => {
if (data.tx) {
self.loading = false
self.processing = false
self.step = 0
self.$router.push({ path: `/confirm/${data.tx}` })
}
}, 2000)
}
let toastMessage = data.tx ? 'You have successfully voted!'
: 'An error occurred while voting, please try again'
self.$toasted.show(toastMessage)
setTimeout(() => {
if (data.tx) {
self.loading = false
self.processing = false
self.step = 0
self.$router.push({ path: `/confirm/${data.tx}` })
}
}, 2000)
}
},
onChangeVoting (event) {
Expand Down

0 comments on commit d8bd9ba

Please sign in to comment.