ti2 1.0.119 → 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 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/Message'
876
+ $ref: '#/components/schemas/CreateAppTokenResponse'
866
877
  delete:
867
878
  tags:
868
879
  - app
@@ -872,6 +872,85 @@ describe('Bookings Product Search Lock Mechanism (Job Queuing on Stale Cache)',
872
872
  expect(addJob).not.toHaveBeenCalled();
873
873
  });
874
874
 
875
+ it('does not save a cold optionId hydration as the full product catalog', async () => {
876
+ await clearProductSearchCache();
877
+
878
+ const scopedProducts = [{
879
+ productId: 'scoped-product',
880
+ productName: 'Scoped Product',
881
+ options: [{
882
+ optionId: 'scoped-option',
883
+ optionName: 'Scoped Option',
884
+ }],
885
+ }];
886
+ const fullCatalog = [{
887
+ productId: 'catalog-product',
888
+ productName: 'Catalog Product',
889
+ options: [{
890
+ optionId: 'catalog-option',
891
+ optionName: 'Catalog Option',
892
+ }],
893
+ }];
894
+ const emitSpy = jest.spyOn(lockTestPlugin.events, 'emit');
895
+ lockTestPlugin.searchProducts
896
+ .mockResolvedValueOnce({ products: scopedProducts })
897
+ .mockResolvedValueOnce({ products: fullCatalog });
898
+
899
+ const scopedResult = await doApiPost({
900
+ url: `/products/${testAppName}/${testUserId}/${ttrTestHint}/search`,
901
+ token: userToken,
902
+ payload: { optionId: ['scoped-option'] },
903
+ });
904
+ const catalogResult = await makeProductSearchRequest();
905
+
906
+ expect(scopedResult.products).toEqual(scopedProducts);
907
+ expect(catalogResult.products).toEqual(fullCatalog);
908
+ expect(lockTestPlugin.searchProducts).toHaveBeenCalledTimes(2);
909
+ expect(lockTestPlugin.searchProducts.mock.calls[0][0].payload.optionId).toEqual([
910
+ 'scoped-option',
911
+ ]);
912
+ expect(lockTestPlugin.searchProducts.mock.calls[1][0].payload.optionId).toBeUndefined();
913
+ expect(emitSpy).toHaveBeenCalledWith(
914
+ 'bookingsProductSearch:cache:decision',
915
+ expect.objectContaining({
916
+ action: 'scoped_result_not_cached',
917
+ returnedProductCount: scopedProducts.length,
918
+ returnedOptionCount: 1,
919
+ }),
920
+ );
921
+ emitSpy.mockRestore();
922
+ });
923
+
924
+ it('queues an unscoped refresh when an optionId request finds stale catalog data', async () => {
925
+ await clearProductSearchCache();
926
+ const cachedProducts = [{
927
+ productId: 'cached-product',
928
+ productName: 'Cached Product',
929
+ options: [{
930
+ optionId: 'cached-option',
931
+ optionName: 'Cached Option',
932
+ }],
933
+ }];
934
+ lockTestPlugin.searchProducts.mockResolvedValueOnce({ products: cachedProducts });
935
+ await makeProductSearchRequest();
936
+ lockTestPlugin.searchProducts.mockClear();
937
+ addJob.mockClear();
938
+ await new Promise(resolve => {
939
+ setTimeout(resolve, 1500);
940
+ });
941
+
942
+ const result = await doApiPost({
943
+ url: `/products/${testAppName}/${testUserId}/${ttrTestHint}/search`,
944
+ token: userToken,
945
+ payload: { optionId: ['cached-option'] },
946
+ });
947
+
948
+ expect(result.products).toEqual(cachedProducts);
949
+ expect(lockTestPlugin.searchProducts).not.toHaveBeenCalled();
950
+ expect(addJob).toHaveBeenCalledTimes(1);
951
+ expect(addJob.mock.calls[0][0].payload.payload).toEqual({ forceRefresh: false });
952
+ });
953
+
875
954
  it('concurrent forceRefresh requests should wait for one plugin fetch', async () => {
876
955
  await clearProductSearchCache();
877
956
 
@@ -20,7 +20,7 @@ describe('user', () => {
20
20
  let doApiGet; let doApiPost; let
21
21
  doApiDelete;
22
22
  let app;
23
- let sqldb;
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 sqldb.query(
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 sqldb.query(
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 => sqldb.query(
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 sqldb.query(
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 sqldb.query(
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 => sqldb.query(
441
+ await Promise.all(originalRows.map(currentRow => db.query(
333
442
  'UPDATE UserAppKeys SET appKey = :appKey WHERE id = :id',
334
443
  {
335
444
  replacements: {
@@ -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
- // check if the user exists
109
- const userRecord = await sqldb.User.findOne({ where: { userId } });
110
- if (!userRecord) { // create the user record
111
- await sqldb.User.create({ userId });
112
- }
113
- // check if the user already has the same app with the same hint
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
- // create any cronjobs related to the app
120
- await bb.each(req.app.plugins, async plugin => {
121
- if (Array.isArray(plugin.jobs)) {
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
- let rawBullJobId = await addJob(jobPayload, jobParams);
152
- existing.bullJobId = (rawBullJobId && typeof rawBullJobId === 'object' && rawBullJobId.id) ? rawBullJobId.id : rawBullJobId;
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), // Use existing.bullJobId
183
+ await queue.getJob(existing.bullJobId),
161
184
  );
162
185
  if (bullCron !== job.cron) {
163
- await queue.removeJobs(existing.bullJobId); // Use existing.bullJobId
164
- let rawBullJobId = await addJob(jobPayload, jobParams);
165
- existing.bullJobId = (rawBullJobId && typeof rawBullJobId === 'object' && rawBullJobId.id) ? rawBullJobId.id : rawBullJobId;
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
- let rawBullJobId = await addJob(jobPayload, jobParams);
171
- const actualBullJobId = (rawBullJobId && typeof rawBullJobId === 'object' && rawBullJobId.id) ? rawBullJobId.id : rawBullJobId;
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({ value: newAppKey.get('id').toString() });
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
  };
@@ -52,8 +52,29 @@ const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
52
52
 
53
53
  const productSearchCacheKey = ({ userId, hint }) => hash({ userId, hint, operationId: productSearchOperationId });
54
54
 
55
+ const productSearchSelectorFields = [
56
+ 'searchInput',
57
+ 'optionId',
58
+ 'productId',
59
+ 'productName',
60
+ 'lastUpdatedFrom',
61
+ 'skipAccommodation',
62
+ ];
63
+
64
+ const hasProductSearchSelector = payload => productSearchSelectorFields.some(field => {
65
+ const value = R.path([field], payload);
66
+ if (Array.isArray(value)) return value.length > 0;
67
+ if (typeof value === 'string') return Boolean(value.trim());
68
+ return value !== undefined && value !== null && value !== false;
69
+ });
70
+
55
71
  const productCount = value => R.pathOr([], ['products'], value).length;
56
72
 
73
+ const optionCount = value => R.pathOr([], ['products'], value).reduce(
74
+ (count, product) => count + R.pathOr([], ['options'], product).length,
75
+ 0,
76
+ );
77
+
57
78
  const hasProductCache = cacheContent => Boolean(cacheContent && cacheContent.products);
58
79
 
59
80
  const safeHash = value => (value === undefined || value === null ? undefined : hash(String(value)));
@@ -291,7 +312,11 @@ const $bookingsProductSearch = plugins => async ({
291
312
 
292
313
  // Payload for the plugin function (func) - pass forceRefresh so plugins can trigger background cache rebuilds
293
314
  const payloadForPlugin = { ...R.omit(['forceRefresh'], originalRequestBody), forceRefresh };
294
- const payloadForBackgroundJob = R.omit(['credentials'], payloadForPlugin);
315
+ const isScopedSearch = hasProductSearchSelector(originalRequestBody);
316
+ const payloadForBackgroundJob = R.omit(
317
+ ['credentials', ...productSearchSelectorFields],
318
+ payloadForPlugin,
319
+ );
295
320
 
296
321
  const { app, token } = await getAppAndToken({ plugins, appKey, userId, hint });
297
322
  assert(userId, 'userId is required');
@@ -359,6 +384,26 @@ const $bookingsProductSearch = plugins => async ({
359
384
 
360
385
  // Helper function to call the plugin, save cache, and return results
361
386
  const fetchFromPluginAndCache = async (reason = 'cache_miss') => {
387
+ if (isScopedSearch) {
388
+ const pluginStartedAt = Date.now();
389
+ const pluginResults = await func({
390
+ axios,
391
+ token,
392
+ payload: payloadForPlugin,
393
+ typeDefsAndQueries,
394
+ requestId,
395
+ userId,
396
+ hint,
397
+ });
398
+ emitDecision('scoped_result_not_cached', {
399
+ reason,
400
+ pluginElapsedMs: Date.now() - pluginStartedAt,
401
+ returnedProductCount: productCount(pluginResults),
402
+ returnedOptionCount: optionCount(pluginResults),
403
+ });
404
+ return pluginResults || { products: [] };
405
+ }
406
+
362
407
  const lockOwnerToken = crypto.randomBytes(16).toString('hex');
363
408
  const lockStartedAt = Date.now();
364
409
  const lockAcquired = await acquirePluginExecutionLock(lockOwnerToken);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ti2",
3
- "version": "1.0.119",
3
+ "version": "1.0.121",
4
4
  "description": "Tourist Industry Exchange (TI2)",
5
5
  "main": "index.js",
6
6
  "scripts": {
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.