diff --git a/packages/nylas-api/routes/auth.js b/packages/nylas-api/routes/auth.js index 89ae8db60..35e771915 100644 --- a/packages/nylas-api/routes/auth.js +++ b/packages/nylas-api/routes/auth.js @@ -49,8 +49,9 @@ const buildAccountWith = ({name, email, provider, settings, credentials}) => { name: name, provider: provider, emailAddress: email, - syncPolicy: SyncPolicy.defaultPolicy(), connectionSettings: settings, + syncPolicy: SyncPolicy.defaultPolicy(), + lastSyncCompletions: [], }) account.setCredentials(credentials); diff --git a/packages/nylas-core/models/shared/account.js b/packages/nylas-core/models/shared/account.js index abd019d85..be0014d7d 100644 --- a/packages/nylas-core/models/shared/account.js +++ b/packages/nylas-core/models/shared/account.js @@ -1,5 +1,5 @@ const crypto = require('crypto'); -const {JSONType} = require('../../database-types'); +const {JSONType, JSONARRAYType} = require('../../database-types'); const {DB_ENCRYPTION_ALGORITHM, DB_ENCRYPTION_PASSWORD} = process.env; @@ -12,6 +12,7 @@ module.exports = (sequelize, Sequelize) => { connectionCredentials: Sequelize.STRING, syncPolicy: JSONType('syncPolicy'), syncError: JSONType('syncError', {defaultValue: null}), + lastSyncCompletions: JSONARRAYType('lastSyncCompletions'), }, { classMethods: { associate: ({AccountToken}) => { @@ -29,6 +30,7 @@ module.exports = (sequelize, Sequelize) => { connection_settings: this.connectionSettings, sync_policy: this.syncPolicy, sync_error: this.syncError, + lastSyncCompletions: this.lastSyncCompletions, } }, diff --git a/packages/nylas-sync/sync-worker.js b/packages/nylas-sync/sync-worker.js index 63c21a860..915c10e25 100644 --- a/packages/nylas-sync/sync-worker.js +++ b/packages/nylas-sync/sync-worker.js @@ -176,6 +176,15 @@ class SyncWorker { onSyncDidComplete() { const {afterSync} = this._account.syncPolicy; + let lastSyncCompletions = [...this._account.lastSyncCompletions] + lastSyncCompletions = [Date.now(), ...lastSyncCompletions] + if (lastSyncCompletions.length > 10) { + lastSyncCompletions.pop() + } + this._account.lastSyncCompletions = lastSyncCompletions + this._account.save() + console.log('Syncworker: Completed sync cycle') + if (afterSync === 'idle') { return this._getIdleFolder() .then((idleFolder) => this._conn.openBox(idleFolder.name))