@things-factory/integration-accounting 9.0.0-beta.8 → 9.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/dist-server/controllers/accounting-api/index.js +1 -1
  2. package/dist-server/controllers/accounting-api/index.js.map +1 -1
  3. package/dist-server/controllers/xero/apis/contact/get-contacts.js +3 -1
  4. package/dist-server/controllers/xero/apis/contact/get-contacts.js.map +1 -1
  5. package/dist-server/controllers/xero/apis/invoice/get-invoices.js +3 -1
  6. package/dist-server/controllers/xero/apis/invoice/get-invoices.js.map +1 -1
  7. package/dist-server/controllers/xero/apis/item/delete-item.js +3 -1
  8. package/dist-server/controllers/xero/apis/item/delete-item.js.map +1 -1
  9. package/dist-server/controllers/xero/apis/item/get-items.js +3 -1
  10. package/dist-server/controllers/xero/apis/item/get-items.js.map +1 -1
  11. package/dist-server/controllers/xero/apis/purchase-order/get-purchase-orders.js +8 -3
  12. package/dist-server/controllers/xero/apis/purchase-order/get-purchase-orders.js.map +1 -1
  13. package/dist-server/controllers/xero/xero.js +7 -2
  14. package/dist-server/controllers/xero/xero.js.map +1 -1
  15. package/dist-server/engine/connector/accounting-connector.js +1 -1
  16. package/dist-server/engine/connector/accounting-connector.js.map +1 -1
  17. package/dist-server/engine/index.d.ts +2 -2
  18. package/dist-server/engine/index.js +2 -2
  19. package/dist-server/engine/index.js.map +1 -1
  20. package/dist-server/graphql/resolvers/accounting/accounts.js +4 -1
  21. package/dist-server/graphql/resolvers/accounting/accounts.js.map +1 -1
  22. package/dist-server/graphql/resolvers/accounting/create-account.js +7 -1
  23. package/dist-server/graphql/resolvers/accounting/create-account.js.map +1 -1
  24. package/dist-server/graphql/resolvers/accounting/delete-account.js +5 -1
  25. package/dist-server/graphql/resolvers/accounting/delete-account.js.map +1 -1
  26. package/dist-server/graphql/resolvers/accounting/delete-accounts.js +5 -1
  27. package/dist-server/graphql/resolvers/accounting/delete-accounts.js.map +1 -1
  28. package/dist-server/graphql/resolvers/accounting/index.js +13 -2
  29. package/dist-server/graphql/resolvers/accounting/index.js.map +1 -1
  30. package/dist-server/graphql/resolvers/accounting/update-account.js +5 -1
  31. package/dist-server/graphql/resolvers/accounting/update-account.js.map +1 -1
  32. package/dist-server/graphql/resolvers/accounting/update-multiple-accounts.js +13 -4
  33. package/dist-server/graphql/resolvers/accounting/update-multiple-accounts.js.map +1 -1
  34. package/dist-server/graphql/resolvers/accounting/xero/deactivate-xero-account.js +5 -1
  35. package/dist-server/graphql/resolvers/accounting/xero/deactivate-xero-account.js.map +1 -1
  36. package/dist-server/graphql/resolvers/accounting/xero/index.js +7 -2
  37. package/dist-server/graphql/resolvers/accounting/xero/index.js.map +1 -1
  38. package/dist-server/graphql/resolvers/accounting/xero/refresh-xero-access-token.js +5 -1
  39. package/dist-server/graphql/resolvers/accounting/xero/refresh-xero-access-token.js.map +1 -1
  40. package/dist-server/graphql/resolvers/accounting-api/index.js +9 -2
  41. package/dist-server/graphql/resolvers/accounting-api/index.js.map +1 -1
  42. package/dist-server/routers/xero-private-router.js +3 -3
  43. package/dist-server/routers/xero-private-router.js.map +1 -1
  44. package/dist-server/routers/xero-router.js +13 -3
  45. package/dist-server/routers/xero-router.js.map +1 -1
  46. package/dist-server/tsconfig.tsbuildinfo +1 -1
  47. package/package.json +14 -14
