From 60f69e0567d4104332cd0cf40b36f42ca04120c5 Mon Sep 17 00:00:00 2001 From: Vu Pham Date: Tue, 27 Nov 2018 11:14:44 +0700 Subject: [PATCH] update getScanningResult api --- apis/voters.js | 57 +++++++++++++------------- app/components/candidates/Withdraw.vue | 33 ++++++--------- app/components/voters/Unvoting.vue | 33 ++++++--------- app/components/voters/Voting.vue | 33 ++++++--------- 4 files changed, 66 insertions(+), 90 deletions(-) diff --git a/apis/voters.js b/apis/voters.js index 09b69e8b..691cc34e 100644 --- a/apis/voters.js +++ b/apis/voters.js @@ -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 diff --git a/app/components/candidates/Withdraw.vue b/app/components/candidates/Withdraw.vue index c8d0be43..6adc339a 100644 --- a/app/components/candidates/Withdraw.vue +++ b/app/components/candidates/Withdraw.vue @@ -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) } } } diff --git a/app/components/voters/Unvoting.vue b/app/components/voters/Unvoting.vue index c41cc578..8f389c31 100644 --- a/app/components/voters/Unvoting.vue +++ b/app/components/voters/Unvoting.vue @@ -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) } } } diff --git a/app/components/voters/Voting.vue b/app/components/voters/Voting.vue index eafc921d..6fea7d73 100644 --- a/app/components/voters/Voting.vue +++ b/app/components/voters/Voting.vue @@ -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) {