Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
Merge pull request #13083 from NejcZdovc/hotfix/#13082-ref
Browse files Browse the repository at this point in the history
Adds promo code to the state
  • Loading branch information
NejcZdovc committed Feb 9, 2018
1 parent c036acb commit efe6957
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 6 deletions.
14 changes: 13 additions & 1 deletion app/browser/api/ledger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -2762,6 +2762,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) {
Expand Down Expand Up @@ -2829,6 +2840,7 @@ const getMethods = () => {
onPublisherTimestamp,
checkVerifiedStatus,
checkReferralActivity,
referralCheck,
roundtrip
}

Expand Down
2 changes: 2 additions & 0 deletions app/browser/reducers/ledgerReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions app/updater.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions docs/state.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down
5 changes: 3 additions & 2 deletions js/actions/appActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1900,10 +1900,11 @@ const appActions = {
})
},

onReferralCodeRead: function (downloadId) {
onReferralCodeRead: function (downloadId, promoCode) {
dispatch({
actionType: appConstants.APP_ON_REFERRAL_CODE_READ,
downloadId
downloadId,
promoCode
})
},

Expand Down
43 changes: 43 additions & 0 deletions test/unit/app/browser/api/ledgerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
})
})
})
3 changes: 2 additions & 1 deletion test/unit/app/browser/reducers/ledgerReducerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ describe('ledgerReducer unit tests', function () {
claimPromotion: () => {},
onPromotionResponse: dummyModifyState,
getPromotion: () => {},
checkReferralActivity: dummyModifyState
checkReferralActivity: dummyModifyState,
referralCheck: () => {}
}
fakeLedgerState = {
resetSynopsis: dummyModifyState,
Expand Down

0 comments on commit efe6957

Please sign in to comment.