@@ -1 +1 @@
1
- {"version":3,"file":"refresh-xero-access-token.js","sourceRoot":"","sources":["../../../../../server/graphql/resolvers/accounting/xero/refresh-xero-access-token.ts"],"names":[],"mappings":";;;;AAAA,oEAA8B;AAE9B,6CAA4C;AAC5C,iDAAqD;AACrD,iDAAgD;AAEhD,mDAA8C;AAE9C,MAAM,UAAU,GAAG,YAAM,CAAC,GAAG,CAAC,2BAA2B,EAAE,EAAE,CAAC,CAAA;AAC9D,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,GAAG,UAAU,CAAA;AAExC,MAAM,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC,iEAAi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C,EAC7C,QAAQ,CAAC,mGAAmG,EAC5G,UAAU,CAAC,sDAAsD,EACjE,UAAU,EAAE,SAAS,CAAC,oBAAoB,EAC1C,aAAa;QACb;WACG;UACJ,GAAG,IAAI,CAAA;QAER,MAAM,EAAE,GAAG,EAAE,GAAG,IAAA,gBAAQ,EAAC,YAAY,CAAC,CAAA;QAEtC,IAAI,KAAK,GAAG;YACV,WAAW,EAAE,YAAY;YACzB,YAAY,EAAE,aAAa;YAC3B,SAAS;YACT,SAAS,EAAE,IAAI,IAAI,CAAC,GAAG,GAAG,IAAI,CAAC;SAChC,CAAA;QAED,OAAO,MAAM,UAAU,CAAC,IAAI,+CACvB,OAAO,GACP,KAAK,KACR,OAAO,EAAE,OAAO,CAAC,KAAK,CAAC,IAAI,IAC3B,CAAA;IACJ,CAAC;CACF,CAAA","sourcesContent":["import fetch from 'node-fetch'\n\nimport { config } from '@things-factory/env'\nimport { getRepository } from '@things-factory/shell'\nimport { parseJwt } from '@things-factory/utils'\n\nimport { Account } from '../../../../entities'\n\nconst xeroConfig = config.get('accountingIntegrationXero', {})\nconst { apiKey, apiSecret } = xeroConfig\n\nconst debug = require('debug')('things-factory:integration-accounting:refresh-xero-access-token')\n\nexport const refreshXeroAccessToken = {\n async refreshXeroAccessToken(_: any, { id }, context: ResolverContext) {\n const repository = getRepository(Account)\n const account: any = await repository.findOne({\n where: { domain: { id: context.state.domain.id }, id }\n })\n\n const refreshRequestData = {\n grant_type: 'refresh_token',\n refresh_token: account.refreshToken\n }\n\n const refreshResponse = await fetch(`https://identity.xero.com/connect/token`, {\n method: 'post',\n headers: {\n Authorization: `Basic ${Buffer.from(apiKey + ':' + apiSecret).toString('base64')}`,\n 'Content-Type': 'application/x-www-form-urlencoded'\n },\n body: Object.entries(refreshRequestData)\n .map(([key, value]) => encodeURIComponent(key) + '=' + encodeURIComponent(value))\n .join('&')\n })\n\n if (!refreshResponse.ok) {\n throw new Error(`get account information failed: ${await refreshResponse.text()}`)\n }\n\n const body = await refreshResponse.json()\n const {\n access_token /* token used to call the API */,\n id_token /* token containing user identity details (only returned if OpenID Connect scopes are requested) */,\n expires_in /* amount of seconds until the access token expires */,\n token_type: tokenType /* must be Bearer */,\n refresh_token\n /* token used to refresh the access token once it has expired (only returned if the offline_access scope is requested).\n */\n } = body\n\n const { exp } = parseJwt(access_token)\n\n var patch = {\n accessToken: access_token,\n refreshToken: refresh_token,\n tokenType,\n expiresIn: new Date(exp * 1000)\n }\n\n return await repository.save({\n ...account,\n ...patch,\n updater: context.state.user\n })\n }\n}\n"]}
1
+ {"version":3,"file":"refresh-xero-access-token.js","sourceRoot":"","sources":["../../../../../server/graphql/resolvers/accounting/xero/refresh-xero-access-token.ts"],"names":[],"mappings":";;;;AAAA,oEAA8B;AAE9B,6CAA4C;AAC5C,iDAAqD;AACrD,iDAAgD;AAEhD,mDAA8C;AAE9C,MAAM,UAAU,GAAG,YAAM,CAAC,GAAG,CAAC,2BAA2B,EAAE,EAAE,CAAC,CAAA;AAC9D,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,GAAG,UAAU,CAAA;AAExC,MAAM,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC,iEAAiE,CAAC,CAAA;AAEpF,QAAA,sBAAsB,GAAG;IACpC,KAAK,CAAC,sBAAsB,CAAC,CAAM,EAAE,EAAE,EAAE,EAAE,EAAE,OAAwB;QACnE,MAAM,UAAU,GAAG,IAAA,qBAAa,EAAC,kBAAO,CAAC,CAAA;QACzC,MAAM,OAAO,GAAQ,MAAM,UAAU,CAAC,OAAO,CAAC;YAC5C,KAAK,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,EAAE,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE;SACvD,CAAC,CAAA;QAEF,MAAM,kBAAkB,GAAG;YACzB,UAAU,EAAE,eAAe;YAC3B,aAAa,EAAE,OAAO,CAAC,YAAY;SACpC,CAAA;QAED,MAAM,eAAe,GAAG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;SAC5B,CAAC,CAAA;IACJ,CAAC;CACF,CAAA","sourcesContent":["import fetch from 'node-fetch'\n\nimport { config } from '@things-factory/env'\nimport { getRepository } from '@things-factory/shell'\nimport { parseJwt } from '@things-factory/utils'\n\nimport { Account } from '../../../../entities'\n\nconst xeroConfig = config.get('accountingIntegrationXero', {})\nconst { apiKey, apiSecret } = xeroConfig\n\nconst debug = require('debug')('things-factory:integration-accounting:refresh-xero-access-token')\n\nexport const refreshXeroAccessToken = {\n async refreshXeroAccessToken(_: any, { id }, context: ResolverContext) {\n const repository = getRepository(Account)\n const account: any = await repository.findOne({\n where: { domain: { id: context.state.domain.id }, id }\n })\n\n const refreshRequestData = {\n grant_type: 'refresh_token',\n refresh_token: account.refreshToken\n }\n\n const refreshResponse = await fetch(`https://identity.xero.com/connect/token`, {\n method: 'post',\n headers: {\n Authorization: `Basic ${Buffer.from(apiKey + ':' + apiSecret).toString('base64')}`,\n 'Content-Type': 'application/x-www-form-urlencoded'\n },\n body: Object.entries(refreshRequestData)\n .map(([key, value]) => encodeURIComponent(key) + '=' + encodeURIComponent(value))\n .join('&')\n })\n\n if (!refreshResponse.ok) {\n throw new Error(`get account information failed: ${await refreshResponse.text()}`)\n }\n\n const body = await refreshResponse.json()\n const {\n access_token /* token used to call the API */,\n id_token /* token containing user identity details (only returned if OpenID Connect scopes are requested) */,\n expires_in /* amount of seconds until the access token expires */,\n token_type: tokenType /* must be Bearer */,\n refresh_token\n /* token used to refresh the access token once it has expired (only returned if the offline_access scope is requested).\n */\n } = body\n\n const { exp } = parseJwt(access_token)\n\n var patch = {\n accessToken: access_token,\n refreshToken: refresh_token,\n tokenType,\n expiresIn: new Date(exp * 1000)\n }\n\n return await repository.save({\n ...account,\n ...patch,\n updater: context.state.user\n })\n }\n}\n"]}
@@ -4,6 +4,13 @@ exports.Mutation = exports.Query = void 0;
4
4
  const accounting_invoice_1 = require("./accounting-invoice");
