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