diff --git a/app/browser/api/ledger.js b/app/browser/api/ledger.js index fb125f9cd1d..9a51f04fb69 100644 --- a/app/browser/api/ledger.js +++ b/app/browser/api/ledger.js @@ -1919,7 +1919,7 @@ const onReferralInit = (err, response, body) => { } if (body && body.download_id) { - appActions.onReferralCodeRead(body.download_id) + appActions.onReferralCodeRead(body.download_id, body.referral_code) promoCodeFirstRunStorage .removePromoCode() .catch(error => { @@ -2768,6 +2768,17 @@ const checkReferralActivity = (state) => { return state } +const referralCheck = (state) => { + const installTime = state.get('firstRunTimestamp') + const period = parseInt(process.env.LEDGER_REFERRAL_DELETE_TIME || (ledgerUtil.milliseconds.day * 90)) + + if (new Date().getTime() >= installTime + period) { + state = updateState.deleteUpdateProp(state, 'referralPromoCode') + } + + return state +} + const activityRoundTrip = (err, response, body) => { if (err) { if (clientOptions.verboseP) { @@ -2835,6 +2846,7 @@ const getMethods = () => { onPublisherTimestamp, checkVerifiedStatus, checkReferralActivity, + referralCheck, roundtrip } diff --git a/app/browser/reducers/ledgerReducer.js b/app/browser/reducers/ledgerReducer.js index fc040f05a04..31bc8045074 100644 --- a/app/browser/reducers/ledgerReducer.js +++ b/app/browser/reducers/ledgerReducer.js @@ -38,6 +38,7 @@ const ledgerReducer = (state, action, immutableAction) => { { state = ledgerApi.migration(state) state = ledgerApi.init(state) + state = ledgerApi.referralCheck(state) break } case appConstants.APP_BACKUP_KEYS: @@ -471,6 +472,7 @@ const ledgerReducer = (state, action, immutableAction) => { case appConstants.APP_ON_REFERRAL_CODE_READ: { state = updateState.setUpdateProp(state, 'referralDownloadId', action.get('downloadId')) + state = updateState.setUpdateProp(state, 'referralPromoCode', action.get('promoCode')) break } case appConstants.APP_ON_REFERRAL_CODE_FAIL: diff --git a/app/updater.js b/app/updater.js index b49343fd162..faa0cf170ca 100644 --- a/app/updater.js +++ b/app/updater.js @@ -159,7 +159,7 @@ var requestVersionInfo = (done, pingOnly) => { debug(`weekOfInstallation= ${weekOfInstallation}`) // The installation promoCode from buildConfig - const promoCode = state.getIn(['updates', 'promoCode'], 'none') + const promoCode = updateState.getUpdateProp(state, 'referralPromoCode') || 'none' debug(`promoCode = ${promoCode}`) // Build query string based on the last date an update request was made @@ -231,7 +231,7 @@ exports.checkForUpdate = (verbose, skipReferral = false) => { updateState.getUpdateProp(state, 'referralDownloadId') ) { const installTime = state.get('firstRunTimestamp') - const month = parseInt(process.env.LEDGER_REFERRAL_CHECK_TIME || ledgerUtil.milliseconds.day * 30) + const month = parseInt(process.env.LEDGER_REFERRAL_CHECK_TIME || (ledgerUtil.milliseconds.day * 30)) if (installTime + month < new Date().getTime()) { appActions.checkReferralActivity() diff --git a/docs/state.md b/docs/state.md index 202a775fccf..f02a7ce1799 100644 --- a/docs/state.md +++ b/docs/state.md @@ -602,6 +602,7 @@ AppStore }, referralDownloadId: string, // download ID that is returned from the referral server referralTimestamp: number, // timestamp when referral was accumulated (after ~30 days) + referralPromoCode: string, // promo code for the referral status: string, // updateStatus from js/constants/updateStatus.js verbose: boolean // whether to show update UI for checking, downloading, and errors }, diff --git a/js/actions/appActions.js b/js/actions/appActions.js index 3766bf25b14..2935b0ae5d3 100644 --- a/js/actions/appActions.js +++ b/js/actions/appActions.js @@ -1900,10 +1900,11 @@ const appActions = { }) }, - onReferralCodeRead: function (downloadId) { + onReferralCodeRead: function (downloadId, promoCode) { dispatch({ actionType: appConstants.APP_ON_REFERRAL_CODE_READ, - downloadId + downloadId, + promoCode }) }, diff --git a/test/unit/app/browser/api/ledgerTest.js b/test/unit/app/browser/api/ledgerTest.js index 2692ff6a696..8e85dc0f0ba 100644 --- a/test/unit/app/browser/api/ledgerTest.js +++ b/test/unit/app/browser/api/ledgerTest.js @@ -18,6 +18,7 @@ describe('ledger api unit tests', function () { let ledgerApi let ledgerNotificationsApi let ledgerState + let updateState let isBusy = false let ledgerClient let ledgerPublisher @@ -204,6 +205,7 @@ describe('ledger api unit tests', function () { ledgerNotificationsApi = require('../../../../../app/browser/api/ledgerNotifications') ledgerState = require('../../../../../app/common/state/ledgerState') + updateState = require('../../../../../app/common/state/updateState') updater = require('../../../../../app/updater') // once everything is stubbed, load the ledger @@ -2355,4 +2357,45 @@ describe('ledger api unit tests', function () { assert(roundtripSpy.calledOnce) }) }) + + describe('referralCheck', function () { + let deleteUpdatePropSpy, fakeClock + + before(function () { + deleteUpdatePropSpy = sinon.spy(updateState, 'deleteUpdateProp') + fakeClock = sinon.useFakeTimers() + fakeClock.tick(172800000) + }) + + afterEach(function () { + deleteUpdatePropSpy.reset() + fakeClock.reset() + }) + + after(function () { + deleteUpdatePropSpy.restore() + fakeClock.restore() + }) + + it('first run is only few days', function () { + const state = defaultAppState + .set('firstRunTimestamp', 1000) + .setIn(['updates', 'referralPromoCode'], '1234') + const returnedState = ledgerApi.referralCheck(state) + assert.deepEqual(returnedState.toJS(), state.toJS()) + assert(deleteUpdatePropSpy.notCalled) + }) + + it('first run is over 90 days so we can delete promo code', function () { + const state = defaultAppState + .set('firstRunTimestamp', 1000) + .setIn(['updates', 'referralPromoCode'], '1234') + fakeClock.tick(7776000000) // 90 days + const expectedState = state + .deleteIn(['updates', 'referralPromoCode']) + const returnedState = ledgerApi.referralCheck(state) + assert.deepEqual(returnedState.toJS(), expectedState.toJS()) + assert(deleteUpdatePropSpy.calledOnce) + }) + }) }) diff --git a/test/unit/app/browser/reducers/ledgerReducerTest.js b/test/unit/app/browser/reducers/ledgerReducerTest.js index a7162590f4c..0f34ff8554a 100644 --- a/test/unit/app/browser/reducers/ledgerReducerTest.js +++ b/test/unit/app/browser/reducers/ledgerReducerTest.js @@ -57,7 +57,8 @@ describe('ledgerReducer unit tests', function () { claimPromotion: () => {}, onPromotionResponse: dummyModifyState, getPromotion: () => {}, - checkReferralActivity: dummyModifyState + checkReferralActivity: dummyModifyState, + referralCheck: () => {} } fakeLedgerState = { resetSynopsis: dummyModifyState,