@friggframework/core 2.0.0-next.53 → 2.0.0-next.54
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/CLAUDE.md +2 -1
- package/application/commands/integration-commands.js +1 -1
- package/application/index.js +1 -1
- package/credential/repositories/credential-repository-documentdb.js +300 -0
- package/credential/repositories/credential-repository-factory.js +8 -1
- package/database/config.js +4 -4
- package/database/documentdb-encryption-service.js +330 -0
- package/database/documentdb-utils.js +136 -0
- package/database/encryption/README.md +50 -0
- package/database/encryption/documentdb-encryption-service.md +3270 -0
- package/database/encryption/encryption-schema-registry.js +46 -0
- package/database/prisma.js +7 -47
- package/database/repositories/health-check-repository-documentdb.js +134 -0
- package/database/repositories/health-check-repository-factory.js +6 -1
- package/database/repositories/health-check-repository-interface.js +29 -34
- package/database/repositories/health-check-repository-mongodb.js +1 -3
- package/database/use-cases/check-database-state-use-case.js +3 -3
- package/database/use-cases/run-database-migration-use-case.js +6 -4
- package/database/use-cases/trigger-database-migration-use-case.js +2 -2
- package/database/utils/mongodb-schema-init.js +5 -5
- package/database/utils/prisma-runner.js +15 -9
- package/generated/prisma-mongodb/edge.js +3 -3
- package/generated/prisma-mongodb/index.d.ts +10 -4
- package/generated/prisma-mongodb/index.js +3 -3
- package/generated/prisma-mongodb/package.json +1 -1
- package/generated/prisma-mongodb/schema.prisma +1 -3
- package/generated/prisma-mongodb/wasm.js +2 -2
- package/generated/prisma-postgresql/edge.js +3 -3
- package/generated/prisma-postgresql/index.d.ts +10 -4
- package/generated/prisma-postgresql/index.js +3 -3
- package/generated/prisma-postgresql/package.json +1 -1
- package/generated/prisma-postgresql/schema.prisma +1 -3
- package/generated/prisma-postgresql/wasm.js +2 -2
- package/handlers/routers/db-migration.js +2 -3
- package/handlers/routers/health.js +0 -3
- package/handlers/workers/db-migration.js +8 -8
- package/integrations/repositories/integration-mapping-repository-documentdb.js +135 -0
- package/integrations/repositories/integration-mapping-repository-factory.js +8 -1
- package/integrations/repositories/integration-repository-documentdb.js +189 -0
- package/integrations/repositories/integration-repository-factory.js +8 -1
- package/integrations/repositories/process-repository-documentdb.js +141 -0
- package/integrations/repositories/process-repository-factory.js +8 -1
- package/modules/repositories/module-repository-documentdb.js +307 -0
- package/modules/repositories/module-repository-factory.js +8 -1
- package/package.json +5 -5
- package/prisma-mongodb/schema.prisma +1 -3
- package/prisma-postgresql/migrations/20251112195422_update_user_unique_constraints/migration.sql +69 -0
- package/prisma-postgresql/schema.prisma +1 -3
- package/syncs/repositories/sync-repository-documentdb.js +240 -0
- package/syncs/repositories/sync-repository-factory.js +6 -1
- package/token/repositories/token-repository-documentdb.js +125 -0
- package/token/repositories/token-repository-factory.js +8 -1
- package/user/repositories/user-repository-documentdb.js +292 -0
- package/user/repositories/user-repository-factory.js +6 -1
- package/websocket/repositories/websocket-connection-repository-documentdb.js +119 -0
- package/websocket/repositories/websocket-connection-repository-factory.js +8 -1
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
const { prisma } = require('../../database/prisma');
|
|
2
|
+
const {
|
|
3
|
+
toObjectId,
|
|
4
|
+
fromObjectId,
|
|
5
|
+
findMany,
|
|
6
|
+
findOne,
|
|
7
|
+
insertOne,
|
|
8
|
+
updateOne,
|
|
9
|
+
deleteOne,
|
|
10
|
+
deleteMany,
|
|
11
|
+
} = require('../../database/documentdb-utils');
|
|
12
|
+
const {
|
|
13
|
+
IntegrationMappingRepositoryInterface,
|
|
14
|
+
} = require('./integration-mapping-repository-interface');
|
|
15
|
+
|
|
16
|
+
class IntegrationMappingRepositoryDocumentDB extends IntegrationMappingRepositoryInterface {
|
|
17
|
+
constructor() {
|
|
18
|
+
super();
|
|
19
|
+
this.prisma = prisma;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async findMappingBy(integrationId, sourceId) {
|
|
23
|
+
const filter = this._compositeFilter(integrationId, sourceId);
|
|
24
|
+
const doc = await findOne(this.prisma, 'IntegrationMapping', filter);
|
|
25
|
+
return doc ? this._mapMapping(doc) : null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async upsertMapping(integrationId, sourceId, mapping) {
|
|
29
|
+
const filter = this._compositeFilter(integrationId, sourceId);
|
|
30
|
+
const existing = await findOne(this.prisma, 'IntegrationMapping', filter);
|
|
31
|
+
const now = new Date();
|
|
32
|
+
|
|
33
|
+
if (existing) {
|
|
34
|
+
await updateOne(
|
|
35
|
+
this.prisma,
|
|
36
|
+
'IntegrationMapping',
|
|
37
|
+
{ _id: existing._id },
|
|
38
|
+
{
|
|
39
|
+
$set: {
|
|
40
|
+
mapping,
|
|
41
|
+
updatedAt: now,
|
|
42
|
+
},
|
|
43
|
+
}
|
|
44
|
+
);
|
|
45
|
+
const updated = await findOne(this.prisma, 'IntegrationMapping', { _id: existing._id });
|
|
46
|
+
return this._mapMapping(updated);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const document = {
|
|
50
|
+
integrationId: toObjectId(integrationId),
|
|
51
|
+
sourceId: sourceId === null || sourceId === undefined ? null : String(sourceId),
|
|
52
|
+
mapping,
|
|
53
|
+
createdAt: now,
|
|
54
|
+
updatedAt: now,
|
|
55
|
+
};
|
|
56
|
+
const insertedId = await insertOne(this.prisma, 'IntegrationMapping', document);
|
|
57
|
+
const created = await findOne(this.prisma, 'IntegrationMapping', { _id: insertedId });
|
|
58
|
+
return this._mapMapping(created);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async findMappingsByIntegration(integrationId) {
|
|
62
|
+
const filter = {};
|
|
63
|
+
const integrationObjectId = toObjectId(integrationId);
|
|
64
|
+
if (integrationObjectId) filter.integrationId = integrationObjectId;
|
|
65
|
+
const docs = await findMany(this.prisma, 'IntegrationMapping', filter);
|
|
66
|
+
return docs.map((doc) => this._mapMapping(doc));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async deleteMapping(integrationId, sourceId) {
|
|
70
|
+
const filter = this._compositeFilter(integrationId, sourceId);
|
|
71
|
+
const result = await deleteOne(this.prisma, 'IntegrationMapping', filter);
|
|
72
|
+
const deleted = result?.n ?? 0;
|
|
73
|
+
return { acknowledged: true, deletedCount: deleted };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async deleteMappingsByIntegration(integrationId) {
|
|
77
|
+
const integrationObjectId = toObjectId(integrationId);
|
|
78
|
+
if (!integrationObjectId) {
|
|
79
|
+
return { acknowledged: true, deletedCount: 0 };
|
|
80
|
+
}
|
|
81
|
+
const result = await deleteMany(this.prisma, 'IntegrationMapping', {
|
|
82
|
+
integrationId: integrationObjectId,
|
|
83
|
+
});
|
|
84
|
+
const deleted = result?.n ?? 0;
|
|
85
|
+
return { acknowledged: true, deletedCount: deleted };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async findMappingById(id) {
|
|
89
|
+
const objectId = toObjectId(id);
|
|
90
|
+
if (!objectId) return null;
|
|
91
|
+
const doc = await findOne(this.prisma, 'IntegrationMapping', { _id: objectId });
|
|
92
|
+
return doc ? this._mapMapping(doc) : null;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async updateMapping(id, updates) {
|
|
96
|
+
const objectId = toObjectId(id);
|
|
97
|
+
if (!objectId) return null;
|
|
98
|
+
await updateOne(
|
|
99
|
+
this.prisma,
|
|
100
|
+
'IntegrationMapping',
|
|
101
|
+
{ _id: objectId },
|
|
102
|
+
{
|
|
103
|
+
$set: {
|
|
104
|
+
...updates,
|
|
105
|
+
updatedAt: new Date(),
|
|
106
|
+
},
|
|
107
|
+
}
|
|
108
|
+
);
|
|
109
|
+
const updated = await findOne(this.prisma, 'IntegrationMapping', { _id: objectId });
|
|
110
|
+
return updated ? this._mapMapping(updated) : null;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
_compositeFilter(integrationId, sourceId) {
|
|
114
|
+
const filter = {};
|
|
115
|
+
const integrationObjectId = toObjectId(integrationId);
|
|
116
|
+
if (integrationObjectId) filter.integrationId = integrationObjectId;
|
|
117
|
+
if (sourceId !== undefined) {
|
|
118
|
+
filter.sourceId = sourceId === null ? null : String(sourceId);
|
|
119
|
+
}
|
|
120
|
+
return filter;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
_mapMapping(doc) {
|
|
124
|
+
return {
|
|
125
|
+
id: fromObjectId(doc?._id),
|
|
126
|
+
integrationId: fromObjectId(doc?.integrationId),
|
|
127
|
+
sourceId: doc?.sourceId ?? null,
|
|
128
|
+
mapping: doc?.mapping ?? null,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
module.exports = { IntegrationMappingRepositoryDocumentDB };
|
|
134
|
+
|
|
135
|
+
|
|
@@ -4,6 +4,9 @@ const {
|
|
|
4
4
|
const {
|
|
5
5
|
IntegrationMappingRepositoryPostgres,
|
|
6
6
|
} = require('./integration-mapping-repository-postgres');
|
|
7
|
+
const {
|
|
8
|
+
IntegrationMappingRepositoryDocumentDB,
|
|
9
|
+
} = require('./integration-mapping-repository-documentdb');
|
|
7
10
|
const config = require('../../database/config');
|
|
8
11
|
|
|
9
12
|
/**
|
|
@@ -35,9 +38,12 @@ function createIntegrationMappingRepository() {
|
|
|
35
38
|
case 'postgresql':
|
|
36
39
|
return new IntegrationMappingRepositoryPostgres();
|
|
37
40
|
|
|
41
|
+
case 'documentdb':
|
|
42
|
+
return new IntegrationMappingRepositoryDocumentDB();
|
|
43
|
+
|
|
38
44
|
default:
|
|
39
45
|
throw new Error(
|
|
40
|
-
`Unsupported database type: ${dbType}. Supported values: 'mongodb', 'postgresql'`
|
|
46
|
+
`Unsupported database type: ${dbType}. Supported values: 'mongodb', 'documentdb', 'postgresql'`
|
|
41
47
|
);
|
|
42
48
|
}
|
|
43
49
|
}
|
|
@@ -47,4 +53,5 @@ module.exports = {
|
|
|
47
53
|
// Export adapters for direct testing
|
|
48
54
|
IntegrationMappingRepositoryMongo,
|
|
49
55
|
IntegrationMappingRepositoryPostgres,
|
|
56
|
+
IntegrationMappingRepositoryDocumentDB,
|
|
50
57
|
};
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
const { prisma } = require('../../database/prisma');
|
|
2
|
+
const {
|
|
3
|
+
toObjectId,
|
|
4
|
+
toObjectIdArray,
|
|
5
|
+
fromObjectId,
|
|
6
|
+
findMany,
|
|
7
|
+
findOne,
|
|
8
|
+
insertOne,
|
|
9
|
+
updateOne,
|
|
10
|
+
deleteOne,
|
|
11
|
+
} = require('../../database/documentdb-utils');
|
|
12
|
+
const {
|
|
13
|
+
IntegrationRepositoryInterface,
|
|
14
|
+
} = require('./integration-repository-interface');
|
|
15
|
+
|
|
16
|
+
class IntegrationRepositoryDocumentDB extends IntegrationRepositoryInterface {
|
|
17
|
+
constructor() {
|
|
18
|
+
super();
|
|
19
|
+
this.prisma = prisma;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async findIntegrationsByUserId(userId) {
|
|
23
|
+
const objectId = toObjectId(userId);
|
|
24
|
+
const filter = objectId ? { userId: objectId } : {};
|
|
25
|
+
const records = await findMany(this.prisma, 'Integration', filter);
|
|
26
|
+
return records.map((doc) => this._mapIntegration(doc));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async deleteIntegrationById(integrationId) {
|
|
30
|
+
const objectId = toObjectId(integrationId);
|
|
31
|
+
if (!objectId) return { acknowledged: true, deletedCount: 0 };
|
|
32
|
+
const result = await deleteOne(this.prisma, 'Integration', { _id: objectId });
|
|
33
|
+
const deleted = result?.n ?? 0;
|
|
34
|
+
return { acknowledged: true, deletedCount: deleted };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async findIntegrationByName(name) {
|
|
38
|
+
const doc = await findOne(this.prisma, 'Integration', { 'config.type': name });
|
|
39
|
+
if (!doc) {
|
|
40
|
+
throw new Error(`Integration with name ${name} not found`);
|
|
41
|
+
}
|
|
42
|
+
return this._mapIntegration(doc);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async findIntegrationById(id) {
|
|
46
|
+
const objectId = toObjectId(id);
|
|
47
|
+
if (!objectId) {
|
|
48
|
+
throw new Error(`Integration with id ${id} not found`);
|
|
49
|
+
}
|
|
50
|
+
const doc = await findOne(this.prisma, 'Integration', { _id: objectId });
|
|
51
|
+
if (!doc) {
|
|
52
|
+
throw new Error(`Integration with id ${id} not found`);
|
|
53
|
+
}
|
|
54
|
+
return this._mapIntegration(doc);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async updateIntegrationStatus(integrationId, status) {
|
|
58
|
+
const objectId = toObjectId(integrationId);
|
|
59
|
+
if (!objectId) return false;
|
|
60
|
+
await updateOne(
|
|
61
|
+
this.prisma,
|
|
62
|
+
'Integration',
|
|
63
|
+
{ _id: objectId },
|
|
64
|
+
{
|
|
65
|
+
$set: { status, updatedAt: new Date() },
|
|
66
|
+
}
|
|
67
|
+
);
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async updateIntegrationMessages(
|
|
72
|
+
integrationId,
|
|
73
|
+
messageType,
|
|
74
|
+
messageTitle,
|
|
75
|
+
messageBody,
|
|
76
|
+
messageTimestamp
|
|
77
|
+
) {
|
|
78
|
+
const objectId = toObjectId(integrationId);
|
|
79
|
+
if (!objectId) {
|
|
80
|
+
throw new Error(`Integration ${integrationId} not found`);
|
|
81
|
+
}
|
|
82
|
+
const existing = await findOne(this.prisma, 'Integration', { _id: objectId });
|
|
83
|
+
if (!existing) {
|
|
84
|
+
throw new Error(`Integration ${integrationId} not found`);
|
|
85
|
+
}
|
|
86
|
+
const messages = this._extractMessages(existing);
|
|
87
|
+
const list = Array.isArray(messages[messageType]) ? [...messages[messageType]] : [];
|
|
88
|
+
list.push({
|
|
89
|
+
title: messageTitle ?? null,
|
|
90
|
+
message: messageBody,
|
|
91
|
+
timestamp: messageTimestamp,
|
|
92
|
+
});
|
|
93
|
+
const updatedMessages = { ...messages, [messageType]: list };
|
|
94
|
+
await updateOne(
|
|
95
|
+
this.prisma,
|
|
96
|
+
'Integration',
|
|
97
|
+
{ _id: objectId },
|
|
98
|
+
{
|
|
99
|
+
$set: {
|
|
100
|
+
messages: updatedMessages,
|
|
101
|
+
errors: updatedMessages.errors ?? [],
|
|
102
|
+
warnings: updatedMessages.warnings ?? [],
|
|
103
|
+
info: updatedMessages.info ?? [],
|
|
104
|
+
logs: updatedMessages.logs ?? [],
|
|
105
|
+
updatedAt: new Date(),
|
|
106
|
+
},
|
|
107
|
+
}
|
|
108
|
+
);
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
async createIntegration(entities, userId, config) {
|
|
113
|
+
const now = new Date();
|
|
114
|
+
const document = {
|
|
115
|
+
userId: toObjectId(userId) || null,
|
|
116
|
+
config,
|
|
117
|
+
version: '0.0.0',
|
|
118
|
+
status: 'ENABLED',
|
|
119
|
+
entityIds: toObjectIdArray(entities),
|
|
120
|
+
messages: { errors: [], warnings: [], info: [], logs: [] },
|
|
121
|
+
errors: [],
|
|
122
|
+
warnings: [],
|
|
123
|
+
info: [],
|
|
124
|
+
logs: [],
|
|
125
|
+
createdAt: now,
|
|
126
|
+
updatedAt: now,
|
|
127
|
+
};
|
|
128
|
+
const insertedId = await insertOne(this.prisma, 'Integration', document);
|
|
129
|
+
const created = await findOne(this.prisma, 'Integration', { _id: insertedId });
|
|
130
|
+
return this._mapIntegration(created);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
async findIntegrationByUserId(userId) {
|
|
134
|
+
const objectId = toObjectId(userId);
|
|
135
|
+
if (!objectId) return null;
|
|
136
|
+
const doc = await findOne(this.prisma, 'Integration', { userId: objectId });
|
|
137
|
+
return doc ? this._mapIntegration(doc) : null;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
async updateIntegrationConfig(integrationId, config) {
|
|
141
|
+
if (config === null || config === undefined) {
|
|
142
|
+
throw new Error('Config parameter is required');
|
|
143
|
+
}
|
|
144
|
+
const objectId = toObjectId(integrationId);
|
|
145
|
+
if (!objectId) {
|
|
146
|
+
throw new Error(`Integration with id ${integrationId} not found`);
|
|
147
|
+
}
|
|
148
|
+
await updateOne(
|
|
149
|
+
this.prisma,
|
|
150
|
+
'Integration',
|
|
151
|
+
{ _id: objectId },
|
|
152
|
+
{
|
|
153
|
+
$set: {
|
|
154
|
+
config,
|
|
155
|
+
updatedAt: new Date(),
|
|
156
|
+
},
|
|
157
|
+
}
|
|
158
|
+
);
|
|
159
|
+
const updated = await findOne(this.prisma, 'Integration', { _id: objectId });
|
|
160
|
+
return this._mapIntegration(updated);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
_mapIntegration(doc) {
|
|
164
|
+
const messages = this._extractMessages(doc);
|
|
165
|
+
return {
|
|
166
|
+
id: fromObjectId(doc?._id),
|
|
167
|
+
entitiesIds: (doc?.entityIds || []).map((value) => fromObjectId(value)),
|
|
168
|
+
userId: fromObjectId(doc?.userId),
|
|
169
|
+
config: doc?.config ?? null,
|
|
170
|
+
version: doc?.version ?? null,
|
|
171
|
+
status: doc?.status ?? null,
|
|
172
|
+
messages,
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
_extractMessages(doc) {
|
|
177
|
+
const base = doc?.messages && typeof doc.messages === 'object' ? doc.messages : {};
|
|
178
|
+
return {
|
|
179
|
+
errors: base.errors ?? doc?.errors ?? [],
|
|
180
|
+
warnings: base.warnings ?? doc?.warnings ?? [],
|
|
181
|
+
info: base.info ?? doc?.info ?? [],
|
|
182
|
+
logs: base.logs ?? doc?.logs ?? [],
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
module.exports = { IntegrationRepositoryDocumentDB };
|
|
188
|
+
|
|
189
|
+
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
const { IntegrationRepositoryMongo } = require('./integration-repository-mongo');
|
|
2
2
|
const { IntegrationRepositoryPostgres } = require('./integration-repository-postgres');
|
|
3
|
+
const {
|
|
4
|
+
IntegrationRepositoryDocumentDB,
|
|
5
|
+
} = require('./integration-repository-documentdb');
|
|
3
6
|
const config = require('../../database/config');
|
|
4
7
|
|
|
5
8
|
/**
|
|
@@ -29,9 +32,12 @@ function createIntegrationRepository() {
|
|
|
29
32
|
case 'postgresql':
|
|
30
33
|
return new IntegrationRepositoryPostgres();
|
|
31
34
|
|
|
35
|
+
case 'documentdb':
|
|
36
|
+
return new IntegrationRepositoryDocumentDB();
|
|
37
|
+
|
|
32
38
|
default:
|
|
33
39
|
throw new Error(
|
|
34
|
-
`Unsupported database type: ${dbType}. Supported values: 'mongodb', 'postgresql'`
|
|
40
|
+
`Unsupported database type: ${dbType}. Supported values: 'mongodb', 'documentdb', 'postgresql'`
|
|
35
41
|
);
|
|
36
42
|
}
|
|
37
43
|
}
|
|
@@ -41,4 +47,5 @@ module.exports = {
|
|
|
41
47
|
// Export adapters for direct testing
|
|
42
48
|
IntegrationRepositoryMongo,
|
|
43
49
|
IntegrationRepositoryPostgres,
|
|
50
|
+
IntegrationRepositoryDocumentDB,
|
|
44
51
|
};
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
const { prisma } = require('../../database/prisma');
|
|
2
|
+
const {
|
|
3
|
+
toObjectId,
|
|
4
|
+
fromObjectId,
|
|
5
|
+
findMany,
|
|
6
|
+
findOne,
|
|
7
|
+
insertOne,
|
|
8
|
+
updateOne,
|
|
9
|
+
deleteOne,
|
|
10
|
+
} = require('../../database/documentdb-utils');
|
|
11
|
+
const { ProcessRepositoryInterface } = require('./process-repository-interface');
|
|
12
|
+
|
|
13
|
+
class ProcessRepositoryDocumentDB extends ProcessRepositoryInterface {
|
|
14
|
+
constructor() {
|
|
15
|
+
super();
|
|
16
|
+
this.prisma = prisma;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async create(processData) {
|
|
20
|
+
const now = new Date();
|
|
21
|
+
const document = {
|
|
22
|
+
userId: toObjectId(processData.userId),
|
|
23
|
+
integrationId: toObjectId(processData.integrationId),
|
|
24
|
+
name: processData.name,
|
|
25
|
+
type: processData.type,
|
|
26
|
+
state: processData.state || 'INITIALIZING',
|
|
27
|
+
context: processData.context || {},
|
|
28
|
+
results: processData.results || {},
|
|
29
|
+
childProcesses: (processData.childProcesses || []).map((id) => toObjectId(id)).filter(Boolean),
|
|
30
|
+
parentProcessId: processData.parentProcessId ? toObjectId(processData.parentProcessId) : null,
|
|
31
|
+
createdAt: now,
|
|
32
|
+
updatedAt: now,
|
|
33
|
+
};
|
|
34
|
+
const insertedId = await insertOne(this.prisma, 'Process', document);
|
|
35
|
+
const created = await findOne(this.prisma, 'Process', { _id: insertedId });
|
|
36
|
+
return this._mapProcess(created);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async findById(processId) {
|
|
40
|
+
const objectId = toObjectId(processId);
|
|
41
|
+
if (!objectId) return null;
|
|
42
|
+
const doc = await findOne(this.prisma, 'Process', { _id: objectId });
|
|
43
|
+
return doc ? this._mapProcess(doc) : null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async update(processId, updates) {
|
|
47
|
+
const objectId = toObjectId(processId);
|
|
48
|
+
if (!objectId) return null;
|
|
49
|
+
const updatePayload = {};
|
|
50
|
+
if (updates.state !== undefined) updatePayload.state = updates.state;
|
|
51
|
+
if (updates.context !== undefined) updatePayload.context = updates.context;
|
|
52
|
+
if (updates.results !== undefined) updatePayload.results = updates.results;
|
|
53
|
+
if (updates.childProcesses !== undefined) {
|
|
54
|
+
updatePayload.childProcesses = (updates.childProcesses || []).map((id) => toObjectId(id)).filter(Boolean);
|
|
55
|
+
}
|
|
56
|
+
if (updates.parentProcessId !== undefined) {
|
|
57
|
+
updatePayload.parentProcessId = updates.parentProcessId ? toObjectId(updates.parentProcessId) : null;
|
|
58
|
+
}
|
|
59
|
+
updatePayload.updatedAt = new Date();
|
|
60
|
+
await updateOne(
|
|
61
|
+
this.prisma,
|
|
62
|
+
'Process',
|
|
63
|
+
{ _id: objectId },
|
|
64
|
+
{ $set: updatePayload }
|
|
65
|
+
);
|
|
66
|
+
const updated = await findOne(this.prisma, 'Process', { _id: objectId });
|
|
67
|
+
return updated ? this._mapProcess(updated) : null;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async findByIntegrationAndType(integrationId, type) {
|
|
71
|
+
const integrationObjectId = toObjectId(integrationId);
|
|
72
|
+
const filter = {
|
|
73
|
+
integrationId: integrationObjectId,
|
|
74
|
+
type,
|
|
75
|
+
};
|
|
76
|
+
const docs = await findMany(this.prisma, 'Process', filter, {
|
|
77
|
+
sort: { createdAt: -1 },
|
|
78
|
+
});
|
|
79
|
+
return docs.map((doc) => this._mapProcess(doc));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async findActiveProcesses(integrationId, excludeStates = ['COMPLETED', 'ERROR']) {
|
|
83
|
+
const integrationObjectId = toObjectId(integrationId);
|
|
84
|
+
const pipeline = [
|
|
85
|
+
{
|
|
86
|
+
$match: {
|
|
87
|
+
integrationId: integrationObjectId,
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
$match: {
|
|
92
|
+
state: { $nin: excludeStates },
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
{ $sort: { createdAt: -1 } },
|
|
96
|
+
];
|
|
97
|
+
const docs = await this.prisma.$runCommandRaw({
|
|
98
|
+
aggregate: 'Process',
|
|
99
|
+
pipeline,
|
|
100
|
+
cursor: {},
|
|
101
|
+
}).then((res) => res?.cursor?.firstBatch || []);
|
|
102
|
+
return docs.map((doc) => this._mapProcess(doc));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async findByName(name) {
|
|
106
|
+
const doc = await findOne(
|
|
107
|
+
this.prisma,
|
|
108
|
+
'Process',
|
|
109
|
+
{ name },
|
|
110
|
+
{ sort: { createdAt: -1 } }
|
|
111
|
+
);
|
|
112
|
+
return doc ? this._mapProcess(doc) : null;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async deleteById(processId) {
|
|
116
|
+
const objectId = toObjectId(processId);
|
|
117
|
+
if (!objectId) return;
|
|
118
|
+
await deleteOne(this.prisma, 'Process', { _id: objectId });
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
_mapProcess(doc) {
|
|
122
|
+
return {
|
|
123
|
+
id: fromObjectId(doc?._id),
|
|
124
|
+
userId: fromObjectId(doc?.userId),
|
|
125
|
+
integrationId: fromObjectId(doc?.integrationId),
|
|
126
|
+
name: doc?.name ?? null,
|
|
127
|
+
type: doc?.type ?? null,
|
|
128
|
+
state: doc?.state ?? null,
|
|
129
|
+
context: doc?.context ?? {},
|
|
130
|
+
results: doc?.results ?? {},
|
|
131
|
+
childProcesses: (doc?.childProcesses || []).map((id) => fromObjectId(id)),
|
|
132
|
+
parentProcessId: doc?.parentProcessId ? fromObjectId(doc.parentProcessId) : null,
|
|
133
|
+
createdAt: doc?.createdAt ? new Date(doc.createdAt) : null,
|
|
134
|
+
updatedAt: doc?.updatedAt ? new Date(doc.updatedAt) : null,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
module.exports = { ProcessRepositoryDocumentDB };
|
|
140
|
+
|
|
141
|
+
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
const { ProcessRepositoryMongo } = require('./process-repository-mongo');
|
|
2
2
|
const { ProcessRepositoryPostgres } = require('./process-repository-postgres');
|
|
3
|
+
const {
|
|
4
|
+
ProcessRepositoryDocumentDB,
|
|
5
|
+
} = require('./process-repository-documentdb');
|
|
3
6
|
const config = require('../../database/config');
|
|
4
7
|
|
|
5
8
|
/**
|
|
@@ -30,9 +33,12 @@ function createProcessRepository() {
|
|
|
30
33
|
case 'postgresql':
|
|
31
34
|
return new ProcessRepositoryPostgres();
|
|
32
35
|
|
|
36
|
+
case 'documentdb':
|
|
37
|
+
return new ProcessRepositoryDocumentDB();
|
|
38
|
+
|
|
33
39
|
default:
|
|
34
40
|
throw new Error(
|
|
35
|
-
`Unsupported database type: ${dbType}. Supported values: 'mongodb', 'postgresql'`
|
|
41
|
+
`Unsupported database type: ${dbType}. Supported values: 'mongodb', 'documentdb', 'postgresql'`
|
|
36
42
|
);
|
|
37
43
|
}
|
|
38
44
|
}
|
|
@@ -42,5 +48,6 @@ module.exports = {
|
|
|
42
48
|
// Export adapters for direct testing
|
|
43
49
|
ProcessRepositoryMongo,
|
|
44
50
|
ProcessRepositoryPostgres,
|
|
51
|
+
ProcessRepositoryDocumentDB,
|
|
45
52
|
};
|
|
46
53
|
|