@darkpos/pricing 1.0.78 → 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.
@@ -873,4 +873,465 @@ describe('Modifier actions', () => {
873
873
  ]);
874
874
  });
875
875
  });
876
+
877
+ test('CU-86dve295v Should throw error when adding a modifier more times than the ones specified in properties.limits.maxItemUse', () => {
878
+ const order = {
879
+ id: 'ord-123',
880
+ items: [],
881
+ modifiers: [],
882
+ };
883
+ const modifier = {
884
+ _id: 1,
885
+ compute: {
886
+ amount: 10,
887
+ action: 'subtract',
888
+ type: 'fixed',
889
+ },
890
+ properties: {
891
+ isQuantityMultiplier: true,
892
+ limits: { maxItemUse: 1 },
893
+ },
894
+ };
895
+ order.items.push({
896
+ quantity: 2,
897
+ itemId: '123',
898
+ price: 100,
899
+ modifiers: [],
900
+ });
901
+
902
+ const conditionsBag = [];
903
+
904
+ const updatedOrder = pricingService.order.addItemModifier({
905
+ order,
906
+ modifier,
907
+ itemIndex: 0,
908
+ onConditionsNotMet: bag => {
909
+ bag.forEach(condition => {
910
+ conditionsBag.push(condition);
911
+ });
912
+ },
913
+ });
914
+
915
+ let error = '';
916
+ const updatedOrder2 = pricingService.order.addItemModifier({
917
+ order: { ...updatedOrder },
918
+ modifier,
919
+ itemIndex: 0,
920
+ onConditionsNotMet: bag => {
921
+ bag.forEach(condition => {
922
+ conditionsBag.push(condition);
923
+ });
924
+ },
925
+ onError: errorMessage => {
926
+ error = errorMessage;
927
+ },
928
+ });
929
+
930
+ expect(conditionsBag).toEqual([]);
931
+ expect(error).toEqual('modifier.has.reached.the.maximum.amount.of.applies');
932
+ expect(updatedOrder2.items[0].modifiers.length).toBe(1);
933
+ expect(updatedOrder2.items[0].modifiers).toMatchObject([
934
+ {
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
+ },
947
+ },
948
+ ]);
949
+ });
950
+
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', () => {
952
+ const order = {
953
+ id: 'ord-123',
954
+ items: [],
955
+ modifiers: [],
956
+ };
957
+ const modifier = {
958
+ _id: 1,
959
+ compute: {
960
+ amount: 10,
961
+ action: 'subtract',
962
+ type: 'fixed',
963
+ },
964
+ properties: {
965
+ isQuantityMultiplier: false,
966
+ limits: { maxItemUse: 5 },
967
+ },
968
+ };
969
+ order.items.push({
970
+ quantity: 2,
971
+ itemId: '123',
972
+ price: 100,
973
+ modifiers: [],
974
+ });
975
+
976
+ const conditionsBag = [];
977
+
978
+ let error = '';
979
+ let currentOrder = { ...order };
980
+
981
+ for (let i = 0; i < 5; i += 1) {
982
+ currentOrder = pricingService.order.addItemModifier({
983
+ order: currentOrder,
984
+ modifier,
985
+ itemIndex: 0,
986
+ onConditionsNotMet: bag => {
987
+ bag.forEach(condition => {
988
+ conditionsBag.push(condition);
989
+ });
990
+ },
991
+ // eslint-disable-next-line no-loop-func
992
+ onError: errorMessage => {
993
+ error = errorMessage;
994
+ },
995
+ });
996
+ }
997
+
998
+ expect(conditionsBag).toEqual([]);
999
+ expect(error).toEqual('');
1000
+
1001
+ expect(currentOrder.items[0].modifiers.length).toBe(5);
1002
+ });
1003
+
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', () => {
1114
+ const order = {
1115
+ id: 'ord-123',
1116
+ items: [],
1117
+ modifiers: [],
1118
+ };
1119
+ const modifier = {
1120
+ _id: 1,
1121
+ compute: {
1122
+ amount: 10,
1123
+ action: 'subtract',
1124
+ type: 'fixed',
1125
+ },
1126
+ properties: {
1127
+ isQuantityMultiplier: false,
1128
+ limits: { maxItemUse: -1 },
1129
+ },
1130
+ };
1131
+ order.items.push({
1132
+ quantity: 2,
1133
+ itemId: '123',
1134
+ price: 100,
1135
+ modifiers: [],
1136
+ });
1137
+
1138
+ const conditionsBag = [];
1139
+
1140
+ let error = '';
1141
+ let currentOrder = { ...order };
1142
+
1143
+ for (let i = 0; i < 5; i += 1) {
1144
+ currentOrder = pricingService.order.addItemModifier({
1145
+ order: currentOrder,
1146
+ modifier,
1147
+ itemIndex: 0,
1148
+ onConditionsNotMet: bag => {
1149
+ bag.forEach(condition => {
1150
+ conditionsBag.push(condition);
1151
+ });
1152
+ },
1153
+ // eslint-disable-next-line no-loop-func
1154
+ onError: errorMessage => {
1155
+ error = errorMessage;
1156
+ },
1157
+ });
1158
+ }
1159
+
1160
+ expect(conditionsBag).toEqual([]);
1161
+ expect(error).toEqual('');
1162
+
1163
+ expect(currentOrder.items[0].modifiers.length).toBe(5);
1164
+ });
1165
+
1166
+ test('CU-86dve295v Should not add the modifiers more times than item.quantity if modifier.properties.limits.maxItemUse is not set', () => {
1167
+ const order = {
1168
+ id: 'ord-123',
1169
+ items: [],
1170
+ modifiers: [],
1171
+ };
1172
+ const modifier = {
1173
+ _id: 1,
1174
+ compute: {
1175
+ amount: 10,
1176
+ action: 'subtract',
1177
+ type: 'fixed',
1178
+ },
1179
+ properties: {
1180
+ isQuantityMultiplier: false,
1181
+ limits: { maxItemUse: 0 },
1182
+ },
1183
+ };
1184
+ order.items.push({
1185
+ quantity: 2,
1186
+ itemId: '123',
1187
+ price: 100,
1188
+ modifiers: [],
1189
+ });
1190
+
1191
+ const conditionsBag = [];
1192
+
1193
+ let error = '';
1194
+ let currentOrder = { ...order };
1195
+
1196
+ for (let i = 0; i < 5; i += 1) {
1197
+ currentOrder = pricingService.order.addItemModifier({
1198
+ order: currentOrder,
1199
+ modifier,
1200
+ itemIndex: 0,
1201
+ onConditionsNotMet: bag => {
1202
+ bag.forEach(condition => {
1203
+ conditionsBag.push(condition);
1204
+ });
1205
+ },
1206
+ // eslint-disable-next-line no-loop-func
1207
+ onError: errorMessage => {
1208
+ error = errorMessage;
1209
+ },
1210
+ });
1211
+ }
1212
+
1213
+ expect(conditionsBag).toEqual([]);
1214
+ expect(error).toEqual('');
1215
+
1216
+ expect(currentOrder.items[0].modifiers.length).toBe(2);
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
+ });
876
1337
  });