check contribution

This commit is contained in:
Danil Kovtonyuk 2020-02-11 19:53:41 +10:00
parent a979220830
commit 36cf7d6c6e
2 changed files with 47 additions and 2 deletions

View File

@ -66,8 +66,7 @@ export default {
if (!this.token) { if (!this.token) {
window.location.replace(window.location.origin) window.location.replace(window.location.origin)
} else { } else {
// TODO try to load contribution data. May be it's already authorized await this.check()
// also set `contributionIndex`
} }
setTimeout(() => { setTimeout(() => {
this.$root.$emit('disableLoading') this.$root.$emit('disableLoading')
@ -103,6 +102,33 @@ export default {
this.status.msg = 'Something went wrong. Please contact support' this.status.msg = 'Something went wrong. Please contact support'
this.status.type = 'is-danger' this.status.type = 'is-danger'
} }
},
async check() {
const body = {
token: this.token
}
try {
const response = await fetch('/api/check_contribution', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
})
if (response.ok) {
const { id } = await response.json()
this.contributionIndex = id
} else {
const error = await response.text()
this.status.msg = error
this.status.type = 'is-danger'
this.hideSaveBtn = true
}
} catch (e) {
this.status.msg = 'Something went wrong. Please contact support'
this.status.type = 'is-danger'
}
} }
} }
} }

View File

@ -146,4 +146,23 @@ router.post('/authorize_contribution', async (req, res) => {
res.send('OK') res.send('OK')
}) })
router.post('/check_contribution', async (req, res) => {
if (!req.body || !req.body.token) {
res.status(404).send('Wrong request params')
}
const contribution = await Contribution.findOne({ where: { token: req.body.token } })
if (!contribution) {
res.status(404).send('There is no such contribution')
return
}
if (contribution.dataValues.socialType !== 'anonymous') {
res.status(404).send('The contribution is already authorized')
return
}
return res.json({ id: contribution.dataValues.id }).send()
})
module.exports = router module.exports = router