ti2 1.0.49 → 1.0.51

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
@@ -208,6 +208,13 @@ components:
208
208
  type: string
209
209
  name:
210
210
  type: string
211
+ pickuppoint:
212
+ type: object
213
+ properties:
214
+ id:
215
+ type: string
216
+ name:
217
+ type: string
211
218
  bookingSearchQuery:
212
219
  type: object
213
220
  additionalProperties: true
@@ -1170,6 +1177,81 @@ paths:
1170
1177
  type: array
1171
1178
  items:
1172
1179
  $ref: '#/components/schemas/agent'
1180
+ /{appKey}/{userId}/pickup-points:
1181
+ get:
1182
+ security:
1183
+ - bearerAuth: ['admin', 'user']
1184
+ tags:
1185
+ - bookings
1186
+ summary: Gets all pickup points for this supplier
1187
+ operationId: getPickupPoints
1188
+ parameters:
1189
+ - name: appKey
1190
+ in: path
1191
+ description: name of the app to get the pickup points from (defaults to first token found)
1192
+ required: true
1193
+ example: 'fareharbor'
1194
+ schema:
1195
+ type: string
1196
+ - name: userId
1197
+ in: path
1198
+ description: the user/company that the appKey belongs to
1199
+ required: true
1200
+ schema:
1201
+ type: string
1202
+ responses:
1203
+ '200':
1204
+ description: pickup points
1205
+ content:
1206
+ application/json:
1207
+ schema:
1208
+ type: object
1209
+ properties:
1210
+ pickupPoints:
1211
+ type: array
1212
+ items:
1213
+ $ref: '#/components/schemas/pickuppoint'
1214
+ /{appKey}/{userId}/{hint}/pickup-points:
1215
+ get:
1216
+ security:
1217
+ - bearerAuth: ['admin', 'user']
1218
+ tags:
1219
+ - bookings
1220
+ summary: Gets all pickup points for this supplier
1221
+ operationId: getPickupPoints
1222
+ parameters:
1223
+ - name: appKey
1224
+ in: path
1225
+ description: name of the app to get the pickup points from (defaults to first token found) using the hint
1226
+ required: true
1227
+ example: 'fareharbor'
1228
+ schema:
1229
+ type: string
1230
+ - name: userId
1231
+ in: path
1232
+ description: the user/company that the appKey belongs to
1233
+ required: true
1234
+ schema:
1235
+ type: string
1236
+ - name: hint
1237
+ in: path
1238
+ description: a specific token hint to be used
1239
+ required: true
1240
+ example: 'fareharbor-helloworld'
1241
+ schema:
1242
+ type: string
1243
+ responses:
1244
+ '200':
1245
+ description: pickup points
1246
+ content:
1247
+ application/json:
1248
+ schema:
1249
+ type: object
1250
+ properties:
1251
+ pickupPoints:
1252
+ type: array
1253
+ items:
1254
+ $ref: '#/components/schemas/pickuppoint'
1173
1255
  /bookings/{appKey}/{userId}/search:
1174
1256
  post:
1175
1257
  security:
package/cache.js CHANGED
@@ -3,7 +3,6 @@ const hash = require('object-hash');
3
3
 
4
4
  const REDIS_URL = process.env.REDIS_URL || 'redis://redis:6379';
5
5
  const cache = new Redis(`${REDIS_URL}/2`);
6
-
7
6
  const defaultTTL = 60 * 60; // 1 hour
8
7
 
