@darkpos/pricing 1.0.79 → 1.0.80

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.
@@ -874,7 +874,7 @@ describe('Modifier actions', () => {
874
874
  });
875
875
  });
876
876
 
877
- test('CU-86dve295v Should throw error when adding a modifier more times than the ones specified in properties.limits.maxAppliesItem', () => {
877
+ test('CU-86dve295v Should throw error when adding a modifier more times than the ones specified in properties.limits.maxItemUse', () => {
878
878
  const order = {
879
879
  id: 'ord-123',
880
880
  items: [],
@@ -889,7 +889,7 @@ describe('Modifier actions', () => {
889
889
  },
890
890
  properties: {
891
891
  isQuantityMultiplier: true,
892
- limits: { maxAppliesItem: 1 },
892
+ limits: { maxItemUse: 1 },
893
893
  },
894
894
  };
895
895
  order.items.push({
@@ -929,18 +929,26 @@ describe('Modifier actions', () => {
929
929
 
930
930
  expect(conditionsBag).toEqual([]);
931
931
  expect(error).toEqual('modifier.has.reached.the.maximum.amount.of.applies');
932
- expect(updatedOrder2.items).toEqual([
932
+ expect(updatedOrder2.items[0].modifiers.length).toBe(1);
933
+ expect(updatedOrder2.items[0].modifiers).toMatchObject([
933
934
  {
934
- itemId: '123',
935
- modifiers: [],
936
- price: 100,
937
- properties: { basePrice: 100 },
938
- quantity: 2,
935
+ compute: {
936
+ action: 'subtract',
937
+ amount: 10,
938
+ type: 'fixed',
939
+ },
940
+ modifierId: 1,
941
+ properties: {
942
+ isQuantityMultiplier: true,
943
+ limits: {
944
+ maxItemUse: 1,
945
+ },
946
+ },
939
947
  },
940
948
  ]);
941
949
  });
942
950
 
943
- test('CU-86dve295v Should not throw error when adding a modifier less times than the ones specified in properties.limits.maxAppliesItem=5 and isQuantityMultiplier=false', () => {
951
+ test('CU-86dve295v Should not throw error when adding a modifier less times than the ones specified in properties.limits.maxItemUse=5 and isQuantityMultiplier=false', () => {
944
952
  const order = {
945
953
  id: 'ord-123',
946
954
  items: [],
@@ -955,7 +963,7 @@ describe('Modifier actions', () => {
955
963
  },
956
964
  properties: {
957
965
  isQuantityMultiplier: false,
958
- limits: { maxAppliesItem: 5 },
966
+ limits: { maxItemUse: 5 },
959
967
  },
960
968
  };
961
969
  order.items.push({
@@ -993,7 +1001,116 @@ describe('Modifier actions', () => {
993
1001
  expect(currentOrder.items[0].modifiers.length).toBe(5);
994
1002
  });
995
1003
 
996
- test('CU-86dve295v Should not throw error when adding a modifier if properties.limits.isUnlimitedAppliesItem=true and isQuantityMultiplier=false', () => {
1004
+ test('CU-86dve295v Should not throw error when adding a modifier less times than the ones specified in properties.limits.maxItemUse=5 and isQuantityMultiplier=false. Considering also the quantity', () => {
1005
+ const order = {
1006
+ id: 'ord-123',
1007
+ items: [],
1008
+ modifiers: [],
1009
+ };
1010
+ const modifier = {
1011
+ _id: 1,
1012
+ compute: {
1013
+ amount: 10,
1014
+ action: 'subtract',
1015
+ type: 'fixed',
1016
+ },
1017
+ properties: {
1018
+ isQuantityMultiplier: false,
1019
+ limits: { maxItemUse: 5 },
1020
+ },
1021
+ direct: true,
1022
+ };
1023
+ order.items.push({
1024
+ quantity: 2,
1025
+ itemId: '123',
1026
+ price: 100,
1027
+ modifiers: [],
1028
+ });
1029
+
1030
+ const conditionsBag = [];
1031
+
1032
+ let error = '';
1033
+ let currentOrder = { ...order };
1034
+
1035
+ for (let i = 0; i < 10; i += 1) {
1036
+ currentOrder = pricingService.order.addItemModifier({
1037
+ order: currentOrder,
1038
+ modifier,
1039
+ itemIndex: 0,
1040
+ onConditionsNotMet: bag => {
1041
+ bag.forEach(condition => {
1042
+ conditionsBag.push(condition);
1043
+ });
1044
+ },
1045
+ // eslint-disable-next-line no-loop-func
1046
+ onError: errorMessage => {
1047
+ error = errorMessage;
1048
+ },
1049
+ });
1050
+ }
1051
+
1052
+ expect(conditionsBag).toEqual([]);
1053
+ expect(error).toEqual('');
1054
+
1055
+ const calculatedOrder = pricingService.order.calculate(currentOrder);
1056
+
1057
+ expect(calculatedOrder.items[0].modifiers.length).toBe(10);
1058
+ });
1059
+
1060
+ test('CU-86dve295v Should throw error when adding a modifier 11 times to an item with quantity=2 and modifier.properties.limits.maxItemUse=5', () => {
1061
+ const order = {
1062
+ id: 'ord-123',
1063
+ items: [],
1064
+ modifiers: [],
1065
+ };
1066
+ const modifier = {
1067
+ _id: 1,
1068
+ compute: {
1069
+ amount: 10,
1070
+ action: 'subtract',
1071
+ type: 'fixed',
1072
+ },
1073
+ properties: {
1074
+ isQuantityMultiplier: false,
1075
+ limits: { maxItemUse: 5 },
1076
+ },
1077
+ };
1078
+ order.items.push({
1079
+ quantity: 2,
1080
+ itemId: '123',
1081
+ price: 100,
1082
+ modifiers: [],
1083
+ });
1084
+
1085
+ const conditionsBag = [];
1086
+
1087
+ let error = '';
1088
+ let currentOrder = { ...order };
1089
+
1090
+ for (let i = 0; i < 11; i += 1) {
1091
+ currentOrder = pricingService.order.addItemModifier({
1092
+ order: currentOrder,
1093
+ modifier,
1094
+ itemIndex: 0,
1095
+ onConditionsNotMet: bag => {
1096
+ bag.forEach(condition => {
1097
+ conditionsBag.push(condition);
1098
+ });
1099
+ },
1100
+ // eslint-disable-next-line no-loop-func
1101
+ onError: errorMessage => {
1102
+ error = errorMessage;
1103
+ },
1104
+ });
1105
+ }
1106
+
1107
+ expect(conditionsBag).toEqual([]);
1108
+ expect(error).toEqual('modifier.has.reached.the.maximum.amount.of.applies');
1109
+
1110
+ expect(currentOrder.items[0].modifiers.length).toBe(10);
1111
+ });
1112
+
1113
+ test('CU-86dve295v Should not throw error when adding a modifier if properties.limits.maxItemUse=-1 and isQuantityMultiplier=false', () => {
997
1114
  const order = {
998
1115
  id: 'ord-123',
999
1116
  items: [],
@@ -1008,7 +1125,7 @@ describe('Modifier actions', () => {
1008
1125
  },
1009
1126
  properties: {
1010
1127
  isQuantityMultiplier: false,
1011
- limits: { isUnlimitedAppliesItem: true },
1128
+ limits: { maxItemUse: -1 },
1012
1129
  },
1013
1130
  };
1014
1131
  order.items.push({
@@ -1046,7 +1163,7 @@ describe('Modifier actions', () => {
1046
1163
  expect(currentOrder.items[0].modifiers.length).toBe(5);
1047
1164
  });
1048
1165
 
1049
- test('CU-86dve295v Should not add the modifiers more times than item.quantity if modifier.properties.limits.isUnlimitedAppliesItem=false and modifier.properties.limits.maxAppliesItem is not set', () => {
1166
+ test('CU-86dve295v Should not add the modifiers more times than item.quantity if modifier.properties.limits.maxItemUse is not set', () => {
1050
1167
  const order = {
1051
1168
  id: 'ord-123',
1052
1169
  items: [],
@@ -1061,7 +1178,7 @@ describe('Modifier actions', () => {
1061
1178
  },
1062
1179
  properties: {
1063
1180
  isQuantityMultiplier: false,
1064
- limits: { isUnlimitedAppliesItem: false },
1181
+ limits: { maxItemUse: 0 },
1065
1182
  },
1066
1183
  };
1067
1184
  order.items.push({
@@ -1098,4 +1215,123 @@ describe('Modifier actions', () => {
1098
1215
 
1099
1216
  expect(currentOrder.items[0].modifiers.length).toBe(2);
1100
1217
  });
1218
+
1219
+ test('CU-86dve295v Should add the modifier as many times as permitted by properties.limits.maxOrderUse', () => {
1220
+ const order = {
1221
+ id: 'ord-123',
1222
+ items: [],
1223
+ modifiers: [],
1224
+ };
1225
+ const modifier = {
1226
+ _id: 1,
1227
+ compute: {
1228
+ amount: 10,
1229
+ action: 'subtract',
1230
+ type: 'fixed',
1231
+ },
1232
+ properties: {
1233
+ isQuantityMultiplier: false,
1234
+ limits: { maxOrderUse: 3 },
1235
+ },
1236
+ };
1237
+ order.items.push({
1238
+ quantity: 1,
1239
+ itemId: '111',
1240
+ price: 100,
1241
+ modifiers: [],
1242
+ });
1243
+ order.items.push({
1244
+ quantity: 1,
1245
+ itemId: '222',
1246
+ price: 100,
1247
+ modifiers: [],
1248
+ });
1249
+ order.items.push({
1250
+ quantity: 1,
1251
+ itemId: '333',
1252
+ price: 100,
1253
+ modifiers: [],
1254
+ });
1255
+
1256
+ order.items.push({
1257
+ quantity: 1,
1258
+ itemId: '444',
1259
+ price: 100,
1260
+ modifiers: [],
1261
+ });
1262
+
1263
+ const conditionsBag = [];
1264
+
1265
+ let error = '';
1266
+ let currentOrder = { ...order };
1267
+
1268
+ currentOrder = pricingService.order.addItemModifier({
1269
+ order: currentOrder,
1270
+ modifier,
1271
+ itemIndex: 0,
1272
+ onConditionsNotMet: bag => {
1273
+ bag.forEach(condition => {
1274
+ conditionsBag.push(condition);
1275
+ });
1276
+ },
1277
+ onError: errorMessage => {
1278
+ error = errorMessage;
1279
+ },
1280
+ });
1281
+
1282
+ currentOrder = pricingService.order.addItemModifier({
1283
+ order: currentOrder,
1284
+ modifier,
1285
+ itemIndex: 1,
1286
+ onConditionsNotMet: bag => {
1287
+ bag.forEach(condition => {
1288
+ conditionsBag.push(condition);
1289
+ });
1290
+ },
1291
+ onError: errorMessage => {
1292
+ error = errorMessage;
1293
+ },
1294
+ });
1295
+
1296
+ currentOrder = pricingService.order.addItemModifier({
1297
+ order: currentOrder,
1298
+ modifier,
1299
+ itemIndex: 2,
1300
+ onConditionsNotMet: bag => {
1301
+ bag.forEach(condition => {
1302
+ conditionsBag.push(condition);
1303
+ });
1304
+ },
1305
+ onError: errorMessage => {
1306
+ error = errorMessage;
1307
+ },
1308
+ });
1309
+
1310
+ expect(conditionsBag).toEqual([]);
1311
+ expect(error).toEqual('');
1312
+ expect(currentOrder.items[0].modifiers.length).toBe(1);
1313
+ expect(currentOrder.items[1].modifiers.length).toBe(1);
1314
+ expect(currentOrder.items[2].modifiers.length).toBe(1);
1315
+ expect(currentOrder.items[3].modifiers.length).toBe(0);
1316
+
1317
+ expect(currentOrder.items.length).toBe(4);
1318
+
1319
+ currentOrder = pricingService.order.addItemModifier({
1320
+ order: currentOrder,
1321
+ modifier,
1322
+ itemIndex: 3,
1323
+ onConditionsNotMet: bag => {
1324
+ bag.forEach(condition => {
1325
+ conditionsBag.push(condition);
1326
+ });
1327
+ },
1328
+ onError: errorMessage => {
1329
+ error = errorMessage;
1330
+ },
1331
+ });
1332
+
1333
+ expect(conditionsBag).toEqual([]);
1334
+ expect(error).toEqual('modifier.has.reached.the.maximum.amount.of.applies');
1335
+ expect(currentOrder.items[3].modifiers.length).toBe(0);
1336
+ });
1101
1337
  });
@@ -680,4 +680,208 @@ describe('Conditions not met for the item', () => {
680
680
  expect(order.items[5].total).toBe(5);
681
681
  expect(order.items[5].modifiers.length).toBe(1);
682
682
  });
683
+
684
+ test('#8: Item number condition is met using aggregatedItems, rule is lastSelected', () => {
685
+ const mod3x2 = {
686
+ _id: '68069a18766f8687858461e7',
687
+ name: '3x2',
688
+ properties: {},
689
+ type: 'discount',
690
+ tags: ['default'],
691
+ conditions: {
692
+ valid: null,
693
+ rules: [
694
+ {
695
+ key: 'itemSet',
696
+ value: { itemSet: '3', itemRule: 'lastSelected' },
697
+ operand: '$eq',
698
+ },
699
+ ],
700
+ },
701
+ compute: {
702
+ type: 'percentage',
703
+ action: 'subtract',
704
+ amount: 100,
705
+ },
706
+ };
707
+
708
+ const item = {
709
+ _id: 'abc',
710
+ price: 10,
711
+ quantity: 3,
712
+ };
713
+
714
+ let order = {
715
+ id: 'ord-123',
716
+ items: [],
717
+ modifiers: [],
718
+ };
719
+
720
+ order = pricingService.order.addItem({
721
+ order,
722
+ item,
723
+ }).updatedOrder;
724
+
725
+ order = pricingService.order.addItemModifier({
726
+ order,
727
+ modifier: mod3x2,
728
+ itemIndex: 0,
729
+ });
730
+
731
+ expect(order.items.length).toEqual(1);
732
+
733
+ order = pricingService.order.calculate(order);
734
+
735
+ expect(order.total).toBe(20);
736
+ expect(order.items[0].total).toBe(20);
737
+ expect(order.items[0].modifiers.length).toBe(1);
738
+ });
739
+
740
+ test('#9: Item number condition is met using aggregatedItems and others, rule is lastSelected', () => {
741
+ const mod3x2 = {
742
+ _id: '68069a18766f8687858461e7',
743
+ name: '3x2',
744
+ properties: {},
745
+ type: 'discount',
746
+ tags: ['default'],
747
+ conditions: {
748
+ valid: null,
749
+ rules: [
750
+ {
751
+ key: 'itemSet',
752
+ value: { itemSet: '3', itemRule: 'lastSelected' },
753
+ operand: '$eq',
754
+ },
755
+ ],
756
+ },
757
+ compute: {
758
+ type: 'percentage',
759
+ action: 'subtract',
760
+ amount: 100,
761
+ },
762
+ };
763
+
764
+ const item = {
765
+ _id: 'abc',
766
+ price: 10,
767
+ quantity: 2,
768
+ };
769
+
770
+ const item2 = {
771
+ _id: 'bcd',
772
+ price: 10,
773
+ quantity: 1,
774
+ };
775
+
776
+ let order = {
777
+ id: 'ord-123',
778
+ items: [],
779
+ modifiers: [],
780
+ };
781
+
782
+ order = pricingService.order.addItem({
783
+ order,
784
+ item,
785
+ }).updatedOrder;
786
+ order = pricingService.order.addItemModifier({
787
+ order,
788
+ modifier: mod3x2,
789
+ itemIndex: 0,
790
+ });
791
+
792
+ order = pricingService.order.addItem({
793
+ order,
794
+ item: item2,
795
+ }).updatedOrder;
796
+
797
+ order = pricingService.order.addItemModifier({
798
+ order,
799
+ modifier: mod3x2,
800
+ itemIndex: 0,
801
+ });
802
+
803
+ expect(order.items.length).toEqual(2);
804
+
805
+ order = pricingService.order.calculate(order);
806
+
807
+ expect(order.total).toBe(20);
808
+ expect(order.items[0].total).toBe(0);
809
+ expect(order.items[0].modifiers.length).toBe(1);
810
+ expect(order.items[1].total).toBe(20);
811
+ expect(order.items[1].modifiers.length).toBe(1);
812
+ });
813
+
814
+ test('#10: Item number condition is met using aggregatedItems and others, rule is lastSelected', () => {
815
+ const mod3x2 = {
816
+ _id: '68069a18766f8687858461e7',
817
+ name: '3x2',
818
+ properties: {},
819
+ type: 'discount',
820
+ tags: ['default'],
821
+ conditions: {
822
+ valid: null,
823
+ rules: [
824
+ {
825
+ key: 'itemSet',
826
+ value: { itemSet: '3', itemRule: 'lastSelected' },
827
+ operand: '$eq',
828
+ },
829
+ ],
830
+ },
831
+ compute: {
832
+ type: 'percentage',
833
+ action: 'subtract',
834
+ amount: 100,
835
+ },
836
+ };
837
+
838
+ const item = {
839
+ _id: 'abc',
840
+ price: 10,
841
+ quantity: 3,
842
+ };
843
+
844
+ const item2 = {
845
+ _id: 'bcd',
846
+ price: 10,
847
+ quantity: 3,
848
+ };
849
+
850
+ let order = {
851
+ id: 'ord-123',
852
+ items: [],
853
+ modifiers: [],
854
+ };
855
+
856
+ order = pricingService.order.addItem({
857
+ order,
858
+ item,
859
+ }).updatedOrder;
860
+ order = pricingService.order.addItemModifier({
861
+ order,
862
+ modifier: mod3x2,
863
+ itemIndex: 0,
864
+ });
865
+
866
+ order = pricingService.order.addItem({
867
+ order,
868
+ item: item2,
869
+ }).updatedOrder;
870
+
871
+ order = pricingService.order.addItemModifier({
872
+ order,
873
+ modifier: mod3x2,
874
+ itemIndex: 0,
875
+ });
876
+
877
+ expect(order.items.length).toEqual(2);
878
+
879
+ order = pricingService.order.calculate(order);
880
+
881
+ expect(order.total).toBe(40);
882
+ expect(order.items[0].total).toBe(20);
883
+ expect(order.items[0].modifiers.length).toBe(1);
884
+ expect(order.items[1].total).toBe(20);
885
+ expect(order.items[1].modifiers.length).toBe(1);
886
+ });
683
887
  });
@@ -3700,7 +3700,7 @@ describe('Order actions', () => {
3700
3700
  expect(splittedOrders[1].items[0].quantity).toEqual(6);
3701
3701
  });
3702
3702
 
3703
- test('CU-86dve295v Should continue to add a modifier while its quantity is less than the specified in properties.limits.maxAppliesOrder=5', () => {
3703
+ test('CU-86dve295v Should add a modifier just once at order level even if properties.limits.maxOrderUse=5', () => {
3704
3704
  const order = {
3705
3705
  id: 'ord-123',
3706
3706
  items: [],
@@ -3715,7 +3715,7 @@ describe('Order actions', () => {
3715
3715
  },
3716
3716
  modifierId: 'abc123',
3717
3717
  properties: {
3718
- limits: { maxAppliesOrder: 5 },
3718
+ limits: { maxOrderUse: 5 },
3719
3719
  },
3720
3720
  };
3721
3721
  order.items.push({
@@ -3734,10 +3734,10 @@ describe('Order actions', () => {
3734
3734
  });
3735
3735
  }
3736
3736
 
3737
- expect(currentOrder.modifiers.length).toBe(5);
3737
+ expect(currentOrder.modifiers.length).toBe(1);
3738
3738
  });
3739
3739
 
3740
- test('CU-86dve295v Should continue to add a modifier if properties.limits.isUnlimitedAppliesOrder=true', () => {
3740
+ test('CU-86dve295v Should add the modifier just once at order level even if properties.limits.maxOrderUse=-1', () => {
3741
3741
  const order = {
3742
3742
  id: 'ord-123',
3743
3743
  items: [],
@@ -3752,7 +3752,7 @@ describe('Order actions', () => {
3752
3752
  },
3753
3753
  modifierId: 'abc123',
3754
3754
  properties: {
3755
- limits: { isUnlimitedAppliesOrder: true },
3755
+ limits: { maxOrderUse: -1 },
3756
3756
  },
3757
3757
  };
3758
3758
  order.items.push({
@@ -3771,10 +3771,10 @@ describe('Order actions', () => {
3771
3771
  });
3772
3772
  }
3773
3773
 
3774
- expect(currentOrder.modifiers.length).toBe(5);
3774
+ expect(currentOrder.modifiers.length).toBe(1);
3775
3775
  });
3776
3776
 
3777
- test('CU-86dve295v Should not add a modifier more than once if properties.limits.isUnlimitedAppliesOrder=false and properties.limits.maxAppliesOrder is not defined', () => {
3777
+ test('CU-86dve295v Should not add a modifier more than once if properties.limits.maxOrderUse is not defined', () => {
3778
3778
  const order = {
3779
3779
  id: 'ord-123',
3780
3780
  items: [],
@@ -3789,7 +3789,7 @@ describe('Order actions', () => {
3789
3789
  },
3790
3790
  modifierId: 'abc123',
3791
3791
  properties: {
3792
- limits: { isUnlimitedAppliesOrder: false, maxAppliesOrder: 0 },
3792
+ limits: { maxOrderUse: 0 },
3793
3793
  },
3794
3794
  };
3795
3795
  order.items.push({
@@ -3810,4 +3810,57 @@ describe('Order actions', () => {
3810
3810
 
3811
3811
  expect(currentOrder.modifiers.length).toBe(1);
3812
3812
  });
3813
+
3814
+ test('CU-86dve295v Should add to modifiers at order level', () => {
3815
+ const order = {
3816
+ id: 'ord-123',
3817
+ items: [],
3818
+ modifiers: [],
3819
+ };
3820
+ const modifier = {
3821
+ _id: 1,
3822
+ compute: {
3823
+ amount: 10,
3824
+ action: 'subtract',
3825
+ type: 'fixed',
3826
+ },
3827
+ modifierId: 'abc123',
3828
+ properties: {
3829
+ limits: { maxOrderUse: 0 },
3830
+ },
3831
+ };
3832
+
3833
+ const modifier2 = {
3834
+ _id: 2,
3835
+ compute: {
3836
+ amount: 10,
3837
+ action: 'subtract',
3838
+ type: 'fixed',
3839
+ },
3840
+ modifierId: 'abc123',
3841
+ };
3842
+
3843
+ order.items.push({
3844
+ quantity: 2,
3845
+ itemId: '123',
3846
+ price: 100,
3847
+ modifiers: [],
3848
+ });
3849
+
3850
+ let currentOrder = { ...order };
3851
+
3852
+ for (let i = 0; i < 5; i += 1) {
3853
+ currentOrder = pricingService.order.addModifier({
3854
+ order: currentOrder,
3855
+ modifier,
3856
+ });
3857
+ }
3858
+
3859
+ currentOrder = pricingService.order.addModifier({
3860
+ order: currentOrder,
3861
+ modifier: modifier2,
3862
+ });
3863
+
3864
+ expect(currentOrder.modifiers.length).toBe(2);
3865
+ });
3813
3866
  });
@@ -8,7 +8,7 @@ module.exports = ({ modifierActions, settings }) =>
8
8
  let modifierTags = item.modifiers
9
9
  .filter(
10
10
  each =>
11
- (modifierActions.isValid(each) || modifierActions.isItemSet(each)) &&
11
+ modifierActions.isValid(each) &&
12
12
  (!modifierActions.isGroup(each) || modifierActions.isDepartment(each))
13
13
  )
14
14
 
@@ -1,4 +1,4 @@
1
- module.exports = ({ modifierActions }) =>
1
+ module.exports = ({ modifierActions, utils }) =>
2
2
  function removeModifiersByQuantity(item) {
3
3
  if (!item || !Array.isArray(item.modifiers)) {
4
4
  return item;
@@ -15,14 +15,15 @@ module.exports = ({ modifierActions }) =>
15
15
  modifierActions.isGroupOfModifiers(modifier) ||
16
16
  !modifier.modifierId ||
17
17
  !modifierActions.isDirect(modifier) ||
18
- modifierActions.isUnlimitedAppliesItem(modifier)
18
+ modifierActions.isUnlimitedItemUse(modifier)
19
19
  )
20
20
  return true;
21
21
 
22
22
  const count = modifierCounts[modifier.modifierId] || 0;
23
23
 
24
- const maxApplies = modifierActions.getMaxAppliesItem(modifier);
25
- const maxQuantity = maxApplies || item.quantity;
24
+ const maxApplies = modifierActions.getMaxItemUse(modifier) || 0;
25
+ const maxQuantity =
26
+ utils.math.mul(item.quantity || 0, maxApplies) || item.quantity;
26
27
 
27
28
  if (count < maxQuantity) {
28
29
  modifierCounts[modifier.modifierId] = count + 1;
@@ -32,29 +32,33 @@ module.exports = ({ actions, utils }) => {
32
32
  }
33
33
  });
34
34
 
35
- const itemIndexInSorted = matchingItems.findIndex(
35
+ let totalItemsLength = 0;
36
+
37
+ matchingItems.forEach(matchItem => {
38
+ totalItemsLength += matchItem.quantity || 0;
39
+ });
40
+
41
+ const itemIndex = matchingItems.findIndex(
36
42
  entry => entry._id === item._id
37
43
  );
38
44
 
39
- const itemSet =
40
- itemIndexInSorted >= 0 ? itemIndexInSorted + 1 : null;
41
-
42
- const matchingItemsLength = matchingItems.length;
45
+ const itemSetIndex = itemIndex >= 0 ? itemIndex + 1 : null;
43
46
 
44
47
  if (
45
48
  condition.operand === '$eq' &&
46
49
  conditionValue &&
47
- matchingItemsLength >= conditionValue
50
+ totalItemsLength >= conditionValue
48
51
  ) {
49
52
  const result = Math.floor(
50
- utils.math.div(matchingItemsLength, conditionValue)
53
+ utils.math.div(totalItemsLength, conditionValue)
51
54
  );
52
- if (itemSet <= result) {
55
+
56
+ if (itemSetIndex <= result) {
53
57
  return true;
54
58
  }
55
59
  }
56
60
  return actions.validateItemNumber(
57
- itemSet,
61
+ itemSetIndex,
58
62
  conditionValue,
59
63
  condition.operand
60
64
  );
@@ -101,7 +105,6 @@ module.exports = ({ actions, utils }) => {
101
105
  startRequestDate = null,
102
106
  allItems = null,
103
107
  } = opts;
104
- // const recommendedEndDate = storeActions.getRecommendedEndDate();
105
108
  return (
106
109
  modifier &&
107
110
  modifierConditionPass(modifier, {
@@ -0,0 +1,5 @@
1
+ module.exports = ({ actions }) =>
2
+ function getMaxItemUse(modifier) {
3
+ if (!actions.isLimits(modifier)) return 0;
4
+ return modifier.properties.limits.maxItemUse;
5
+ };
@@ -0,0 +1,5 @@
1
+ module.exports = ({ actions }) =>
2
+ function getMaxOrderUse(modifier) {
3
+ if (!actions.isLimits(modifier)) return 0;
4
+ return modifier.properties.limits.maxOrderUse;
5
+ };
@@ -150,13 +150,14 @@ const isGroupPath = require('./isGroupPath');
150
150
  const isSpreadFrom = require('./isSpreadFrom');
151
151
  const getRelatedModifiers = require('./getRelatedModifiers');
152
152
  const isRelatedModifier = require('./isRelatedModifier');
153
- const getMaxAppliesItem = require('./getMaxAppliesItem');
154
- const getMaxAppliesOrder = require('./getMaxAppliesOrder');
155
- const isUnlimitedAppliesItem = require('./isUnlimitedAppliesItem');
156
- const isUnlimitedAppliesOrder = require('./isUnlimitedAppliesOrder');
153
+ const getMaxItemUse = require('./getMaxItemUse');
154
+ const getMaxOrderUse = require('./getMaxOrderUse');
155
+ const isUnlimitedItemUse = require('./isUnlimitedItemUse');
156
+ const isUnlimitedOrderUse = require('./isUnlimitedOrderUse');
157
157
  const validateItemNumber = require('./validateItemNumber');
158
158
  const isLimits = require('./isLimits');
159
159
  const isItemSet = require('./isItemSet');
160
+ const isOrderUseValid = require('./isOrderUseValid');
160
161
 
161
162
  const modifierActions = (deps = {}) => {
162
163
  const actions = {};
@@ -321,12 +322,13 @@ const modifierActions = (deps = {}) => {
321
322
  isSpreadFrom: isSpreadFrom(innerDeps),
322
323
  getRelatedModifiers: getRelatedModifiers(innerDeps),
323
324
  isRelatedModifier: isRelatedModifier(innerDeps),
324
- getMaxAppliesItem: getMaxAppliesItem(innerDeps),
325
- getMaxAppliesOrder: getMaxAppliesOrder(innerDeps),
326
- isUnlimitedAppliesItem: isUnlimitedAppliesItem(innerDeps),
327
- isUnlimitedAppliesOrder: isUnlimitedAppliesOrder(innerDeps),
325
+ getMaxItemUse: getMaxItemUse(innerDeps),
326
+ getMaxOrderUse: getMaxOrderUse(innerDeps),
327
+ isUnlimitedItemUse: isUnlimitedItemUse(innerDeps),
328
+ isUnlimitedOrderUse: isUnlimitedOrderUse(innerDeps),
328
329
  isLimits: isLimits(innerDeps),
329
330
  isItemSet: isItemSet(innerDeps),
331
+ isOrderUseValid: isOrderUseValid(innerDeps),
330
332
  });
331
333
 
332
334
  Object.keys(freezedActions).forEach(actionName => {
@@ -0,0 +1,25 @@
1
+ module.exports = ({ actions }) =>
2
+ function isOrderUseValid({ order, modifier, item: itemParam }) {
3
+ if (
4
+ !actions.isLimits(modifier) ||
5
+ actions.isUnlimitedOrderUse(modifier) ||
6
+ !actions.getMaxOrderUse(modifier)
7
+ )
8
+ return true;
9
+
10
+ const limit = actions.getMaxOrderUse(modifier);
11
+
12
+ const usedIn = new Set();
13
+
14
+ // eslint-disable-next-line no-restricted-syntax
15
+ for (const item of order.items) {
16
+ if (item.modifiers.some(mod => mod.modifierId === modifier._id)) {
17
+ usedIn.add(item.itemId);
18
+ }
19
+ }
20
+
21
+ if (itemParam && itemParam.itemId && usedIn.has(itemParam.itemId))
22
+ return true;
23
+
24
+ return usedIn.size < limit;
25
+ };
@@ -0,0 +1,4 @@
1
+ module.exports = ({ actions }) =>
2
+ function isUnlimitedItemUse(modifier) {
3
+ return actions.getMaxItemUse(modifier) === -1;
4
+ };
@@ -0,0 +1,4 @@
1
+ module.exports = ({ actions }) =>
2
+ function isUnlimitedOrderUse(modifier) {
3
+ return actions.getMaxOrderUse(modifier) === -1;
4
+ };
@@ -86,12 +86,26 @@ module.exports = ({ actions, itemActions, modifierActions, utils, _ }) => {
86
86
  };
87
87
 
88
88
  const addModifier = ({
89
+ order,
89
90
  modifier: _modifier,
90
91
  item: itemProp,
91
92
  customer,
92
93
  originalItem,
93
94
  onConditionsNotMet,
95
+ onError,
94
96
  }) => {
97
+ if (
98
+ !modifierActions.isOrderUseValid({
99
+ order,
100
+ modifier: _modifier,
101
+ item: itemProp,
102
+ })
103
+ ) {
104
+ onError('modifier.has.reached.the.maximum.amount.of.applies');
105
+
106
+ return itemProp;
107
+ }
108
+
95
109
  const modifier = modifierActions.removeGroupRelations(_modifier); // to avoid no param reassign lint rule
96
110
  let item = { ...itemProp };
97
111
  const compute = getComputeModField(modifier);
@@ -191,17 +205,22 @@ module.exports = ({ actions, itemActions, modifierActions, utils, _ }) => {
191
205
  }
192
206
  // Remove if it has it already only if it is not group of value or override modifier
193
207
 
194
- const maxApplies = modifierActions.getMaxAppliesItem(modifier);
195
- const maxQuantity = maxApplies || item.quantity;
208
+ const maxApplies = modifierActions.getMaxItemUse(modifier) || 0;
209
+ const isUnlimitedItemUse = modifierActions.isUnlimitedItemUse(modifier);
210
+
211
+ const maxQuantity =
212
+ math.mul(item.quantity || 0, maxApplies) || item.quantity;
196
213
 
197
214
  if (
198
- !modifierActions.isUnlimitedAppliesItem(modifier) &&
215
+ !isUnlimitedItemUse &&
199
216
  contains &&
200
217
  usingCount >= maxQuantity &&
201
218
  !modifierActions.isOverride(modifier)
202
219
  ) {
203
- if (maxApplies && onError)
220
+ if (maxApplies && onError) {
204
221
  onError('modifier.has.reached.the.maximum.amount.of.applies');
222
+ return order;
223
+ }
205
224
 
206
225
  order = actions.removeItemModifier({
207
226
  order,
@@ -247,11 +266,13 @@ module.exports = ({ actions, itemActions, modifierActions, utils, _ }) => {
247
266
  }
248
267
 
249
268
  item = addModifier({
269
+ order,
250
270
  item,
251
271
  modifier,
252
272
  originalItem,
253
273
  customer,
254
274
  onConditionsNotMet,
275
+ onError,
255
276
  });
256
277
 
257
278
  // Recursive Rules:
@@ -263,10 +284,12 @@ module.exports = ({ actions, itemActions, modifierActions, utils, _ }) => {
263
284
  item = relatedModifiers.reduce(
264
285
  (acc, each) =>
265
286
  addModifier({
287
+ order,
266
288
  item: acc,
267
289
  modifier: each,
268
290
  originalItem,
269
291
  customer,
292
+ onError,
270
293
  }),
271
294
  item
272
295
  );
@@ -1,25 +1,15 @@
1
1
  module.exports = ({ modifierActions }) =>
2
2
  function addModifier({ order, modifier }) {
3
3
  if (!modifier) return order;
4
- const { modifiers = [] } = order;
5
4
 
6
- let usingCount = 0;
5
+ const orderModifiers = Array.isArray(order.modifiers)
6
+ ? order.modifiers
7
+ : [];
7
8
 
8
- modifiers.forEach(orderMod => {
9
- if (orderMod.modifierId === modifier.modifierId) {
10
- usingCount += 1;
11
- }
12
- });
13
- const maxQty = modifierActions.getMaxAppliesOrder(modifier) || 1;
9
+ if (modifierActions.contains(orderModifiers, modifier)) return order;
14
10
 
15
- if (
16
- modifierActions.isUnlimitedAppliesOrder(modifier) ||
17
- usingCount < maxQty
18
- )
19
- return {
20
- ...order,
21
- modifiers: [...modifiers, { ...modifier }],
22
- };
23
-
24
- return order;
11
+ return {
12
+ ...order,
13
+ modifiers: [...orderModifiers, { ...modifier }],
14
+ };
25
15
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@darkpos/pricing",
3
- "version": "1.0.79",
3
+ "version": "1.0.80",
4
4
  "description": "Pricing calculator",
5
5
  "author": "Dark POS",
6
6
  "license": "ISC",
@@ -51,5 +51,5 @@
51
51
  "supertest": "^6.2.3",
52
52
  "supervisor": "^0.12.0"
53
53
  },
54
- "gitHead": "1ebec123fd5b4e7d3aaa6b259cd4467c9dccb74b"
54
+ "gitHead": "8c3967eacf0122f2024d9b29b66acc0e3a23d51d"
55
55
  }
@@ -1,5 +0,0 @@
1
- module.exports = ({ actions }) =>
2
- function getMaxAppliesItem(modifier) {
3
- if (!actions.isLimits(modifier)) return false;
4
- return modifier.properties.limits.maxAppliesItem;
5
- };
@@ -1,5 +0,0 @@
1
- module.exports = ({ actions }) =>
2
- function getMaxAppliesOrder(modifier) {
3
- if (!actions.isLimits(modifier)) return false;
4
- return modifier.properties.limits.maxAppliesOrder;
5
- };
@@ -1,6 +0,0 @@
1
- module.exports = ({ actions }) =>
2
- function isUnlimitedAppliesItem(modifier) {
3
- if (!actions.isLimits(modifier)) return false;
4
-
5
- return !!modifier.properties.limits.isUnlimitedAppliesItem;
6
- };
@@ -1,6 +0,0 @@
1
- module.exports = ({ actions }) =>
2
- function isUnlimitedAppliesOrder(modifier) {
3
- if (!actions.isLimits(modifier)) return false;
4
-
5
- return !!modifier.properties.limits.isUnlimitedAppliesOrder;
6
- };