Add "unverified pledges" table

Pledges will now be saved in the "unverified pledges" table until
they are verified by email.
This commit is contained in:
Hippo 2022-01-05 19:21:10 +05:30
parent 87afc8688e
commit 1bf1e3a55e

View file

@ -28,10 +28,8 @@ const knex = require('knex')({
useNullAsDefault: true useNullAsDefault: true
}) })
knex.schema.hasTable('pledges').then(function(exists) { // schema to save pledges
if (!exists) { let pledgeSchema = function(t) {
if (DEBUG) console.debug('No table exists! Creating one now...')
return knex.schema.createTable('pledges', function(t) {
t.increments('id').primary() t.increments('id').primary()
t.timestamp('created_at').defaultTo(knex.fn.now()).notNullable() t.timestamp('created_at').defaultTo(knex.fn.now()).notNullable()
t.boolean('was_robot').defaultTo(true) t.boolean('was_robot').defaultTo(true)
@ -45,7 +43,21 @@ knex.schema.hasTable('pledges').then(function(exists) {
t.integer('retry_times').defaultTo(10) t.integer('retry_times').defaultTo(10)
t.boolean('get_newsletter').defaultTo(false) t.boolean('get_newsletter').defaultTo(false)
t.text('other_message') t.text('other_message')
}
// make sure pledges table exists
knex.schema.hasTable('pledges').then(function(exists) {
if (!exists) {
if (DEBUG) console.debug('No pledge table exists! Creating one now...')
return knex.schema.createTable('pledges', pledgeSchema)
}
}) })
// make sure unverified pledge table exists
knex.schema.hasTable('unverified_pledges').then(function(exists) {
if (!exists) {
if (DEBUG) console.debug('No unverified pledge table exists! Creating one now...')
return knex.schema.createTable('unverified_pledges', pledgeSchema)
} }
}) })
@ -56,6 +68,10 @@ const Pledge = bookshelf.model('Pledge', {
tableName: 'pledges', tableName: 'pledges',
}) })
const UnverifiedPledge = bookshelf.model('UnverifiedPledge', {
tableName: 'unverified_pledges',
})
// decide base url and port for app (can be configured) // decide base url and port for app (can be configured)
const baseUrl = process.env.CROWDFUNDING_SITE_BASE_URL || '/' const baseUrl = process.env.CROWDFUNDING_SITE_BASE_URL || '/'
const port = process.env.CROWDFUNDING_SITE_PORT || 5000 const port = process.env.CROWDFUNDING_SITE_PORT || 5000
@ -103,7 +119,7 @@ router.get('/', async (req, res) => {
}) })
// function to validate pledges before saving // function to validate pledges before saving
function validatePledge(body) { function validatePledge(body, PledgeModel = Pledge) {
// errors get saved here // errors get saved here
let errors = [] let errors = []
@ -156,7 +172,7 @@ function validatePledge(body) {
let messages = body.messages let messages = body.messages
// enter the info // enter the info
let pledge = new Pledge() let pledge = new PledgeModel() // may be Pledge or UnverifiedPledge
pledge.set('was_robot', robo) pledge.set('was_robot', robo)
pledge.set('amount', amount) pledge.set('amount', amount)
pledge.set('overseas', overseas) pledge.set('overseas', overseas)
@ -179,7 +195,7 @@ router.post('/pledge', async (req, res) => {
if (DEBUG) console.debug('New pledge:', req.body) if (DEBUG) console.debug('New pledge:', req.body)
// process the pledge with our handy function // process the pledge with our handy function
let {pledge, errors} = validatePledge(req.body) let {pledge, errors} = validatePledge(req.body, UnverifiedPledge)
// fail if there were errors // fail if there were errors
if (!!errors.length) { if (!!errors.length) {