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

Reset old reconcileStamp when re-enabling Payments #4314

Merged
merged 1 commit into from
Sep 27, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 35 additions & 9 deletions app/ledger.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,16 @@ const doAction = (action) => {
break

case appConstants.APP_CHANGE_SETTING:
if (action.key === settings.PAYMENTS_ENABLED) return initialize(action.value)
if (action.key === settings.PAYMENTS_CONTRIBUTION_AMOUNT) return setPaymentInfo(action.value)
switch (action.key) {
case settings.PAYMENTS_ENABLED:
initialize(action.value, 'changeSettingPaymentsEnabled')
break
case settings.PAYMENTS_CONTRIBUTION_AMOUNT:
setPaymentInfo(action.value)
break
default:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you for adding the default case!

break
}
break

case appConstants.APP_CHANGE_SITE_SETTING:
Expand Down Expand Up @@ -407,16 +415,16 @@ eventStore.addChangeListener(() => {
* module initialization
*/

var initialize = (onoff) => {
enable(onoff)
var initialize = (paymentsEnabled, reason) => {
enable(paymentsEnabled)

// Check if relevant browser notifications should be shown every 15 minutes
if (notificationTimeout) {
clearInterval(notificationTimeout)
}
notificationTimeout = setInterval(showNotifications, 15 * msecs.minute)

if (!onoff) {
if (!paymentsEnabled) {
client = null
return appActions.updateLedgerInfo({})
}
Expand All @@ -441,9 +449,27 @@ var initialize = (onoff) => {
}

getStateInfo(state)

try {
client = ledgerClient(state.personaId,
underscore.extend(state.options, { roundtrip: roundtrip }, clientOptions), state)
underscore.extend(state.options, { roundtrip: roundtrip }, clientOptions),
state)

// Scenario: User enables Payments, disables it, waits 30+ days, then
// enables it again -> reconcileStamp is in the past.
// In this case reset reconcileStamp to the future.
if (reason === 'changeSettingPaymentsEnabled') {
let timeUntilReconcile = client.timeUntilReconcile()
if (typeof timeUntilReconcile === 'number' && timeUntilReconcile < 0) {
client.setTimeUntilReconcile(null, (_, stateResult) => {
if (!stateResult) {
return
}
ledgerInfo.reconcileStamp = stateResult.reconcileStamp
syncWriter(pathName(statePath), stateResult, () => {})
})
}
}
} catch (ex) {
return console.log('ledger client creation error: ' + ex.toString() + '\n' + ex.stack)
}
Expand All @@ -462,12 +488,12 @@ var initialize = (onoff) => {
})
}

var enable = (onoff) => {
if (onoff && !getSetting(settings.PAYMENTS_NOTIFICATION_TRY_PAYMENTS_DISMISSED)) {
var enable = (paymentsEnabled) => {
if (paymentsEnabled && !getSetting(settings.PAYMENTS_NOTIFICATION_TRY_PAYMENTS_DISMISSED)) {
appActions.changeSetting(settings.PAYMENTS_NOTIFICATION_TRY_PAYMENTS_DISMISSED, true)
}

if (!onoff) {
if (!paymentsEnabled) {
synopsis = null
return updatePublisherInfo()
}
Expand Down