5
5
  const accounting_item_1 = require("./accounting-item");
6
6
  const accounting_purchase_order_1 = require("./accounting-purchase-order");
7
- exports.Query = Object.assign(Object.assign(Object.assign({}, accounting_invoice_1.queryAccountingInvoice), accounting_item_1.queryAccountingItem), accounting_purchase_order_1.queryAccountingPurchaseOrder);
8
- exports.Mutation = Object.assign(Object.assign({}, accounting_invoice_1.mutateAccountingInvoice), accounting_item_1.mutateAccountingItem);
7
+ exports.Query = {
8
+ ...accounting_invoice_1.queryAccountingInvoice,
9
+ ...accounting_item_1.queryAccountingItem,
10
+ ...accounting_purchase_order_1.queryAccountingPurchaseOrder
11
+ };
12
+ exports.Mutation = {
13
+ ...accounting_invoice_1.mutateAccountingInvoice,
14
+ ...accounting_item_1.mutateAccountingItem
15
+ };
9
16
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../server/graphql/resolvers/accounting-api/index.ts"],"names":[],"mappings":";;;AAAA,6DAAsF;AACtF,uDAA6E;AAC7E,2EAA0E;AAE7D,QAAA,KAAK,iDACb,2CAAsB,GACtB,qCAAmB,GACnB,wDAA4B,EAChC;AAEY,QAAA,QAAQ,mCAChB,4CAAuB,GACvB,sCAAoB,EACxB","sourcesContent":["import { queryAccountingInvoice, mutateAccountingInvoice } from './accounting-invoice'\nimport { queryAccountingItem, mutateAccountingItem } from './accounting-item'\nimport { queryAccountingPurchaseOrder } from './accounting-purchase-order'\n\nexport const Query = {\n ...queryAccountingInvoice,\n ...queryAccountingItem,\n ...queryAccountingPurchaseOrder\n}\n\nexport const Mutation = {\n ...mutateAccountingInvoice,\n ...mutateAccountingItem\n}\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../server/graphql/resolvers/accounting-api/index.ts"],"names":[],"mappings":";;;AAAA,6DAAsF;AACtF,uDAA6E;AAC7E,2EAA0E;AAE7D,QAAA,KAAK,GAAG;IACnB,GAAG,2CAAsB;IACzB,GAAG,qCAAmB;IACtB,GAAG,wDAA4B;CAChC,CAAA;AAEY,QAAA,QAAQ,GAAG;IACtB,GAAG,4CAAuB;IAC1B,GAAG,sCAAoB;CACxB,CAAA","sourcesContent":["import { queryAccountingInvoice, mutateAccountingInvoice } from './accounting-invoice'\nimport { queryAccountingItem, mutateAccountingItem } from './accounting-item'\nimport { queryAccountingPurchaseOrder } from './accounting-purchase-order'\n\nexport const Query = {\n ...queryAccountingInvoice,\n ...queryAccountingItem,\n ...queryAccountingPurchaseOrder\n}\n\nexport const Mutation = {\n ...mutateAccountingInvoice,\n ...mutateAccountingItem\n}\n"]}
@@ -5,13 +5,13 @@ const tslib_1 = require("tslib");
5
5
  const koa_router_1 = tslib_1.__importDefault(require("koa-router"));
