2021-12-29 06:52:17 -05:00
const express = require ( 'express' )
2021-12-29 12:05:25 -05:00
const bodyParser = require ( 'body-parser' )
2021-12-29 06:52:17 -05:00
const path = require ( 'path' )
2021-12-29 06:35:51 -05:00
require ( 'dotenv' ) . config ( )
2021-12-29 09:15:21 -05:00
// set up debug
2021-12-29 06:35:51 -05:00
let DEBUG
if ( process . env . DEBUG || process . env . CROWDFUNDING _SITE _DEBUG ) {
DEBUG = true
} else {
DEBUG = false
}
if ( DEBUG ) console . log ( 'Starting website in debug mode' )
2021-12-29 09:15:21 -05:00
// get goal details
const goalPeople = Number ( process . env . CROWDFUNDING _SITE _GOAL _PEOPLE ) || 750
const goalRupees = Number ( process . env . CROWDFUNDING _SITE _GOAL _RUPEES ) || 500000
2021-12-29 06:35:51 -05:00
// set up knex (for database)
const knex = require ( 'knex' ) ( {
client : 'sqlite3' ,
connection : {
filename : './donors.sqlite'
} ,
useNullAsDefault : true
} )
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 )
t . integer ( 'amount' ) . notNullable ( )
2021-12-29 12:06:40 -05:00
t . boolean ( 'overseas' )
2021-12-29 06:35:51 -05:00
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' )
} )
}
} )
// set up bookshelf (for easy interface for database)
bookshelf = require ( 'bookshelf' ) ( knex )
const Pledge = bookshelf . model ( 'Pledge' , {
tableName : 'pledges' ,
} )
2021-12-29 06:52:17 -05:00
// 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
2021-12-29 09:15:21 -05:00
// set up twing
const { TwingEnvironment , TwingLoaderFilesystem } = require ( 'twing' )
let loader = new TwingLoaderFilesystem ( path . resolve ( _ _dirname , '..' , 'dist' ) )
let twing = new TwingEnvironment ( loader )
2021-12-29 06:52:17 -05:00
// set up express
const app = express ( )
const router = express . Router ( )
app . use ( baseUrl , router )
2021-12-29 12:05:25 -05:00
router . use ( bodyParser . urlencoded ( {
extended : true ,
} ) )
2021-12-29 06:52:17 -05:00
// main views
2021-12-29 09:15:21 -05:00
router . get ( '/' , async ( req , res ) => {
2021-12-29 06:52:17 -05:00
if ( DEBUG ) console . debug ( 'Returning home page' )
2021-12-29 09:15:21 -05:00
// count people
// TODO: optimise to do using SQL only
let total _people = 0
let total _rupees = 0
let result = await Pledge . fetchAll ( )
for ( let pledge of result . models ) {
total _people += 1
total _rupees += pledge . get ( 'amount' )
}
twing . render ( 'index.html' , {
'goal_rupees' : Number ( goalRupees ) . toLocaleString ( 'en-IN' ) ,
'goal_people' : Number ( goalPeople ) . toLocaleString ( 'en-IN' ) ,
'progress_rupees' : Number ( total _rupees ) . toLocaleString ( 'en-IN' ) ,
'progress_people' : Number ( total _people ) . toLocaleString ( 'en-IN' ) ,
'percent_rupees' : ` style="width: ${ total _rupees / goalRupees * 100 } %" ` ,
'percent_people' : ` style="width: ${ total _people / goalPeople * 100 } %" ` ,
} ) . then ( ( output ) => {
res . end ( output )
} )
2021-12-29 06:52:17 -05:00
} )
2021-12-29 12:05:25 -05:00
router . post ( '/pledge' , async ( req , res ) => {
if ( DEBUG ) console . debug ( 'New pledge:' , req . body )
// validate pledge
let errors = [ ]
let submit = req . body . submit
if ( submit != 'Save Pledge' ) {
errors . push ( "This request seems to have been tampered with. Are you sure it wasn't you doing the tampering?" )
}
let robo = req . body . robo
if ( robo != 'no' ) {
errors . push ( 'Only humans are allowed to donate money. Robots are too digital 🙁' )
}
let amount = req . body . amount
if ( ! amount || amount == 'custom' ) {
amount = req . body [ 'amount-custom' ]
}
if ( ! amount || amount <= 0 ) {
errors . push ( 'Pledge amount too small. Please choose at least a rupee!' )
}
try {
amount = Number ( amount )
} catch ( err ) {
errors . push ( 'Invalid amount. Please choose a positive number!' )
}
let name = req . body . name
if ( name . length <= 0 ) {
errors . push ( 'What is your name? You can be anonymous to the world but at least we should know...' )
}
let anonymous = req . body . anonymous == 'on' ? true : false
let email = req . body . email
if ( email . length < 5 ) {
errors . push ( 'Please enter a valid email address' )
}
let phone = req . body . phone
let newsletter = req . body . newsletter == 'yes' ? true : false
2021-12-29 12:06:40 -05:00
let overseas = req . body . overseas == 'yes' ? true : false
2021-12-29 12:05:25 -05:00
let messages = req . body . messages
if ( ! ! errors . length ) {
2021-12-29 12:50:30 -05:00
res . send ( ` Errors: ${ '' + errors } ` )
2021-12-29 12:05:25 -05:00
return
}
// save the info
let pledge = new Pledge ( )
pledge . set ( 'was_robot' , robo )
pledge . set ( 'amount' , amount )
2021-12-29 12:06:40 -05:00
pledge . set ( 'overseas' , overseas )
2021-12-29 12:05:25 -05:00
pledge . set ( 'name' , name )
pledge . set ( 'anonymous' , anonymous )
pledge . set ( 'email' , email )
pledge . set ( 'phone' , phone )
pledge . set ( 'get_newsletter' , newsletter )
pledge . set ( 'other_message' , messages )
if ( DEBUG ) console . debug ( ` Saving pledge: ${ JSON . stringify ( pledge ) } ` )
try {
await pledge . save ( )
} catch ( err ) {
2021-12-29 12:50:30 -05:00
res . send ( "Sorry, something went wrong while saving your pledge and we don't know what 🙁. Please try again..." )
2021-12-29 12:05:25 -05:00
return
}
2021-12-29 12:50:30 -05:00
res . send ( "Thank you! We're still working on setting up this website, so as you can see this page doesn't look great at the moment, but we will be sending you a confirmation email in a few days. Watch out for an email from editors@snipettemag.com, and if it doesn't reach, check your spam box :P" )
2021-12-29 12:05:25 -05:00
} )
router . use ( express . static ( 'dist' ) )
2021-12-29 06:52:17 -05:00
// start the listener!
app . listen ( port , ( ) => {
console . log ( ` Server is up at port ${ port } ` )
} )
// end note: in case we want to import this somewhere for testing
2021-12-29 06:35:51 -05:00
module . exports = {
knex ,
bookshelf ,
Pledge ,
2021-12-29 06:52:17 -05:00
router ,
2021-12-29 06:35:51 -05:00
}