Skip to content

Commit

Permalink
feat: Do not create trigger with full webhooks flag
Browse files Browse the repository at this point in the history
  • Loading branch information
doubleface authored and doubleface committed Oct 24, 2022
1 parent fb046e4 commit 8ce5e78
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 18 deletions.
2 changes: 1 addition & 1 deletion packages/cozy-harvest-lib/src/components/FlowProvider.jsx
Expand Up @@ -141,7 +141,7 @@ export class FlowProvider extends Component {
// created, this is why a trigger like object is created here
const triggerLike = {
message: {
account: flow.account._id
account: flow?.account?._id
},
...flow.trigger
}
Expand Down
Expand Up @@ -114,7 +114,9 @@ const DumbKonnectorAccountTabs = props => {

const handleClick = useCallback(() => {
setShowBIWebView(true)
flow.expectTriggerLaunch()
if (flag('harvest.bi.fullwebhooks')) {
flow.expectTriggerLaunch()
}
}, [flow])
const errorActionButton = konnectorPolicy.isBIWebView ? (
<Button
Expand Down
3 changes: 2 additions & 1 deletion packages/cozy-harvest-lib/src/components/OAuthForm.jsx
Expand Up @@ -13,6 +13,7 @@ import TriggerErrorInfo from './infos/TriggerErrorInfo'
import { ERROR_EVENT, LOGIN_SUCCESS_EVENT } from '../models/flowEvents'
import { KonnectorJobError } from '../helpers/konnectors'
import { findKonnectorPolicy } from '../konnector-policies'
import flag from 'cozy-flags'

/**
* The OAuth Form is responsible for displaying a form for OAuth konnectors. It
Expand Down Expand Up @@ -63,7 +64,7 @@ export const OAuthForm = props => {
const handleConnect = useCallback(() => {
setShowOAuthWindow(true)
const konnectorPolicy = findKonnectorPolicy(konnector)
if (konnectorPolicy.isBIWebView) {
if (konnectorPolicy.isBIWebView && flag('harvest.bi.fullwebhooks')) {
flow.expectTriggerLaunch({ konnector })
}
}, [client, flow, konnector.slug])
Expand Down
6 changes: 3 additions & 3 deletions packages/cozy-harvest-lib/src/components/TriggerManager.jsx
Expand Up @@ -95,9 +95,9 @@ export class DumbTriggerManager extends Component {

const konnectorPolicy = findKonnectorPolicy(konnector)

let foundOAuthConnection = false
let needsTriggerCreation = true
if (konnectorPolicy.handleOAuthAccount) {
foundOAuthConnection = await konnectorPolicy.handleOAuthAccount({
needsTriggerCreation = await konnectorPolicy.handleOAuthAccount({
account: oAuthAccount,
flow,
konnector,
Expand All @@ -108,7 +108,7 @@ export class DumbTriggerManager extends Component {
}

// for "normal" OAuth connectors
if (!foundOAuthConnection) {
if (needsTriggerCreation) {
flow.ensureTriggerAndLaunch(client, {
account: oAuthAccount,
konnector: konnector,
Expand Down
14 changes: 8 additions & 6 deletions packages/cozy-harvest-lib/src/models/ConnectionFlow.js
Expand Up @@ -441,12 +441,14 @@ export class ConnectionFlow {

logger.info(`ConnectionFlow: Saved account ${account._id}`)

await this.ensureTriggerAndLaunch(client, {
trigger,
account,
konnector,
t
})
if (!konnectorPolicy.needsTriggerCreation) {
await this.ensureTriggerAndLaunch(client, {
trigger,
account,
konnector,
t
})
}
this.setState({ accountError: null })
} catch (e) {
logger.error(e)
Expand Down
11 changes: 8 additions & 3 deletions packages/cozy-harvest-lib/src/services/biWebView.js
Expand Up @@ -77,7 +77,7 @@ export const checkBIConnection = async ({ connId, client, konnector }) => {
* @param {CozyClient} options.client CozyClient object
* @param {Boolean} options.reconnect If this is a reconnection
* @param {Function} options.t Translation fonction
* @return {Promise<Number|null>} Connection Id
* @return {Promise<Boolean>} true if the trigger manager should create the trigger itself
*/
export const handleOAuthAccount = async ({
account,
Expand All @@ -87,10 +87,14 @@ export const handleOAuthAccount = async ({
reconnect,
t
}) => {
if (flag('harvest.bi.fullwebhooks')) {
flow.triggerEvent(LOGIN_SUCCESS_EVENT)
return false
}
if (reconnect) {
// No need for specific action here. The trigger will be launched by the trigger manager
const connId = getBIConnectionIdFromAccount(account)
return connId
return Boolean(connId)
}
const cozyBankIds = getCozyBankIds({ konnector, account })
let biWebviewAccount = {
Expand All @@ -117,7 +121,7 @@ export const handleOAuthAccount = async ({
})
}

return connectionId
return Boolean(connectionId)
}

/**
Expand Down Expand Up @@ -452,6 +456,7 @@ export const createTemporaryToken = async ({ client, konnector, account }) => {

export const konnectorPolicy = {
isBIWebView: true,
needsTriggerCreation: flag('harvest.bi.fullwebhook'),
name: 'budget-insight-webview',
match: isBiWebViewConnector,
saveInVault: false,
Expand Down
2 changes: 1 addition & 1 deletion packages/cozy-harvest-lib/src/services/biWebView.spec.js
Expand Up @@ -114,7 +114,7 @@ describe('handleOAuthAccount', () => {
})
expect(flow.handleFormSubmit).not.toHaveBeenCalled()
expect(flow.saveAccount).not.toHaveBeenCalled()
expect(result).toEqual(15)
expect(result).toEqual(true)
})
})

Expand Down
4 changes: 2 additions & 2 deletions packages/cozy-harvest-lib/src/services/budget-insight.js
Expand Up @@ -220,7 +220,7 @@ export const setBIConnectionId = (originalAccount, biConnectionId) => {
* @param {Object} options.konnector connector manifest content
* @param {Object} options.client CozyClient object
*
* @return {Integer} Connection Id
* @return {Promise<Boolean>} true if the trigger manager should create the trigger itself
*/
export const handleOAuthAccount = async ({
account,
Expand Down Expand Up @@ -252,7 +252,7 @@ export const handleOAuthAccount = async ({
})
}

return connectionId
return Boolean(connectionId)
}

/**
Expand Down

0 comments on commit 8ce5e78

Please sign in to comment.