9
8
  const save = async ({
@@ -57,10 +56,11 @@ const getOrExec = async ({
57
56
 
58
57
  const drop = async ({
59
58
  pluginName,
59
+ key: keyParam,
60
60
  fn,
61
61
  fnParams,
62
62
  }) => {
63
- let key = hash({ fn, fnParams });
63
+ let key = keyParam || hash({ fn, fnParams });
64
64
  key = `${pluginName}:${key}`;
65
65
  await cache.del(key);
66
66
  };
@@ -70,4 +70,5 @@ module.exports = {
70
70
  save,
71
71
  getOrExec,
72
72
  drop,
73
+ get,
73
74
  };
@@ -5,6 +5,7 @@ const { typeDefs: productTypeDefs, query: productQuery } = require('./graphql-sc
5
5
  const { typeDefs: availTypeDefs, query: availQuery } = require('./graphql-schemas/availability');
6
6
  const { typeDefs: bookingTypeDefs, query: bookingQuery } = require('./graphql-schemas/booking');
7
7
  const { typeDefs: rateTypeDefs, query: rateQuery } = require('./graphql-schemas/rate');
8
+ const { typeDefs: pickupTypeDefs, query: pickupQuery } = require('./graphql-schemas/pickup-point');
8
9
 
9
10
  const typeDefsAndQueries = {
10
11
  productTypeDefs,
@@ -15,6 +16,8 @@ const typeDefsAndQueries = {
15
16
  bookingQuery,
16
17
  rateTypeDefs,
17
18
  rateQuery,
19
+ pickupTypeDefs,
20
+ pickupQuery,
18
21
  };
19
22
 
20
23
  const bookingsSearch = plugins => async (req, res, next) => {
@@ -305,6 +308,35 @@ const getAffiliateDesks = plugins => async (req, res, next) => {
305
308
  }
306
309
  };
307
310
 
311
+ const getPickupPoints = plugins => async (req, res, next) => {
312
+ const {
313
+ params: { appKey, userId, hint },
314
+ body: payload,
315
+ } = req;
316
+ try {
317
+ const app = plugins.find(({ name }) => name === appKey);
318
+ // const app = load(appKey);
319
+ const userAppKeys = (await UserAppKey.findOne({
320
+ where: {
321
+ userId,
322
+ integrationId: appKey,
323
+ ...(hint ? { hint } : {}),
324
+ },
325
+ }));
326
+ assert(userAppKeys, 'could not find the app key');
327
+ const token = await userAppKeys.token;
328
+ assert(app.getPickupPoints, `getPickupPoints is not available for ${appKey}`);
329
+ const results = await app.getPickupPoints({
330
+ token,
331
+ payload,
332
+ typeDefsAndQueries,
333
+ });
334
+ return res.json(results);
335
+ } catch (err) {
336
+ return next(err);
337
+ }
338
+ };
339
+
308
340
  module.exports = plugins => ({
309
341
  bookingsSearch: bookingsSearch(plugins),
310
342
  bookingsCancel: bookingsCancel(plugins),
@@ -317,4 +349,5 @@ module.exports = plugins => ({
317
349
  createBooking: createBooking(plugins),
318
350
  getAffiliateAgents: getAffiliateAgents(plugins),
319
351
  getAffiliateDesks: getAffiliateDesks(plugins),
352
+ getPickupPoints: getPickupPoints(plugins),
320
353
  });
@@ -0,0 +1,16 @@
1
+ const typeDefs = `
2
+ type Query {
3
+ id: ID
4
+ name: String
5
+ }
6
+ `;
7
+
8
+ const query = `{
9
+ id
10
+ name
11
+ }`;
12
+
13
+ module.exports = {
14
+ typeDefs,
15
+ query,
16
+ };
package/index.js CHANGED
@@ -3,6 +3,7 @@ const createMiddleware = require('swagger-express-middleware');
3
3
  const { connector } = require('swagger-routes-express');
4
4
  const swaggerUi = require('swagger-ui-express');
5
5
  const yaml = require('js-yaml');
6
+ const hash = require('object-hash');
6
7
  const fs = require('fs');
7
8
  const { pickBy } = require('ramda');
8
9
  const bb = require('bluebird');
@@ -11,6 +12,17 @@ const R = require('ramda');
11
12
  const { v4: uuidv4 } = require('uuid');
12
13
  const EventEmitter = require('eventemitter2');
13
14
 
15
+ const cacheSettings = {
16
+ '*': [
17
+ 'tokenTemplate',
18
+ 'getAffiliateAgents',
19
+ 'getAffiliateDesks',
20
+ 'getPickupPoints',
21
+ 'bookingsProductSearch',
22
+ ],
23
+ ventrata: [],
24
+ fareharbor: [],
25
+ };
14
26
  const ti2Events = new EventEmitter({ captureRejections: true, wildcard: true });
15
27
  ti2Events.on('event error', console.error);
16
28
 
@@ -136,11 +148,9 @@ module.exports = async ({
136
148
  eventHandlerPlugins.forEach(plugin => {
137
149
  plugin.eventHandler(ti2Events);
138
150
  });
139
-
140
- app.use((req, res, next) => {
141
- const startHrTime = process.hrtime();
151
+ const composeBodyFromReq = req => {
142
152
  const requestId = uuidv4();
143
- const body = {
153
+ return {
144
154
  requestId,
145
155
  date: Math.floor(Date.now() / 1e3),
146
156
  url: req.url,
@@ -152,16 +162,21 @@ module.exports = async ({
152
162
  operationId: R.path(['openapi', 'operation', 'operationId'], req),
153
163
  client: req.headers['x-forwarded-for'] || req.connection.remoteAddress,
154
164
  };
165
+ };
166
+ app.use(async (req, res, next) => {
167
+ const startHrTime = process.hrtime();
168
+ const body = composeBodyFromReq(req);
169
+ req.customBody = body;
155
170
  ti2Events.emit('request.start', body);
156
- req.requestId = requestId;
157
- res.on('finish', () => {
171
+ req.requestId = body.requestId;
172
+ res.on('finish', async () => {
158
173
  const elapsedHrTime = process.hrtime(startHrTime);
159
174
  const responseTimeInMs = parseInt(
160
175
  elapsedHrTime[0] * 1e3 + elapsedHrTime[1] / 1e6,
161
176
  10,
162
177
  );
163
178
  ti2Events.emit('request.end', {
164
- ...body,
179
+ ...req.customBody,
165
180
  responseTimeInMs,
166
181
  responseStatusCode: res.statusCode,
167
182
  });
@@ -169,6 +184,46 @@ module.exports = async ({
169
184
  next();
170
185
  });
171
186
 
187
+ app.use(async (req, res, next) => {
188
+ try {
189
+ const currentPlugin = plugins.find(p => p.name === req.pathParams.appKey);
190
+ const cachingOperations = [
191
+ ...cacheSettings['*'],
192
+ ...(currentPlugin ? R.pathOr([], [currentPlugin.name], cacheSettings) : []),
193
+ ];
194
+ const body = req.customBody;
195
+ if (cachingOperations.indexOf(body.operationId) > -1) {
196
+ const cacheKey = hash(R.omit(['requestId', 'date'], body));
197
+
198
+ const foundCache = await cache.get({
199
+ pluginName: body.params.appKey,
200
+ key: cacheKey,
201
+ });
202
+ if (foundCache) {
203
+ // console.log('foundCache', cacheKey);
204
+ req.customBody.usedCache = cacheKey;
205
+ res.json(foundCache);
206
+ }
207
+ const realSend = res.json;
208
+ res.json = newData => { // new res.json
209
+ cache.save({
210
+ pluginName: req.pathParams.appKey,
211
+ key: cacheKey,
212
+ value: newData,
213
+ ttl: 60 * 60 * 24, // one day
214
+ });
215
+ // console.log('newData', cacheKey);
216
+ if (!res.headersSent) {
217
+ // console.log('send new data', cacheKey);
218
+ return realSend.apply(res, [newData]);
219
+ }
220
+ };
221
+ }
222
+ return next();
223
+ } catch (err) {
224
+ return next(err);
225
+ }
226
+ });
172
227
  connect(app);
173
228
  app.use(middleware.mock());
174
229
  // global error Handling
@@ -179,10 +234,11 @@ module.exports = async ({
179
234
  if (process.env.CONSOLE_ERRORS || process.env.JEST_WORKER_ID) {
180
235
  console.error(R.path(['response', 'data'], err), err);
181
236
  }
237
+ console.log('hit hre');
182
238
  return res.status((() => {
183
239
  if (!isNumber(err.status)) return 500;
184
240
  return Number(err.status);
185
- })()).json({
241
+ })()).send({
186
242
  message: R.path(['response', 'data', 'errorMessage'], err) || err.message || 'Internal Error',
187
243
  });
188
244
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ti2",
3
- "version": "1.0.49",
3
+ "version": "1.0.51",
4
4
  "description": "Tourist Industry Exchange (TI2)",
5
5
  "main": "index.js",
6
6
  "scripts": {