ti2 1.0.120 → 1.0.121
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/api.yml +12 -1
- package/controllers/__tests__/user.js +119 -10
- package/controllers/app.js +61 -27
- package/package.json +1 -1
- package/.windsurfrules +0 -9
- package/CLAUDE.md +0 -166
package/api.yml
CHANGED
|
@@ -101,6 +101,17 @@ components:
|
|
|
101
101
|
properties:
|
|
102
102
|
value:
|
|
103
103
|
type: string
|
|
104
|
+
CreateAppTokenResponse:
|
|
105
|
+
type: object
|
|
106
|
+
required:
|
|
107
|
+
- value
|
|
108
|
+
- isFirstIntegration
|
|
109
|
+
properties:
|
|
110
|
+
value:
|
|
111
|
+
type: string
|
|
112
|
+
isFirstIntegration:
|
|
113
|
+
type: boolean
|
|
114
|
+
description: True only when this transaction created the user's first integration credential.
|
|
104
115
|
Status:
|
|
105
116
|
type: object
|
|
106
117
|
properties:
|
|
@@ -862,7 +873,7 @@ paths:
|
|
|
862
873
|
content:
|
|
863
874
|
application/json:
|
|
864
875
|
schema:
|
|
865
|
-
$ref: '#/components/schemas/
|
|
876
|
+
$ref: '#/components/schemas/CreateAppTokenResponse'
|
|
866
877
|
delete:
|
|
867
878
|
tags:
|
|
868
879
|
- app
|
|
@@ -20,7 +20,7 @@ describe('user', () => {
|
|
|
20
20
|
let doApiGet; let doApiPost; let
|
|
21
21
|
doApiDelete;
|
|
22
22
|
let app;
|
|
23
|
-
let
|
|
23
|
+
let db;
|
|
24
24
|
let appKey;
|
|
25
25
|
const userId = chance.guid();
|
|
26
26
|
let apiKey = chance.guid();
|
|
@@ -33,7 +33,7 @@ describe('user', () => {
|
|
|
33
33
|
beforeAll(async () => {
|
|
34
34
|
({
|
|
35
35
|
app,
|
|
36
|
-
sqldb,
|
|
36
|
+
sqldb: db,
|
|
37
37
|
doApiDelete,
|
|
38
38
|
doApiGet,
|
|
39
39
|
doApiPost,
|
|
@@ -41,6 +41,7 @@ describe('user', () => {
|
|
|
41
41
|
} = await testUtils({
|
|
42
42
|
plugins: [appName],
|
|
43
43
|
}));
|
|
44
|
+
await db.models.UserAppKey.destroy({ where: { userId } });
|
|
44
45
|
// create an App
|
|
45
46
|
({ value: appKey } = await doApiPost({
|
|
46
47
|
url: '/app',
|
|
@@ -117,7 +118,7 @@ describe('user', () => {
|
|
|
117
118
|
// set up new token
|
|
118
119
|
apiKey = chance.guid();
|
|
119
120
|
token.apiKey = apiKey;
|
|
120
|
-
await doApiPost({
|
|
121
|
+
const createResult = await doApiPost({
|
|
121
122
|
url: `/${appName}/${userId}`,
|
|
122
123
|
token: userKey,
|
|
123
124
|
payload: {
|
|
@@ -125,6 +126,7 @@ describe('user', () => {
|
|
|
125
126
|
token,
|
|
126
127
|
},
|
|
127
128
|
});
|
|
129
|
+
expect(createResult.isFirstIntegration).toBe(true);
|
|
128
130
|
// make sure the app token is there
|
|
129
131
|
const { userAppKeys } = await doApiGet({
|
|
130
132
|
url: `/user/${userId}/apps`,
|
|
@@ -140,11 +142,117 @@ describe('user', () => {
|
|
|
140
142
|
]),
|
|
141
143
|
);
|
|
142
144
|
});
|
|
145
|
+
it('reports post-save cron setup failure while preserving the committed credential', async () => {
|
|
146
|
+
const postCommitUserId = chance.guid();
|
|
147
|
+
const postCommitUserKey = (await doApiPost({
|
|
148
|
+
url: '/user',
|
|
149
|
+
token: adminKey,
|
|
150
|
+
payload: { userId: postCommitUserId },
|
|
151
|
+
})).value;
|
|
152
|
+
const cronLookup = jest.spyOn(db.models.CronJobs, 'findOne')
|
|
153
|
+
.mockRejectedValueOnce(new Error('cron unavailable'));
|
|
154
|
+
const consoleError = jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
155
|
+
try {
|
|
156
|
+
const result = await doApiPost({
|
|
157
|
+
url: `/${appName}/${postCommitUserId}`,
|
|
158
|
+
token: postCommitUserKey,
|
|
159
|
+
payload: { tokenHint: 'post-commit', token },
|
|
160
|
+
expectStatusCode: 500,
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
expect(result.message).toContain('cron unavailable');
|
|
164
|
+
expect(await db.models.UserAppKey.count({ where: { userId: postCommitUserId } })).toBe(1);
|
|
165
|
+
} finally {
|
|
166
|
+
cronLookup.mockRestore();
|
|
167
|
+
consoleError.mockRestore();
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('serializes competing first integrations and marks exactly one as first', async () => {
|
|
172
|
+
const concurrentUserId = chance.guid();
|
|
173
|
+
const concurrentUserKey = (await doApiPost({
|
|
174
|
+
url: '/user',
|
|
175
|
+
token: adminKey,
|
|
176
|
+
payload: { userId: concurrentUserId },
|
|
177
|
+
})).value;
|
|
178
|
+
const lockTransaction = await db.transaction();
|
|
179
|
+
await db.models.User.findOne({
|
|
180
|
+
where: { userId: concurrentUserId },
|
|
181
|
+
transaction: lockTransaction,
|
|
182
|
+
lock: lockTransaction.LOCK.UPDATE,
|
|
183
|
+
});
|
|
184
|
+
const originalFindOne = db.models.User.findOne.bind(db.models.User);
|
|
185
|
+
const lockAttempts = [];
|
|
186
|
+
let releaseLockAttempts;
|
|
187
|
+
const bothLockAttempts = new Promise(resolveLockAttempts => {
|
|
188
|
+
releaseLockAttempts = resolveLockAttempts;
|
|
189
|
+
});
|
|
190
|
+
const findOne = jest.spyOn(db.models.User, 'findOne').mockImplementation(options => {
|
|
191
|
+
if (options.transaction !== lockTransaction && options.lock) {
|
|
192
|
+
lockAttempts.push(options);
|
|
193
|
+
if (lockAttempts.length === 2) releaseLockAttempts();
|
|
194
|
+
}
|
|
195
|
+
return originalFindOne(options);
|
|
196
|
+
});
|
|
197
|
+
let settled = 0;
|
|
198
|
+
let requests;
|
|
199
|
+
try {
|
|
200
|
+
requests = [
|
|
201
|
+
doApiPost({
|
|
202
|
+
url: `/${appName}/${concurrentUserId}`,
|
|
203
|
+
token: concurrentUserKey,
|
|
204
|
+
payload: { tokenHint: 'first-a', token },
|
|
205
|
+
}),
|
|
206
|
+
doApiPost({
|
|
207
|
+
url: `/${appName}/${concurrentUserId}`,
|
|
208
|
+
token: concurrentUserKey,
|
|
209
|
+
payload: { tokenHint: 'first-b', token },
|
|
210
|
+
}),
|
|
211
|
+
].map(postRequest => postRequest.finally(() => { settled += 1; }));
|
|
212
|
+
await bothLockAttempts;
|
|
213
|
+
expect(lockAttempts).toHaveLength(2);
|
|
214
|
+
expect(settled).toBe(0);
|
|
215
|
+
} finally {
|
|
216
|
+
findOne.mockRestore();
|
|
217
|
+
await lockTransaction.commit();
|
|
218
|
+
}
|
|
219
|
+
const results = await Promise.all(requests);
|
|
220
|
+
|
|
221
|
+
expect(results.map(result => result.isFirstIntegration).sort())
|
|
222
|
+
.toEqual([false, true]);
|
|
223
|
+
expect(await db.models.UserAppKey.count({ where: { userId: concurrentUserId } })).toBe(2);
|
|
224
|
+
});
|
|
143
225
|
const apiKey2 = chance.guid();
|
|
144
226
|
const token2 = { ...token };
|
|
227
|
+
it('replaces a sole existing integration without marking the update as first', async () => {
|
|
228
|
+
const replaceUserId = chance.guid();
|
|
229
|
+
const replaceUserKey = (await doApiPost({
|
|
230
|
+
url: '/user',
|
|
231
|
+
token: adminKey,
|
|
232
|
+
payload: { userId: replaceUserId },
|
|
233
|
+
})).value;
|
|
234
|
+
const replaceHint = 'replace-existing';
|
|
235
|
+
const first = await doApiPost({
|
|
236
|
+
url: `/${appName}/${replaceUserId}`,
|
|
237
|
+
token: replaceUserKey,
|
|
238
|
+
payload: { tokenHint: replaceHint, token },
|
|
239
|
+
});
|
|
240
|
+
const replacement = await doApiPost({
|
|
241
|
+
url: `/${appName}/${replaceUserId}`,
|
|
242
|
+
token: replaceUserKey,
|
|
243
|
+
payload: { tokenHint: replaceHint, token: { ...token, apiKey: chance.guid() } },
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
expect(first.isFirstIntegration).toBe(true);
|
|
247
|
+
expect(replacement.isFirstIntegration).toBe(false);
|
|
248
|
+
expect(await db.models.UserAppKey.count({
|
|
249
|
+
where: { userId: replaceUserId, integrationId: appName, hint: replaceHint },
|
|
250
|
+
})).toBe(1);
|
|
251
|
+
});
|
|
252
|
+
|
|
145
253
|
it('create a second token', async () => {
|
|
146
254
|
// set up new token
|
|
147
|
-
await doApiPost({
|
|
255
|
+
const createResult = await doApiPost({
|
|
148
256
|
url: `/${appName}/${userId}`,
|
|
149
257
|
token: userKey,
|
|
150
258
|
payload: {
|
|
@@ -152,6 +260,7 @@ describe('user', () => {
|
|
|
152
260
|
token: token2,
|
|
153
261
|
},
|
|
154
262
|
});
|
|
263
|
+
expect(createResult.isFirstIntegration).toBe(false);
|
|
155
264
|
});
|
|
156
265
|
it('should be able to read a user/app token', async () => {
|
|
157
266
|
const returnValue = await doApiGet({
|
|
@@ -257,7 +366,7 @@ describe('user', () => {
|
|
|
257
366
|
it('should return 422 when stored app token is corrupted', async () => {
|
|
258
367
|
const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
|
|
259
368
|
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
260
|
-
const [originalRows] = await
|
|
369
|
+
const [originalRows] = await db.query(
|
|
261
370
|
'SELECT id, appKey FROM UserAppKeys WHERE integrationId = :integrationId AND userId = :userId',
|
|
262
371
|
{
|
|
263
372
|
replacements: {
|
|
@@ -266,7 +375,7 @@ describe('user', () => {
|
|
|
266
375
|
},
|
|
267
376
|
},
|
|
268
377
|
);
|
|
269
|
-
await
|
|
378
|
+
await db.query(
|
|
270
379
|
'UPDATE UserAppKeys SET appKey = :appKey WHERE integrationId = :integrationId AND userId = :userId',
|
|
271
380
|
{
|
|
272
381
|
replacements: {
|
|
@@ -284,7 +393,7 @@ describe('user', () => {
|
|
|
284
393
|
});
|
|
285
394
|
expect(returnValue.message).toBe('Stored integration token is invalid. Please re-save credentials.');
|
|
286
395
|
} finally {
|
|
287
|
-
await Promise.all(originalRows.map(currentRow =>
|
|
396
|
+
await Promise.all(originalRows.map(currentRow => db.query(
|
|
288
397
|
'UPDATE UserAppKeys SET appKey = :appKey WHERE id = :id',
|
|
289
398
|
{
|
|
290
399
|
replacements: {
|
|
@@ -300,7 +409,7 @@ describe('user', () => {
|
|
|
300
409
|
it('should return 422 when stored app token is corrupted for hinted endpoint', async () => {
|
|
301
410
|
const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
|
|
302
411
|
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
303
|
-
const [originalRows] = await
|
|
412
|
+
const [originalRows] = await db.query(
|
|
304
413
|
'SELECT id, appKey FROM UserAppKeys WHERE integrationId = :integrationId AND userId = :userId AND hint = :hint',
|
|
305
414
|
{
|
|
306
415
|
replacements: {
|
|
@@ -310,7 +419,7 @@ describe('user', () => {
|
|
|
310
419
|
},
|
|
311
420
|
},
|
|
312
421
|
);
|
|
313
|
-
await
|
|
422
|
+
await db.query(
|
|
314
423
|
'UPDATE UserAppKeys SET appKey = :appKey WHERE integrationId = :integrationId AND userId = :userId AND hint = :hint',
|
|
315
424
|
{
|
|
316
425
|
replacements: {
|
|
@@ -329,7 +438,7 @@ describe('user', () => {
|
|
|
329
438
|
});
|
|
330
439
|
expect(returnValue.message).toBe('Stored integration token is invalid. Please re-save credentials.');
|
|
331
440
|
} finally {
|
|
332
|
-
await Promise.all(originalRows.map(currentRow =>
|
|
441
|
+
await Promise.all(originalRows.map(currentRow => db.query(
|
|
333
442
|
'UPDATE UserAppKeys SET appKey = :appKey WHERE id = :id',
|
|
334
443
|
{
|
|
335
444
|
replacements: {
|
package/controllers/app.js
CHANGED
|
@@ -98,6 +98,7 @@ const createAppToken = async (req, res, next) => {
|
|
|
98
98
|
userId,
|
|
99
99
|
},
|
|
100
100
|
} = req;
|
|
101
|
+
let transaction;
|
|
101
102
|
try {
|
|
102
103
|
const payload = {
|
|
103
104
|
integrationId,
|
|
@@ -105,20 +106,45 @@ const createAppToken = async (req, res, next) => {
|
|
|
105
106
|
hint,
|
|
106
107
|
appKey,
|
|
107
108
|
};
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
109
|
+
await sqldb.User.findOrCreate({ where: { userId }, defaults: { userId } });
|
|
110
|
+
|
|
111
|
+
transaction = await sqldb.sequelize.transaction();
|
|
112
|
+
await sqldb.User.findOne({
|
|
113
|
+
where: { userId },
|
|
114
|
+
transaction,
|
|
115
|
+
lock: transaction.LOCK.UPDATE,
|
|
116
|
+
});
|
|
117
|
+
const integrationCount = await sqldb.UserAppKey.count({
|
|
118
|
+
where: { userId },
|
|
119
|
+
transaction,
|
|
120
|
+
});
|
|
121
|
+
const isFirstIntegration = integrationCount === 0;
|
|
122
|
+
// Replacing an existing app/hint is an update, never a first integration.
|
|
114
123
|
const userAppKeyDup = await sqldb.UserAppKey.findOne({
|
|
115
124
|
where: { userId, integrationId, hint },
|
|
125
|
+
transaction,
|
|
116
126
|
});
|
|
117
|
-
if (userAppKeyDup) await userAppKeyDup.destroy();
|
|
118
|
-
const newAppKey = await sqldb.UserAppKey.create(payload);
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
127
|
+
if (userAppKeyDup) await userAppKeyDup.destroy({ transaction });
|
|
128
|
+
const newAppKey = await sqldb.UserAppKey.create(payload, { transaction });
|
|
129
|
+
try {
|
|
130
|
+
await transaction.commit();
|
|
131
|
+
} catch (commitErr) {
|
|
132
|
+
// Sequelize marks failed commits as finished, so a rollback would mask the original error.
|
|
133
|
+
transaction = null;
|
|
134
|
+
throw commitErr;
|
|
135
|
+
}
|
|
136
|
+
transaction = null;
|
|
137
|
+
|
|
138
|
+
const responsePayload = {
|
|
139
|
+
value: newAppKey.get('id').toString(),
|
|
140
|
+
isFirstIntegration,
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
// Credential persistence is already committed, but cron provisioning must
|
|
144
|
+
// remain caller-visible so missing scheduled functionality is not silent.
|
|
145
|
+
try {
|
|
146
|
+
await bb.each(req.app.plugins, async plugin => {
|
|
147
|
+
if (!Array.isArray(plugin.jobs)) return;
|
|
122
148
|
const validJobs = plugin.jobs.filter(job => Boolean(job.cron) && Boolean(job.method));
|
|
123
149
|
await bb.each(validJobs, async job => {
|
|
124
150
|
const where = {
|
|
@@ -144,40 +170,48 @@ const createAppToken = async (req, res, next) => {
|
|
|
144
170
|
...(job.params || {}),
|
|
145
171
|
removeOnComplete: false,
|
|
146
172
|
};
|
|
147
|
-
let bullJobId;
|
|
148
173
|
if (existing) {
|
|
149
174
|
const bullJob = await queue.getJob(job.bullJobId);
|
|
150
175
|
if (!bullJob) {
|
|
151
|
-
|
|
152
|
-
existing.bullJobId = (rawBullJobId && typeof rawBullJobId === 'object'
|
|
176
|
+
const rawBullJobId = await addJob(jobPayload, jobParams);
|
|
177
|
+
existing.bullJobId = (rawBullJobId && typeof rawBullJobId === 'object'
|
|
178
|
+
&& rawBullJobId.id) ? rawBullJobId.id : rawBullJobId;
|
|
153
179
|
await existing.save();
|
|
154
180
|
} else {
|
|
155
|
-
// make sure the cron is the same
|
|
156
|
-
// bullJobId here is from existing.bullJobId, which should be a string.
|
|
157
|
-
// The queue.getJob expects a string ID.
|
|
158
181
|
const bullCron = R.path(
|
|
159
182
|
['opts', 'repeat', 'cron'],
|
|
160
|
-
await queue.getJob(existing.bullJobId),
|
|
183
|
+
await queue.getJob(existing.bullJobId),
|
|
161
184
|
);
|
|
162
185
|
if (bullCron !== job.cron) {
|
|
163
|
-
await queue.removeJobs(existing.bullJobId);
|
|
164
|
-
|
|
165
|
-
existing.bullJobId = (rawBullJobId && typeof rawBullJobId === 'object'
|
|
186
|
+
await queue.removeJobs(existing.bullJobId);
|
|
187
|
+
const rawBullJobId = await addJob(jobPayload, jobParams);
|
|
188
|
+
existing.bullJobId = (rawBullJobId && typeof rawBullJobId === 'object'
|
|
189
|
+
&& rawBullJobId.id) ? rawBullJobId.id : rawBullJobId;
|
|
166
190
|
await existing.save();
|
|
167
191
|
}
|
|
168
192
|
}
|
|
169
193
|
} else {
|
|
170
|
-
|
|
171
|
-
const actualBullJobId = (rawBullJobId && typeof rawBullJobId === 'object'
|
|
194
|
+
const rawBullJobId = await addJob(jobPayload, jobParams);
|
|
195
|
+
const actualBullJobId = (rawBullJobId && typeof rawBullJobId === 'object'
|
|
196
|
+
&& rawBullJobId.id) ? rawBullJobId.id : rawBullJobId;
|
|
172
197
|
await sqldb.CronJobs.create({ ...where, bullJobId: actualBullJobId });
|
|
173
198
|
}
|
|
174
199
|
});
|
|
175
|
-
}
|
|
176
|
-
})
|
|
177
|
-
|
|
200
|
+
});
|
|
201
|
+
} catch (cronErr) {
|
|
202
|
+
console.error('Unable to configure integration cron jobs after save', cronErr);
|
|
203
|
+
throw cronErr;
|
|
204
|
+
}
|
|
178
205
|
|
|
179
|
-
return res.json(
|
|
206
|
+
return res.json(responsePayload);
|
|
180
207
|
} catch (err) {
|
|
208
|
+
if (transaction && !transaction.finished) {
|
|
209
|
+
try {
|
|
210
|
+
await transaction.rollback();
|
|
211
|
+
} catch (rollbackErr) {
|
|
212
|
+
console.error('Unable to roll back integration credential transaction', rollbackErr);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
181
215
|
return next(err);
|
|
182
216
|
}
|
|
183
217
|
};
|
package/package.json
CHANGED
package/.windsurfrules
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
This is a Node.js project using Sequelize for database operations and Express.js for the web server.
|
|
2
|
-
The API is defined in api.yml using OpenAPI 3.0.2
|
|
3
|
-
The database is defined in models/index.js
|
|
4
|
-
The models are defined in models/
|
|
5
|
-
to run tests you have to run them on the ti2 named docker container, and consider this codebase is part of node_modules/ti2 path for example :
|
|
6
|
-
`` $ docker exec ti2 bash -c "cd /ti2 && npx jest lib/__tests__/callback.js --forceExit"``
|
|
7
|
-
When writing / editing tests, make use of test/utils.js and review their implementation to understand how to mock the environment.
|
|
8
|
-
Do not make any changes until you 95% confident of the changes you want to make, ask follow up questions until you have that confidence.
|
|
9
|
-
when sending jobs to the background worker using the queue, asssume a worker is always runing to execute it but is on a different instance thread, so to review / wait for jobs we have to use the wokrer/queue.js listJobs fn to check for it
|
package/CLAUDE.md
DELETED
|
@@ -1,166 +0,0 @@
|
|
|
1
|
-
# CLAUDE.md
|
|
2
|
-
|
|
3
|
-
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
|
4
|
-
|
|
5
|
-
## Project Overview
|
|
6
|
-
|
|
7
|
-
Ti2 (Tourism Information Interchange) is an open-source integration framework for the tourism industry. It provides standardized functions for bookings, content, and rates management through a plugin-based architecture.
|
|
8
|
-
|
|
9
|
-
## Critical Development Rules
|
|
10
|
-
|
|
11
|
-
1. **Docker Container Execution**: ALL commands must be run inside the `ti2` Docker container
|
|
12
|
-
- The codebase is located at `/ti2` path inside the container
|
|
13
|
-
- This project is part of `node_modules/ti2` in the container environment
|
|
14
|
-
- **NEVER run npm, node, or npx commands on the host machine**
|
|
15
|
-
- Always use: `docker exec ti2 bash -c "cd /ti2 && <command>"`
|
|
16
|
-
- Example for npm install: `docker exec ti2 bash -c "cd /ti2 && npm install <package>"`
|
|
17
|
-
|
|
18
|
-
2. **Testing Rules**:
|
|
19
|
-
- Tests MUST be run inside the Docker container
|
|
20
|
-
- Example: `docker exec ti2 bash -c "cd /ti2 && npx jest lib/__tests__/callback.js --forceExit"`
|
|
21
|
-
- Use `test/utils.js` for mocking the environment
|
|
22
|
-
- Review test utility implementations before writing/editing tests
|
|
23
|
-
|
|
24
|
-
3. **Background Jobs**:
|
|
25
|
-
- Jobs sent to the queue run on a separate worker thread/instance
|
|
26
|
-
- Use `worker/queue.js` `listJobs` function to check job status
|
|
27
|
-
- Workers are always assumed to be running
|
|
28
|
-
|
|
29
|
-
4. **Code Confidence**: Do not make changes until 95% confident. Ask follow-up questions if uncertain.
|
|
30
|
-
|
|
31
|
-
## Public Repo Privacy
|
|
32
|
-
|
|
33
|
-
- `ti2` is open source. Before opening or updating public PRs, review summaries, inline comments, release notes, or commit messages, scrub internal TourConnect service names and private implementation details.
|
|
34
|
-
- Do not mention internal services such as `pyfilematch`, `filematch`, `tcoutlook`, private frontend names, private trace URLs, private customer identifiers, or internal deployment architecture in public GitHub text.
|
|
35
|
-
- Use generic wording such as `upstream caller`, `internal catalog caller`, `private frontend`, `caller-side timeout`, or `internal workflow` when context is needed.
|
|
36
|
-
- After editing public GitHub text, search the PR body, issue comments, review comments, and review summaries for internal service names before handoff.
|
|
37
|
-
|
|
38
|
-
## Common Development Commands
|
|
39
|
-
|
|
40
|
-
```bash
|
|
41
|
-
# Run tests (inside Docker container)
|
|
42
|
-
docker exec ti2 bash -c "cd /ti2 && npm test"
|
|
43
|
-
docker exec ti2 bash -c "cd /ti2 && npx jest [test-file] --forceExit"
|
|
44
|
-
|
|
45
|
-
# Run specific test file
|
|
46
|
-
docker exec ti2 bash -c "cd /ti2 && npx jest controllers/__tests__/bookings.js --forceExit"
|
|
47
|
-
|
|
48
|
-
# Database migrations
|
|
49
|
-
docker exec ti2 bash -c "cd /ti2 && npm run sequelize db:migrate"
|
|
50
|
-
|
|
51
|
-
# Generate documentation
|
|
52
|
-
docker exec ti2 bash -c "cd /ti2 && npm run doc"
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
## Architecture
|
|
56
|
-
|
|
57
|
-
### Core Components
|
|
58
|
-
|
|
59
|
-
1. **Main Entry Point** (`index.js`):
|
|
60
|
-
- Initializes Express server on port 10010 (default)
|
|
61
|
-
- Sets up Swagger documentation at `/api-docs`
|
|
62
|
-
- Handles plugin instantiation and middleware
|
|
63
|
-
- Implements caching mechanism
|
|
64
|
-
- Manages background job processing via `worker/queue.js`
|
|
65
|
-
|
|
66
|
-
2. **API Definition** (`api.yml`):
|
|
67
|
-
- OpenAPI 3.0.0 specification
|
|
68
|
-
- Defines all endpoints and schemas
|
|
69
|
-
- JWT Bearer authentication
|
|
70
|
-
|
|
71
|
-
3. **Plugin System**:
|
|
72
|
-
- Two types: Integration Plugins (connect systems) and App Plugins (value-added tools)
|
|
73
|
-
- Plugins are instantiated with cache, axios, events, and configuration
|
|
74
|
-
- Plugin schemas are merged with main API schema
|
|
75
|
-
- Each plugin gets its own routes under `/apps/{pluginName}`
|
|
76
|
-
|
|
77
|
-
4. **Controllers** (`controllers/`):
|
|
78
|
-
- `app.js`: Core application controller
|
|
79
|
-
- `bookings.js`: Booking operations (searchProducts, createBooking, etc.)
|
|
80
|
-
- `admin.js`: Administrative functions
|
|
81
|
-
- `user.js`: User management
|
|
82
|
-
- `ping.js`: Health check endpoints
|
|
83
|
-
- `allotment.js`: Allotment management
|
|
84
|
-
|
|
85
|
-
5. **Database Models** (`models/`):
|
|
86
|
-
- Sequelize ORM with MySQL
|
|
87
|
-
- Key models: Integration, User, UserAppKey, CronJobs, ApiCronJobs
|
|
88
|
-
- Migrations in `migrations/` directory
|
|
89
|
-
|
|
90
|
-
6. **Worker System** (`worker/`):
|
|
91
|
-
- Bull queue for background job processing
|
|
92
|
-
- Redis-backed job queue
|
|
93
|
-
- Supports plugin jobs, API jobs, and callback jobs
|
|
94
|
-
- Multi-worker support with throng
|
|
95
|
-
|
|
96
|
-
7. **Authentication** (`auth/authHandler.js`):
|
|
97
|
-
- JWT-based authentication
|
|
98
|
-
- Bearer token scheme
|
|
99
|
-
|
|
100
|
-
### Key Features
|
|
101
|
-
|
|
102
|
-
- **Caching**: Redis-based caching with configurable TTL
|
|
103
|
-
- **Event System**: EventEmitter2 for plugin communication
|
|
104
|
-
- **Background Jobs**: Async job processing with Bull/Redis
|
|
105
|
-
- **Cron Jobs**: Scheduled task execution
|
|
106
|
-
- **Multi-tenancy**: Support for multiple integrations/plugins
|
|
107
|
-
|
|
108
|
-
### Plugin Development
|
|
109
|
-
|
|
110
|
-
Plugins must implement standard methods:
|
|
111
|
-
- `validateToken`: Token validation
|
|
112
|
-
- `tokenTemplate`: Token configuration template
|
|
113
|
-
- Booking methods: `searchProducts`, `searchAvailability`, `createBooking`, `cancelBooking`
|
|
114
|
-
- Content methods: `getProducts`, `getProduct`, `createProduct`, `updateProduct`
|
|
115
|
-
|
|
116
|
-
### Testing Strategy
|
|
117
|
-
|
|
118
|
-
- Jest testing framework
|
|
119
|
-
- Test fixtures in `__fixtures__/`
|
|
120
|
-
- Mock plugins in `__mocks__/`
|
|
121
|
-
- Use `test/utils.js` for creating test environments
|
|
122
|
-
- Tests organized by controller in `controllers/__tests__/`
|
|
123
|
-
|
|
124
|
-
## Database
|
|
125
|
-
|
|
126
|
-
- Sequelize ORM with MySQL/MariaDB
|
|
127
|
-
- Connection managed through `models/db.js`
|
|
128
|
-
- Migrations handled via sequelize-cli
|
|
129
|
-
- Models auto-loaded from `models/` directory
|
|
130
|
-
|
|
131
|
-
## Environment Variables
|
|
132
|
-
|
|
133
|
-
Key variables:
|
|
134
|
-
- `PORT`: Server port (default: 10010)
|
|
135
|
-
- `REDIS_URL`: Redis connection (default: redis://redis:6379)
|
|
136
|
-
- `jwtSecret`: JWT signing secret
|
|
137
|
-
- `adminKey`: Admin authentication key
|
|
138
|
-
- `WEB_CONCURRENCY`: Number of workers
|
|
139
|
-
- `MAX_JOBS_PER_WORKER`: Job concurrency per worker
|
|
140
|
-
- `SSL_INSECURE_ALLOWED_DOMAINS`: Pipe-separated list of domains where SSL certificate verification is skipped (e.g., `localhost|staging.example.com|192.168.1.100`)
|
|
141
|
-
|
|
142
|
-
Plugin-specific variables follow pattern: `ti2_{pluginName}_{setting}`
|
|
143
|
-
|
|
144
|
-
### SSL Configuration
|
|
145
|
-
|
|
146
|
-
The `SSL_INSECURE_ALLOWED_DOMAINS` environment variable allows you to specify domains where SSL certificate verification should be skipped. This is useful for:
|
|
147
|
-
- Development environments with self-signed certificates
|
|
148
|
-
- Testing against staging servers with invalid certificates
|
|
149
|
-
- Local development with HTTPS
|
|
150
|
-
|
|
151
|
-
Example usage:
|
|
152
|
-
```bash
|
|
153
|
-
# Skip SSL verification for specific domains
|
|
154
|
-
export SSL_INSECURE_ALLOWED_DOMAINS="localhost|staging.api.com|192.168.1.100"
|
|
155
|
-
|
|
156
|
-
# Multiple domains separated by pipe
|
|
157
|
-
export SSL_INSECURE_ALLOWED_DOMAINS="dev.example.com|test.example.com"
|
|
158
|
-
```
|
|
159
|
-
|
|
160
|
-
**Important Implementation Notes**:
|
|
161
|
-
- SSL configuration is ONLY applied to plugin axios instances (for external API calls) and worker axios instances
|
|
162
|
-
- Internal communication (callbacks, per-request axios) do NOT use SSL configuration
|
|
163
|
-
- SSL configuration uses a per-request interceptor approach for better isolation
|
|
164
|
-
- The configuration checks domains dynamically on each request
|
|
165
|
-
|
|
166
|
-
**Warning**: Only use this in development/testing environments. Never skip SSL verification in production for security reasons.
|