2015-03-25 15:22:44 -04:00
// Bulk create prepaid codes + email message
// Usage:
// mongo <address>:<port>/<database> <script file> -u <username> -p <password>
var num = 10 ;
2015-04-14 17:59:51 -04:00
var message = "Thanks for filling out the form. You can follow this link to enable your free subscription for teachers. If you have any questions or comments, please let me know." ;
2015-03-25 15:22:44 -04:00
var urlPrefix = "https://codecombat.com/account/subscription?_ppc=" ;
var creatorID = "52f94443fcb334581466a992" ;
for ( var i = 0 ; i < num ; i ++ ) {
createPrepaid ( ) ;
}
function createPrepaid ( )
{
generateNewCode ( function ( code ) {
if ( ! code ) {
print ( "ERROR: no code" ) ;
return ;
}
criteria = {
creator : creatorID ,
type : 'subscription' ,
2015-08-12 18:51:16 -04:00
maxRedeemers : 1
2015-03-25 15:22:44 -04:00
code : code ,
properties : {
couponID : 'free'
} ,
_ _v : 0
} ;
db . prepaids . insert ( criteria ) ;
print ( message + " " + urlPrefix + code ) ;
} ) ;
}
function generateNewCode ( done )
{
function tryCode ( ) {
code = createCode ( 8 ) ;
criteria = { code : code } ;
if ( db . prepaids . findOne ( criteria ) ) {
return tryCode ( ) ;
}
return done ( code ) ;
}
tryCode ( ) ;
}
function createCode ( length )
{
var text = "" ;
2015-10-07 17:06:09 -04:00
var possible = "abcdefghijklmnopqrstuvwxyz0123456789" ;
2015-03-25 15:22:44 -04:00
for ( var i = 0 ; i < length ; i ++ )
text += possible . charAt ( Math . floor ( Math . random ( ) * possible . length ) ) ;
return text ;
}