diff --git a/packages/cozy-harvest-lib/src/models/ConnectionFlow.js b/packages/cozy-harvest-lib/src/models/ConnectionFlow.js index 964ef14d0c..1c75ae8a05 100644 --- a/packages/cozy-harvest-lib/src/models/ConnectionFlow.js +++ b/packages/cozy-harvest-lib/src/models/ConnectionFlow.js @@ -175,6 +175,8 @@ export class ConnectionFlow { this.twoFAWaiters = this.twoFAWaiters || [] this.realtime = new Realtime({ client }) + + this.watchCurrentJobIfTriggerIsAlreadyRunning() } getTwoFACodeProvider() { @@ -577,16 +579,29 @@ export class ConnectionFlow { logger.info('Found no client connector launcher') } } + this.watchJob({ autoSuccessTimer: computedAutoSuccessTimer }) + } + /** + * If the trigger we display is already running, subscribe to the associated job + */ + watchCurrentJobIfTriggerIsAlreadyRunning() { + if (get(this, 'trigger.current_state.status') === 'running') { + this.job = { + _id: get(this, 'trigger.current_state.last_executed_job_id') + } + this.watchJob() + } + } + + watchJob(options = {autoSuccessTimer: false}) { this.realtime.subscribe( 'updated', JOBS_DOCTYPE, this.job._id, this.handleJobUpdated.bind(this) ) - this.jobWatcher = watchKonnectorJob(this.client, this.job, { - autoSuccessTimer: computedAutoSuccessTimer - }) + this.jobWatcher = watchKonnectorJob(this.client, this.job, options) logger.info(`ConnectionFlow: Subscribed to ${JOBS_DOCTYPE}:${this.job._id}`) for (const ev of JOB_EVENTS) { @@ -622,7 +637,9 @@ export class ConnectionFlow { const { status, accountError } = this.state const triggerError = triggersModel.getKonnectorJobError(trigger) return { - running: ![ERRORED, IDLE, SUCCESS].includes(status), + running: + get(trigger, 'current_state.status') === 'running' || + ![ERRORED, IDLE, SUCCESS].includes(status), twoFARunning: status === RUNNING_TWOFA, twoFARetry: status == TWO_FA_MISMATCH, triggerError: triggerError, diff --git a/packages/cozy-harvest-lib/src/models/ConnectionFlow.spec.js b/packages/cozy-harvest-lib/src/models/ConnectionFlow.spec.js index 2cfcdc9970..d22fe5552e 100644 --- a/packages/cozy-harvest-lib/src/models/ConnectionFlow.spec.js +++ b/packages/cozy-harvest-lib/src/models/ConnectionFlow.spec.js @@ -8,12 +8,13 @@ import { launchTrigger } from '../connections/triggers' import CozyRealtime from 'cozy-realtime' -import KonnectorJobWatcher from './konnector/KonnectorJobWatcher' +import KonnectorJobWatcher, { watchKonnectorJob } from './konnector/KonnectorJobWatcher' import { konnectorPolicy as biKonnectorPolicy } from '../services/budget-insight' import fixtures from '../../test/fixtures' import sentryHub from '../sentry' import { Q } from 'cozy-client' +jest.mock('./konnector/KonnectorJobWatcher') jest.mock('../sentry', () => { const mockScope = { setTag: jest.fn() @@ -116,6 +117,17 @@ describe('ConnectionFlow', () => { const isSubmitting = flow => { return flow.getState().running === true } + beforeAll(() => { + watchKonnectorJob.mockReturnValue({on: () => ({})}) + }) + + afterEach(() => { + jest.clearAllMocks() + }) + + afterAll(() => { + jest.restoreAllMocks() + }) it('should render as submitting when there is no account', async () => { const { flow } = setup() @@ -213,6 +225,10 @@ describe('ConnectionFlow', () => { userCredentials: { login: 'foo', password: 'bar' + }, + account: { + login: 'old', + password: 'old' } }) @@ -314,6 +330,7 @@ describe('ConnectionFlow', () => { describe('ensureTriggerAndLaunch', () => { beforeAll(() => { jest.spyOn(cronHelpers, 'fromFrequency').mockReturnValue('0 0 0 * * 0') + watchKonnectorJob.mockReturnValue({on: () => ({})}) }) afterEach(() => { @@ -481,6 +498,24 @@ describe('ConnectionFlow', () => { delete window.cozy delete window.ReactNativeWebView }) + + describe('contructor', () => { + beforeAll(() => { + watchKonnectorJob.mockReturnValue({on: () => ({})}) + }) + + afterEach(() => { + jest.clearAllMocks() + }) + + afterAll(() => { + jest.restoreAllMocks() + }) + it('should watch a running trigger', () => { + setup({trigger: fixtures.runningTrigger}) + expect(watchKonnectorJob).toHaveBeenCalledWith(expect.any(Object), {_id: 'runningjobid'}, {autoSuccessTimer: false}) + }) + }) }) // it should have running false on trigger updates diff --git a/packages/cozy-harvest-lib/test/fixtures.js b/packages/cozy-harvest-lib/test/fixtures.js index 1c2b37b4c0..6120a4c73b 100644 --- a/packages/cozy-harvest-lib/test/fixtures.js +++ b/packages/cozy-harvest-lib/test/fixtures.js @@ -125,6 +125,23 @@ const fixtures = { } } }, + runningTrigger: { + id: 'running-trigger-id', + _type: 'io.cozy.triggers', + current_state: { + status: 'running', + last_executed_job_id: 'runningjobid' + }, + attributes: { + arguments: '0 0 0 * * 0', + type: '@cron', + worker: 'konnector', + message: { + account: 'updated-account-id', + konnector: 'konnectest' + } + } + }, createdTriggerWithFolder: { id: 'created-trigger-id', _type: 'io.cozy.triggers',