@friggframework/core 2.0.0-next.43 → 2.0.0-next.45
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/database/config.js +29 -1
- package/database/use-cases/test-encryption-use-case.js +6 -5
- package/docs/PROCESS_MANAGEMENT_QUEUE_SPEC.md +517 -0
- package/handlers/WEBHOOKS.md +653 -0
- package/handlers/backend-utils.js +118 -3
- package/handlers/integration-event-dispatcher.test.js +68 -0
- package/handlers/routers/integration-webhook-routers.js +67 -0
- package/handlers/routers/integration-webhook-routers.test.js +126 -0
- package/handlers/webhook-flow.integration.test.js +356 -0
- package/handlers/workers/integration-defined-workers.test.js +184 -0
- package/index.js +16 -0
- package/integrations/WEBHOOK-QUICKSTART.md +151 -0
- package/integrations/integration-base.js +74 -3
- package/integrations/repositories/process-repository-factory.js +46 -0
- package/integrations/repositories/process-repository-interface.js +90 -0
- package/integrations/repositories/process-repository-mongo.js +190 -0
- package/integrations/repositories/process-repository-postgres.js +217 -0
- package/integrations/tests/doubles/dummy-integration-class.js +1 -8
- package/integrations/use-cases/create-process.js +128 -0
- package/integrations/use-cases/create-process.test.js +178 -0
- package/integrations/use-cases/get-process.js +87 -0
- package/integrations/use-cases/get-process.test.js +190 -0
- package/integrations/use-cases/index.js +8 -0
- package/integrations/use-cases/update-process-metrics.js +201 -0
- package/integrations/use-cases/update-process-metrics.test.js +308 -0
- package/integrations/use-cases/update-process-state.js +119 -0
- package/integrations/use-cases/update-process-state.test.js +256 -0
- package/package.json +5 -5
- package/prisma-mongodb/schema.prisma +44 -0
- package/prisma-postgresql/schema.prisma +45 -0
- package/queues/queuer-util.js +10 -0
- package/user/repositories/user-repository-mongo.js +53 -12
- package/user/repositories/user-repository-postgres.js +53 -14
- package/user/tests/use-cases/login-user.test.js +85 -5
- package/user/tests/user-password-encryption-isolation.test.js +237 -0
- package/user/tests/user-password-hashing.test.js +235 -0
- package/user/use-cases/login-user.js +1 -1
- package/user/user.js +2 -2
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
const { prisma } = require('../../database/prisma');
|
|
2
|
+
const {
|
|
3
|
+
ProcessRepositoryInterface,
|
|
4
|
+
} = require('./process-repository-interface');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* PostgreSQL Process Repository Adapter
|
|
8
|
+
* Handles process persistence using Prisma with PostgreSQL
|
|
9
|
+
*
|
|
10
|
+
* PostgreSQL-specific characteristics:
|
|
11
|
+
* - Uses foreign key constraints for relations
|
|
12
|
+
* - JSONB type for context and results (efficient querying)
|
|
13
|
+
* - Array type for childProcesses references
|
|
14
|
+
* - Transactional support available if needed
|
|
15
|
+
*
|
|
16
|
+
* Design Philosophy:
|
|
17
|
+
* - Same interface as MongoDB repository
|
|
18
|
+
* - Prisma abstracts away most database-specific details
|
|
19
|
+
* - Minor differences in JSON handling internally managed by Prisma
|
|
20
|
+
*/
|
|
21
|
+
class ProcessRepositoryPostgres extends ProcessRepositoryInterface {
|
|
22
|
+
constructor() {
|
|
23
|
+
super();
|
|
24
|
+
this.prisma = prisma;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Convert string ID to integer for PostgreSQL queries
|
|
29
|
+
* @private
|
|
30
|
+
* @param {string|number|null|undefined} id - ID to convert
|
|
31
|
+
* @returns {number|null|undefined} Integer ID or null/undefined
|
|
32
|
+
* @throws {Error} If ID cannot be converted to integer
|
|
33
|
+
*/
|
|
34
|
+
_convertId(id) {
|
|
35
|
+
if (id === null || id === undefined) return id;
|
|
36
|
+
const parsed = parseInt(id, 10);
|
|
37
|
+
if (isNaN(parsed)) {
|
|
38
|
+
throw new Error(`Invalid ID: ${id} cannot be converted to integer`);
|
|
39
|
+
}
|
|
40
|
+
return parsed;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Create a new process record
|
|
45
|
+
* @param {Object} processData - Process data to create
|
|
46
|
+
* @returns {Promise<Object>} Created process record
|
|
47
|
+
*/
|
|
48
|
+
async create(processData) {
|
|
49
|
+
const process = await this.prisma.process.create({
|
|
50
|
+
data: {
|
|
51
|
+
userId: this._convertId(processData.userId),
|
|
52
|
+
integrationId: this._convertId(processData.integrationId),
|
|
53
|
+
name: processData.name,
|
|
54
|
+
type: processData.type,
|
|
55
|
+
state: processData.state || 'INITIALIZING',
|
|
56
|
+
context: processData.context || {},
|
|
57
|
+
results: processData.results || {},
|
|
58
|
+
parentProcessId: this._convertId(processData.parentProcessId),
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
return this._toPlainObject(process);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Find a process by ID
|
|
67
|
+
* @param {string} processId - Process ID to find
|
|
68
|
+
* @returns {Promise<Object|null>} Process record or null if not found
|
|
69
|
+
*/
|
|
70
|
+
async findById(processId) {
|
|
71
|
+
const process = await this.prisma.process.findUnique({
|
|
72
|
+
where: { id: this._convertId(processId) },
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
return process ? this._toPlainObject(process) : null;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Update a process record
|
|
80
|
+
* @param {string} processId - Process ID to update
|
|
81
|
+
* @param {Object} updates - Fields to update
|
|
82
|
+
* @returns {Promise<Object>} Updated process record
|
|
83
|
+
*/
|
|
84
|
+
async update(processId, updates) {
|
|
85
|
+
// Prepare update data, excluding undefined values
|
|
86
|
+
const updateData = {};
|
|
87
|
+
|
|
88
|
+
if (updates.state !== undefined) {
|
|
89
|
+
updateData.state = updates.state;
|
|
90
|
+
}
|
|
91
|
+
if (updates.context !== undefined) {
|
|
92
|
+
updateData.context = updates.context;
|
|
93
|
+
}
|
|
94
|
+
if (updates.results !== undefined) {
|
|
95
|
+
updateData.results = updates.results;
|
|
96
|
+
}
|
|
97
|
+
if (updates.parentProcessId !== undefined) {
|
|
98
|
+
updateData.parentProcessId = this._convertId(
|
|
99
|
+
updates.parentProcessId
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const process = await this.prisma.process.update({
|
|
104
|
+
where: { id: this._convertId(processId) },
|
|
105
|
+
data: updateData,
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
return this._toPlainObject(process);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Find processes by integration and type
|
|
113
|
+
* @param {string} integrationId - Integration ID
|
|
114
|
+
* @param {string} type - Process type
|
|
115
|
+
* @returns {Promise<Array>} Array of process records
|
|
116
|
+
*/
|
|
117
|
+
async findByIntegrationAndType(integrationId, type) {
|
|
118
|
+
const processes = await this.prisma.process.findMany({
|
|
119
|
+
where: {
|
|
120
|
+
integrationId: this._convertId(integrationId),
|
|
121
|
+
type,
|
|
122
|
+
},
|
|
123
|
+
orderBy: {
|
|
124
|
+
createdAt: 'desc',
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
return processes.map((p) => this._toPlainObject(p));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Find active processes (not in excluded states)
|
|
133
|
+
* @param {string} integrationId - Integration ID
|
|
134
|
+
* @param {string[]} [excludeStates=['COMPLETED', 'ERROR']] - States to exclude
|
|
135
|
+
* @returns {Promise<Array>} Array of active process records
|
|
136
|
+
*/
|
|
137
|
+
async findActiveProcesses(
|
|
138
|
+
integrationId,
|
|
139
|
+
excludeStates = ['COMPLETED', 'ERROR']
|
|
140
|
+
) {
|
|
141
|
+
const processes = await this.prisma.process.findMany({
|
|
142
|
+
where: {
|
|
143
|
+
integrationId: this._convertId(integrationId),
|
|
144
|
+
state: {
|
|
145
|
+
notIn: excludeStates,
|
|
146
|
+
},
|
|
147
|
+
},
|
|
148
|
+
orderBy: {
|
|
149
|
+
createdAt: 'desc',
|
|
150
|
+
},
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
return processes.map((p) => this._toPlainObject(p));
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Find a process by name (most recent)
|
|
158
|
+
* @param {string} name - Process name
|
|
159
|
+
* @returns {Promise<Object|null>} Most recent process with given name, or null
|
|
160
|
+
*/
|
|
161
|
+
async findByName(name) {
|
|
162
|
+
const process = await this.prisma.process.findFirst({
|
|
163
|
+
where: { name },
|
|
164
|
+
orderBy: {
|
|
165
|
+
createdAt: 'desc',
|
|
166
|
+
},
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
return process ? this._toPlainObject(process) : null;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Delete a process by ID
|
|
174
|
+
* @param {string} processId - Process ID to delete
|
|
175
|
+
* @returns {Promise<void>}
|
|
176
|
+
*/
|
|
177
|
+
async deleteById(processId) {
|
|
178
|
+
await this.prisma.process.delete({
|
|
179
|
+
where: { id: this._convertId(processId) },
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Convert Prisma model to plain JavaScript object
|
|
185
|
+
* Ensures consistent API across repository implementations
|
|
186
|
+
* @private
|
|
187
|
+
* @param {Object} process - Prisma process model
|
|
188
|
+
* @returns {Object} Plain process object
|
|
189
|
+
*/
|
|
190
|
+
_toPlainObject(process) {
|
|
191
|
+
return {
|
|
192
|
+
id: String(process.id),
|
|
193
|
+
userId: String(process.userId),
|
|
194
|
+
integrationId: String(process.integrationId),
|
|
195
|
+
name: process.name,
|
|
196
|
+
type: process.type,
|
|
197
|
+
state: process.state,
|
|
198
|
+
context: process.context,
|
|
199
|
+
results: process.results,
|
|
200
|
+
childProcesses: Array.isArray(process.childProcesses)
|
|
201
|
+
? process.childProcesses.length > 0 &&
|
|
202
|
+
typeof process.childProcesses[0] === 'object' &&
|
|
203
|
+
process.childProcesses[0] !== null
|
|
204
|
+
? process.childProcesses.map((child) => String(child.id))
|
|
205
|
+
: process.childProcesses
|
|
206
|
+
: [],
|
|
207
|
+
parentProcessId:
|
|
208
|
+
process.parentProcessId !== null
|
|
209
|
+
? String(process.parentProcessId)
|
|
210
|
+
: null,
|
|
211
|
+
createdAt: process.createdAt,
|
|
212
|
+
updatedAt: process.updatedAt,
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
module.exports = { ProcessRepositoryPostgres };
|
|
@@ -47,23 +47,16 @@ class DummyIntegration extends IntegrationBase {
|
|
|
47
47
|
this.updateIntegrationMessages = {
|
|
48
48
|
execute: jest.fn().mockResolvedValue({})
|
|
49
49
|
};
|
|
50
|
-
|
|
51
|
-
this.registerEventHandlers();
|
|
52
50
|
}
|
|
53
51
|
|
|
54
52
|
async loadDynamicUserActions() {
|
|
55
53
|
return {};
|
|
56
54
|
}
|
|
57
55
|
|
|
58
|
-
async registerEventHandlers() {
|
|
59
|
-
super.registerEventHandlers();
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
56
|
async send(event, data) {
|
|
64
57
|
this.sendSpy(event, data);
|
|
65
58
|
this.eventCallHistory.push({ event, data, timestamp: Date.now() });
|
|
66
|
-
return
|
|
59
|
+
return { event, data };
|
|
67
60
|
}
|
|
68
61
|
|
|
69
62
|
async initialize() {
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CreateProcess Use Case
|
|
3
|
+
*
|
|
4
|
+
* Creates a new process record for tracking long-running operations.
|
|
5
|
+
* Validates required fields and delegates persistence to the repository.
|
|
6
|
+
*
|
|
7
|
+
* Design Philosophy:
|
|
8
|
+
* - Use cases encapsulate business logic
|
|
9
|
+
* - Validation happens at the use case layer
|
|
10
|
+
* - Repositories handle only data access
|
|
11
|
+
* - Process model is generic and reusable
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const createProcess = new CreateProcess({ processRepository });
|
|
15
|
+
* const process = await createProcess.execute({
|
|
16
|
+
* userId: 'user123',
|
|
17
|
+
* integrationId: 'integration456',
|
|
18
|
+
* name: 'zoho-crm-contact-sync',
|
|
19
|
+
* type: 'CRM_SYNC',
|
|
20
|
+
* state: 'INITIALIZING',
|
|
21
|
+
* context: { syncType: 'INITIAL', totalRecords: 0 },
|
|
22
|
+
* results: { aggregateData: { totalSynced: 0, totalFailed: 0 } }
|
|
23
|
+
* });
|
|
24
|
+
*/
|
|
25
|
+
class CreateProcess {
|
|
26
|
+
/**
|
|
27
|
+
* @param {Object} params
|
|
28
|
+
* @param {ProcessRepositoryInterface} params.processRepository - Repository for process data access
|
|
29
|
+
*/
|
|
30
|
+
constructor({ processRepository }) {
|
|
31
|
+
if (!processRepository) {
|
|
32
|
+
throw new Error('processRepository is required');
|
|
33
|
+
}
|
|
34
|
+
this.processRepository = processRepository;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Execute the use case to create a process
|
|
39
|
+
* @param {Object} processData - Process data to create
|
|
40
|
+
* @param {string} processData.userId - User ID (required)
|
|
41
|
+
* @param {string} processData.integrationId - Integration ID (required)
|
|
42
|
+
* @param {string} processData.name - Process name (required)
|
|
43
|
+
* @param {string} processData.type - Process type (required)
|
|
44
|
+
* @param {string} [processData.state='INITIALIZING'] - Initial state
|
|
45
|
+
* @param {Object} [processData.context={}] - Process context
|
|
46
|
+
* @param {Object} [processData.results={}] - Process results
|
|
47
|
+
* @param {string[]} [processData.childProcesses=[]] - Child process IDs
|
|
48
|
+
* @param {string} [processData.parentProcessId] - Parent process ID
|
|
49
|
+
* @returns {Promise<Object>} Created process record
|
|
50
|
+
* @throws {Error} If validation fails or creation errors
|
|
51
|
+
*/
|
|
52
|
+
async execute(processData) {
|
|
53
|
+
// Validate required fields
|
|
54
|
+
this._validateProcessData(processData);
|
|
55
|
+
|
|
56
|
+
// Set defaults for optional fields
|
|
57
|
+
const processToCreate = {
|
|
58
|
+
userId: processData.userId,
|
|
59
|
+
integrationId: processData.integrationId,
|
|
60
|
+
name: processData.name,
|
|
61
|
+
type: processData.type,
|
|
62
|
+
state: processData.state || 'INITIALIZING',
|
|
63
|
+
context: processData.context || {},
|
|
64
|
+
results: processData.results || {},
|
|
65
|
+
childProcesses: processData.childProcesses || [],
|
|
66
|
+
parentProcessId: processData.parentProcessId || null,
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
// Delegate to repository
|
|
70
|
+
try {
|
|
71
|
+
const createdProcess = await this.processRepository.create(processToCreate);
|
|
72
|
+
return createdProcess;
|
|
73
|
+
} catch (error) {
|
|
74
|
+
throw new Error(`Failed to create process: ${error.message}`);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Validate process data
|
|
80
|
+
* @private
|
|
81
|
+
* @param {Object} processData - Process data to validate
|
|
82
|
+
* @throws {Error} If validation fails
|
|
83
|
+
*/
|
|
84
|
+
_validateProcessData(processData) {
|
|
85
|
+
const requiredFields = ['userId', 'integrationId', 'name', 'type'];
|
|
86
|
+
const missingFields = requiredFields.filter(field => !processData[field]);
|
|
87
|
+
|
|
88
|
+
if (missingFields.length > 0) {
|
|
89
|
+
throw new Error(
|
|
90
|
+
`Missing required fields for process creation: ${missingFields.join(', ')}`
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Validate field types
|
|
95
|
+
if (typeof processData.userId !== 'string') {
|
|
96
|
+
throw new Error('userId must be a string');
|
|
97
|
+
}
|
|
98
|
+
if (typeof processData.integrationId !== 'string') {
|
|
99
|
+
throw new Error('integrationId must be a string');
|
|
100
|
+
}
|
|
101
|
+
if (typeof processData.name !== 'string') {
|
|
102
|
+
throw new Error('name must be a string');
|
|
103
|
+
}
|
|
104
|
+
if (typeof processData.type !== 'string') {
|
|
105
|
+
throw new Error('type must be a string');
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Validate optional fields if provided
|
|
109
|
+
if (processData.state && typeof processData.state !== 'string') {
|
|
110
|
+
throw new Error('state must be a string');
|
|
111
|
+
}
|
|
112
|
+
if (processData.context && typeof processData.context !== 'object') {
|
|
113
|
+
throw new Error('context must be an object');
|
|
114
|
+
}
|
|
115
|
+
if (processData.results && typeof processData.results !== 'object') {
|
|
116
|
+
throw new Error('results must be an object');
|
|
117
|
+
}
|
|
118
|
+
if (processData.childProcesses && !Array.isArray(processData.childProcesses)) {
|
|
119
|
+
throw new Error('childProcesses must be an array');
|
|
120
|
+
}
|
|
121
|
+
if (processData.parentProcessId && typeof processData.parentProcessId !== 'string') {
|
|
122
|
+
throw new Error('parentProcessId must be a string');
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
module.exports = { CreateProcess };
|
|
128
|
+
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CreateProcess Use Case Tests
|
|
3
|
+
*
|
|
4
|
+
* Tests process creation with validation and error handling.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const { CreateProcess } = require('./create-process');
|
|
8
|
+
|
|
9
|
+
describe('CreateProcess', () => {
|
|
10
|
+
let createProcessUseCase;
|
|
11
|
+
let mockProcessRepository;
|
|
12
|
+
|
|
13
|
+
beforeEach(() => {
|
|
14
|
+
mockProcessRepository = {
|
|
15
|
+
create: jest.fn(),
|
|
16
|
+
};
|
|
17
|
+
createProcessUseCase = new CreateProcess({
|
|
18
|
+
processRepository: mockProcessRepository,
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
describe('constructor', () => {
|
|
23
|
+
it('should require processRepository', () => {
|
|
24
|
+
expect(() => new CreateProcess({})).toThrow('processRepository is required');
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('should initialize with processRepository', () => {
|
|
28
|
+
expect(createProcessUseCase.processRepository).toBe(mockProcessRepository);
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
describe('execute', () => {
|
|
33
|
+
const validProcessData = {
|
|
34
|
+
userId: 'user-123',
|
|
35
|
+
integrationId: 'integration-456',
|
|
36
|
+
name: 'test-crm-contact-sync',
|
|
37
|
+
type: 'CRM_SYNC',
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
it('should create a process with minimal required data', async () => {
|
|
41
|
+
const mockCreatedProcess = { id: 'process-789', ...validProcessData };
|
|
42
|
+
mockProcessRepository.create.mockResolvedValue(mockCreatedProcess);
|
|
43
|
+
|
|
44
|
+
const result = await createProcessUseCase.execute(validProcessData);
|
|
45
|
+
|
|
46
|
+
expect(mockProcessRepository.create).toHaveBeenCalledWith({
|
|
47
|
+
userId: 'user-123',
|
|
48
|
+
integrationId: 'integration-456',
|
|
49
|
+
name: 'test-crm-contact-sync',
|
|
50
|
+
type: 'CRM_SYNC',
|
|
51
|
+
state: 'INITIALIZING',
|
|
52
|
+
context: {},
|
|
53
|
+
results: {},
|
|
54
|
+
childProcesses: [],
|
|
55
|
+
parentProcessId: null,
|
|
56
|
+
});
|
|
57
|
+
expect(result).toEqual(mockCreatedProcess);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('should create a process with all optional data', async () => {
|
|
61
|
+
const processDataWithOptions = {
|
|
62
|
+
...validProcessData,
|
|
63
|
+
state: 'FETCHING_TOTAL',
|
|
64
|
+
context: { syncType: 'INITIAL', totalRecords: 100 },
|
|
65
|
+
results: { aggregateData: { totalSynced: 0 } },
|
|
66
|
+
childProcesses: ['child-1', 'child-2'],
|
|
67
|
+
parentProcessId: 'parent-123',
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const mockCreatedProcess = { id: 'process-789', ...processDataWithOptions };
|
|
71
|
+
mockProcessRepository.create.mockResolvedValue(mockCreatedProcess);
|
|
72
|
+
|
|
73
|
+
const result = await createProcessUseCase.execute(processDataWithOptions);
|
|
74
|
+
|
|
75
|
+
expect(mockProcessRepository.create).toHaveBeenCalledWith(processDataWithOptions);
|
|
76
|
+
expect(result).toEqual(mockCreatedProcess);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('should throw error if userId is missing', async () => {
|
|
80
|
+
const invalidData = { integrationId: 'int-123', name: 'test', type: 'CRM_SYNC' };
|
|
81
|
+
|
|
82
|
+
await expect(createProcessUseCase.execute(invalidData))
|
|
83
|
+
.rejects.toThrow('Missing required fields for process creation: userId');
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('should throw error if integrationId is missing', async () => {
|
|
87
|
+
const invalidData = { userId: 'user-123', name: 'test', type: 'CRM_SYNC' };
|
|
88
|
+
|
|
89
|
+
await expect(createProcessUseCase.execute(invalidData))
|
|
90
|
+
.rejects.toThrow('Missing required fields for process creation: integrationId');
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('should throw error if name is missing', async () => {
|
|
94
|
+
const invalidData = { userId: 'user-123', integrationId: 'int-123', type: 'CRM_SYNC' };
|
|
95
|
+
|
|
96
|
+
await expect(createProcessUseCase.execute(invalidData))
|
|
97
|
+
.rejects.toThrow('Missing required fields for process creation: name');
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('should throw error if type is missing', async () => {
|
|
101
|
+
const invalidData = { userId: 'user-123', integrationId: 'int-123', name: 'test' };
|
|
102
|
+
|
|
103
|
+
await expect(createProcessUseCase.execute(invalidData))
|
|
104
|
+
.rejects.toThrow('Missing required fields for process creation: type');
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('should throw error if userId is not a string', async () => {
|
|
108
|
+
const invalidData = { ...validProcessData, userId: 123 };
|
|
109
|
+
|
|
110
|
+
await expect(createProcessUseCase.execute(invalidData))
|
|
111
|
+
.rejects.toThrow('userId must be a string');
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('should throw error if integrationId is not a string', async () => {
|
|
115
|
+
const invalidData = { ...validProcessData, integrationId: 456 };
|
|
116
|
+
|
|
117
|
+
await expect(createProcessUseCase.execute(invalidData))
|
|
118
|
+
.rejects.toThrow('integrationId must be a string');
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('should throw error if name is not a string', async () => {
|
|
122
|
+
const invalidData = { ...validProcessData, name: 789 };
|
|
123
|
+
|
|
124
|
+
await expect(createProcessUseCase.execute(invalidData))
|
|
125
|
+
.rejects.toThrow('name must be a string');
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('should throw error if type is not a string', async () => {
|
|
129
|
+
const invalidData = { ...validProcessData, type: 999 };
|
|
130
|
+
|
|
131
|
+
await expect(createProcessUseCase.execute(invalidData))
|
|
132
|
+
.rejects.toThrow('type must be a string');
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('should throw error if state is provided but not a string', async () => {
|
|
136
|
+
const invalidData = { ...validProcessData, state: 123 };
|
|
137
|
+
|
|
138
|
+
await expect(createProcessUseCase.execute(invalidData))
|
|
139
|
+
.rejects.toThrow('state must be a string');
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it('should throw error if context is provided but not an object', async () => {
|
|
143
|
+
const invalidData = { ...validProcessData, context: 'invalid' };
|
|
144
|
+
|
|
145
|
+
await expect(createProcessUseCase.execute(invalidData))
|
|
146
|
+
.rejects.toThrow('context must be an object');
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it('should throw error if results is provided but not an object', async () => {
|
|
150
|
+
const invalidData = { ...validProcessData, results: 'invalid' };
|
|
151
|
+
|
|
152
|
+
await expect(createProcessUseCase.execute(invalidData))
|
|
153
|
+
.rejects.toThrow('results must be an object');
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it('should throw error if childProcesses is provided but not an array', async () => {
|
|
157
|
+
const invalidData = { ...validProcessData, childProcesses: 'invalid' };
|
|
158
|
+
|
|
159
|
+
await expect(createProcessUseCase.execute(invalidData))
|
|
160
|
+
.rejects.toThrow('childProcesses must be an array');
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it('should throw error if parentProcessId is provided but not a string', async () => {
|
|
164
|
+
const invalidData = { ...validProcessData, parentProcessId: 123 };
|
|
165
|
+
|
|
166
|
+
await expect(createProcessUseCase.execute(invalidData))
|
|
167
|
+
.rejects.toThrow('parentProcessId must be a string');
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it('should handle repository errors', async () => {
|
|
171
|
+
const repositoryError = new Error('Database connection failed');
|
|
172
|
+
mockProcessRepository.create.mockRejectedValue(repositoryError);
|
|
173
|
+
|
|
174
|
+
await expect(createProcessUseCase.execute(validProcessData))
|
|
175
|
+
.rejects.toThrow('Failed to create process: Database connection failed');
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
});
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GetProcess Use Case
|
|
3
|
+
*
|
|
4
|
+
* Retrieves a process by ID with proper error handling.
|
|
5
|
+
* Simple use case that delegates to repository.
|
|
6
|
+
*
|
|
7
|
+
* Design Philosophy:
|
|
8
|
+
* - Use cases provide consistent error handling
|
|
9
|
+
* - Business logic layer between controllers and repositories
|
|
10
|
+
* - Return null for not found vs throwing error (configurable)
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const getProcess = new GetProcess({ processRepository });
|
|
14
|
+
* const process = await getProcess.execute(processId);
|
|
15
|
+
* // or
|
|
16
|
+
* const process = await getProcess.executeOrThrow(processId);
|
|
17
|
+
*/
|
|
18
|
+
class GetProcess {
|
|
19
|
+
/**
|
|
20
|
+
* @param {Object} params
|
|
21
|
+
* @param {ProcessRepositoryInterface} params.processRepository - Repository for process data access
|
|
22
|
+
*/
|
|
23
|
+
constructor({ processRepository }) {
|
|
24
|
+
if (!processRepository) {
|
|
25
|
+
throw new Error('processRepository is required');
|
|
26
|
+
}
|
|
27
|
+
this.processRepository = processRepository;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Execute the use case to get a process by ID
|
|
32
|
+
* @param {string} processId - Process ID to retrieve
|
|
33
|
+
* @returns {Promise<Object|null>} Process record or null if not found
|
|
34
|
+
* @throws {Error} If processId is invalid
|
|
35
|
+
*/
|
|
36
|
+
async execute(processId) {
|
|
37
|
+
// Validate input
|
|
38
|
+
if (!processId || typeof processId !== 'string') {
|
|
39
|
+
throw new Error('processId must be a non-empty string');
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Delegate to repository
|
|
43
|
+
try {
|
|
44
|
+
const process = await this.processRepository.findById(processId);
|
|
45
|
+
return process;
|
|
46
|
+
} catch (error) {
|
|
47
|
+
throw new Error(`Failed to retrieve process: ${error.message}`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Execute and throw if process not found
|
|
53
|
+
* @param {string} processId - Process ID to retrieve
|
|
54
|
+
* @returns {Promise<Object>} Process record
|
|
55
|
+
* @throws {Error} If process not found or retrieval fails
|
|
56
|
+
*/
|
|
57
|
+
async executeOrThrow(processId) {
|
|
58
|
+
const process = await this.execute(processId);
|
|
59
|
+
|
|
60
|
+
if (!process) {
|
|
61
|
+
throw new Error(`Process not found: ${processId}`);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return process;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Get multiple processes by IDs
|
|
69
|
+
* @param {string[]} processIds - Array of process IDs
|
|
70
|
+
* @returns {Promise<Array>} Array of process records (excludes not found)
|
|
71
|
+
*/
|
|
72
|
+
async executeMany(processIds) {
|
|
73
|
+
if (!Array.isArray(processIds)) {
|
|
74
|
+
throw new Error('processIds must be an array');
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const processes = await Promise.all(
|
|
78
|
+
processIds.map(id => this.execute(id))
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
// Filter out nulls (not found)
|
|
82
|
+
return processes.filter(p => p !== null);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
module.exports = { GetProcess };
|
|
87
|
+
|