@friggframework/core 2.0.0--canary.398.a314355.0 → 2.0.0--canary.397.4957a89.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.
- package/README.md +931 -50
- package/core/create-handler.js +1 -0
- package/credential/credential-repository.js +42 -0
- package/credential/use-cases/get-credential-for-user.js +21 -0
- package/credential/use-cases/update-authentication-status.js +15 -0
- package/database/models/WebsocketConnection.js +0 -5
- package/handlers/app-definition-loader.js +38 -0
- package/handlers/app-handler-helpers.js +0 -3
- package/handlers/backend-utils.js +35 -34
- package/handlers/routers/auth.js +3 -14
- package/handlers/routers/integration-defined-routers.js +8 -5
- package/handlers/routers/user.js +25 -5
- package/handlers/workers/integration-defined-workers.js +6 -3
- package/index.js +1 -16
- package/integrations/index.js +0 -5
- package/integrations/integration-base.js +42 -44
- package/integrations/integration-repository.js +67 -0
- package/integrations/integration-router.js +301 -178
- package/integrations/integration.js +233 -0
- package/integrations/options.js +1 -1
- package/integrations/tests/doubles/dummy-integration-class.js +90 -0
- package/integrations/tests/doubles/test-integration-repository.js +89 -0
- package/integrations/tests/use-cases/create-integration.test.js +124 -0
- package/integrations/tests/use-cases/delete-integration-for-user.test.js +143 -0
- package/integrations/tests/use-cases/get-integration-for-user.test.js +143 -0
- package/integrations/tests/use-cases/get-integration-instance.test.js +169 -0
- package/integrations/tests/use-cases/get-integrations-for-user.test.js +169 -0
- package/integrations/tests/use-cases/get-possible-integrations.test.js +188 -0
- package/integrations/tests/use-cases/update-integration-messages.test.js +142 -0
- package/integrations/tests/use-cases/update-integration-status.test.js +103 -0
- package/integrations/tests/use-cases/update-integration.test.js +134 -0
- package/integrations/use-cases/create-integration.js +72 -0
- package/integrations/use-cases/delete-integration-for-user.js +73 -0
- package/integrations/use-cases/get-integration-for-user.js +79 -0
- package/integrations/use-cases/get-integration-instance.js +84 -0
- package/integrations/use-cases/get-integrations-for-user.js +77 -0
- package/integrations/use-cases/get-possible-integrations.js +27 -0
- package/integrations/use-cases/index.js +11 -0
- package/integrations/use-cases/update-integration-messages.js +31 -0
- package/integrations/use-cases/update-integration-status.js +28 -0
- package/integrations/use-cases/update-integration.js +92 -0
- package/integrations/utils/map-integration-dto.js +36 -0
- package/{module-plugin → modules}/index.js +0 -8
- package/modules/module-factory.js +54 -0
- package/modules/module-repository.js +107 -0
- package/modules/module.js +221 -0
- package/{module-plugin → modules}/test/mock-api/api.js +8 -3
- package/{module-plugin → modules}/test/mock-api/definition.js +12 -8
- package/modules/tests/doubles/test-module-factory.js +16 -0
- package/modules/tests/doubles/test-module-repository.js +19 -0
- package/modules/use-cases/get-entities-for-user.js +32 -0
- package/modules/use-cases/get-entity-options-by-id.js +58 -0
- package/modules/use-cases/get-entity-options-by-type.js +34 -0
- package/modules/use-cases/get-module-instance-from-type.js +31 -0
- package/modules/use-cases/get-module.js +56 -0
- package/modules/use-cases/process-authorization-callback.js +114 -0
- package/modules/use-cases/refresh-entity-options.js +58 -0
- package/modules/use-cases/test-module-auth.js +54 -0
- package/modules/utils/map-module-dto.js +18 -0
- package/package.json +5 -5
- package/syncs/sync.js +0 -1
- package/types/integrations/index.d.ts +2 -6
- package/types/module-plugin/index.d.ts +4 -56
- package/types/syncs/index.d.ts +0 -2
- package/user/tests/doubles/test-user-repository.js +72 -0
- package/user/tests/use-cases/create-individual-user.test.js +24 -0
- package/user/tests/use-cases/create-organization-user.test.js +28 -0
- package/user/tests/use-cases/create-token-for-user-id.test.js +19 -0
- package/user/tests/use-cases/get-user-from-bearer-token.test.js +64 -0
- package/user/tests/use-cases/login-user.test.js +140 -0
- package/user/use-cases/create-individual-user.js +61 -0
- package/user/use-cases/create-organization-user.js +47 -0
- package/user/use-cases/create-token-for-user-id.js +30 -0
- package/user/use-cases/get-user-from-bearer-token.js +77 -0
- package/user/use-cases/login-user.js +122 -0
- package/user/user-repository.js +62 -0
- package/user/user.js +77 -0
- package/handlers/routers/middleware/loadUser.js +0 -15
- package/handlers/routers/middleware/requireLoggedInUser.js +0 -12
- package/integrations/create-frigg-backend.js +0 -31
- package/integrations/integration-factory.js +0 -251
- package/integrations/integration-user.js +0 -144
- package/integrations/test/integration-base.test.js +0 -144
- package/module-plugin/auther.js +0 -393
- package/module-plugin/entity-manager.js +0 -70
- package/module-plugin/manager.js +0 -169
- package/module-plugin/module-factory.js +0 -61
- /package/{module-plugin → modules}/ModuleConstants.js +0 -0
- /package/{module-plugin → modules}/credential.js +0 -0
- /package/{module-plugin → modules}/entity.js +0 -0
- /package/{module-plugin → modules}/requester/api-key.js +0 -0
- /package/{module-plugin → modules}/requester/basic.js +0 -0
- /package/{module-plugin → modules}/requester/oauth-2.js +0 -0
- /package/{module-plugin → modules}/requester/requester.js +0 -0
- /package/{module-plugin → modules}/requester/requester.test.js +0 -0
- /package/{module-plugin → modules}/test/auther.test.js +0 -0
- /package/{module-plugin → modules}/test/mock-api/mocks/hubspot.js +0 -0
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
const { Delegate } = require('../core');
|
|
2
|
+
const _ = require('lodash');
|
|
3
|
+
const { flushDebugLog } = require('../logs');
|
|
4
|
+
const { ModuleConstants } = require('./ModuleConstants');
|
|
5
|
+
|
|
6
|
+
// todo: this class should be a Domain class, and the Delegate function is preventing us from
|
|
7
|
+
// doing that, we probably have to get rid of the Delegate class as well as the event based
|
|
8
|
+
// calls since they go against the Domain Driven Design principles (eg. a domain class should not call repository methods or use cases)
|
|
9
|
+
class Module extends Delegate {
|
|
10
|
+
|
|
11
|
+
//todo: entity should be replaced with actual entity properties
|
|
12
|
+
/**
|
|
13
|
+
*
|
|
14
|
+
* @param {Object} params
|
|
15
|
+
* @param {Object} params.definition The definition of the Api Module
|
|
16
|
+
* @param {string} params.userId The user id
|
|
17
|
+
* @param {Object} params.entity The entity record from the database
|
|
18
|
+
*/
|
|
19
|
+
constructor({ definition, userId = null, entity: entityObj = null }) {
|
|
20
|
+
super({ definition, userId, entity: entityObj });
|
|
21
|
+
|
|
22
|
+
this.validateDefinition(definition);
|
|
23
|
+
|
|
24
|
+
this.userId = userId;
|
|
25
|
+
this.entity = entityObj;
|
|
26
|
+
this.credential = entityObj?.credential;
|
|
27
|
+
this.definition = definition;
|
|
28
|
+
this.name = this.definition.moduleName;
|
|
29
|
+
this.modelName = this.definition.modelName;
|
|
30
|
+
this.apiClass = this.definition.API;
|
|
31
|
+
|
|
32
|
+
// Repository used for persistence operations related to credentials.
|
|
33
|
+
const { CredentialRepository } = require('../credential/credential-repository');
|
|
34
|
+
this.credentialRepository = new CredentialRepository();
|
|
35
|
+
|
|
36
|
+
// Repository responsible for entity persistence actions
|
|
37
|
+
const { ModuleRepository } = require('./module-repository');
|
|
38
|
+
this.moduleRepository = new ModuleRepository();
|
|
39
|
+
|
|
40
|
+
Object.assign(this, this.definition.requiredAuthMethods);
|
|
41
|
+
|
|
42
|
+
const apiParams = {
|
|
43
|
+
...this.definition.env,
|
|
44
|
+
delegate: this,
|
|
45
|
+
...this.apiParamsFromCredential(this.credential),
|
|
46
|
+
...this.apiParamsFromEntity(this.entity),
|
|
47
|
+
};
|
|
48
|
+
this.api = new this.apiClass(apiParams);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
getName() {
|
|
52
|
+
return this.name;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
getEntityOptions() {
|
|
56
|
+
return this.definition.getEntityOptions()
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async refreshEntityOptions(options) {
|
|
60
|
+
await this.definition.refreshEntityOptions(options);
|
|
61
|
+
return this.getEntityOptions();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
apiParamsFromCredential(credential) {
|
|
65
|
+
return _.pick(credential, ...this.apiPropertiesToPersist?.credential);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
apiParamsFromEntity(entity) {
|
|
69
|
+
return _.pick(entity, ...this.apiPropertiesToPersist?.entity);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async validateAuthorizationRequirements() {
|
|
73
|
+
const requirements = await this.getAuthorizationRequirements();
|
|
74
|
+
let valid = true;
|
|
75
|
+
if (
|
|
76
|
+
['oauth1', 'oauth2'].includes(requirements.type) &&
|
|
77
|
+
!requirements.url
|
|
78
|
+
) {
|
|
79
|
+
valid = false;
|
|
80
|
+
}
|
|
81
|
+
return valid;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async getAuthorizationRequirements(params) {
|
|
85
|
+
return this.api.getAuthorizationRequirements();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async testAuth() {
|
|
89
|
+
let validAuth = false;
|
|
90
|
+
try {
|
|
91
|
+
if (await this.testAuthRequest(this.api)) validAuth = true;
|
|
92
|
+
} catch (e) {
|
|
93
|
+
flushDebugLog(e);
|
|
94
|
+
}
|
|
95
|
+
return validAuth;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async onTokenUpdate() {
|
|
99
|
+
const credentialDetails = await this.getCredentialDetails(
|
|
100
|
+
this.api,
|
|
101
|
+
this.userId
|
|
102
|
+
);
|
|
103
|
+
Object.assign(
|
|
104
|
+
credentialDetails.details,
|
|
105
|
+
this.apiParamsFromCredential(this.api)
|
|
106
|
+
);
|
|
107
|
+
credentialDetails.details.auth_is_valid = true;
|
|
108
|
+
|
|
109
|
+
const persisted = await this.credentialRepository.upsertCredential(credentialDetails);
|
|
110
|
+
this.credential = persisted;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async receiveNotification(notifier, delegateString, object = null) {
|
|
114
|
+
if (delegateString === this.api.DLGT_TOKEN_UPDATE) {
|
|
115
|
+
await this.onTokenUpdate();
|
|
116
|
+
} else if (delegateString === this.api.DLGT_TOKEN_DEAUTHORIZED) {
|
|
117
|
+
await this.deauthorize();
|
|
118
|
+
} else if (delegateString === this.api.DLGT_INVALID_AUTH) {
|
|
119
|
+
await this.markCredentialsInvalid();
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
async markCredentialsInvalid() {
|
|
124
|
+
if (!this.credential) return;
|
|
125
|
+
|
|
126
|
+
// Persist flag change through repository – works even when the
|
|
127
|
+
// credential object is a plain JavaScript object (lean query).
|
|
128
|
+
const credentialId = this.credential._id || this.credential.id;
|
|
129
|
+
if (!credentialId) return;
|
|
130
|
+
|
|
131
|
+
await this.credentialRepository.updateAuthenticationStatus(
|
|
132
|
+
credentialId,
|
|
133
|
+
false
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
// Keep the in-memory snapshot consistent so that callers can read the
|
|
137
|
+
// updated state without another fetch.
|
|
138
|
+
this.credential.auth_is_valid = false;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async deauthorize() {
|
|
142
|
+
this.api = new this.apiClass();
|
|
143
|
+
|
|
144
|
+
// Remove persisted credential (if any)
|
|
145
|
+
if (this.entity?.credential) {
|
|
146
|
+
const credentialId =
|
|
147
|
+
this.entity.credential._id || this.entity.credential.id || this.entity.credential;
|
|
148
|
+
|
|
149
|
+
// Delete credential via repository
|
|
150
|
+
await this.credentialRepository.deleteCredentialById(credentialId);
|
|
151
|
+
|
|
152
|
+
// Unset credential reference on the Entity document
|
|
153
|
+
const entityId = this.entity._id || this.entity.id;
|
|
154
|
+
if (entityId) {
|
|
155
|
+
await this.moduleRepository.unsetCredential(entityId);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Keep in-memory snapshot consistent
|
|
159
|
+
this.entity.credential = undefined;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// todo: check if all these props are still up to date
|
|
164
|
+
validateDefinition(definition) {
|
|
165
|
+
if (!definition) {
|
|
166
|
+
throw new Error('Module definition is required');
|
|
167
|
+
}
|
|
168
|
+
if (!definition.moduleName) {
|
|
169
|
+
throw new Error('Module definition requires moduleName');
|
|
170
|
+
}
|
|
171
|
+
if (!definition.API) {
|
|
172
|
+
throw new Error('Module definition requires API class');
|
|
173
|
+
}
|
|
174
|
+
if (!definition.requiredAuthMethods) {
|
|
175
|
+
throw new Error('Module definition requires requiredAuthMethods');
|
|
176
|
+
} else {
|
|
177
|
+
if (
|
|
178
|
+
definition.API.requesterType ===
|
|
179
|
+
ModuleConstants.authType.oauth2 &&
|
|
180
|
+
!definition.requiredAuthMethods.getToken
|
|
181
|
+
) {
|
|
182
|
+
throw new Error(
|
|
183
|
+
'Module definition requires requiredAuthMethods.getToken'
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
if (!definition.requiredAuthMethods.getEntityDetails) {
|
|
187
|
+
throw new Error(
|
|
188
|
+
'Module definition requires requiredAuthMethods.getEntityDetails'
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
if (!definition.requiredAuthMethods.getCredentialDetails) {
|
|
192
|
+
throw new Error(
|
|
193
|
+
'Module definition requires requiredAuthMethods.getCredentialDetails'
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
if (!definition.requiredAuthMethods.apiPropertiesToPersist) {
|
|
197
|
+
throw new Error(
|
|
198
|
+
'Module definition requires requiredAuthMethods.apiPropertiesToPersist'
|
|
199
|
+
);
|
|
200
|
+
} else if (definition.Credential) {
|
|
201
|
+
for (const prop of definition.requiredAuthMethods
|
|
202
|
+
.apiPropertiesToPersist?.credential) {
|
|
203
|
+
if (
|
|
204
|
+
!definition.Credential.schema.paths.hasOwnProperty(prop)
|
|
205
|
+
) {
|
|
206
|
+
throw new Error(
|
|
207
|
+
`Module definition requires Credential schema to have property ${prop}`
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
if (!definition.requiredAuthMethods.testAuthRequest) {
|
|
213
|
+
throw new Error(
|
|
214
|
+
'Module definition requires requiredAuthMethods.testAuth'
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
module.exports = { Module };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
const { get } = require('
|
|
2
|
-
const { OAuth2Requester } = require('
|
|
1
|
+
const { get } = require('../../../assertions');
|
|
2
|
+
const { OAuth2Requester } = require('../..');
|
|
3
3
|
|
|
4
4
|
class Api extends OAuth2Requester {
|
|
5
5
|
constructor(params) {
|
|
@@ -23,7 +23,12 @@ class Api extends OAuth2Requester {
|
|
|
23
23
|
return this.authorizationUri;
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
|
|
26
|
+
getAuthorizationRequirements() {
|
|
27
|
+
return {
|
|
28
|
+
url: this.getAuthUri(),
|
|
29
|
+
type: 'oauth2',
|
|
30
|
+
};
|
|
31
|
+
}
|
|
27
32
|
}
|
|
28
33
|
|
|
29
34
|
module.exports = { Api };
|
|
@@ -1,19 +1,23 @@
|
|
|
1
1
|
require('dotenv').config();
|
|
2
|
-
const {Api} = require('./api');
|
|
3
|
-
const {get} = require('
|
|
4
|
-
const config = {name: 'anapi'}
|
|
2
|
+
const { Api } = require('./api');
|
|
3
|
+
const { get } = require('../../../assertions');
|
|
4
|
+
const config = { name: 'anapi' }
|
|
5
5
|
|
|
6
6
|
const Definition = {
|
|
7
7
|
API: Api,
|
|
8
|
-
|
|
8
|
+
getAuthorizationRequirements: () => ({
|
|
9
|
+
url: 'http://localhost:3000/redirect/anapi',
|
|
10
|
+
type: 'oauth2',
|
|
11
|
+
}),
|
|
12
|
+
getName: function () { return config.name },
|
|
9
13
|
moduleName: config.name,
|
|
10
14
|
modelName: 'AnApi',
|
|
11
15
|
requiredAuthMethods: {
|
|
12
|
-
getToken: async function(api, params){
|
|
16
|
+
getToken: async function (api, params) {
|
|
13
17
|
const code = get(params.data, 'code');
|
|
14
18
|
return api.getTokenFromCode(code);
|
|
15
19
|
},
|
|
16
|
-
getEntityDetails: async function(api, callbackParams, tokenResponse, userId) {
|
|
20
|
+
getEntityDetails: async function (api, callbackParams, tokenResponse, userId) {
|
|
17
21
|
const userDetails = await api.getUserDetails();
|
|
18
22
|
return {
|
|
19
23
|
identifiers: { externalId: userDetails.portalId, user: userId },
|
|
@@ -26,14 +30,14 @@ const Definition = {
|
|
|
26
30
|
],
|
|
27
31
|
entity: [],
|
|
28
32
|
},
|
|
29
|
-
getCredentialDetails: async function(api, userId) {
|
|
33
|
+
getCredentialDetails: async function (api, userId) {
|
|
30
34
|
const userDetails = await api.getUserDetails();
|
|
31
35
|
return {
|
|
32
36
|
identifiers: { externalId: userDetails.portalId, user: userId },
|
|
33
37
|
details: {}
|
|
34
38
|
};
|
|
35
39
|
},
|
|
36
|
-
testAuthRequest: async function(api){
|
|
40
|
+
testAuthRequest: async function (api) {
|
|
37
41
|
return api.getUserDetails()
|
|
38
42
|
},
|
|
39
43
|
},
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
class TestModuleFactory {
|
|
2
|
+
constructor() { }
|
|
3
|
+
|
|
4
|
+
async getModuleInstance(entityId, userId) {
|
|
5
|
+
// return minimal stub module with getName and api property
|
|
6
|
+
return {
|
|
7
|
+
getName() { return 'stubModule'; },
|
|
8
|
+
api: {},
|
|
9
|
+
entityId,
|
|
10
|
+
userId,
|
|
11
|
+
testAuth: async () => true,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
module.exports = { TestModuleFactory };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
class TestModuleRepository {
|
|
2
|
+
constructor() {
|
|
3
|
+
this.entities = new Map();
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
addEntity(entity) {
|
|
7
|
+
this.entities.set(entity.id, entity);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
async findEntityById(id) {
|
|
11
|
+
return this.entities.get(id);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async findEntitiesByIds(ids) {
|
|
15
|
+
return ids.map((id) => this.entities.get(id));
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
module.exports = { TestModuleRepository };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const { Module } = require('../module');
|
|
2
|
+
const { mapModuleClassToModuleDTO } = require('../utils/map-module-dto');
|
|
3
|
+
|
|
4
|
+
class GetEntitiesForUser {
|
|
5
|
+
constructor({ moduleRepository, moduleDefinitions }) {
|
|
6
|
+
this.moduleRepository = moduleRepository;
|
|
7
|
+
|
|
8
|
+
this.definitionMap = new Map();
|
|
9
|
+
for (const definition of moduleDefinitions) {
|
|
10
|
+
this.definitionMap.set(definition.modelName, definition);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async execute(userId) {
|
|
15
|
+
const entities = await this.moduleRepository.findEntitiesByUserId(
|
|
16
|
+
userId
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
return entities.map((entity) => {
|
|
20
|
+
const definition = this.definitionMap.get(entity.moduleName);
|
|
21
|
+
|
|
22
|
+
const moduleInstance = new Module({
|
|
23
|
+
userId,
|
|
24
|
+
definition: definition,
|
|
25
|
+
entity: entity,
|
|
26
|
+
});
|
|
27
|
+
return mapModuleClassToModuleDTO(moduleInstance);
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
module.exports = { GetEntitiesForUser };
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
const { Module } = require('../module');
|
|
2
|
+
|
|
3
|
+
class GetEntityOptionsById {
|
|
4
|
+
/**
|
|
5
|
+
* @param {Object} params
|
|
6
|
+
* @param {import('../module-repository').ModuleRepository} params.moduleRepository
|
|
7
|
+
* @param {} params.moduleDefinitions
|
|
8
|
+
*/
|
|
9
|
+
constructor({ moduleRepository, moduleDefinitions }) {
|
|
10
|
+
this.moduleRepository = moduleRepository;
|
|
11
|
+
this.moduleDefinitions = moduleDefinitions;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Retrieve a Module instance for a given user and entity/module type.
|
|
16
|
+
* @param {string} userId
|
|
17
|
+
* @param {string} entityId
|
|
18
|
+
*/
|
|
19
|
+
async execute(entityId, userId) {
|
|
20
|
+
const entity = await this.moduleRepository.findEntityById(
|
|
21
|
+
entityId,
|
|
22
|
+
userId
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
if (!entity) {
|
|
26
|
+
throw new Error(`Entity ${entityId} not found`);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (entity.userId !== userId) {
|
|
30
|
+
throw new Error(
|
|
31
|
+
`Entity ${entityId} does not belong to user ${userId}`
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const entityType = entity.type;
|
|
36
|
+
const moduleDefinition = this.moduleDefinitions.find((def) => {
|
|
37
|
+
const modelName = Module.getEntityModelFromDefinition(def).modelName;
|
|
38
|
+
return entityType === modelName;
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
if (!moduleDefinition) {
|
|
42
|
+
throw new Error(
|
|
43
|
+
`Module definition not found for entity type: ${entityType}`
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const module = new Module({
|
|
48
|
+
userId,
|
|
49
|
+
entity,
|
|
50
|
+
definition: moduleDefinition,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const entityOptions = await module.getEntityOptions();
|
|
54
|
+
return entityOptions;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
module.exports = { GetEntityOptionsById };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
const { Module } = require('../module');
|
|
2
|
+
|
|
3
|
+
class GetEntityOptionsByType {
|
|
4
|
+
/**
|
|
5
|
+
* @param {Object} params
|
|
6
|
+
* @param {} params.moduleDefinitions
|
|
7
|
+
*/
|
|
8
|
+
constructor({ moduleDefinitions }) {
|
|
9
|
+
this.moduleDefinitions = moduleDefinitions;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Retrieve a Module instance for a given user and entity/module type.
|
|
14
|
+
* @param {string} userId
|
|
15
|
+
* @param {string} type – human-readable module/entity type (e.g. "Hubspot")
|
|
16
|
+
*/
|
|
17
|
+
async execute(userId, type) {
|
|
18
|
+
const moduleDefinition = this.moduleDefinitions.find(
|
|
19
|
+
(def) => def.getName() === type
|
|
20
|
+
);
|
|
21
|
+
if (!moduleDefinition) {
|
|
22
|
+
throw new Error(`Module definition not found for type: ${type}`);
|
|
23
|
+
}
|
|
24
|
+
const moduleInstance = new Module({
|
|
25
|
+
userId,
|
|
26
|
+
definition: moduleDefinition,
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const entityOptions = await moduleInstance.getEntityOptions();
|
|
30
|
+
return entityOptions;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
module.exports = { GetEntityOptionsByType };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
const { Module } = require('../module');
|
|
2
|
+
|
|
3
|
+
class GetModuleInstanceFromType {
|
|
4
|
+
/**
|
|
5
|
+
* @param {Object} params
|
|
6
|
+
* @param {} params.moduleDefinitions
|
|
7
|
+
*/
|
|
8
|
+
constructor({ moduleDefinitions }) {
|
|
9
|
+
this.moduleDefinitions = moduleDefinitions;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Retrieve a Module instance for a given user and entity/module type.
|
|
14
|
+
* @param {string} userId
|
|
15
|
+
* @param {string} type – human-readable module/entity type (e.g. "Hubspot")
|
|
16
|
+
*/
|
|
17
|
+
async execute(userId, type) {
|
|
18
|
+
const moduleDefinition = this.moduleDefinitions.find(
|
|
19
|
+
(def) => def.getName() === type
|
|
20
|
+
);
|
|
21
|
+
if (!moduleDefinition) {
|
|
22
|
+
throw new Error(`Module definition not found for type: ${type}`);
|
|
23
|
+
}
|
|
24
|
+
return new Module({
|
|
25
|
+
userId,
|
|
26
|
+
definition: moduleDefinition,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
module.exports = { GetModuleInstanceFromType };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
const { Module } = require('../module');
|
|
2
|
+
|
|
3
|
+
class GetModule {
|
|
4
|
+
constructor({ moduleRepository, moduleDefinitions }) {
|
|
5
|
+
this.moduleRepository = moduleRepository;
|
|
6
|
+
this.moduleDefinitions = moduleDefinitions;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
async execute(entityId, userId) {
|
|
10
|
+
const entity = await this.moduleRepository.findEntityById(
|
|
11
|
+
entityId,
|
|
12
|
+
userId
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
if (!entity) {
|
|
16
|
+
throw new Error(`Entity ${entityId} not found`);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (entity.userId !== userId) {
|
|
20
|
+
throw new Error(
|
|
21
|
+
`Entity ${entityId} does not belong to user ${userId}`
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const entityType = entity.type;
|
|
26
|
+
const moduleDefinition = this.moduleDefinitions.find((def) => {
|
|
27
|
+
const modelName = Module.getEntityModelFromDefinition(def).modelName;
|
|
28
|
+
return entityType === modelName;
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
if (!moduleDefinition) {
|
|
32
|
+
throw new Error(
|
|
33
|
+
`Module definition not found for entity type: ${entityType}`
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const module = new Module({
|
|
38
|
+
userId,
|
|
39
|
+
entity,
|
|
40
|
+
definition: moduleDefinition,
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
// todo: this properties should be methods in the Module class
|
|
44
|
+
return {
|
|
45
|
+
id: module.entity.id,
|
|
46
|
+
name: module.entity.name,
|
|
47
|
+
type: module.entity.moduleName,
|
|
48
|
+
moduleName: module.entity.moduleName,
|
|
49
|
+
credential: module.credential,
|
|
50
|
+
externalId: module.entity.externalId,
|
|
51
|
+
userId: module.entity.user.toString(),
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
module.exports = { GetModule };
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
const { Module } = require('../module');
|
|
2
|
+
const { ModuleConstants } = require('../ModuleConstants');
|
|
3
|
+
|
|
4
|
+
class ProcessAuthorizationCallback {
|
|
5
|
+
/**
|
|
6
|
+
* @param {Object} params - Configuration parameters.
|
|
7
|
+
* @param {import('../module-repository').ModuleRepository} params.moduleRepository - Repository for module data operations.
|
|
8
|
+
* @param {import('../../credential/credential-repository').CredentialRepository} params.credentialRepository - Repository for credential data operations.
|
|
9
|
+
* @param {Array<Object>} params.moduleDefinitions - Array of module definitions.
|
|
10
|
+
*/
|
|
11
|
+
constructor({ moduleRepository, credentialRepository, moduleDefinitions }) {
|
|
12
|
+
this.moduleRepository = moduleRepository;
|
|
13
|
+
this.credentialRepository = credentialRepository;
|
|
14
|
+
this.moduleDefinitions = moduleDefinitions;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async execute(userId, entityType, params, credentialId = null) {
|
|
18
|
+
const moduleDefinition = this.moduleDefinitions.find((def) => {
|
|
19
|
+
return entityType === def.modelName;
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
if (!moduleDefinition) {
|
|
23
|
+
throw new Error(
|
|
24
|
+
`Module definition not found for entity type: ${entityType}`
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
let credential = null;
|
|
29
|
+
let entity = null;
|
|
30
|
+
|
|
31
|
+
if (credentialId) {
|
|
32
|
+
credential = await this.credentialRepository.findCredentialById(credentialId);
|
|
33
|
+
if (!credential) {
|
|
34
|
+
throw new Error(`Credential ${credentialId} not found`);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const module = new Module({
|
|
39
|
+
userId,
|
|
40
|
+
entity,
|
|
41
|
+
definition: moduleDefinition,
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
let tokenResponse;
|
|
45
|
+
if (module.apiClass.requesterType === ModuleConstants.authType.oauth2) {
|
|
46
|
+
tokenResponse = await moduleDefinition.requiredAuthMethods.getToken(module.api, params);
|
|
47
|
+
} else {
|
|
48
|
+
tokenResponse = await moduleDefinition.requiredAuthMethods.setAuthParams(module.api, params);
|
|
49
|
+
await this.onTokenUpdate(module, moduleDefinition, userId);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const authRes = await module.testAuth();
|
|
53
|
+
if (!authRes) {
|
|
54
|
+
throw new Error('Authorization failed');
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const entityDetails = await moduleDefinition.requiredAuthMethods.getEntityDetails(
|
|
58
|
+
module.api,
|
|
59
|
+
params,
|
|
60
|
+
tokenResponse,
|
|
61
|
+
userId
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
Object.assign(
|
|
65
|
+
entityDetails.details,
|
|
66
|
+
module.apiParamsFromEntity(module.api)
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
const persistedEntity = await this.findOrCreateEntity(entityDetails, entityType);
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
credential_id: module.credential.id,
|
|
73
|
+
entity_id: persistedEntity.id,
|
|
74
|
+
type: module.getName(),
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async onTokenUpdate(module, moduleDefinition, userId) {
|
|
79
|
+
const credentialDetails = await moduleDefinition.requiredAuthMethods.getCredentialDetails(
|
|
80
|
+
module.api,
|
|
81
|
+
userId
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
Object.assign(
|
|
85
|
+
credentialDetails.details,
|
|
86
|
+
module.apiParamsFromCredential(module.api)
|
|
87
|
+
);
|
|
88
|
+
credentialDetails.details.auth_is_valid = true;
|
|
89
|
+
|
|
90
|
+
await this.credentialRepository.upsertCredential(credentialDetails);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async findOrCreateEntity(entityDetails, entityType) {
|
|
94
|
+
const { identifiers, details } = entityDetails;
|
|
95
|
+
|
|
96
|
+
const existingEntity = await this.moduleRepository.findEntity({
|
|
97
|
+
externalId: identifiers.externalId,
|
|
98
|
+
user: identifiers.user,
|
|
99
|
+
__t: entityType
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
if (existingEntity) {
|
|
103
|
+
return existingEntity;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return await this.moduleRepository.createEntity({
|
|
107
|
+
...identifiers,
|
|
108
|
+
...details,
|
|
109
|
+
__t: entityType
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
module.exports = { ProcessAuthorizationCallback };
|