diff --git a/packages/backend/src/apps/you-need-a-budget/assets/favicon.svg b/packages/backend/src/apps/you-need-a-budget/assets/favicon.svg new file mode 100644 index 00000000..c83333c8 --- /dev/null +++ b/packages/backend/src/apps/you-need-a-budget/assets/favicon.svg @@ -0,0 +1,25 @@ + + + + My Budget + + \ No newline at end of file diff --git a/packages/backend/src/apps/you-need-a-budget/auth/generate-auth-url.js b/packages/backend/src/apps/you-need-a-budget/auth/generate-auth-url.js new file mode 100644 index 00000000..12d647e1 --- /dev/null +++ b/packages/backend/src/apps/you-need-a-budget/auth/generate-auth-url.js @@ -0,0 +1,22 @@ +import { URLSearchParams } from 'url'; + +export default async function generateAuthUrl($) { + const oauthRedirectUrlField = $.app.auth.fields.find( + (field) => field.key == 'oAuthRedirectUrl' + ); + const redirectUri = oauthRedirectUrlField.value; + const state = Math.random().toString(); + const searchParams = new URLSearchParams({ + client_id: $.auth.data.clientId, + redirect_uri: redirectUri, + response_type: 'code', + state, + }); + + const url = `https://app.ynab.com/oauth/authorize?${searchParams.toString()}`; + + await $.auth.set({ + url, + originalState: state, + }); +} diff --git a/packages/backend/src/apps/you-need-a-budget/auth/index.js b/packages/backend/src/apps/you-need-a-budget/auth/index.js new file mode 100644 index 00000000..32eef53e --- /dev/null +++ b/packages/backend/src/apps/you-need-a-budget/auth/index.js @@ -0,0 +1,60 @@ +import generateAuthUrl from './generate-auth-url.js'; +import verifyCredentials from './verify-credentials.js'; +import refreshToken from './refresh-token.js'; +import isStillVerified from './is-still-verified.js'; + +export default { + fields: [ + { + key: 'oAuthRedirectUrl', + label: 'OAuth Redirect URL', + type: 'string', + required: true, + readOnly: true, + value: '{WEB_APP_URL}/app/you-need-a-budget/connections/add', + placeholder: null, + description: + 'When asked to input a redirect URL in You Need A Budget, enter the URL above.', + clickToCopy: true, + }, + { + key: 'screenName', + label: 'Screen Name', + type: 'string', + required: true, + readOnly: false, + value: null, + placeholder: null, + description: + 'Screen name of your connection to be used on Automatisch UI.', + clickToCopy: false, + }, + { + key: 'clientId', + label: 'Client ID', + type: 'string', + required: true, + readOnly: false, + value: null, + placeholder: null, + description: null, + clickToCopy: false, + }, + { + key: 'clientSecret', + label: 'Client Secret', + type: 'string', + required: true, + readOnly: false, + value: null, + placeholder: null, + description: null, + clickToCopy: false, + }, + ], + + generateAuthUrl, + verifyCredentials, + isStillVerified, + refreshToken, +}; diff --git a/packages/backend/src/apps/you-need-a-budget/auth/is-still-verified.js b/packages/backend/src/apps/you-need-a-budget/auth/is-still-verified.js new file mode 100644 index 00000000..6d792b12 --- /dev/null +++ b/packages/backend/src/apps/you-need-a-budget/auth/is-still-verified.js @@ -0,0 +1,8 @@ +import getCurrentUser from '../common/get-current-user.js'; + +const isStillVerified = async ($) => { + const currentUser = await getCurrentUser($); + return !!currentUser; +}; + +export default isStillVerified; diff --git a/packages/backend/src/apps/you-need-a-budget/auth/refresh-token.js b/packages/backend/src/apps/you-need-a-budget/auth/refresh-token.js new file mode 100644 index 00000000..9c7830e4 --- /dev/null +++ b/packages/backend/src/apps/you-need-a-budget/auth/refresh-token.js @@ -0,0 +1,25 @@ +import { URLSearchParams } from 'node:url'; + +const refreshToken = async ($) => { + const params = new URLSearchParams({ + client_id: $.auth.data.clientId, + client_secret: $.auth.data.clientSecret, + grant_type: 'refresh_token', + refresh_token: $.auth.data.refreshToken, + }); + + const { data } = await $.http.post( + 'https://app.ynab.com/oauth/token', + params.toString() + ); + + await $.auth.set({ + accessToken: data.access_token, + refreshToken: data.refresh_token, + expiresIn: data.expires_in, + scope: data.scope, + tokenType: data.token_type, + }); +}; + +export default refreshToken; diff --git a/packages/backend/src/apps/you-need-a-budget/auth/verify-credentials.js b/packages/backend/src/apps/you-need-a-budget/auth/verify-credentials.js new file mode 100644 index 00000000..19d88fa3 --- /dev/null +++ b/packages/backend/src/apps/you-need-a-budget/auth/verify-credentials.js @@ -0,0 +1,33 @@ +const verifyCredentials = async ($) => { + if ($.auth.data.originalState !== $.auth.data.state) { + throw new Error(`The 'state' parameter does not match.`); + } + const oauthRedirectUrlField = $.app.auth.fields.find( + (field) => field.key == 'oAuthRedirectUrl' + ); + const redirectUri = oauthRedirectUrlField.value; + const { data } = await $.http.post('https://app.ynab.com/oauth/token', { + client_id: $.auth.data.clientId, + client_secret: $.auth.data.clientSecret, + redirect_uri: redirectUri, + grant_type: 'authorization_code', + code: $.auth.data.code, + }); + + await $.auth.set({ + accessToken: data.access_token, + tokenType: data.token_type, + }); + + await $.auth.set({ + clientId: $.auth.data.clientId, + clientSecret: $.auth.data.clientSecret, + scope: data.scope, + createdAt: data.created_at, + expiresIn: data.expires_in, + refreshToken: data.refresh_token, + screenName: $.auth.data.screenName, + }); +}; + +export default verifyCredentials; diff --git a/packages/backend/src/apps/you-need-a-budget/common/add-auth-header.js b/packages/backend/src/apps/you-need-a-budget/common/add-auth-header.js new file mode 100644 index 00000000..02477aa4 --- /dev/null +++ b/packages/backend/src/apps/you-need-a-budget/common/add-auth-header.js @@ -0,0 +1,9 @@ +const addAuthHeader = ($, requestConfig) => { + if ($.auth.data?.accessToken) { + requestConfig.headers.Authorization = `${$.auth.data.tokenType} ${$.auth.data.accessToken}`; + } + + return requestConfig; +}; + +export default addAuthHeader; diff --git a/packages/backend/src/apps/you-need-a-budget/common/get-current-user.js b/packages/backend/src/apps/you-need-a-budget/common/get-current-user.js new file mode 100644 index 00000000..65dea829 --- /dev/null +++ b/packages/backend/src/apps/you-need-a-budget/common/get-current-user.js @@ -0,0 +1,6 @@ +const getCurrentUser = async ($) => { + const { data: currentUser } = await $.http.get('/user'); + return currentUser.data.user.id; +}; + +export default getCurrentUser; diff --git a/packages/backend/src/apps/you-need-a-budget/index.js b/packages/backend/src/apps/you-need-a-budget/index.js new file mode 100644 index 00000000..0cb69d26 --- /dev/null +++ b/packages/backend/src/apps/you-need-a-budget/index.js @@ -0,0 +1,18 @@ +import defineApp from '../../helpers/define-app.js'; +import addAuthHeader from './common/add-auth-header.js'; +import auth from './auth/index.js'; +import triggers from './triggers/index.js'; + +export default defineApp({ + name: 'You Need A Budget', + key: 'you-need-a-budget', + baseUrl: 'https://app.ynab.com', + apiBaseUrl: 'https://api.ynab.com/v1', + iconUrl: '{BASE_URL}/apps/you-need-a-budget/assets/favicon.svg', + authDocUrl: '{DOCS_URL}/apps/you-need-a-budget/connection', + primaryColor: '19223C', + supportsConnections: true, + beforeRequest: [addAuthHeader], + auth, + triggers, +}); diff --git a/packages/backend/src/apps/you-need-a-budget/triggers/category-overspent/index.js b/packages/backend/src/apps/you-need-a-budget/triggers/category-overspent/index.js new file mode 100644 index 00000000..9e3c953f --- /dev/null +++ b/packages/backend/src/apps/you-need-a-budget/triggers/category-overspent/index.js @@ -0,0 +1,35 @@ +import { DateTime } from 'luxon'; +import defineTrigger from '../../../../helpers/define-trigger.js'; + +export default defineTrigger({ + name: 'Category overspent', + key: 'categoryOverspent', + pollInterval: 15, + description: + 'Triggers when a category exceeds its budget, resulting in a negative balance.', + + async run($) { + const monthYear = DateTime.now().toFormat('MM-yyyy'); + const categoryWithNegativeBalance = []; + + const response = await $.http.get('/budgets/default/categories'); + const categoryGroups = response.data.data.category_groups; + + categoryGroups.forEach((group) => { + group.categories.forEach((category) => { + if (category.balance < 0) { + categoryWithNegativeBalance.push(category); + } + }); + }); + + for (const category of categoryWithNegativeBalance) { + $.pushTriggerItem({ + raw: category, + meta: { + internalId: `${category.id}-${monthYear}`, + }, + }); + } + }, +}); diff --git a/packages/backend/src/apps/you-need-a-budget/triggers/goal-completed/index.js b/packages/backend/src/apps/you-need-a-budget/triggers/goal-completed/index.js new file mode 100644 index 00000000..53e87840 --- /dev/null +++ b/packages/backend/src/apps/you-need-a-budget/triggers/goal-completed/index.js @@ -0,0 +1,34 @@ +import { DateTime } from 'luxon'; +import defineTrigger from '../../../../helpers/define-trigger.js'; + +export default defineTrigger({ + name: 'Goal completed', + key: 'goalCompleted', + pollInterval: 15, + description: 'Triggers when a goal is completed.', + + async run($) { + const monthYear = DateTime.now().toFormat('MM-yyyy'); + const goalCompletedCategories = []; + + const response = await $.http.get('/budgets/default/categories'); + const categoryGroups = response.data.data.category_groups; + + categoryGroups.forEach((group) => { + group.categories.forEach((category) => { + if (category.goal_percentage_complete === 100) { + goalCompletedCategories.push(category); + } + }); + }); + + for (const category of goalCompletedCategories) { + $.pushTriggerItem({ + raw: category, + meta: { + internalId: `${category.id}-${monthYear}`, + }, + }); + } + }, +}); diff --git a/packages/backend/src/apps/you-need-a-budget/triggers/index.js b/packages/backend/src/apps/you-need-a-budget/triggers/index.js new file mode 100644 index 00000000..81ac9694 --- /dev/null +++ b/packages/backend/src/apps/you-need-a-budget/triggers/index.js @@ -0,0 +1,11 @@ +import categoryOverspent from './category-overspent/index.js'; +import goalCompleted from './goal-completed/index.js'; +import lowAccountBalance from './low-account-balance/index.js'; +import newTransactions from './new-transactions/index.js'; + +export default [ + categoryOverspent, + goalCompleted, + lowAccountBalance, + newTransactions, +]; diff --git a/packages/backend/src/apps/you-need-a-budget/triggers/low-account-balance/index.js b/packages/backend/src/apps/you-need-a-budget/triggers/low-account-balance/index.js new file mode 100644 index 00000000..2eeacae3 --- /dev/null +++ b/packages/backend/src/apps/you-need-a-budget/triggers/low-account-balance/index.js @@ -0,0 +1,41 @@ +import { DateTime } from 'luxon'; +import defineTrigger from '../../../../helpers/define-trigger.js'; + +export default defineTrigger({ + name: 'Low account balance', + key: 'lowAccountBalance', + pollInterval: 15, + description: + 'Triggers when the balance of a Checking or Savings account falls below a specified amount within a given month.', + arguments: [ + { + label: 'Balance Below Amount', + key: 'balanceBelowAmount', + type: 'string', + required: true, + description: 'Account balance falls below this amount (e.g. "250.00")', + variables: true, + }, + ], + + async run($) { + const monthYear = DateTime.now().toFormat('MM-yyyy'); + const balanceBelowAmount = $.step.parameters.balanceBelowAmount; + const formattedBalance = balanceBelowAmount * 1000; + + const response = await $.http.get('/budgets/default/accounts'); + + if (response.data?.data?.accounts?.length) { + for (const account of response.data.data.accounts) { + if (account.balance < formattedBalance) { + $.pushTriggerItem({ + raw: account, + meta: { + internalId: `${account.id}-${monthYear}`, + }, + }); + } + } + } + }, +}); diff --git a/packages/backend/src/apps/you-need-a-budget/triggers/new-transactions/index.js b/packages/backend/src/apps/you-need-a-budget/triggers/new-transactions/index.js new file mode 100644 index 00000000..00aa4c02 --- /dev/null +++ b/packages/backend/src/apps/you-need-a-budget/triggers/new-transactions/index.js @@ -0,0 +1,24 @@ +import defineTrigger from '../../../../helpers/define-trigger.js'; + +export default defineTrigger({ + name: 'New transactions', + key: 'newTransactions', + pollInterval: 15, + description: 'Triggers when a new transaction is created.', + + async run($) { + const response = await $.http.get('/budgets/default/transactions'); + const transactions = response.data.data?.transactions; + + if (transactions?.length) { + for (const transaction of transactions) { + $.pushTriggerItem({ + raw: transaction, + meta: { + internalId: transaction.id, + }, + }); + } + } + }, +}); diff --git a/packages/docs/pages/.vitepress/config.js b/packages/docs/pages/.vitepress/config.js index 627a7737..7d49637e 100644 --- a/packages/docs/pages/.vitepress/config.js +++ b/packages/docs/pages/.vitepress/config.js @@ -513,6 +513,15 @@ export default defineConfig({ { text: 'Connection', link: '/apps/xero/connection' }, ], }, + { + text: 'You Need A Budget', + collapsible: true, + collapsed: true, + items: [ + { text: 'Triggers', link: '/apps/you-need-a-budget/triggers' }, + { text: 'Connection', link: '/apps/you-need-a-budget/connection' }, + ], + }, { text: 'Youtube', collapsible: true, diff --git a/packages/docs/pages/apps/you-need-a-budget/connection.md b/packages/docs/pages/apps/you-need-a-budget/connection.md new file mode 100644 index 00000000..be7307d2 --- /dev/null +++ b/packages/docs/pages/apps/you-need-a-budget/connection.md @@ -0,0 +1,19 @@ +# You Need A Budget + +:::info +This page explains the steps you need to follow to set up the You Need A Budget +connection in Automatisch. If any of the steps are outdated, please let us know! +::: + +1. Go to the [link](https://www.ynab.com/) and login your account. +2. Click on the account name in the top left and go to **Account Settings**. +3. Click on the **Developer Settings**. +4. Click on the **New Application** under the **OAuth Applications** section. +5. Fill the new application form. +6. Copy **OAuth Redirect URL** from Automatisch and paste it in **Redirect URI(s)** section. +7. Enable the option **Enable default budget selection when users authorize this application**. +8. Click on the **Save Application** button. +9. Copy the **Client ID** value to the **Client ID** field on Automatisch. +10. Copy the **Client Secret** value to the **Client Secret** field on Automatisch. +11. Fill the **Screen Name** field on Automatisch. +12. Congrats! Start using your new You Need A Budget connection within the flows. diff --git a/packages/docs/pages/apps/you-need-a-budget/triggers.md b/packages/docs/pages/apps/you-need-a-budget/triggers.md new file mode 100644 index 00000000..4394dd0b --- /dev/null +++ b/packages/docs/pages/apps/you-need-a-budget/triggers.md @@ -0,0 +1,18 @@ +--- +favicon: /favicons/you-need-a-budget.svg +items: + - name: Category overspent + desc: Triggers when a category exceeds its budget, resulting in a negative balance. + - name: Goal completed + desc: Triggers when a goal is completed. + - name: Low account balance + desc: Triggers when the balance of a Checking or Savings account falls below a specified amount within a given month. + - name: New transactions + desc: Triggers when a new transaction is created. +--- + + + + diff --git a/packages/docs/pages/guide/available-apps.md b/packages/docs/pages/guide/available-apps.md index 4ea1ad34..73931c57 100644 --- a/packages/docs/pages/guide/available-apps.md +++ b/packages/docs/pages/guide/available-apps.md @@ -54,5 +54,6 @@ The following integrations are currently supported by Automatisch. - [Webhooks](/apps/webhooks/triggers) - [WordPress](/apps/wordpress/triggers) - [Xero](/apps/xero/triggers) +- [You Need A Budget](/apps/you-need-a-budget/triggers) - [Youtube](/apps/youtube/triggers) - [Zendesk](/apps/zendesk/actions) diff --git a/packages/docs/pages/public/favicons/you-need-a-budget.svg b/packages/docs/pages/public/favicons/you-need-a-budget.svg new file mode 100644 index 00000000..c83333c8 --- /dev/null +++ b/packages/docs/pages/public/favicons/you-need-a-budget.svg @@ -0,0 +1,25 @@ + + + + My Budget + + \ No newline at end of file