@friggframework/core 2.0.0--canary.397.4957a89.0 → 2.0.0--canary.398.bdb6d27.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 +50 -931
- package/core/create-handler.js +0 -1
- package/database/models/WebsocketConnection.js +5 -0
- package/handlers/app-handler-helpers.js +3 -0
- package/handlers/backend-utils.js +34 -35
- package/handlers/routers/auth.js +14 -3
- package/handlers/routers/integration-defined-routers.js +5 -8
- package/handlers/routers/middleware/loadUser.js +15 -0
- package/handlers/routers/middleware/requireLoggedInUser.js +12 -0
- package/handlers/routers/user.js +5 -25
- package/handlers/workers/integration-defined-workers.js +3 -6
- package/index.js +16 -1
- package/integrations/create-frigg-backend.js +31 -0
- package/integrations/index.js +5 -0
- package/integrations/integration-base.js +44 -42
- package/integrations/integration-factory.js +251 -0
- package/integrations/integration-router.js +178 -301
- package/integrations/integration-user.js +144 -0
- package/integrations/options.js +1 -1
- package/integrations/test/integration-base.test.js +144 -0
- package/module-plugin/auther.js +393 -0
- package/module-plugin/entity-manager.js +70 -0
- package/{modules → module-plugin}/index.js +8 -0
- package/module-plugin/manager.js +169 -0
- package/module-plugin/module-factory.js +61 -0
- package/{modules → module-plugin}/test/mock-api/api.js +3 -8
- package/{modules → module-plugin}/test/mock-api/definition.js +8 -12
- package/package.json +5 -5
- package/syncs/sync.js +1 -0
- package/types/integrations/index.d.ts +6 -2
- package/types/module-plugin/index.d.ts +56 -4
- package/types/syncs/index.d.ts +2 -0
- package/credential/credential-repository.js +0 -42
- package/credential/use-cases/get-credential-for-user.js +0 -21
- package/credential/use-cases/update-authentication-status.js +0 -15
- package/handlers/app-definition-loader.js +0 -38
- package/integrations/integration-repository.js +0 -67
- package/integrations/integration.js +0 -233
- package/integrations/tests/doubles/dummy-integration-class.js +0 -90
- package/integrations/tests/doubles/test-integration-repository.js +0 -89
- package/integrations/tests/use-cases/create-integration.test.js +0 -124
- package/integrations/tests/use-cases/delete-integration-for-user.test.js +0 -143
- package/integrations/tests/use-cases/get-integration-for-user.test.js +0 -143
- package/integrations/tests/use-cases/get-integration-instance.test.js +0 -169
- package/integrations/tests/use-cases/get-integrations-for-user.test.js +0 -169
- package/integrations/tests/use-cases/get-possible-integrations.test.js +0 -188
- package/integrations/tests/use-cases/update-integration-messages.test.js +0 -142
- package/integrations/tests/use-cases/update-integration-status.test.js +0 -103
- package/integrations/tests/use-cases/update-integration.test.js +0 -134
- package/integrations/use-cases/create-integration.js +0 -72
- package/integrations/use-cases/delete-integration-for-user.js +0 -73
- package/integrations/use-cases/get-integration-for-user.js +0 -79
- package/integrations/use-cases/get-integration-instance.js +0 -84
- package/integrations/use-cases/get-integrations-for-user.js +0 -77
- package/integrations/use-cases/get-possible-integrations.js +0 -27
- package/integrations/use-cases/index.js +0 -11
- package/integrations/use-cases/update-integration-messages.js +0 -31
- package/integrations/use-cases/update-integration-status.js +0 -28
- package/integrations/use-cases/update-integration.js +0 -92
- package/integrations/utils/map-integration-dto.js +0 -36
- package/modules/module-factory.js +0 -54
- package/modules/module-repository.js +0 -107
- package/modules/module.js +0 -221
- package/modules/tests/doubles/test-module-factory.js +0 -16
- package/modules/tests/doubles/test-module-repository.js +0 -19
- package/modules/use-cases/get-entities-for-user.js +0 -32
- package/modules/use-cases/get-entity-options-by-id.js +0 -58
- package/modules/use-cases/get-entity-options-by-type.js +0 -34
- package/modules/use-cases/get-module-instance-from-type.js +0 -31
- package/modules/use-cases/get-module.js +0 -56
- package/modules/use-cases/process-authorization-callback.js +0 -114
- package/modules/use-cases/refresh-entity-options.js +0 -58
- package/modules/use-cases/test-module-auth.js +0 -54
- package/modules/utils/map-module-dto.js +0 -18
- package/user/tests/doubles/test-user-repository.js +0 -72
- package/user/tests/use-cases/create-individual-user.test.js +0 -24
- package/user/tests/use-cases/create-organization-user.test.js +0 -28
- package/user/tests/use-cases/create-token-for-user-id.test.js +0 -19
- package/user/tests/use-cases/get-user-from-bearer-token.test.js +0 -64
- package/user/tests/use-cases/login-user.test.js +0 -140
- package/user/use-cases/create-individual-user.js +0 -61
- package/user/use-cases/create-organization-user.js +0 -47
- package/user/use-cases/create-token-for-user-id.js +0 -30
- package/user/use-cases/get-user-from-bearer-token.js +0 -77
- package/user/use-cases/login-user.js +0 -122
- package/user/user-repository.js +0 -62
- package/user/user.js +0 -77
- /package/{modules → module-plugin}/ModuleConstants.js +0 -0
- /package/{modules → module-plugin}/credential.js +0 -0
- /package/{modules → module-plugin}/entity.js +0 -0
- /package/{modules → module-plugin}/requester/api-key.js +0 -0
- /package/{modules → module-plugin}/requester/basic.js +0 -0
- /package/{modules → module-plugin}/requester/oauth-2.js +0 -0
- /package/{modules → module-plugin}/requester/requester.js +0 -0
- /package/{modules → module-plugin}/requester/requester.test.js +0 -0
- /package/{modules → module-plugin}/test/auther.test.js +0 -0
- /package/{modules → module-plugin}/test/mock-api/mocks/hubspot.js +0 -0
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
const { ModuleFactory, Credential, Entity } = require('../module-plugin');
|
|
2
|
+
const { IntegrationModel } = require('./integration-model');
|
|
3
|
+
const _ = require('lodash');
|
|
4
|
+
|
|
5
|
+
class IntegrationFactory {
|
|
6
|
+
constructor(integrationClasses = []) {
|
|
7
|
+
this.integrationClasses = integrationClasses;
|
|
8
|
+
this.moduleFactory = new ModuleFactory(...this.getModules());
|
|
9
|
+
this.integrationTypes = this.integrationClasses.map(
|
|
10
|
+
(IntegrationClass) => IntegrationClass.getName()
|
|
11
|
+
);
|
|
12
|
+
this.getIntegrationDefinitions = this.integrationClasses.map(
|
|
13
|
+
(IntegrationClass) => IntegrationClass.Definition
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async getIntegrationOptions() {
|
|
18
|
+
const options = this.integrationClasses.map(
|
|
19
|
+
(IntegrationClass) => IntegrationClass
|
|
20
|
+
);
|
|
21
|
+
return {
|
|
22
|
+
entities: {
|
|
23
|
+
options: options.map((IntegrationClass) =>
|
|
24
|
+
IntegrationClass.getOptionDetails()
|
|
25
|
+
),
|
|
26
|
+
authorized: [],
|
|
27
|
+
},
|
|
28
|
+
integrations: [],
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
getModules() {
|
|
33
|
+
return [
|
|
34
|
+
...new Set(
|
|
35
|
+
this.integrationClasses
|
|
36
|
+
.map((integration) =>
|
|
37
|
+
Object.values(integration.Definition.modules).map(
|
|
38
|
+
(module) => module.definition
|
|
39
|
+
)
|
|
40
|
+
)
|
|
41
|
+
.flat()
|
|
42
|
+
),
|
|
43
|
+
];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
getIntegrationClassByType(type) {
|
|
47
|
+
const integrationClassIndex = this.integrationTypes.indexOf(type);
|
|
48
|
+
return this.integrationClasses[integrationClassIndex];
|
|
49
|
+
}
|
|
50
|
+
getModuleTypesAndKeys(integrationClass) {
|
|
51
|
+
const moduleTypesAndKeys = {};
|
|
52
|
+
const moduleTypeCount = {};
|
|
53
|
+
|
|
54
|
+
if (integrationClass && integrationClass.Definition.modules) {
|
|
55
|
+
for (const [key, moduleClass] of Object.entries(
|
|
56
|
+
integrationClass.Definition.modules
|
|
57
|
+
)) {
|
|
58
|
+
if (
|
|
59
|
+
moduleClass &&
|
|
60
|
+
typeof moduleClass.definition.getName === 'function'
|
|
61
|
+
) {
|
|
62
|
+
const moduleType = moduleClass.definition.getName();
|
|
63
|
+
|
|
64
|
+
// Check if this module type has already been seen
|
|
65
|
+
if (moduleType in moduleTypesAndKeys) {
|
|
66
|
+
throw new Error(
|
|
67
|
+
`Duplicate module type "${moduleType}" found in integration class definition.`
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Well how baout now
|
|
72
|
+
|
|
73
|
+
moduleTypesAndKeys[moduleType] = key;
|
|
74
|
+
moduleTypeCount[moduleType] =
|
|
75
|
+
(moduleTypeCount[moduleType] || 0) + 1;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Check for any module types with count > 1
|
|
81
|
+
for (const [moduleType, count] of Object.entries(moduleTypeCount)) {
|
|
82
|
+
if (count > 1) {
|
|
83
|
+
throw new Error(
|
|
84
|
+
`Multiple instances of module type "${moduleType}" found in integration class definition.`
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return moduleTypesAndKeys;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async getInstanceFromIntegrationId(params) {
|
|
93
|
+
const integrationRecord = await IntegrationHelper.getIntegrationById(
|
|
94
|
+
params.integrationId
|
|
95
|
+
);
|
|
96
|
+
let { userId } = params;
|
|
97
|
+
if (!integrationRecord) {
|
|
98
|
+
throw new Error(
|
|
99
|
+
`No integration found by the ID of ${params.integrationId}`
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (!userId) {
|
|
104
|
+
userId = integrationRecord.user._id.toString();
|
|
105
|
+
} else if (userId.toString() !== integrationRecord.user.toString()) {
|
|
106
|
+
throw new Error(
|
|
107
|
+
`Integration ${
|
|
108
|
+
params.integrationId
|
|
109
|
+
} does not belong to User ${userId}, ${integrationRecord.user.toString()}`
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const integrationClass = this.getIntegrationClassByType(
|
|
114
|
+
integrationRecord.config.type
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
const instance = new integrationClass({
|
|
118
|
+
userId,
|
|
119
|
+
integrationId: params.integrationId,
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
if (
|
|
123
|
+
integrationRecord.entityReference &&
|
|
124
|
+
Object.keys(integrationRecord.entityReference) > 0
|
|
125
|
+
) {
|
|
126
|
+
// Use the specified entityReference to find the modules and load them according to their key
|
|
127
|
+
// entityReference will be a map of entityIds with their corresponding desired key
|
|
128
|
+
for (const [entityId, key] of Object.entries(
|
|
129
|
+
integrationRecord.entityReference
|
|
130
|
+
)) {
|
|
131
|
+
const moduleInstance =
|
|
132
|
+
await this.moduleFactory.getModuleInstanceFromEntityId(
|
|
133
|
+
entityId,
|
|
134
|
+
integrationRecord.user
|
|
135
|
+
);
|
|
136
|
+
instance[key] = moduleInstance;
|
|
137
|
+
}
|
|
138
|
+
} else {
|
|
139
|
+
// for each entity, get the moduleinstance and load them according to their keys
|
|
140
|
+
// If it's the first entity, load the moduleinstance into primary as well
|
|
141
|
+
// If it's the second entity, load the moduleinstance into target as well
|
|
142
|
+
const moduleTypesAndKeys =
|
|
143
|
+
this.getModuleTypesAndKeys(integrationClass);
|
|
144
|
+
for (let i = 0; i < integrationRecord.entities.length; i++) {
|
|
145
|
+
const entityId = integrationRecord.entities[i];
|
|
146
|
+
const moduleInstance =
|
|
147
|
+
await this.moduleFactory.getModuleInstanceFromEntityId(
|
|
148
|
+
entityId,
|
|
149
|
+
integrationRecord.user
|
|
150
|
+
);
|
|
151
|
+
const moduleType = moduleInstance.getName();
|
|
152
|
+
const key = moduleTypesAndKeys[moduleType];
|
|
153
|
+
instance[key] = moduleInstance;
|
|
154
|
+
if (i === 0) {
|
|
155
|
+
instance.primary = moduleInstance;
|
|
156
|
+
} else if (i === 1) {
|
|
157
|
+
instance.target = moduleInstance;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
instance.record = integrationRecord;
|
|
162
|
+
|
|
163
|
+
try {
|
|
164
|
+
const additionalUserActions =
|
|
165
|
+
await instance.loadDynamicUserActions();
|
|
166
|
+
instance.events = { ...instance.events, ...additionalUserActions };
|
|
167
|
+
} catch (e) {
|
|
168
|
+
instance.record.status = 'ERROR';
|
|
169
|
+
instance.record.messages.errors.push(e);
|
|
170
|
+
await instance.record.save();
|
|
171
|
+
}
|
|
172
|
+
// Register all of the event handlers
|
|
173
|
+
|
|
174
|
+
await instance.registerEventHandlers();
|
|
175
|
+
return instance;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
async createIntegration(entities, userId, config) {
|
|
179
|
+
const integrationRecord = await IntegrationModel.create({
|
|
180
|
+
entities: entities,
|
|
181
|
+
user: userId,
|
|
182
|
+
config,
|
|
183
|
+
version: '0.0.0',
|
|
184
|
+
});
|
|
185
|
+
return await this.getInstanceFromIntegrationId({
|
|
186
|
+
integrationId: integrationRecord.id,
|
|
187
|
+
userId,
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const IntegrationHelper = {
|
|
193
|
+
getFormattedIntegration: async function (integrationRecord) {
|
|
194
|
+
const integrationObj = {
|
|
195
|
+
id: integrationRecord.id,
|
|
196
|
+
status: integrationRecord.status,
|
|
197
|
+
config: integrationRecord.config,
|
|
198
|
+
entities: [],
|
|
199
|
+
version: integrationRecord.version,
|
|
200
|
+
messages: integrationRecord.messages,
|
|
201
|
+
};
|
|
202
|
+
for (const entityId of integrationRecord.entities) {
|
|
203
|
+
// Only return non-internal fields. Leverages "select" and "options" to non-excepted fields and a pure object.
|
|
204
|
+
const entity = await Entity.findById(
|
|
205
|
+
entityId,
|
|
206
|
+
'-createdAt -updatedAt -user -credentials -credential -_id -__t -__v',
|
|
207
|
+
{ lean: true }
|
|
208
|
+
);
|
|
209
|
+
integrationObj.entities.push({
|
|
210
|
+
id: entityId,
|
|
211
|
+
...entity,
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
return integrationObj;
|
|
215
|
+
},
|
|
216
|
+
|
|
217
|
+
getIntegrationsForUserId: async function (userId) {
|
|
218
|
+
const integrationList = await IntegrationModel.find({ user: userId });
|
|
219
|
+
return await Promise.all(
|
|
220
|
+
integrationList.map(
|
|
221
|
+
async (integrationRecord) =>
|
|
222
|
+
await IntegrationHelper.getFormattedIntegration(
|
|
223
|
+
integrationRecord
|
|
224
|
+
)
|
|
225
|
+
)
|
|
226
|
+
);
|
|
227
|
+
},
|
|
228
|
+
|
|
229
|
+
deleteIntegrationForUserById: async function (userId, integrationId) {
|
|
230
|
+
const integrationList = await IntegrationModel.find({
|
|
231
|
+
user: userId,
|
|
232
|
+
_id: integrationId,
|
|
233
|
+
});
|
|
234
|
+
if (integrationList.length !== 1) {
|
|
235
|
+
throw new Error(
|
|
236
|
+
`Integration with id of ${integrationId} does not exist for this user`
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
await IntegrationModel.deleteOne({ _id: integrationId });
|
|
240
|
+
},
|
|
241
|
+
|
|
242
|
+
getIntegrationById: async function (id) {
|
|
243
|
+
return IntegrationModel.findById(id).populate('entities');
|
|
244
|
+
},
|
|
245
|
+
|
|
246
|
+
listCredentials: async function (options) {
|
|
247
|
+
return Credential.find(options);
|
|
248
|
+
},
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
module.exports = { IntegrationFactory, IntegrationHelper };
|