@@ -42,13 +42,11 @@ const { argv } = require('yargs')
4242 // ensure that this user exists
4343 assert . equal ( await redis . exists ( metaKey ) , 1 , 'user does not exist' ) ;
4444
45+ const currentReferral = await redis . hget ( metaKey , USERS_REFERRAL_FIELD ) ;
46+
4547 // ensure that referral was not already set
4648 if ( argv . overwrite !== true ) {
47- assert . equal (
48- await redis . hexists ( metaKey , USERS_REFERRAL_FIELD ) ,
49- 0 ,
50- 'referral was already set, pass --overwrite to args to force it'
51- ) ;
49+ assert . equal ( currentReferral , null , 'referral was already set, pass --overwrite to args to force it' ) ;
5250 }
5351
5452 // verify that referral code is valid, which is basically
@@ -60,10 +58,14 @@ const { argv } = require('yargs')
6058 ) ;
6159
6260 const commands = [
63- [ 'hset' , audience , `"${ argv . code } "` ] ,
61+ [ 'hset' , metaKey , audience , `"${ argv . code } "` ] ,
6462 [ 'sadd' , `${ USERS_REFERRAL_INDEX } :${ argv . code } ` , argv . id ] ,
6563 ] ;
6664
65+ if ( currentReferral ) {
66+ commands . push ( [ 'srem' , `${ USERS_REFERRAL_INDEX } :${ JSON . parse ( currentReferral ) } ` , argv . id ] ) ;
67+ }
68+
6769 await redis . pipeline ( commands ) . exec ( ) . then ( handleRedisPipelineError ) ;
6870 } catch ( e ) {
6971 console . error ( 'Failed' , e ) ; // eslint-disable-line no-console
0 commit comments