@friggframework/core 2.0.0--canary.405.b81908d.0 → 2.0.0--canary.397.c07f148.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 (100) hide show
  1. package/README.md +931 -50
  2. package/core/create-handler.js +1 -0
  3. package/credential/credential-repository.js +56 -0
  4. package/credential/use-cases/get-credential-for-user.js +21 -0
  5. package/credential/use-cases/update-authentication-status.js +15 -0
  6. package/encrypt/encrypt.js +27 -4
  7. package/handlers/app-definition-loader.js +38 -0
  8. package/handlers/app-handler-helpers.js +0 -3
  9. package/handlers/backend-utils.js +42 -44
  10. package/handlers/routers/auth.js +3 -14
  11. package/handlers/routers/integration-defined-routers.js +8 -5
  12. package/handlers/routers/user.js +25 -5
  13. package/handlers/workers/integration-defined-workers.js +6 -3
  14. package/index.js +1 -16
  15. package/integrations/index.js +0 -5
  16. package/integrations/integration-base.js +142 -46
  17. package/integrations/integration-repository.js +80 -0
  18. package/integrations/integration-router.js +303 -181
  19. package/integrations/options.js +1 -1
  20. package/integrations/tests/doubles/dummy-integration-class.js +90 -0
  21. package/integrations/tests/doubles/test-integration-repository.js +89 -0
  22. package/integrations/tests/use-cases/create-integration.test.js +124 -0
  23. package/integrations/tests/use-cases/delete-integration-for-user.test.js +143 -0
  24. package/integrations/tests/use-cases/get-integration-for-user.test.js +143 -0
  25. package/integrations/tests/use-cases/get-integration-instance.test.js +169 -0
  26. package/integrations/tests/use-cases/get-integrations-for-user.test.js +169 -0
  27. package/integrations/tests/use-cases/get-possible-integrations.test.js +188 -0
  28. package/integrations/tests/use-cases/update-integration-messages.test.js +142 -0
  29. package/integrations/tests/use-cases/update-integration-status.test.js +103 -0
  30. package/integrations/tests/use-cases/update-integration.test.js +134 -0
  31. package/integrations/use-cases/create-integration.js +71 -0
  32. package/integrations/use-cases/delete-integration-for-user.js +72 -0
  33. package/integrations/use-cases/get-integration-for-user.js +78 -0
  34. package/integrations/use-cases/get-integration-instance-by-definition.js +67 -0
  35. package/integrations/use-cases/get-integration-instance.js +82 -0
  36. package/integrations/use-cases/get-integrations-for-user.js +76 -0
  37. package/integrations/use-cases/get-possible-integrations.js +27 -0
  38. package/integrations/use-cases/index.js +11 -0
  39. package/integrations/use-cases/update-integration-messages.js +31 -0
  40. package/integrations/use-cases/update-integration-status.js +28 -0
  41. package/integrations/use-cases/update-integration.js +91 -0
  42. package/integrations/utils/map-integration-dto.js +36 -0
  43. package/{module-plugin → modules}/entity.js +1 -0
  44. package/{module-plugin → modules}/index.js +0 -8
  45. package/modules/module-factory.js +54 -0
  46. package/modules/module-repository.js +107 -0
  47. package/modules/module.js +218 -0
  48. package/{module-plugin → modules}/test/mock-api/api.js +8 -3
  49. package/{module-plugin → modules}/test/mock-api/definition.js +12 -8
  50. package/modules/tests/doubles/test-module-factory.js +16 -0
  51. package/modules/tests/doubles/test-module-repository.js +19 -0
  52. package/modules/use-cases/get-entities-for-user.js +32 -0
  53. package/modules/use-cases/get-entity-options-by-id.js +58 -0
  54. package/modules/use-cases/get-entity-options-by-type.js +34 -0
  55. package/modules/use-cases/get-module-instance-from-type.js +31 -0
  56. package/modules/use-cases/get-module.js +56 -0
  57. package/modules/use-cases/process-authorization-callback.js +108 -0
  58. package/modules/use-cases/refresh-entity-options.js +58 -0
  59. package/modules/use-cases/test-module-auth.js +54 -0
  60. package/modules/utils/map-module-dto.js +18 -0
  61. package/package.json +5 -5
  62. package/syncs/sync.js +0 -1
  63. package/types/integrations/index.d.ts +2 -6
  64. package/types/module-plugin/index.d.ts +4 -56
  65. package/types/syncs/index.d.ts +0 -2
  66. package/user/tests/doubles/test-user-repository.js +72 -0
  67. package/user/tests/use-cases/create-individual-user.test.js +24 -0
  68. package/user/tests/use-cases/create-organization-user.test.js +28 -0
  69. package/user/tests/use-cases/create-token-for-user-id.test.js +19 -0
  70. package/user/tests/use-cases/get-user-from-bearer-token.test.js +64 -0
  71. package/user/tests/use-cases/login-user.test.js +140 -0
  72. package/user/use-cases/create-individual-user.js +61 -0
  73. package/user/use-cases/create-organization-user.js +47 -0
  74. package/user/use-cases/create-token-for-user-id.js +30 -0
  75. package/user/use-cases/get-user-from-bearer-token.js +77 -0
  76. package/user/use-cases/login-user.js +122 -0
  77. package/user/user-repository.js +62 -0
  78. package/user/user.js +77 -0
  79. package/handlers/routers/HEALTHCHECK.md +0 -240
  80. package/handlers/routers/health.js +0 -451
  81. package/handlers/routers/health.test.js +0 -203
  82. package/handlers/routers/middleware/loadUser.js +0 -15
  83. package/handlers/routers/middleware/requireLoggedInUser.js +0 -12
  84. package/integrations/create-frigg-backend.js +0 -31
  85. package/integrations/integration-factory.js +0 -251
  86. package/integrations/integration-user.js +0 -144
  87. package/integrations/test/integration-base.test.js +0 -144
  88. package/module-plugin/auther.js +0 -393
  89. package/module-plugin/entity-manager.js +0 -70
  90. package/module-plugin/manager.js +0 -169
  91. package/module-plugin/module-factory.js +0 -61
  92. /package/{module-plugin → modules}/ModuleConstants.js +0 -0
  93. /package/{module-plugin → modules}/credential.js +0 -0
  94. /package/{module-plugin → modules}/requester/api-key.js +0 -0
  95. /package/{module-plugin → modules}/requester/basic.js +0 -0
  96. /package/{module-plugin → modules}/requester/oauth-2.js +0 -0
  97. /package/{module-plugin → modules}/requester/requester.js +0 -0
  98. /package/{module-plugin → modules}/requester/requester.test.js +0 -0
  99. /package/{module-plugin → modules}/test/auther.test.js +0 -0
  100. /package/{module-plugin → modules}/test/mock-api/mocks/hubspot.js +0 -0