6
6
  const env_1 = require("@things-factory/env");
7
7
  const shell_1 = require("@things-factory/shell");
8
- const isPathBaseDomain = !env_1.config.get('subdomain') && !env_1.config.get('useVirtualHostBasedDomain');
8
+ const isPathBaseDomain = !env_1.config.get('subdomain');
9
9
  const debug = require('debug')('things-factory:integration-accounting:xero-router');
10
10
  exports.xeroPrivateRouter = new koa_router_1.default();
11
- exports.xeroPrivateRouter.get(`${isPathBaseDomain ? '/domain/:domain' : ''}/callback-private-xero`, async (context, next) => {
11
+ exports.xeroPrivateRouter.get(`${isPathBaseDomain ? `/${(0, shell_1.getRoutePrefixForDomainType)()}/:domain` : ''}/callback-private-xero`, async (context, next) => {
12
12
  const { domain } = context.state;
13
13
  const xeroAccountingCookies = context.cookies.get('xero-accounting');
14
14
  const { id, status, message } = JSON.parse(xeroAccountingCookies);
15
- context.redirect(`${(0, shell_1.getRedirectSubdomainPath)(context, domain.subdomain)}account-xero/${id}/connect-callback?status=${status}&message=${message}`);
15
+ context.redirect(`${(0, shell_1.getRedirectSubdomainPath)(context, domain)}account-xero/${id}/connect-callback?status=${status}&message=${message}`);
16
16
  });
17
17
  //# sourceMappingURL=xero-private-router.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"xero-private-router.js","sourceRoot":"","sources":["../../server/routers/xero-private-router.ts"],"names":[],"mappings":";;;;AAAA,oEAA+B;AAE/B,6CAA4C;AAC5C,iDAAgE;AAEhE,MAAM,gBAAgB,GAAG,CAAC,YAAM,CAAC,GAAG,CAAC,WAAW,CAAC,IAAI,CAAC,YAAM,CAAC,GAAG,CAAC,2BAA2B,CAAC,CAAA;AAE7F,MAAM,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC,mDAAmD,CAAC,CAAA;AAEtE,QAAA,iBAAiB,GAAG,IAAI,oBAAM,EAAE,CAAA;AAE7C,yBAAiB,CAAC,GAAG,CAAC,GAAG,gBAAgB,CAAC,CAAC,CAAC,iBAAiB,CAAC,CAAC,CAAC,EAAE,wBAAwB,EAAE,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE;IAClH,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,KAAK,CAAA;IAEhC,MAAM,qBAAqB,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAA;IACpE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,qBAAqB,CAAC,CAAA;IAEjE,OAAO,CAAC,QAAQ,CACd,GAAG,IAAA,gCAAwB,EACzB,OAAO,EACP,MAAM,CAAC,SAAS,CACjB,gBAAgB,EAAE,4BAA4B,MAAM,YAAY,OAAO,EAAE,CAC3E,CAAA;AACH,CAAC,CAAC,CAAA","sourcesContent":["import Router from 'koa-router'\n\nimport { config } from '@things-factory/env'\nimport { getRedirectSubdomainPath } from '@things-factory/shell'\n\nconst isPathBaseDomain = !config.get('subdomain') && !config.get('useVirtualHostBasedDomain')\n\nconst debug = require('debug')('things-factory:integration-accounting:xero-router')\n\nexport const xeroPrivateRouter = new Router()\n\nxeroPrivateRouter.get(`${isPathBaseDomain ? '/domain/:domain' : ''}/callback-private-xero`, async (context, next) => {\n const { domain } = context.state\n\n const xeroAccountingCookies = context.cookies.get('xero-accounting')\n const { id, status, message } = JSON.parse(xeroAccountingCookies)\n\n context.redirect(\n `${getRedirectSubdomainPath(\n context,\n domain.subdomain\n )}account-xero/${id}/connect-callback?status=${status}&message=${message}`\n )\n})\n"]}
1
+ {"version":3,"file":"xero-private-router.js","sourceRoot":"","sources":["../../server/routers/xero-private-router.ts"],"names":[],"mappings":";;;;AAAA,oEAA+B;AAE/B,6CAA4C;AAC5C,iDAA6F;AAE7F,MAAM,gBAAgB,GAAG,CAAC,YAAM,CAAC,GAAG,CAAC,WAAW,CAAC,CAAA;AAEjD,MAAM,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC,mDAAmD,CAAC,CAAA;AAEtE,QAAA,iBAAiB,GAAG,IAAI,oBAAM,EAAE,CAAA;AAE7C,yBAAiB,CAAC,GAAG,CACnB,GAAG,gBAAgB,CAAC,CAAC,CAAC,IAAI,IAAA,mCAA2B,GAAE,UAAU,CAAC,CAAC,CAAC,EAAE,wBAAwB,EAC9F,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE;IACtB,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,KAAK,CAAA;IAEhC,MAAM,qBAAqB,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAA;IACpE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,qBAAqB,CAAC,CAAA;IAEjE,OAAO,CAAC,QAAQ,CACd,GAAG,IAAA,gCAAwB,EACzB,OAAO,EACP,MAAM,CACP,gBAAgB,EAAE,4BAA4B,MAAM,YAAY,OAAO,EAAE,CAC3E,CAAA;AACH,CAAC,CACF,CAAA","sourcesContent":["import Router from 'koa-router'\n\nimport { config } from '@things-factory/env'\nimport { getRedirectSubdomainPath, getRoutePrefixForDomainType } from '@things-factory/shell'\n\nconst isPathBaseDomain = !config.get('subdomain')\n\nconst debug = require('debug')('things-factory:integration-accounting:xero-router')\n\nexport const xeroPrivateRouter = new Router()\n\nxeroPrivateRouter.get(\n `${isPathBaseDomain ? `/${getRoutePrefixForDomainType()}/:domain` : ''}/callback-private-xero`,\n async (context, next) => {\n const { domain } = context.state\n\n const xeroAccountingCookies = context.cookies.get('xero-accounting')\n const { id, status, message } = JSON.parse(xeroAccountingCookies)\n\n context.redirect(\n `${getRedirectSubdomainPath(\n context,\n domain\n )}account-xero/${id}/connect-callback?status=${status}&message=${message}`\n )\n }\n)\n"]}
@@ -85,8 +85,15 @@ exports.xeroRouter.get('/callback-xero', async (context, next) => {
85
85
  expiresIn: new Date(exp * 1000)
86
86
  };
87
87
  var user = await (0, shell_1.getRepository)(auth_base_1.User).findOneBy({ id: userId });
88
- account = await (0, shell_1.getRepository)(entities_1.Account).save(Object.assign(Object.assign(Object.assign({}, account), patch), { updater: user }));
89
- context.cookies.set('xero-accounting', JSON.stringify(Object.assign(Object.assign({}, account), { message: 'success to activate' })), {
88
+ account = await (0, shell_1.getRepository)(entities_1.Account).save({
89
+ ...account,
90
+ ...patch,
91
+ updater: user
92
+ });
93
+ context.cookies.set('xero-accounting', JSON.stringify({
94
+ ...account,
95
+ message: 'success to activate'
96
+ }), {
90
97
  httpOnly: true
91
98
  });
92
99
  context.redirect((0, shell_1.getUrlFromContext)(context, '/callback-private-xero'));
@@ -94,7 +101,10 @@ exports.xeroRouter.get('/callback-xero', async (context, next) => {
94
101
  catch (err) {
95
102
  var message = err.message;
96
103
  if (account) {
97
- context.cookies.set('xero-accounting', JSON.stringify(Object.assign(Object.assign({}, account), { message })), {
104
+ context.cookies.set('xero-accounting', JSON.stringify({
105
+ ...account,
106
+ message
107
+ }), {
98
108
  httpOnly: true
99
109
  });
100
110
  context.redirect((0, shell_1.getUrlFromContext)(context, '/callback-private-xero'));
@@ -1 +1 @@
1
- {"version":3,"file":"xero-router.js","sourceRoot":"","sources":["../../server/routers/xero-router.ts"],"names":[],"mappings":";;;;AAAA,oEAA+B;AAC/B,oEAA8B;AAE9B,yDAA0F;AAC1F,6CAA4C;AAC5C,iDAAwE;AACxE,i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yBAAiB,EAAC,OAAO,EAAE,wBAAwB,CAAC,CAAC,CAAA;IACxE,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,IAAI,OAAO,GAAW,GAAG,CAAC,OAAO,CAAA;QAEjC,IAAI,OAAO,EAAE,CAAC;YACZ,OAAO,CAAC,OAAO,CAAC,GAAG,CACjB,iBAAiB,EACjB,IAAI,CAAC,SAAS,iCACT,OAAO,KACV,OAAO,IACP,EACF;gBACE,QAAQ,EAAE,IAAI;aACf,CACF,CAAA;YAED,OAAO,CAAC,QAAQ,CAAC,IAAA,yBAAiB,EAAC,OAAO,EAAE,wBAAwB,CAAC,CAAC,CAAA;YACtE,OAAM;QACR,CAAC;QAED,MAAM,GAAG,CAAA;IACX,CAAC;AACH,CAAC,CAAC,CAAA;AAEF,kBAAU,CAAC,IAAI,CAAC,eAAe,EAAE,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE;IACvD,OAAO,CAAC,MAAM,GAAG,GAAG,CAAA;AACtB,CAAC,CAAC,CAAA","sourcesContent":["import Router from 'koa-router'\nimport fetch from 'node-fetch'\n\nimport { User, VerificationToken, VerificationTokenType } from '@things-factory/auth-base'\nimport { config } from '@things-factory/env'\nimport { getRepository, getUrlFromContext } from '@things-factory/shell'\nimport { parseJwt } from '@things-factory/utils'\n\nimport { ACCOUNTING_STATUS, AccountingAPI } from '../controllers/accounting-api'\nimport { Account } from '../entities'\n\nconst { apiKey, apiSecret, hostname, callback: redirect_uri } = config.get('accountingIntegrationXero', {})\n\nconst debug = require('debug')('things-factory:integration-accounting:xero-router')\n\nexport const xeroRouter = new Router()\n\nxeroRouter.get('/callback-xero', async (context, next) => {\n try {\n const { code, state: nonce } = context.query\n\n const verificationTokenRepository = getRepository(VerificationToken)\n\n var verification: VerificationToken = await verificationTokenRepository.findOne({\n where: {\n token: nonce,\n type: VerificationTokenType.REQUEST_ACCESS_TOKEN\n }\n })\n\n const { userId, suppliment: accountId } = verification\n\n await verificationTokenRepository.delete({\n userId,\n token: nonce,\n type: VerificationTokenType.REQUEST_ACCESS_TOKEN\n })\n\n var account = await AccountingAPI.getAccounting(accountId)\n const connectRequestData = {\n grant_type: 'authorization_code',\n code,\n redirect_uri\n }\n\n const connectResponse = await fetch(`https://identity.xero.com/connect/token`, {\n method: 'post',\n headers: {\n Authorization: `Basic ${Buffer.from(apiKey + ':' + apiSecret).toString('base64')}`,\n 'Content-Type': 'application/x-www-form-urlencoded'\n },\n body: Object.entries(connectRequestData)\n .map(([key, value]) => encodeURIComponent(key) + '=' + encodeURIComponent(value))\n .join('&')\n })\n\n if (!connectResponse.ok) {\n throw new Error(`get account information failed: ${await connectResponse.text()}`)\n }\n\n const body = await connectResponse.json()\n const {\n access_token /* token used to call the API */,\n id_token /* token containing user identity details (only returned if OpenID Connect scopes are requested) */,\n expires_in /* amount of seconds until the access token expires */,\n token_type: tokenType /* must be Bearer */,\n refresh_token\n /* token used to refresh the access token once it has expired (only returned if the offline_access scope is requested).\n */\n } = body\n\n /* decode access_token to get auth-event-id */\n const accessInfo = parseJwt(access_token)\n const { authentication_event_id, exp } = accessInfo\n\n /* get connections through the authentication_event_id */\n const connectionsResponse = await fetch(`https://api.xero.com/connections?authEventId=${authentication_event_id}`, {\n method: 'get',\n headers: {\n Authorization: `Bearer ${access_token}`,\n 'Content-Type': 'application/json'\n }\n })\n\n if (!connectionsResponse.ok) {\n context.throw(connectionsResponse.status, `get connection list failed: ${await connectionsResponse.text()}`)\n }\n\n const connections = await connectionsResponse.json()\n debug('connections', connections)\n\n var connection = connections[0]\n if (!connection) {\n context.throw(400, 'connection not found for this access token')\n }\n\n var patch = {\n name: connection.tenantName,\n accessToken: access_token,\n refreshToken: refresh_token,\n accessInfo: JSON.stringify(accessInfo, null, 2),\n accountInfo: JSON.stringify(connection, null, 2),\n accountId: connection.tenantId,\n status: ACCOUNTING_STATUS.ACTIVE,\n tokenType,\n expiresIn: new Date(exp * 1000)\n }\n\n var user = await getRepository(User).findOneBy({ id: userId })\n\n account = await getRepository(Account).save({\n ...account,\n ...patch,\n updater: user\n })\n\n context.cookies.set(\n 'xero-accounting',\n JSON.stringify({\n ...account,\n message: 'success to activate'\n }),\n {\n httpOnly: true\n }\n )\n\n context.redirect(getUrlFromContext(context, '/callback-private-xero'))\n } catch (err) {\n var message: string = err.message\n\n if (account) {\n context.cookies.set(\n 'xero-accounting',\n JSON.stringify({\n ...account,\n message\n }),\n {\n httpOnly: true\n }\n )\n\n context.redirect(getUrlFromContext(context, '/callback-private-xero'))\n return\n }\n\n throw err\n }\n})\n\nxeroRouter.post('/webhook-xero', async (context, next) => {\n context.status = 200\n})\n"]}
1
+ {"version":3,"file":"xero-router.js","sourceRoot":"","sources":["../../server/routers/xero-router.ts"],"names":[],"mappings":";;;;AAAA,oEAA+B;AAC/B,oEAA8B;AAE9B,yDAA0F;AAC1F,6CAA4C;AAC5C,iDAAwE;AACxE,iDAAgD;AAEhD,kEAAgF;AAChF,0CAAqC;AAErC,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,QAAQ,EAAE,QAAQ,EAAE,YAAY,EAAE,GAAG,YAAM,CAAC,GAAG,CAAC,2BAA2B,EAAE,EAAE,CAAC,CAAA;AAE3G,MAAM,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC,mDAAmD,CAAC,CAAA;AAEtE,QAAA,UAAU,GAAG,IAAI,oBAAM,EAAE,CAAA;AAEtC,kBAAU,CAAC,GAAG,CAAC,gBAAgB,EAAE,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE;IACvD,IAAI,CAAC;QACH,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,OAAO,CAAC,KAAK,CAAA;QAE5C,MAAM,2BAA2B,GAAG,IAAA,qBAAa,EAAC,6BAAiB,CAAC,CAAA;QAEpE,IAAI,YAAY,GAAsB,MAAM,2BAA2B,CAAC,OAAO,CAAC;YAC9E,KAAK,EAAE;gBACL,KAAK,EAAE,KAAK;gBACZ,IAA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AACtB,CAAC,CAAC,CAAA","sourcesContent":["import Router from 'koa-router'\nimport fetch from 'node-fetch'\n\nimport { User, VerificationToken, VerificationTokenType } from '@things-factory/auth-base'\nimport { config } from '@things-factory/env'\nimport { getRepository, getUrlFromContext } from '@things-factory/shell'\nimport { parseJwt } from '@things-factory/utils'\n\nimport { ACCOUNTING_STATUS, AccountingAPI } from '../controllers/accounting-api'\nimport { Account } from '../entities'\n\nconst { apiKey, apiSecret, hostname, callback: redirect_uri } = config.get('accountingIntegrationXero', {})\n\nconst debug = require('debug')('things-factory:integration-accounting:xero-router')\n\nexport const xeroRouter = new Router()\n\nxeroRouter.get('/callback-xero', async (context, next) => {\n try {\n const { code, state: nonce } = context.query\n\n const verificationTokenRepository = getRepository(VerificationToken)\n\n var verification: VerificationToken = await verificationTokenRepository.findOne({\n where: {\n token: nonce,\n type: VerificationTokenType.REQUEST_ACCESS_TOKEN\n }\n })\n\n const { userId, suppliment: accountId } = verification\n\n await verificationTokenRepository.delete({\n userId,\n token: nonce,\n type: VerificationTokenType.REQUEST_ACCESS_TOKEN\n })\n\n var account = await AccountingAPI.getAccounting(accountId)\n const connectRequestData = {\n grant_type: 'authorization_code',\n code,\n redirect_uri\n }\n\n const connectResponse = await fetch(`https://identity.xero.com/connect/token`, {\n method: 'post',\n headers: {\n Authorization: `Basic ${Buffer.from(apiKey + ':' + apiSecret).toString('base64')}`,\n 'Content-Type': 'application/x-www-form-urlencoded'\n },\n body: Object.entries(connectRequestData)\n .map(([key, value]) => encodeURIComponent(key) + '=' + encodeURIComponent(value))\n .join('&')\n })\n\n if (!connectResponse.ok) {\n throw new Error(`get account information failed: ${await connectResponse.text()}`)\n }\n\n const body = await connectResponse.json()\n const {\n access_token /* token used to call the API */,\n id_token /* token containing user identity details (only returned if OpenID Connect scopes are requested) */,\n expires_in /* amount of seconds until the access token expires */,\n token_type: tokenType /* must be Bearer */,\n refresh_token\n /* token used to refresh the access token once it has expired (only returned if the offline_access scope is requested).\n */\n } = body\n\n /* decode access_token to get auth-event-id */\n const accessInfo = parseJwt(access_token)\n const { authentication_event_id, exp } = accessInfo\n\n /* get connections through the authentication_event_id */\n const connectionsResponse = await fetch(`https://api.xero.com/connections?authEventId=${authentication_event_id}`, {\n method: 'get',\n headers: {\n Authorization: `Bearer ${access_token}`,\n 'Content-Type': 'application/json'\n }\n })\n\n if (!connectionsResponse.ok) {\n context.throw(connectionsResponse.status, `get connection list failed: ${await connectionsResponse.text()}`)\n }\n\n const connections = await connectionsResponse.json()\n debug('connections', connections)\n\n var connection = connections[0]\n if (!connection) {\n context.throw(400, 'connection not found for this access token')\n }\n\n var patch = {\n name: connection.tenantName,\n accessToken: access_token,\n refreshToken: refresh_token,\n accessInfo: JSON.stringify(accessInfo, null, 2),\n accountInfo: JSON.stringify(connection, null, 2),\n accountId: connection.tenantId,\n status: ACCOUNTING_STATUS.ACTIVE,\n tokenType,\n expiresIn: new Date(exp * 1000)\n }\n\n var user = await getRepository(User).findOneBy({ id: userId })\n\n account = await getRepository(Account).save({\n ...account,\n ...patch,\n updater: user\n })\n\n context.cookies.set(\n 'xero-accounting',\n JSON.stringify({\n ...account,\n message: 'success to activate'\n }),\n {\n httpOnly: true\n }\n )\n\n context.redirect(getUrlFromContext(context, '/callback-private-xero'))\n } catch (err) {\n var message: string = err.message\n\n if (account) {\n context.cookies.set(\n 'xero-accounting',\n JSON.stringify({\n ...account,\n message\n }),\n {\n httpOnly: true\n }\n )\n\n context.redirect(getUrlFromContext(context, '/callback-private-xero'))\n return\n }\n\n throw err\n }\n})\n\nxeroRouter.post('/webhook-xero', async (context, next) => {\n context.status = 200\n})\n"]}