diff --git a/packages/backend/src/apps/discord/auth/index.ts b/packages/backend/src/apps/discord/auth/index.ts index 8ebc550c..88499b86 100644 --- a/packages/backend/src/apps/discord/auth/index.ts +++ b/packages/backend/src/apps/discord/auth/index.ts @@ -133,96 +133,6 @@ export default { ] } ], - reconnectionSteps: [ - { - type: 'mutation' as const, - name: 'resetConnection', - arguments: [ - { - name: 'id', - value: '{connection.id}' - } - ] - }, - { - type: 'mutation' as const, - name: 'updateConnection', - arguments: [ - { - name: 'id', - value: '{connection.id}' - }, - { - name: 'formattedData', - value: null, - properties: [ - { - name: 'consumerKey', - value: '{fields.consumerKey}' - }, - { - name: 'consumerSecret', - value: '{fields.consumerSecret}' - }, - { - name: 'botToken', - value: '{fields.botToken}' - } - ] - } - ] - }, - { - type: 'mutation' as const, - name: 'createAuthData', - arguments: [ - { - name: 'id', - value: '{connection.id}' - } - ] - }, - { - type: 'openWithPopup' as const, - name: 'openAuthPopup', - arguments: [ - { - name: 'url', - value: '{createAuthData.url}' - } - ] - }, - { - type: 'mutation' as const, - name: 'updateConnection', - arguments: [ - { - name: 'id', - value: '{connection.id}' - }, - { - name: 'formattedData', - value: null, - properties: [ - { - name: 'oauthVerifier', - value: '{openAuthPopup.code}' - } - ] - } - ] - }, - { - type: 'mutation' as const, - name: 'verifyConnection', - arguments: [ - { - name: 'id', - value: '{connection.id}' - } - ] - } - ], createAuthData, verifyCredentials, diff --git a/packages/backend/src/apps/flickr/auth/index.ts b/packages/backend/src/apps/flickr/auth/index.ts index 4b78c335..0ee4a0b5 100644 --- a/packages/backend/src/apps/flickr/auth/index.ts +++ b/packages/backend/src/apps/flickr/auth/index.ts @@ -117,92 +117,6 @@ export default { ] } ], - reconnectionSteps: [ - { - type: 'mutation' as const, - name: 'resetConnection', - arguments: [ - { - name: 'id', - value: '{connection.id}' - } - ] - }, - { - type: 'mutation' as const, - name: 'updateConnection', - arguments: [ - { - name: 'id', - value: '{connection.id}' - }, - { - name: 'formattedData', - value: null, - properties: [ - { - name: 'consumerKey', - value: '{fields.consumerKey}' - }, - { - name: 'consumerSecret', - value: '{fields.consumerSecret}' - } - ] - } - ] - }, - { - type: 'mutation' as const, - name: 'createAuthData', - arguments: [ - { - name: 'id', - value: '{connection.id}' - } - ] - }, - { - type: 'openWithPopup' as const, - name: 'openAuthPopup', - arguments: [ - { - name: 'url', - value: '{createAuthData.url}' - } - ] - }, - { - type: 'mutation' as const, - name: 'updateConnection', - arguments: [ - { - name: 'id', - value: '{connection.id}' - }, - { - name: 'formattedData', - value: null, - properties: [ - { - name: 'oauthVerifier', - value: '{openAuthPopup.oauth_verifier}' - } - ] - } - ] - }, - { - type: 'mutation' as const, - name: 'verifyConnection', - arguments: [ - { - name: 'id', - value: '{connection.id}' - } - ] - } - ], createAuthData, verifyCredentials, diff --git a/packages/backend/src/apps/github/auth/index.ts b/packages/backend/src/apps/github/auth/index.ts index 5cfc9f59..f49c4dd5 100644 --- a/packages/backend/src/apps/github/auth/index.ts +++ b/packages/backend/src/apps/github/auth/index.ts @@ -117,92 +117,6 @@ export default { ] } ], - reconnectionSteps: [ - { - type: 'mutation' as const, - name: 'resetConnection', - arguments: [ - { - name: 'id', - value: '{connection.id}' - } - ] - }, - { - type: 'mutation' as const, - name: 'updateConnection', - arguments: [ - { - name: 'id', - value: '{connection.id}' - }, - { - name: 'formattedData', - value: null, - properties: [ - { - name: 'consumerKey', - value: '{fields.consumerKey}' - }, - { - name: 'consumerSecret', - value: '{fields.consumerSecret}' - } - ] - } - ] - }, - { - type: 'mutation' as const, - name: 'createAuthData', - arguments: [ - { - name: 'id', - value: '{connection.id}' - } - ] - }, - { - type: 'openWithPopup' as const, - name: 'openAuthPopup', - arguments: [ - { - name: 'url', - value: '{createAuthData.url}' - } - ] - }, - { - type: 'mutation' as const, - name: 'updateConnection', - arguments: [ - { - name: 'id', - value: '{connection.id}' - }, - { - name: 'formattedData', - value: null, - properties: [ - { - name: 'oauthVerifier', - value: '{openAuthPopup.code}' - } - ] - } - ] - }, - { - type: 'mutation' as const, - name: 'verifyConnection', - arguments: [ - { - name: 'id', - value: '{connection.id}' - } - ] - } - ], createAuthData, verifyCredentials, diff --git a/packages/backend/src/apps/slack/auth/index.ts b/packages/backend/src/apps/slack/auth/index.ts index a4d02952..d6d95165 100644 --- a/packages/backend/src/apps/slack/auth/index.ts +++ b/packages/backend/src/apps/slack/auth/index.ts @@ -119,96 +119,6 @@ export default { ], }, ], - reconnectionSteps: [ - { - type: 'mutation' as const, - name: 'resetConnection', - arguments: [ - { - name: 'id', - value: '{connection.id}', - }, - ], - }, - { - type: 'mutation' as const, - name: 'updateConnection', - arguments: [ - { - name: 'id', - value: '{connection.id}', - }, - { - name: 'formattedData', - value: null, - properties: [ - { - name: 'consumerKey', - value: '{fields.consumerKey}', - }, - { - name: 'consumerSecret', - value: '{fields.consumerSecret}', - }, - ], - }, - ], - }, - { - type: 'mutation' as const, - name: 'createAuthData', - arguments: [ - { - name: 'id', - value: '{connection.id}', - }, - ], - }, - { - type: 'openWithPopup' as const, - name: 'openAuthPopup', - arguments: [ - { - name: 'url', - value: '{createAuthData.url}', - }, - ], - }, - { - type: 'mutation' as const, - name: 'updateConnection', - arguments: [ - { - name: 'id', - value: '{connection.id}', - }, - { - name: 'formattedData', - value: null, - properties: [ - { - name: 'code', - value: '{openAuthPopup.code}', - }, - { - name: 'state', - value: '{openAuthPopup.state}', - }, - ], - }, - ], - }, - { - type: 'mutation' as const, - name: 'verifyConnection', - arguments: [ - { - name: 'id', - value: '{connection.id}', - }, - ], - }, - ], createAuthData, verifyCredentials, diff --git a/packages/backend/src/apps/smtp/auth/index.ts b/packages/backend/src/apps/smtp/auth/index.ts index 0a4ad9e0..2868abad 100644 --- a/packages/backend/src/apps/smtp/auth/index.ts +++ b/packages/backend/src/apps/smtp/auth/index.ts @@ -138,68 +138,6 @@ export default { ], }, ], - reconnectionSteps: [ - { - type: 'mutation' as const, - name: 'resetConnection', - arguments: [ - { - name: 'id', - value: '{connection.id}', - }, - ], - }, - { - type: 'mutation' as const, - name: 'updateConnection', - arguments: [ - { - name: 'id', - value: '{connection.id}', - }, - { - name: 'formattedData', - value: null, - properties: [ - { - name: 'host', - value: '{fields.host}', - }, - { - name: 'username', - value: '{fields.username}', - }, - { - name: 'password', - value: '{fields.password}', - }, - { - name: 'useTLS', - value: '{fields.useTls}', - }, - { - name: 'port', - value: '{fields.port}', - }, - { - name: 'fromEmail', - value: '{fields.fromEmail}', - }, - ], - }, - ], - }, - { - type: 'mutation' as const, - name: 'verifyConnection', - arguments: [ - { - name: 'id', - value: '{connection.id}', - }, - ], - }, - ], verifyCredentials, isStillVerified, }; diff --git a/packages/backend/src/apps/twilio/auth/index.ts b/packages/backend/src/apps/twilio/auth/index.ts index d33548ba..40e67615 100644 --- a/packages/backend/src/apps/twilio/auth/index.ts +++ b/packages/backend/src/apps/twilio/auth/index.ts @@ -63,52 +63,6 @@ export default { ], }, ], - reconnectionSteps: [ - { - type: 'mutation' as const, - name: 'resetConnection', - arguments: [ - { - name: 'id', - value: '{connection.id}', - }, - ], - }, - { - type: 'mutation' as const, - name: 'updateConnection', - arguments: [ - { - name: 'id', - value: '{connection.id}', - }, - { - name: 'formattedData', - value: null, - properties: [ - { - name: 'accountSid', - value: '{fields.accountSid}', - }, - { - name: 'authToken', - value: '{fields.authToken}', - }, - ], - }, - ], - }, - { - type: 'mutation' as const, - name: 'verifyConnection', - arguments: [ - { - name: 'id', - value: '{connection.id}', - }, - ], - }, - ], verifyCredentials, isStillVerified, diff --git a/packages/backend/src/apps/twitter/auth/index.ts b/packages/backend/src/apps/twitter/auth/index.ts index ecf5ae3f..972504eb 100644 --- a/packages/backend/src/apps/twitter/auth/index.ts +++ b/packages/backend/src/apps/twitter/auth/index.ts @@ -115,92 +115,6 @@ export default { ], }, ], - reconnectionSteps: [ - { - type: 'mutation' as const, - name: 'resetConnection', - arguments: [ - { - name: 'id', - value: '{connection.id}', - }, - ], - }, - { - type: 'mutation' as const, - name: 'updateConnection', - arguments: [ - { - name: 'id', - value: '{connection.id}', - }, - { - name: 'formattedData', - value: null, - properties: [ - { - name: 'consumerKey', - value: '{fields.consumerKey}', - }, - { - name: 'consumerSecret', - value: '{fields.consumerSecret}', - }, - ], - }, - ], - }, - { - type: 'mutation' as const, - name: 'createAuthData', - arguments: [ - { - name: 'id', - value: '{connection.id}', - }, - ], - }, - { - type: 'openWithPopup' as const, - name: 'openAuthPopup', - arguments: [ - { - name: 'url', - value: '{createAuthData.url}', - }, - ], - }, - { - type: 'mutation' as const, - name: 'updateConnection', - arguments: [ - { - name: 'id', - value: '{connection.id}', - }, - { - name: 'formattedData', - value: null, - properties: [ - { - name: 'oauthVerifier', - value: '{openAuthPopup.oauth_verifier}', - }, - ], - }, - ], - }, - { - type: 'mutation' as const, - name: 'verifyConnection', - arguments: [ - { - name: 'id', - value: '{connection.id}', - }, - ], - }, - ], createAuthData, verifyCredentials, diff --git a/packages/backend/src/graphql/schema.graphql b/packages/backend/src/graphql/schema.graphql index 2808552f..b928b995 100644 --- a/packages/backend/src/graphql/schema.graphql +++ b/packages/backend/src/graphql/schema.graphql @@ -126,7 +126,6 @@ type Auth { } type AuthenticationStep { - step: Int type: String name: String arguments: [AuthenticationStepArgument] @@ -316,7 +315,6 @@ input PreviousStepInput { } type ReconnectionStep { - step: Int type: String name: String arguments: [ReconnectionStepArgument] diff --git a/packages/backend/src/helpers/add-reconnection-steps.ts b/packages/backend/src/helpers/add-reconnection-steps.ts new file mode 100644 index 00000000..5110c5b4 --- /dev/null +++ b/packages/backend/src/helpers/add-reconnection-steps.ts @@ -0,0 +1,81 @@ +import { IApp, IAuthenticationStep, IAuthenticationStepField } from '@automatisch/types'; +import cloneDeep from 'lodash/cloneDeep'; + +const connectionIdArgument = { + name: 'id', + value: '{connection.id}', +}; + +const resetConnectionStep = { + type: 'mutation' as const, + name: 'resetConnection', + arguments: [ + connectionIdArgument, + ], +}; + +function replaceCreateConnection(string: string) { + return string.replace( + '{createConnection.id}', + '{connection.id}' + ); +} + +function removeAppKeyArgument(args: IAuthenticationStepField[]) { + return args.filter((argument) => argument.name !== 'key'); +} + +function addConnectionId(step: IAuthenticationStep) { + step.arguments = step.arguments.map((argument) => { + if (typeof argument.value === 'string') { + argument.value = replaceCreateConnection(argument.value); + } + + if (argument.properties) { + argument.properties = argument.properties.map((property) => { + return { + name: property.name, + value: replaceCreateConnection(property.value), + } + }); + } + + return argument; + }); + + return step; +} + +function replaceCreateConnectionsWithUpdate(steps: IAuthenticationStep[]) { + const updatedSteps = cloneDeep(steps); + return updatedSteps.map((step) => { + const updatedStep = addConnectionId(step); + + if (step.name === 'createConnection') { + updatedStep.name = 'updateConnection'; + updatedStep.arguments = removeAppKeyArgument(updatedStep.arguments); + updatedStep.arguments.unshift(connectionIdArgument); + + return updatedStep; + } + + return step; + }) +} + +function addReconnectionSteps(app: IApp): IApp { + const hasReconnectionSteps = app.auth.reconnectionSteps; + + if (hasReconnectionSteps) return app; + + const updatedSteps = replaceCreateConnectionsWithUpdate(app.auth.authenticationSteps); + + app.auth.reconnectionSteps = [ + resetConnectionStep, + ...updatedSteps, + ] + + return app; +} + +export default addReconnectionSteps; diff --git a/packages/backend/src/helpers/get-app.ts b/packages/backend/src/helpers/get-app.ts index c3217d72..be42581e 100644 --- a/packages/backend/src/helpers/get-app.ts +++ b/packages/backend/src/helpers/get-app.ts @@ -6,6 +6,7 @@ import { ITrigger, } from '@automatisch/types'; import { omit, cloneDeep } from 'lodash'; +import addReconnectionSteps from './add-reconnection-steps'; async function getDefaultExport(path: string) { return (await import(path)).default; @@ -16,7 +17,11 @@ function stripFunctions(data: C): C { } const getApp = async (appKey: string, stripFuncs = true) => { - const appData: IApp = cloneDeep(await getDefaultExport(`../apps/${appKey}`)); + let appData: IApp = cloneDeep(await getDefaultExport(`../apps/${appKey}`)); + + if (appData.auth) { + appData = addReconnectionSteps(appData); + } appData.triggers = appData?.triggers?.map((trigger: IRawTrigger) => { return addStaticSubsteps('trigger', appData, trigger); diff --git a/packages/types/index.d.ts b/packages/types/index.d.ts index 639d33a7..09513629 100644 --- a/packages/types/index.d.ts +++ b/packages/types/index.d.ts @@ -143,7 +143,6 @@ export interface IAuthenticationStepField { } export interface IAuthenticationStep { - step: number; type: 'mutation' | 'openWithPopup'; name: string; arguments: IAuthenticationStepField[]; @@ -183,7 +182,7 @@ export interface IAuth { isStillVerified($: IGlobalVariable): Promise; fields: IField[]; authenticationSteps: IAuthenticationStep[]; - reconnectionSteps: IAuthenticationStep[]; + reconnectionSteps?: IAuthenticationStep[]; } export interface IService { diff --git a/packages/web/src/components/AppConnectionRow/index.tsx b/packages/web/src/components/AppConnectionRow/index.tsx index 55a82904..090de246 100644 --- a/packages/web/src/components/AppConnectionRow/index.tsx +++ b/packages/web/src/components/AppConnectionRow/index.tsx @@ -87,7 +87,7 @@ function AppConnectionRow(props: AppConnectionRowProps): React.ReactElement { alignItems="flex-start" spacing={1} > - + {formattedData?.screenName} diff --git a/packages/web/src/graphql/queries/get-app.ts b/packages/web/src/graphql/queries/get-app.ts index bacb5f0f..b0f49df5 100644 --- a/packages/web/src/graphql/queries/get-app.ts +++ b/packages/web/src/graphql/queries/get-app.ts @@ -27,7 +27,6 @@ export const GET_APP = gql` } } authenticationSteps { - step type name arguments { @@ -41,7 +40,6 @@ export const GET_APP = gql` } } reconnectionSteps { - step type name arguments { diff --git a/packages/web/src/graphql/queries/get-apps.ts b/packages/web/src/graphql/queries/get-apps.ts index 6c6df728..59cf4c79 100644 --- a/packages/web/src/graphql/queries/get-apps.ts +++ b/packages/web/src/graphql/queries/get-apps.ts @@ -24,7 +24,6 @@ export const GET_APPS = gql` clickToCopy } authenticationSteps { - step type name arguments { @@ -38,7 +37,6 @@ export const GET_APPS = gql` } } reconnectionSteps { - step type name arguments {