Skip to content

Commit

Permalink
fix: BiWebview handle accounts created after bi webhooks
Browse files Browse the repository at this point in the history
When an account is created by stack after a BI webhook, it does not have
the `oauth.query.connection_id` attribute but in `data.auth.bi.connId`.

We keep the handling of `oauth.query.connection_id` for compatibility
while bi webhooks are not fully deployed.
  • Loading branch information
doubleface authored and doubleface committed Nov 16, 2022
1 parent c5a7a80 commit 3715fe7
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions packages/cozy-harvest-lib/src/services/biWebView.js
Expand Up @@ -16,8 +16,7 @@ import {
setBIConnectionId,
findAccountWithBiConnection,
convertBIErrortoKonnectorJobError,
isBudgetInsightConnector,
getBIConnectionIdFromAccount
isBudgetInsightConnector
} from './budget-insight'
import { KonnectorJobError } from '../helpers/konnectors'
import { waitForRealtimeEvent } from './jobUtils'
Expand Down Expand Up @@ -93,7 +92,7 @@ export const handleOAuthAccount = async ({
}
if (reconnect) {
// No need for specific action here. The trigger will be launched by the trigger manager
const connId = getBIConnectionIdFromAccount(account)
const connId = getWebviewBIConnectionId(account)
return Boolean(connId)
}
const cozyBankIds = getCozyBankIds({ konnector, account })
Expand Down Expand Up @@ -149,15 +148,24 @@ const getBiAggregatorParentRelationship = konnector => {
}

/**
* Gets BI webview connection id which is returned in the account by the stack
* via oauth callback url
* Gets BI webview connection id from the account. The connection id can be in different places depending on context :
* - after connection creation : in account.oauth.query.connection_id. The account is created by the stack after webview return
* - after connection manage : in account.data.auth.bi.connId. The account is created by the stack after webview return
*
* @param {IoCozyAccount} account The account content created by the stack
* @param {IoCozyAccount} account The account content
*
* @return {Number|null} Connection Id or null if no connexion
*/
const getWebviewBIConnectionId = account => {
return Number(account?.oauth?.query?.connection_id?.[0] || null)
if (flag('harvest.bi.fullwebhooks')) {
return Number(account?.data?.auth?.bi?.connId || null)
} else {
return Number(
account?.oauth?.query?.connection_id?.[0] ||
account?.data?.auth?.bi?.connId ||
null
)
}
}

/**
Expand Down Expand Up @@ -206,7 +214,7 @@ export const onBIAccountCreation = async ({
* @param {object} options
* @param {Array<String>} options.biBankIds - connector bank ids (for webview connectors)
* @param {String} options.token - BI temporary token
* @param {Number} options.connId - BI bi connection id
* @param {Number|null} options.connId - BI bi connection id
* @return {Object}
*/
const getReconnectOrManageExtraOAuthUrlParams = ({
Expand Down Expand Up @@ -249,7 +257,7 @@ export const fetchExtraOAuthUrlParams = async ({
account
})

const connId = getBIConnectionIdFromAccount(account)
const connId = getWebviewBIConnectionId(account)

if (reconnect || manage) {
return getReconnectOrManageExtraOAuthUrlParams({
Expand Down

0 comments on commit 3715fe7

Please sign in to comment.