@@ -34,6 +34,7 @@ const createHandler = (optionByName = {}) => {
34
34
  // Helps mongoose reuse the connection. Lowers response times.
35
35
  context.callbackWaitsForEmptyEventLoop = false;
36
36
 
37
+ // todo: this should not be necessary anymore
37
38
  if (shouldUseDatabase) {
38
39
  await connectToDatabase();
39
40
  }
@@ -0,0 +1,56 @@
1
+ const { Credential } = require('../modules');
2
+
3
+ class CredentialRepository {
4
+ async findCredentialById(id) {
5
+ return Credential.findById(id);
6
+ }
7
+
8
+ async updateAuthenticationStatus(credentialId, authIsValid) {
9
+ return Credential.updateOne({ _id: credentialId }, { $set: { auth_is_valid: authIsValid } });
10
+ }
11
+
12
+ /**
13
+ * Permanently remove a credential document.
14
+ * @param {string} credentialId
15
+ * @returns {Promise<import('mongoose').DeleteResult>}
16
+ */
17
+ async deleteCredentialById(credentialId) {
18
+ return Credential.deleteOne({ _id: credentialId });
19
+ }
20
+
21
+ /**
22
+ * Create a new credential or update an existing one matching the identifiers.
23
+ * `credentialDetails` format: { identifiers: { ... }, details: { ... } }
24
+ * Identifiers are used in the query filter; details are merged into the document.
25
+ * @param {{identifiers: Object, details: Object}} credentialDetails
26
+ * @returns {Promise<Object>} The persisted credential (lean object)
27
+ */
28
+ async upsertCredential(credentialDetails) {
29
+ const { identifiers, details } = credentialDetails;
30
+ if (!identifiers) throw new Error('identifiers required to upsert credential');
31
+
32
+ const query = { ...identifiers };
33
+
34
+ const update = { $set: { ...details } };
35
+
36
+ const options = {
37
+ upsert: true,
38
+ new: true,
39
+ setDefaultsOnInsert: true,
40
+ lean: true,
41
+ strict: false,
42
+ };
43
+
44
+ const credential = await Credential.findOneAndUpdate(query, update, options);
45
+ return {
46
+ id: credential._id.toString(),
47
+ externalId: credential.externalId,
48
+ userId: credential.user.toString(),
49
+ auth_is_valid: credential.auth_is_valid,
50
+ access_token: credential.access_token,
51
+ refresh_token: credential.refresh_token,
52
+ }
53
+ }
54
+ }
55
+
56
+ module.exports = { CredentialRepository };
@@ -0,0 +1,21 @@
1
+ class GetCredentialForUser {
2
+ constructor({ credentialRepository }) {
3
+ this.credentialRepository = credentialRepository;
4
+ }
5
+
6
+ async execute(credentialId, userId) {
7
+ const credential = await this.credentialRepository.findCredentialById(credentialId);
8
+
9
+ if (!credential) {
10
+ throw new Error(`Credential with id ${credentialId} not found`);
11
+ }
12
+
13
+ if (credential.user.toString() !== userId.toString()) {
14
+ throw new Error(`Credential ${credentialId} does not belong to user ${userId}`);
15
+ }
16
+
17
+ return credential;
18
+ }
19
+ }
20
+
21
+ module.exports = { GetCredentialForUser };
@@ -0,0 +1,15 @@
1
+ class UpdateAuthenticationStatus {
2
+ constructor({ credentialRepository }) {
3
+ this.credentialRepository = credentialRepository;
4
+ }
5
+
6
+ /**
7
+ * @param {string} credentialId
8
+ * @param {boolean} authIsValid
9
+ */
10
+ async execute(credentialId, authIsValid) {
11
+ await this.credentialRepository.updateAuthenticationStatus(credentialId, authIsValid);
12
+ }
13
+ }
14
+
15
+ module.exports = { UpdateAuthenticationStatus };
@@ -17,6 +17,7 @@ const findOneEvents = [
17
17
  const shouldBypassEncryption = (STAGE) => {
18
18
  const defaultBypassStages = ['dev', 'test', 'local'];
19
19
  const bypassStageEnv = process.env.BYPASS_ENCRYPTION_STAGE;
20
+ // If the env is set to anything or an empty string, use the env. Otherwise, use the default array
20
21
  const useEnv = !String(bypassStageEnv) || !!bypassStageEnv;
21
22
  const bypassStages = useEnv
22
23
  ? bypassStageEnv.split(',').map((stage) => stage.trim())
@@ -24,15 +25,19 @@ const shouldBypassEncryption = (STAGE) => {
24
25
  return bypassStages.includes(STAGE);
25
26
  };
26
27
 
27
- function Encrypt(schema) {
28
+ // The Mongoose plug-in function
29
+ function Encrypt(schema, options) {
28
30
  const { STAGE, KMS_KEY_ARN, AES_KEY_ID } = process.env;
29
31
 
30
32
  if (shouldBypassEncryption(STAGE)) {
31
33
  return;
32
34
  }
33
35
 
34
- const hasAES = AES_KEY_ID && AES_KEY_ID.trim() !== '';
35
- const hasKMS = KMS_KEY_ARN && KMS_KEY_ARN.trim() !== '' && !hasAES;
36
+ if (KMS_KEY_ARN && AES_KEY_ID) {
37
+ throw new Error(
38
+ 'Local and AWS encryption keys are both set in the environment.'
39
+ );
40
+ }
36
41
 
37
42
  const fields = Object.values(schema.paths)
38
43
  .map(({ path, options }) => (options.lhEncrypt === true ? path : ''))
@@ -43,17 +48,25 @@ function Encrypt(schema) {
43
48
  }
44
49
 
45
50
  const cryptor = new Cryptor({
46
- shouldUseAws: hasKMS,
51
+ // Use AWS if the CMK is present
52
+ shouldUseAws: !!KMS_KEY_ARN,
53
+ // Find all the fields in the schema with lhEncrypt === true
47
54
  fields: fields,
48
55
  });
49
56
 
57
+ // ---------------------------------------------
58
+ // ### Encrypt fields before save/update/insert.
59
+ // ---------------------------------------------
60
+
50
61
  schema.pre('save', async function encryptionPreSave() {
62
+ // `this` will be a doc
51
63
  await cryptor.encryptFieldsInDocuments([this]);
52
64
  });
53
65
 
54
66
  schema.pre(
55
67
  'insertMany',
56
68
  async function encryptionPreInsertMany(_, docs, options) {
69
+ // `this` will be the model
57
70
  if (options?.rawResult) {
58
71
  throw new Error(
59
72
  'Raw result not supported for insertMany with Encrypt plugin'
@@ -65,14 +78,17 @@ function Encrypt(schema) {
65
78
  );
66
79
 
67
80
  schema.pre(updateOneEvents, async function encryptionPreUpdateOne() {
81
+ // `this` will be a query
68
82
  await cryptor.encryptFieldsInQuery(this);
69
83
  });
70
84
 
71
85
  schema.pre('updateMany', async function encryptionPreUpdateMany() {
86
+ // `this` will be a query
72
87
  cryptor.expectNotToUpdateManyEncrypted(this.getUpdate());
73
88
  });
74
89
 
75
90
  schema.pre('update', async function encryptionPreUpdate() {
91
+ // `this` will be a query
76
92
  const { multiple } = this.getOptions();
77
93
 
78
94
  if (multiple) {
@@ -83,11 +99,16 @@ function Encrypt(schema) {
83
99
  await cryptor.encryptFieldsInQuery(this);
84
100
  });
85
101
 
102
+ // --------------------------------------------
103
+ // ### Decrypt documents after they are loaded.
104
+ // --------------------------------------------
86
105
  schema.post('save', async function encryptionPreSave() {
106
+ // `this` will be a doc
87
107
  await cryptor.decryptFieldsInDocuments([this]);
88
108
  });
89
109
 
90
110
  schema.post(findOneEvents, async function encryptionPostFindOne(doc) {
111
+ // `this` will be a query
91
112
  const { rawResult } = this.getOptions();
92
113
 
93
114
  if (rawResult) {
@@ -98,10 +119,12 @@ function Encrypt(schema) {
98
119
  });
99
120
 
100
121
  schema.post('find', async function encryptionPostFind(docs) {
122
+ // `this` will be a query
101
123
  await cryptor.decryptFieldsInDocuments(docs);
102
124
  });
103
125
 
104
126
  schema.post('insertMany', async function encryptionPostInsertMany(docs) {
127
+ // `this` will be the model
105
128
  await cryptor.decryptFieldsInDocuments(docs);
106
129
  });
107
130
  }
@@ -0,0 +1,38 @@
1
+ const { findNearestBackendPackageJson } = require('@friggframework/core/utils');
2
+ const path = require('node:path');
3
+ const fs = require('fs-extra');
4
+
5
+ /**
6
+ * Loads the App definition from the nearest backend package
7
+ * @function loadAppDefinition
8
+ * @description Searches for the nearest backend package.json, loads the corresponding index.js file,
9
+ * and extracts the application definition containing integrations and user configuration.
10
+ * @returns {{integrations: Array<object>, userConfig: object | null}} An object containing the application definition.
11
+ * @throws {Error} Throws error if backend package.json cannot be found.
12
+ * @throws {Error} Throws error if index.js file cannot be found in the backend directory.
13
+ * @example
14
+ * const { integrations, userConfig } = loadAppDefinition();
15
+ * console.log(`Found ${integrations.length} integrations`);
16
+ */
17
+ function loadAppDefinition() {
18
+ const backendPath = findNearestBackendPackageJson();
19
+ if (!backendPath) {
20
+ throw new Error('Could not find backend package.json');
21
+ }
22
+
23
+ const backendDir = path.dirname(backendPath);
24
+ const backendFilePath = path.join(backendDir, 'index.js');
25
+ if (!fs.existsSync(backendFilePath)) {
26
+ throw new Error('Could not find index.js');
27
+ }
28
+
29
+ const backendJsFile = require(backendFilePath);
30
+ const appDefinition = backendJsFile.Definition;
31
+
32
+ const { integrations = [], user: userConfig = null } = appDefinition;
33
+ return { integrations, userConfig };
34
+ }
35
+
36
+ module.exports = {
37
+ loadAppDefinition,
38
+ };
@@ -3,7 +3,6 @@ const express = require('express');
3
3
  const bodyParser = require('body-parser');
4
4
  const cors = require('cors');
5
5
  const Boom = require('@hapi/boom');
6
- const loadUserManager = require('./routers/middleware/loadUser');
7
6
  const serverlessHttp = require('serverless-http');
8
7
 
9
8
  const createApp = (applyMiddleware) => {
@@ -20,8 +19,6 @@ const createApp = (applyMiddleware) => {
20
19
  })
21
20
  );
22
21
 
23
- app.use(loadUserManager);
24
-
25
22
  if (applyMiddleware) applyMiddleware(app);
26
23
 
27
24
  // Handle sending error response and logging server errors to console
@@ -1,36 +1,35 @@
1
- const { createFriggBackend, Worker } = require('@friggframework/core');
2
- const { findNearestBackendPackageJson } = require('@friggframework/core/utils');
3
- const path = require('node:path');
4
- const fs = require('fs-extra');
5
-
6
- const backendPath = findNearestBackendPackageJson();
7
- if (!backendPath) {
8
- throw new Error('Could not find backend package.json');
9
- }
10
-
11
- const backendDir = path.dirname(backendPath);
12
- const backendFilePath = path.join(backendDir, 'index.js');
13
- if (!fs.existsSync(backendFilePath)) {
14
- throw new Error('Could not find index.js');
15
- }
16
-
17
- const backendJsFile = require(backendFilePath);
18
1
  const { Router } = require('express');
19
- const appDefinition = backendJsFile.Definition;
2
+ const { Worker } = require('@friggframework/core');
3
+ const { IntegrationRepository } = require('../integrations/integration-repository');
4
+ const { ModuleFactory } = require('../modules/module-factory');
5
+ const { getModulesDefinitionFromIntegrationClasses } = require('../integrations/utils/map-integration-dto');
6
+ const { ModuleRepository } = require('../modules/module-repository');
7
+ const { GetIntegrationInstanceByDefinition } = require('../integrations/use-cases/get-integration-instance-by-definition');
20
8
 
21
- const backend = createFriggBackend(appDefinition);
22
9
  const loadRouterFromObject = (IntegrationClass, routerObject) => {
10
+
11
+ const integrationRepository = new IntegrationRepository();
12
+ const moduleRepository = new ModuleRepository();
13
+ const moduleFactory = new ModuleFactory({
14
+ moduleRepository,
15
+ moduleDefinitions: getModulesDefinitionFromIntegrationClasses([IntegrationClass]),
16
+ });
23
17
  const router = Router();
24
18
  const { path, method, event } = routerObject;
19
+
25
20
  console.log(
26
21
  `Registering ${method} ${path} for ${IntegrationClass.Definition.name}`
27
22
  );
23
+
28
24
  router[method.toLowerCase()](path, async (req, res, next) => {
29
25
  try {
30
- const integration = new IntegrationClass({});
31
- await integration.loadModules();
32
- await integration.registerEventHandlers();
33
- const result = await integration.send(event, {req, res, next});
26
+ const getIntegrationInstanceByDefinition = new GetIntegrationInstanceByDefinition({
27
+ integrationRepository,
28
+ moduleFactory,
29
+ moduleRepository,
30
+ });
31
+ const integration = await getIntegrationInstanceByDefinition.execute(IntegrationClass);
32
+ const result = await integration.send(event, { req, res, next });
34
33
  res.json(result);
35
34
  } catch (error) {
36
35
  next(error);
@@ -39,29 +38,29 @@ const loadRouterFromObject = (IntegrationClass, routerObject) => {
39
38
 
40
39
  return router;
41
40
  };
42
- const createQueueWorker = (integrationClass, integrationFactory) => {
41
+
42
+ //todo: this should be in a use case class
43
+ const createQueueWorker = (integrationClass) => {
43
44
  class QueueWorker extends Worker {
45
+
46
+ integrationRepository = new IntegrationRepository();
47
+ moduleRepository = new ModuleRepository();
48
+ moduleFactory = new ModuleFactory({
49
+ moduleRepository: this.moduleRepository,
50
+ moduleDefinitions: getModulesDefinitionFromIntegrationClasses([integrationClass]),
51
+ });
52
+
44
53
  async _run(params, context) {
45
54
  try {
46
- let instance;
47
- if (!params.integrationId) {
48
- instance = new integrationClass({});
49
- await instance.loadModules();
50
- // await instance.loadUserActions();
51
- await instance.registerEventHandlers();
52
- console.log(
53
- `${params.event} for ${integrationClass.Definition.name} integration with no integrationId`
54
- );
55
- } else {
56
- instance =
57
- await integrationFactory.getInstanceFromIntegrationId({
58
- integrationId: params.integrationId,
59
- });
60
- console.log(
61
- `${params.event} for ${instance.record.config.type} of integrationId: ${params.integrationId}`
62
- );
63
- }
64
- const res = await instance.send(params.event, {
55
+ const getIntegrationInstanceByDefinition = new GetIntegrationInstanceByDefinition({
56
+ integrationRepository: this.integrationRepository,
57
+ moduleFactory: this.moduleFactory,
58
+ moduleRepository: this.moduleRepository,
59
+ });
60
+
61
+ const integration = await getIntegrationInstanceByDefinition.execute(integrationClass);
62
+
63
+ const res = await integration.send(params.event, {
65
64
  data: params.data,
66
65
  context,
67
66
  });
@@ -79,7 +78,6 @@ const createQueueWorker = (integrationClass, integrationFactory) => {
79
78
  };
80
79
 
81
80
  module.exports = {
82
- ...backend,
83
81
  loadRouterFromObject,
84
82
  createQueueWorker,
85
83
  };
@@ -1,26 +1,15 @@
1
1
  const { createIntegrationRouter } = require('@friggframework/core');
2
2
  const { createAppHandler } = require('./../app-handler-helpers');
3
- const { requireLoggedInUser } = require('./middleware/requireLoggedInUser');
4
- const {
5
- moduleFactory,
6
- integrationFactory,
7
- IntegrationHelper,
8
- } = require('./../backend-utils');
9
3
 
10
- const router = createIntegrationRouter({
11
- factory: { moduleFactory, integrationFactory, IntegrationHelper },
12
- requireLoggedInUser,
13
- getUserId: (req) => req.user.getUserId(),
14
- });
4
+ const router = createIntegrationRouter();
15
5
 
16
6
  router.route('/redirect/:appId').get((req, res) => {
17
7
  res.redirect(
18
- `${process.env.FRONTEND_URI}/redirect/${
19
- req.params.appId
8
+ `${process.env.FRONTEND_URI}/redirect/${req.params.appId
20
9
  }?${new URLSearchParams(req.query)}`
21
10
  );
22
11
  });
23
12
 
24
13
  const handler = createAppHandler('HTTP Event: Auth', router);
25
14
 
26
- module.exports = { handler, router };
15
+ module.exports = { handler };
@@ -1,15 +1,18 @@
1
1
  const { createAppHandler } = require('./../app-handler-helpers');
2
2
  const {
3
- integrationFactory,
4
- loadRouterFromObject,
5
- } = require('./../backend-utils');
3
+ loadAppDefinition,
4
+ } = require('../app-definition-loader');
6
5
  const { Router } = require('express');
6
+ const { loadRouterFromObject } = require('../backend-utils');
7
7
 
8
8
  const handlers = {};
9
- for (const IntegrationClass of integrationFactory.integrationClasses) {
9
+ const { integrations: integrationClasses } = loadAppDefinition();
10
+
11
+ //todo: this should be in a use case class
12
+ for (const IntegrationClass of integrationClasses) {
10
13
  const router = Router();
11
14
  const basePath = `/api/${IntegrationClass.Definition.name}-integration`;
12
-
15
+
13
16
  console.log(`\n│ Configuring routes for ${IntegrationClass.Definition.name} Integration:`);
14
17
 
15
18
  for (const routeDef of IntegrationClass.Definition.routes) {
@@ -1,10 +1,29 @@
1
1
  const express = require('express');
2
2
  const { createAppHandler } = require('../app-handler-helpers');
3
3
  const { checkRequiredParams } = require('@friggframework/core');
4
- const { User } = require('../backend-utils');
4
+ const { UserRepository } = require('../../user/user-repository');
5
+ const {
6
+ CreateIndividualUser,
7
+ } = require('../../user/use-cases/create-individual-user');
8
+ const { LoginUser } = require('../../user/use-cases/login-user');
9
+ const {
10
+ CreateTokenForUserId,
11
+ } = require('../../user/use-cases/create-token-for-user-id');
5
12
  const catchAsyncError = require('express-async-handler');
13
+ const { loadAppDefinition } = require('../app-definition-loader');
6
14
 
7
15
  const router = express();
16
+ const { userConfig } = loadAppDefinition();
17
+ const userRepository = new UserRepository({ userConfig });
18
+ const createIndividualUser = new CreateIndividualUser({
19
+ userRepository,
20
+ userConfig,
21
+ });
22
+ const loginUser = new LoginUser({
23
+ userRepository,
24
+ userConfig,
25
+ });
26
+ const createTokenForUserId = new CreateTokenForUserId({ userRepository });
8
27
 
9
28
  // define the login endpoint
10
29
  router.route('/user/login').post(
@@ -13,8 +32,8 @@ router.route('/user/login').post(
13
32
  'username',
14
33
  'password',
15
34
  ]);
16
- const user = await User.loginUser({ username, password });
17
- const token = await user.createUserToken(120);
35
+ const user = await loginUser.execute({ username, password });
36
+ const token = await createTokenForUserId.execute(user.getId(), 120);
18
37
  res.status(201);
19
38
  res.json({ token });
20
39
  })
@@ -26,11 +45,12 @@ router.route('/user/create').post(
26
45
  'username',
27
46
  'password',
28
47
  ]);
29
- const user = await User.createIndividualUser({
48
+
49
+ const user = await createIndividualUser.execute({
30
50
  username,
31
51
  password,
32
52
  });
33
- const token = await user.createUserToken(120);
53
+ const token = await createTokenForUserId.execute(user.getId(), 120);
34
54
  res.status(201);
35
55
  res.json({ token });
36
56
  })
@@ -1,9 +1,12 @@
1
1
  const { createHandler } = require('@friggframework/core');
2
- const { integrationFactory, createQueueWorker } = require('../backend-utils');
2
+ const { loadAppDefinition } = require('../app-definition-loader');
3
+ const { createQueueWorker } = require('../backend-utils');
3
4
 
4
5
  const handlers = {};
5
- integrationFactory.integrationClasses.forEach((IntegrationClass) => {
6
- const defaultQueueWorker = createQueueWorker(IntegrationClass, integrationFactory);
6
+ const { integrations: integrationClasses } = loadAppDefinition();
7
+
8
+ integrationClasses.forEach((IntegrationClass) => {
9
+ const defaultQueueWorker = createQueueWorker(IntegrationClass);
7
10
 
8
11
  handlers[`${IntegrationClass.Definition.name}`] = {
9
12
  queueWorker: createHandler({
package/index.js CHANGED
@@ -38,27 +38,20 @@ const {
38
38
  IntegrationModel,
39
39
  Options,
40
40
  IntegrationMapping,
41
- IntegrationFactory,
42
- IntegrationHelper,
43
41
  createIntegrationRouter,
44
42
  checkRequiredParams,
45
- createFriggBackend,
46
43
  } = require('./integrations/index');
47
44
  const { TimeoutCatcher } = require('./lambda/index');
48
45
  const { debug, initDebugLog, flushDebugLog } = require('./logs/index');
49
46
  const {
50
47
  Credential,
51
- EntityManager,
52
48
  Entity,
53
- ModuleManager,
54
49
  ApiKeyRequester,
55
50
  BasicAuthRequester,
56
51
  OAuth2Requester,
57
52
  Requester,
58
53
  ModuleConstants,
59
- ModuleFactory,
60
- Auther,
61
- } = require('./module-plugin/index');
54
+ } = require('./modules/index');
62
55
  const utils = require('./utils');
63
56
 
64
57
  // const {Sync } = require('./syncs/model');
@@ -109,11 +102,8 @@ module.exports = {
109
102
  IntegrationModel,
110
103
  Options,
111
104
  IntegrationMapping,
112
- IntegrationFactory,
113
- IntegrationHelper,
114
105
  checkRequiredParams,
115
106
  createIntegrationRouter,
116
- createFriggBackend,
117
107
 
118
108
  // lambda
119
109
  TimeoutCatcher,
@@ -125,17 +115,12 @@ module.exports = {
125
115
 
126
116
  // module plugin
127
117
  Credential,
128
- EntityManager,
129
118
  Entity,
130
- ModuleManager,
131
119
  ApiKeyRequester,
132
120
  BasicAuthRequester,
133
121
  OAuth2Requester,
134
122
  Requester,
135
123
  ModuleConstants,
136
- ModuleFactory,
137
- Auther,
138
-
139
124
  // queues
140
125
  QueuerUtil,
141
126
 
@@ -2,18 +2,13 @@ const { IntegrationBase } = require('./integration-base');
2
2
  const { IntegrationModel } = require('./integration-model');
3
3
  const { Options } = require('./options');
4
4
  const { IntegrationMapping } = require('./integration-mapping');
5
- const { IntegrationFactory, IntegrationHelper } = require('./integration-factory');
6
5
  const { createIntegrationRouter, checkRequiredParams } = require('./integration-router');
7
- const { createFriggBackend } = require('./create-frigg-backend');
8
6
 
9
7
  module.exports = {
10
8
  IntegrationBase,
11
9
  IntegrationModel,
12
10
  Options,
13
11
  IntegrationMapping,
14
- IntegrationFactory,
15
- IntegrationHelper,
16
12
  createIntegrationRouter,
17
13
  checkRequiredParams,
18
- createFriggBackend
19
14
  };