ti2-ventrata 1.0.36 → 1.0.37

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.
Files changed (2) hide show
  1. package/index.js +39 -37
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -359,6 +359,34 @@ class Plugin {
359
359
  acceptLanguage,
360
360
  });
361
361
  const dataForCreateBooking = await jwt.verify(availabilityKey, this.jwtKey);
362
+ // if customFieldValues contains perUnitItem fields
363
+ // we need to overwrite unitItems from the availabilityKey
364
+ if (customFieldValues && customFieldValues.length) {
365
+ const productCFV = customFieldValues.filter(o => !R.isNil(o.value) && !o.field.isPerUnitItem);
366
+ const unitItemCFV = customFieldValues.filter(o => !R.isNil(o.value) && o.field.isPerUnitItem);
367
+ if (productCFV.length) {
368
+ dataForCreateBooking.questionAnswers = productCFV.map(o => ({
369
+ questionId: o.field.id,
370
+ value: o.value,
371
+ }));
372
+ }
373
+ if (unitItemCFV.length) {
374
+ dataForCreateBooking.unitItems = R.call(R.compose(
375
+ R.map(arr => ({
376
+ unitId: arr[0].field.unitId,
377
+ questionAnswers: arr.map(o => ({
378
+ questionId: o.field.id.split('|')[0],
379
+ value: o.value,
380
+ })),
381
+ })),
382
+ R.values,
383
+ R.groupBy(o => {
384
+ const [questionId, unitItemIndex] = o.field.id.split('|');
385
+ return unitItemIndex;
386
+ }),
387
+ ), unitItemCFV);
388
+ }
389
+ }
362
390
  let booking = R.path(['data'], await axios({
363
391
  method: rebookingId ? 'patch' : 'post',
364
392
  url: `${endpoint || this.endpoint}/bookings${rebookingId ? `/${rebookingId}` : ''}`,
@@ -371,36 +399,6 @@ class Plugin {
371
399
  },
372
400
  headers,
373
401
  }));
374
- const answers = customFieldValues && customFieldValues.length
375
- ? customFieldValues.filter(o => !R.isNil(o.value)).map(o => ({
376
- questionId: o.field.id,
377
- value: o.value,
378
- })) : [];
379
- // for answers to custom fields, ventrata suggest to modify the booking
380
- if (answers.length) {
381
- let modifierBooking = {
382
- questionAnswers: booking.questionAnswers.map(o => {
383
- const answer = answers.find(a => a.questionId === o.questionId);
384
- return answer || o;
385
- }),
386
- unitItems: booking.unitItems.map(u => ({
387
- ...u,
388
- questionAnswers: u.questionAnswers.map(o => {
389
- const answer = answers.find(a => a.questionId === o.questionId);
390
- return answer || o;
391
- }),
392
- })),
393
- };
394
- if (!modifierBooking.unitItems.find(u => u.questionAnswers && u.questionAnswers.length)) {
395
- modifierBooking = R.omit(['unitItems'], modifierBooking);
396
- }
397
- booking = R.path(['data'], await axios({
398
- method: 'patch',
399
- url: `${endpoint || this.endpoint}/bookings/${booking.uuid}`,
400
- data: modifierBooking,
401
- headers,
402
- }));
403
- }
404
402
  // for booking update, we may not need to confirm again
405
403
  if (!booking.utcConfirmedAt && !partialOrder) {
406
404
  const dataForConfirmBooking = {
@@ -623,8 +621,6 @@ class Plugin {
623
621
  },
624
622
  query: {
625
623
  productId,
626
- date,
627
- dateFormat,
628
624
  },
629
625
  }) {
630
626
  const headers = getHeaders({
@@ -647,6 +643,7 @@ class Plugin {
647
643
  return 'long';
648
644
  })(),
649
645
  options: o.selectOptions,
646
+ isPerUnitItem: o.isPerUnitItem,
650
647
  });
651
648
  if (!productId) {
652
649
  const allProducts = R.pathOr([], ['data'], await axios({
@@ -655,13 +652,15 @@ class Plugin {
655
652
  headers,
656
653
  }));
657
654
  const allOptions = R.chain(R.propOr([], 'options'), allProducts);
658
- const allUnits = R.chain(R.propOr([], 'units'), allOptions);
655
+ const allUnits = R.chain(R.propOr([], 'units'), allOptions).map(o => ({ ...o, isPerUnitItem: true }));
659
656
  const allQuestions = R.call(R.compose(
660
657
  R.uniqBy(R.prop('id')),
661
- R.chain(R.propOr([], 'questions')),
658
+ R.chain(obj => (obj.questions || []).map(q => ({
659
+ ...q,
660
+ isPerUnitItem: Boolean(obj.isPerUnitItem),
661
+ }))),
662
662
  R.filter(o => o.questions && o.questions.length),
663
663
  ), [...allOptions, ...allUnits]);
664
- console.log(allQuestions);
665
664
  return {
666
665
  fields: [],
667
666
  customFields: allQuestions.map(convertQToField),
@@ -677,11 +676,14 @@ class Plugin {
677
676
  const allOptions = R.propOr([], 'options', product);
678
677
  const allQuestions = R.call(R.compose(
679
678
  R.uniqBy(R.prop('id')),
680
- R.chain(R.propOr([], 'questions')),
679
+ R.chain(obj => (obj.questions || []).map(q => ({
680
+ ...q,
681
+ isPerUnitItem: Boolean(obj.isPerUnitItem),
682
+ }))),
681
683
  R.filter(o => o.questions && o.questions.length),
682
684
  ), [
683
685
  ...allOptions,
684
- ...R.chain(R.propOr([], 'units'), allOptions),
686
+ ...R.chain(R.propOr([], 'units'), allOptions).map(o => ({ ...o, isPerUnitItem: true })),
685
687
  ]);
686
688
  if (!product) return { fields: [] };
687
689
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ti2-ventrata",
3
- "version": "1.0.36",
3
+ "version": "1.0.37",
4
4
  "description": "Ventrata's TI2 Plugin",
5
5
  "main": "index.js",
6
6
  "scripts": {