@friggframework/core 2.0.0--canary.396.0dd37aa.0 → 2.0.0--canary.397.216d54b.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.
@@ -1,195 +0,0 @@
1
- const { ModuleFactory, Entity } = require('../module-plugin');
2
- const { IntegrationModel } = require('./integration-model');
3
-
4
- class IntegrationFactory {
5
- constructor(integrationClasses = []) {
6
- this.integrationClasses = integrationClasses;
7
- this.moduleFactory = new ModuleFactory(...this.getModules());
8
- this.integrationTypes = this.integrationClasses.map(
9
- (IntegrationClass) => IntegrationClass.getName()
10
- );
11
- this.getIntegrationDefinitions = this.integrationClasses.map(
12
- (IntegrationClass) => IntegrationClass.Definition
13
- );
14
- }
15
-
16
- async getIntegrationOptions() {
17
- const options = this.integrationClasses.map(
18
- (IntegrationClass) => IntegrationClass
19
- );
20
- return {
21
- entities: {
22
- options: options.map((IntegrationClass) =>
23
- IntegrationClass.getOptionDetails()
24
- ),
25
- authorized: [],
26
- },
27
- integrations: [],
28
- };
29
- }
30
-
31
- getModules() {
32
- return [
33
- ...new Set(
34
- this.integrationClasses
35
- .map((integration) =>
36
- Object.values(integration.Definition.modules).map(
37
- (module) => module.definition
38
- )
39
- )
40
- .flat()
41
- ),
42
- ];
43
- }
44
-
45
- getIntegrationClassByType(type) {
46
- const integrationClassIndex = this.integrationTypes.indexOf(type);
47
- return this.integrationClasses[integrationClassIndex];
48
- }
49
- getModuleTypesAndKeys(integrationClass) {
50
- const moduleTypesAndKeys = {};
51
- const moduleTypeCount = {};
52
-
53
- if (integrationClass && integrationClass.Definition.modules) {
54
- for (const [key, moduleClass] of Object.entries(
55
- integrationClass.Definition.modules
56
- )) {
57
- if (
58
- moduleClass &&
59
- typeof moduleClass.definition.getName === 'function'
60
- ) {
61
- const moduleType = moduleClass.definition.getName();
62
-
63
- // Check if this module type has already been seen
64
- if (moduleType in moduleTypesAndKeys) {
65
- throw new Error(
66
- `Duplicate module type "${moduleType}" found in integration class definition.`
67
- );
68
- }
69
-
70
- // Well how baout now
71
-
72
- moduleTypesAndKeys[moduleType] = key;
73
- moduleTypeCount[moduleType] =
74
- (moduleTypeCount[moduleType] || 0) + 1;
75
- }
76
- }
77
- }
78
-
79
- // Check for any module types with count > 1
80
- for (const [moduleType, count] of Object.entries(moduleTypeCount)) {
81
- if (count > 1) {
82
- throw new Error(
83
- `Multiple instances of module type "${moduleType}" found in integration class definition.`
84
- );
85
- }
86
- }
87
-
88
- return moduleTypesAndKeys;
89
- }
90
-
91
- async getInstanceFromIntegrationId({ integrationId, userId }) {
92
- const integrationRecord = await IntegrationHelper.getIntegrationById(
93
- integrationId
94
- );
95
- if (!integrationRecord) {
96
- throw new Error(
97
- `No integration found by the ID of ${integrationId}`
98
- );
99
- }
100
-
101
- if (!userId) {
102
- userId = integrationRecord.user._id.toString();
103
- } else if (userId.toString() !== integrationRecord.user.toString()) {
104
- throw new Error(
105
- `Integration ${integrationId
106
- } does not belong to User ${userId}, ${integrationRecord.user.toString()}`
107
- );
108
- }
109
-
110
- // getIntegrationClassByType is only used here
111
- const integrationClass = this.getIntegrationClassByType(
112
- integrationRecord.config.type
113
- );
114
-
115
- // here we should instantiate an Integration Domain class along with the integration record and it should happen in a use case class
116
- // Actually, this integrationClass is a subclass of IntegrationBase.
117
- const instance = new integrationClass({
118
- userId,
119
- 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
- }
139
-
140
- instance.record = integrationRecord;
141
-
142
- try {
143
- const additionalUserActions =
144
- await instance.loadDynamicUserActions();
145
- instance.events = { ...instance.events, ...additionalUserActions };
146
- } catch (e) {
147
- instance.record.status = 'ERROR';
148
- instance.record.messages.errors.push(e);
149
- await instance.record.save();
150
- }
151
- // Register all of the event handlers
152
-
153
- await instance.registerEventHandlers();
154
- return instance;
155
- }
156
-
157
- async createIntegration(entities, userId, config) {
158
- const integrationRecord = await IntegrationModel.create({
159
- entities: entities,
160
- user: userId,
161
- config,
162
- version: '0.0.0',
163
- });
164
- return await this.getInstanceFromIntegrationId({
165
- integrationId: integrationRecord.id,
166
- userId,
167
- });
168
- }
169
-
170
- async getFormattedIntegration(integrationRecord) {
171
- const integrationObj = {
172
- id: integrationRecord.id,
173
- status: integrationRecord.status,
174
- config: integrationRecord.config,
175
- entities: [],
176
- version: integrationRecord.version,
177
- messages: integrationRecord.messages,
178
- };
179
- for (const entityId of integrationRecord.entities) {
180
- // Only return non-internal fields. Leverages "select" and "options" to non-excepted fields and a pure object.
181
- const entity = await Entity.findById(
182
- entityId,
183
- '-createdAt -updatedAt -user -credentials -credential -_id -__t -__v',
184
- { lean: true }
185
- );
186
- integrationObj.entities.push({
187
- id: entityId,
188
- ...entity,
189
- });
190
- }
191
- return integrationObj;
192
- }
193
- }
194
-
195
- module.exports = { IntegrationFactory };
@@ -1,42 +0,0 @@
1
- const { ModuleRepository } = require('./module-repository');
2
- const { ModuleService } = require('./module-service');
3
- const { Module } = require('./module');
4
-
5
- class ModuleFactory {
6
- constructor(...params) {
7
- this.moduleDefinitions = params;
8
- this.moduleTypes = this.moduleDefinitions.map((def) => def.moduleName);
9
- this.moduleRepository = new ModuleRepository();
10
- this.moduleService = new ModuleService({
11
- moduleRepository: this.moduleRepository,
12
- moduleDefinitions: this.moduleDefinitions,
13
- });
14
- }
15
-
16
- checkIsValidType(entityType) {
17
- return this.moduleTypes.includes(entityType);
18
- }
19
-
20
- getModuleDefinitionFromTypeName(typeName) {
21
- return;
22
- }
23
-
24
- async getModuleInstanceFromEntityId(entityId, userId) {
25
- return this.moduleService.getModuleInstance(entityId, userId);
26
- }
27
-
28
- async getInstanceFromTypeName(typeName, userId) {
29
- const moduleDefinition = this.moduleDefinitions.find(
30
- (def) => def.getName() === typeName
31
- );
32
- if (!moduleDefinition) {
33
- throw new Error(`Module definition not found for type: ${typeName}`);
34
- }
35
- return new Module({
36
- userId,
37
- definition: moduleDefinition,
38
- });
39
- }
40
- }
41
-
42
- module.exports = { ModuleFactory };