@wtree/payload-ecommerce-coupon 3.78.3 → 3.78.4
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/dist/endpoints/applyCoupon.d.ts +1 -1
- package/dist/endpoints/applyCoupon.d.ts.map +1 -1
- package/dist/endpoints/validateCoupon.d.ts +1 -1
- package/dist/endpoints/validateCoupon.d.ts.map +1 -1
- package/dist/index.js +137 -44
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +101 -8
- package/dist/index.mjs.map +1 -1
- package/dist/plugin.d.ts.map +1 -1
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"applyCoupon.d.ts","sourceRoot":"","sources":["../../src/endpoints/applyCoupon.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"applyCoupon.d.ts","sourceRoot":"","sources":["../../src/endpoints/applyCoupon.ts"],"names":[],"mappings":"AAAA,OAAO,EAA2B,KAAK,QAAQ,EAAE,KAAK,cAAc,EAAE,MAAM,SAAS,CAAA;AACrF,OAAO,KAAK,EAAE,4BAA4B,EAAE,MAAM,UAAU,CAAA;AAQ5D,KAAK,IAAI,GAAG;IACV,YAAY,EAAE,4BAA4B,CAAA;CAC3C,CAAA;AAqGD,eAAO,MAAM,kBAAkB,GAC5B,kBAAkB,IAAI,KAAG,cAgHzB,CAAA;AAgRH,eAAO,MAAM,mBAAmB,GAAI,kBAAkB,IAAI,KAAG,QAI3D,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validateCoupon.d.ts","sourceRoot":"","sources":["../../src/endpoints/validateCoupon.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"validateCoupon.d.ts","sourceRoot":"","sources":["../../src/endpoints/validateCoupon.ts"],"names":[],"mappings":"AAAA,OAAO,EAA2B,KAAK,QAAQ,EAAE,KAAK,cAAc,EAAE,MAAM,SAAS,CAAA;AAErF,OAAO,KAAK,EAAE,4BAA4B,EAAE,MAAM,UAAU,CAAA;AAO5D,KAAK,IAAI,GAAG;IACV,YAAY,EAAE,4BAA4B,CAAA;CAC3C,CAAA;AA4FD,eAAO,MAAM,qBAAqB,GAC/B,kBAAkB,IAAI,KAAG,cAiEzB,CAAA;AA+OH,eAAO,MAAM,sBAAsB,GAAI,kBAAkB,IAAI,KAAG,QAI9D,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -958,38 +958,56 @@ function writeField$1(doc, field, value) {
|
|
|
958
958
|
function normalizeCode$1(value) {
|
|
959
959
|
return typeof value === "string" ? value.trim().toUpperCase() : "";
|
|
960
960
|
}
|
|
961
|
-
async function
|
|
962
|
-
|
|
961
|
+
async function ensureRequestData$1(req) {
|
|
962
|
+
if (req?.data && typeof req.data === "object") return req.data;
|
|
963
|
+
try {
|
|
964
|
+
await (0, payload.addDataAndFileToRequest)(req);
|
|
965
|
+
} catch {}
|
|
966
|
+
if (req?.data && typeof req.data === "object") return req.data;
|
|
967
|
+
try {
|
|
968
|
+
const parsed = await req?.json?.();
|
|
969
|
+
if (parsed && typeof parsed === "object") {
|
|
970
|
+
req.data = parsed;
|
|
971
|
+
return parsed;
|
|
972
|
+
}
|
|
973
|
+
} catch {}
|
|
974
|
+
req.data = {};
|
|
975
|
+
return req.data;
|
|
976
|
+
}
|
|
977
|
+
async function findByNormalizedCode$1({ payload: payload$5, collection, normalizedCode }) {
|
|
978
|
+
const exactQuery = await payload$5.find({
|
|
963
979
|
collection,
|
|
964
980
|
where: { normalizedCode: { equals: normalizedCode } },
|
|
965
981
|
limit: 1
|
|
966
982
|
});
|
|
967
983
|
if (exactQuery?.docs?.[0]) return exactQuery.docs[0];
|
|
968
|
-
const lowerQuery = await payload.find({
|
|
984
|
+
const lowerQuery = await payload$5.find({
|
|
969
985
|
collection,
|
|
970
986
|
where: { code: { equals: normalizedCode.toLowerCase() } },
|
|
971
987
|
limit: 1
|
|
972
988
|
});
|
|
973
989
|
if (lowerQuery?.docs?.[0]) return lowerQuery.docs[0];
|
|
974
|
-
const upperQuery = await payload.find({
|
|
990
|
+
const upperQuery = await payload$5.find({
|
|
975
991
|
collection,
|
|
976
992
|
where: { code: { equals: normalizedCode.toUpperCase() } },
|
|
977
993
|
limit: 1
|
|
978
994
|
});
|
|
979
995
|
if (upperQuery?.docs?.[0]) return upperQuery.docs[0];
|
|
980
|
-
return (await payload.find({
|
|
996
|
+
return (await payload$5.find({
|
|
981
997
|
collection,
|
|
982
998
|
where: { code: { equals: normalizedCode } },
|
|
983
999
|
limit: 1
|
|
984
1000
|
}))?.docs?.[0] ?? null;
|
|
985
1001
|
}
|
|
986
1002
|
const applyCouponHandler = ({ pluginConfig }) => async (req) => {
|
|
987
|
-
const { payload } = req;
|
|
1003
|
+
const { payload: payload$6 } = req;
|
|
988
1004
|
const fields = pluginConfig.integration.fields;
|
|
989
1005
|
const collections = pluginConfig.integration.collections;
|
|
990
|
-
const
|
|
991
|
-
const
|
|
992
|
-
const
|
|
1006
|
+
const data = await ensureRequestData$1(req);
|
|
1007
|
+
const rawCode = data?.code;
|
|
1008
|
+
const cartIDRaw = data?.cartID;
|
|
1009
|
+
const cartID = typeof cartIDRaw === "string" || typeof cartIDRaw === "number" ? cartIDRaw : null;
|
|
1010
|
+
const customerEmail = typeof data?.customerEmail === "string" ? data.customerEmail : void 0;
|
|
993
1011
|
const normalizedCode = normalizeCode$1(rawCode);
|
|
994
1012
|
if (!normalizedCode || !cartID) return Response.json({
|
|
995
1013
|
success: false,
|
|
@@ -998,19 +1016,19 @@ const applyCouponHandler = ({ pluginConfig }) => async (req) => {
|
|
|
998
1016
|
const allowCoupon = await Promise.resolve(pluginConfig.policies.canApplyCoupon({
|
|
999
1017
|
req,
|
|
1000
1018
|
user: req?.user,
|
|
1001
|
-
payload
|
|
1019
|
+
payload: payload$6
|
|
1002
1020
|
}));
|
|
1003
1021
|
const allowReferral = await Promise.resolve(pluginConfig.policies.canApplyReferral({
|
|
1004
1022
|
req,
|
|
1005
1023
|
user: req?.user,
|
|
1006
|
-
payload
|
|
1024
|
+
payload: payload$6
|
|
1007
1025
|
}));
|
|
1008
1026
|
if (!allowCoupon && !(pluginConfig.enableReferrals && allowReferral)) return Response.json({
|
|
1009
1027
|
success: false,
|
|
1010
1028
|
error: "Forbidden"
|
|
1011
1029
|
}, { status: 403 });
|
|
1012
1030
|
try {
|
|
1013
|
-
const cart = await payload.findByID({
|
|
1031
|
+
const cart = await payload$6.findByID({
|
|
1014
1032
|
collection: collections.cartsSlug,
|
|
1015
1033
|
id: cartID,
|
|
1016
1034
|
depth: 2
|
|
@@ -1027,14 +1045,14 @@ const applyCouponHandler = ({ pluginConfig }) => async (req) => {
|
|
|
1027
1045
|
}, { status: 400 });
|
|
1028
1046
|
if (pluginConfig.enableReferrals && allowReferral) {
|
|
1029
1047
|
const referralResult = await handleReferralCode({
|
|
1030
|
-
payload,
|
|
1048
|
+
payload: payload$6,
|
|
1031
1049
|
cart,
|
|
1032
1050
|
cartID,
|
|
1033
1051
|
normalizedCode,
|
|
1034
1052
|
pluginConfig
|
|
1035
1053
|
});
|
|
1036
1054
|
if (!referralResult.ok && referralResult.status === 404 && pluginConfig.referralConfig.allowBothSystems && allowCoupon) return await handleCouponCode({
|
|
1037
|
-
payload,
|
|
1055
|
+
payload: payload$6,
|
|
1038
1056
|
cart,
|
|
1039
1057
|
cartID,
|
|
1040
1058
|
normalizedCode,
|
|
@@ -1048,7 +1066,7 @@ const applyCouponHandler = ({ pluginConfig }) => async (req) => {
|
|
|
1048
1066
|
error: "Forbidden"
|
|
1049
1067
|
}, { status: 403 });
|
|
1050
1068
|
return await handleCouponCode({
|
|
1051
|
-
payload,
|
|
1069
|
+
payload: payload$6,
|
|
1052
1070
|
cart,
|
|
1053
1071
|
cartID,
|
|
1054
1072
|
normalizedCode,
|
|
@@ -1063,11 +1081,11 @@ const applyCouponHandler = ({ pluginConfig }) => async (req) => {
|
|
|
1063
1081
|
}, { status: 500 });
|
|
1064
1082
|
}
|
|
1065
1083
|
};
|
|
1066
|
-
async function handleCouponCode({ payload, cart, cartID, normalizedCode, customerEmail, pluginConfig }) {
|
|
1084
|
+
async function handleCouponCode({ payload: payload$7, cart, cartID, normalizedCode, customerEmail, pluginConfig }) {
|
|
1067
1085
|
const fields = pluginConfig.integration.fields;
|
|
1068
1086
|
const resolvers = pluginConfig.integration.resolvers;
|
|
1069
1087
|
const coupon = await findByNormalizedCode$1({
|
|
1070
|
-
payload,
|
|
1088
|
+
payload: payload$7,
|
|
1071
1089
|
collection: pluginConfig.collections.couponsSlug,
|
|
1072
1090
|
normalizedCode
|
|
1073
1091
|
});
|
|
@@ -1096,7 +1114,7 @@ async function handleCouponCode({ payload, cart, cartID, normalizedCode, custome
|
|
|
1096
1114
|
success: false,
|
|
1097
1115
|
error: "Customer email is required for this coupon."
|
|
1098
1116
|
}, { status: 400 });
|
|
1099
|
-
if ((await payload.find({
|
|
1117
|
+
if ((await payload$7.find({
|
|
1100
1118
|
collection: pluginConfig.orderIntegration.ordersSlug,
|
|
1101
1119
|
where: { and: [
|
|
1102
1120
|
{ [fields.orderAppliedCouponField]: { equals: coupon.id } },
|
|
@@ -1132,7 +1150,7 @@ async function handleCouponCode({ payload, cart, cartID, normalizedCode, custome
|
|
|
1132
1150
|
writeField$1(data, fields.cartAppliedCouponField, coupon.id);
|
|
1133
1151
|
writeField$1(data, fields.cartDiscountAmountField, discountAmount);
|
|
1134
1152
|
writeField$1(data, fields.cartTotalField, nextTotal);
|
|
1135
|
-
await payload.update({
|
|
1153
|
+
await payload$7.update({
|
|
1136
1154
|
collection: pluginConfig.integration.collections.cartsSlug,
|
|
1137
1155
|
id: cartID,
|
|
1138
1156
|
data
|
|
@@ -1149,11 +1167,11 @@ async function handleCouponCode({ payload, cart, cartID, normalizedCode, custome
|
|
|
1149
1167
|
currency: pluginConfig.defaultCurrency
|
|
1150
1168
|
});
|
|
1151
1169
|
}
|
|
1152
|
-
async function handleReferralCode({ payload, cart, cartID, normalizedCode, pluginConfig }) {
|
|
1170
|
+
async function handleReferralCode({ payload: payload$8, cart, cartID, normalizedCode, pluginConfig }) {
|
|
1153
1171
|
const fields = pluginConfig.integration.fields;
|
|
1154
1172
|
const resolvers = pluginConfig.integration.resolvers;
|
|
1155
1173
|
const referralCode = await findByNormalizedCode$1({
|
|
1156
|
-
payload,
|
|
1174
|
+
payload: payload$8,
|
|
1157
1175
|
collection: pluginConfig.collections.referralCodesSlug,
|
|
1158
1176
|
normalizedCode
|
|
1159
1177
|
});
|
|
@@ -1174,7 +1192,7 @@ async function handleReferralCode({ payload, cart, cartID, normalizedCode, plugi
|
|
|
1174
1192
|
error: "Referral code usage limit exceeded"
|
|
1175
1193
|
}, { status: 400 });
|
|
1176
1194
|
const programId = typeof referralCode.program === "string" || typeof referralCode.program === "number" ? referralCode.program : referralCode.program?.id;
|
|
1177
|
-
const program = await payload.findByID({
|
|
1195
|
+
const program = await payload$8.findByID({
|
|
1178
1196
|
collection: pluginConfig.collections.referralProgramsSlug,
|
|
1179
1197
|
id: programId
|
|
1180
1198
|
});
|
|
@@ -1211,7 +1229,7 @@ async function handleReferralCode({ payload, cart, cartID, normalizedCode, plugi
|
|
|
1211
1229
|
writeField$1(data, fields.cartPartnerCommissionField, roundedPartnerCommission);
|
|
1212
1230
|
writeField$1(data, fields.cartCustomerDiscountField, roundedCustomerDiscount);
|
|
1213
1231
|
writeField$1(data, fields.cartTotalField, nextTotal);
|
|
1214
|
-
await payload.update({
|
|
1232
|
+
await payload$8.update({
|
|
1215
1233
|
collection: pluginConfig.integration.collections.cartsSlug,
|
|
1216
1234
|
id: cartID,
|
|
1217
1235
|
data
|
|
@@ -1418,37 +1436,55 @@ function relationId$2(value) {
|
|
|
1418
1436
|
function normalizeCode(value) {
|
|
1419
1437
|
return typeof value === "string" ? value.trim().toUpperCase() : "";
|
|
1420
1438
|
}
|
|
1421
|
-
async function
|
|
1422
|
-
|
|
1439
|
+
async function ensureRequestData(req) {
|
|
1440
|
+
if (req?.data && typeof req.data === "object") return req.data;
|
|
1441
|
+
try {
|
|
1442
|
+
await (0, payload.addDataAndFileToRequest)(req);
|
|
1443
|
+
} catch {}
|
|
1444
|
+
if (req?.data && typeof req.data === "object") return req.data;
|
|
1445
|
+
try {
|
|
1446
|
+
const parsed = await req?.json?.();
|
|
1447
|
+
if (parsed && typeof parsed === "object") {
|
|
1448
|
+
req.data = parsed;
|
|
1449
|
+
return parsed;
|
|
1450
|
+
}
|
|
1451
|
+
} catch {}
|
|
1452
|
+
req.data = {};
|
|
1453
|
+
return req.data;
|
|
1454
|
+
}
|
|
1455
|
+
async function findByNormalizedCode({ payload: payload$1, collection, normalizedCode }) {
|
|
1456
|
+
const normalizedQuery = await payload$1.find({
|
|
1423
1457
|
collection,
|
|
1424
1458
|
where: { normalizedCode: { equals: normalizedCode } },
|
|
1425
1459
|
limit: 1
|
|
1426
1460
|
});
|
|
1427
1461
|
if (normalizedQuery?.docs?.length) return normalizedQuery.docs[0];
|
|
1428
|
-
const lowerQuery = await payload.find({
|
|
1462
|
+
const lowerQuery = await payload$1.find({
|
|
1429
1463
|
collection,
|
|
1430
1464
|
where: { code: { equals: normalizedCode.toLowerCase() } },
|
|
1431
1465
|
limit: 1
|
|
1432
1466
|
});
|
|
1433
1467
|
if (lowerQuery?.docs?.length) return lowerQuery.docs[0];
|
|
1434
|
-
const upperQuery = await payload.find({
|
|
1468
|
+
const upperQuery = await payload$1.find({
|
|
1435
1469
|
collection,
|
|
1436
1470
|
where: { code: { equals: normalizedCode.toUpperCase() } },
|
|
1437
1471
|
limit: 1
|
|
1438
1472
|
});
|
|
1439
1473
|
if (upperQuery?.docs?.length) return upperQuery.docs[0];
|
|
1440
|
-
return (await payload.find({
|
|
1474
|
+
return (await payload$1.find({
|
|
1441
1475
|
collection,
|
|
1442
1476
|
where: { code: { equals: normalizedCode } },
|
|
1443
1477
|
limit: 1
|
|
1444
1478
|
}))?.docs?.[0] ?? null;
|
|
1445
1479
|
}
|
|
1446
1480
|
const validateCouponHandler = ({ pluginConfig }) => async (req) => {
|
|
1447
|
-
const { payload } = req;
|
|
1448
|
-
const
|
|
1449
|
-
const
|
|
1450
|
-
const
|
|
1451
|
-
const
|
|
1481
|
+
const { payload: payload$2 } = req;
|
|
1482
|
+
const data = await ensureRequestData(req);
|
|
1483
|
+
const rawCode = data?.code;
|
|
1484
|
+
const cartValue = typeof data?.cartValue === "number" ? data.cartValue : void 0;
|
|
1485
|
+
const cartIDRaw = data?.cartID;
|
|
1486
|
+
const cartID = typeof cartIDRaw === "string" || typeof cartIDRaw === "number" ? cartIDRaw : void 0;
|
|
1487
|
+
const customerEmail = typeof data?.customerEmail === "string" ? data.customerEmail : void 0;
|
|
1452
1488
|
const normalizedCode = normalizeCode(rawCode);
|
|
1453
1489
|
if (!normalizedCode) return Response.json({
|
|
1454
1490
|
success: false,
|
|
@@ -1459,13 +1495,13 @@ const validateCouponHandler = ({ pluginConfig }) => async (req) => {
|
|
|
1459
1495
|
if (!await Promise.resolve(pluginConfig.policies.canApplyReferral({
|
|
1460
1496
|
req,
|
|
1461
1497
|
user: req?.user,
|
|
1462
|
-
payload
|
|
1498
|
+
payload: payload$2
|
|
1463
1499
|
}))) return Response.json({
|
|
1464
1500
|
success: false,
|
|
1465
1501
|
error: "Forbidden"
|
|
1466
1502
|
}, { status: 403 });
|
|
1467
1503
|
return await validateReferralCode({
|
|
1468
|
-
payload,
|
|
1504
|
+
payload: payload$2,
|
|
1469
1505
|
normalizedCode,
|
|
1470
1506
|
cartID,
|
|
1471
1507
|
pluginConfig
|
|
@@ -1474,13 +1510,13 @@ const validateCouponHandler = ({ pluginConfig }) => async (req) => {
|
|
|
1474
1510
|
if (!await Promise.resolve(pluginConfig.policies.canApplyCoupon({
|
|
1475
1511
|
req,
|
|
1476
1512
|
user: req?.user,
|
|
1477
|
-
payload
|
|
1513
|
+
payload: payload$2
|
|
1478
1514
|
}))) return Response.json({
|
|
1479
1515
|
success: false,
|
|
1480
1516
|
error: "Forbidden"
|
|
1481
1517
|
}, { status: 403 });
|
|
1482
1518
|
return await validateCouponCode$1({
|
|
1483
|
-
payload,
|
|
1519
|
+
payload: payload$2,
|
|
1484
1520
|
normalizedCode,
|
|
1485
1521
|
cartValue,
|
|
1486
1522
|
customerEmail,
|
|
@@ -1494,10 +1530,10 @@ const validateCouponHandler = ({ pluginConfig }) => async (req) => {
|
|
|
1494
1530
|
}, { status: 500 });
|
|
1495
1531
|
}
|
|
1496
1532
|
};
|
|
1497
|
-
async function validateCouponCode$1({ payload, normalizedCode, cartValue, customerEmail, pluginConfig }) {
|
|
1533
|
+
async function validateCouponCode$1({ payload: payload$3, normalizedCode, cartValue, customerEmail, pluginConfig }) {
|
|
1498
1534
|
const fields = pluginConfig.integration.fields;
|
|
1499
1535
|
const couponData = await findByNormalizedCode({
|
|
1500
|
-
payload,
|
|
1536
|
+
payload: payload$3,
|
|
1501
1537
|
collection: pluginConfig.collections.couponsSlug,
|
|
1502
1538
|
normalizedCode
|
|
1503
1539
|
});
|
|
@@ -1522,7 +1558,7 @@ async function validateCouponCode$1({ payload, normalizedCode, cartValue, custom
|
|
|
1522
1558
|
}, { status: 400 });
|
|
1523
1559
|
if (couponData.perCustomerLimit != null && couponData.perCustomerLimit > 0 && typeof customerEmail === "string" && customerEmail.trim().length > 0) {
|
|
1524
1560
|
const email = customerEmail.trim();
|
|
1525
|
-
if ((await payload.find({
|
|
1561
|
+
if ((await payload$3.find({
|
|
1526
1562
|
collection: pluginConfig.orderIntegration.ordersSlug,
|
|
1527
1563
|
where: { and: [
|
|
1528
1564
|
{ [fields.orderAppliedCouponField]: { equals: couponData.id } },
|
|
@@ -1569,11 +1605,11 @@ async function validateCouponCode$1({ payload, normalizedCode, cartValue, custom
|
|
|
1569
1605
|
currency: pluginConfig.defaultCurrency
|
|
1570
1606
|
});
|
|
1571
1607
|
}
|
|
1572
|
-
async function validateReferralCode({ payload, normalizedCode, cartID, pluginConfig }) {
|
|
1608
|
+
async function validateReferralCode({ payload: payload$4, normalizedCode, cartID, pluginConfig }) {
|
|
1573
1609
|
const collections = pluginConfig.integration.collections;
|
|
1574
1610
|
const resolvers = pluginConfig.integration.resolvers;
|
|
1575
1611
|
const referralData = await findByNormalizedCode({
|
|
1576
|
-
payload,
|
|
1612
|
+
payload: payload$4,
|
|
1577
1613
|
collection: pluginConfig.collections.referralCodesSlug,
|
|
1578
1614
|
normalizedCode
|
|
1579
1615
|
});
|
|
@@ -1598,7 +1634,7 @@ async function validateReferralCode({ payload, normalizedCode, cartID, pluginCon
|
|
|
1598
1634
|
success: false,
|
|
1599
1635
|
error: "Referral program not found"
|
|
1600
1636
|
}, { status: 404 });
|
|
1601
|
-
const program = await payload.findByID({
|
|
1637
|
+
const program = await payload$4.findByID({
|
|
1602
1638
|
collection: pluginConfig.collections.referralProgramsSlug,
|
|
1603
1639
|
id: programId
|
|
1604
1640
|
});
|
|
@@ -1606,7 +1642,7 @@ async function validateReferralCode({ payload, normalizedCode, cartID, pluginCon
|
|
|
1606
1642
|
success: false,
|
|
1607
1643
|
error: "Referral program is not active"
|
|
1608
1644
|
}, { status: 400 });
|
|
1609
|
-
const cart = cartID ? await payload.findByID({
|
|
1645
|
+
const cart = cartID ? await payload$4.findByID({
|
|
1610
1646
|
collection: collections.cartsSlug,
|
|
1611
1647
|
id: cartID,
|
|
1612
1648
|
depth: 2
|
|
@@ -2143,6 +2179,55 @@ const sanitizePluginConfig = ({ pluginConfig }) => {
|
|
|
2143
2179
|
const RECALCULATE_HOOK_KEY = "__payloadEcommerceCouponRecalculateHook__";
|
|
2144
2180
|
const asArray = (value) => Array.isArray(value) ? value : [];
|
|
2145
2181
|
const hasNamedField = (collection, fieldName) => asArray(collection.fields).some((f) => f?.name === fieldName);
|
|
2182
|
+
const normalizePath$1 = (path) => {
|
|
2183
|
+
if (!path) return "/";
|
|
2184
|
+
return (path.startsWith("/") ? path : `/${path}`).replace(/\/+$/, "") || "/";
|
|
2185
|
+
};
|
|
2186
|
+
const toCollectionEndpointPath = ({ endpointPath, collectionSlug }) => {
|
|
2187
|
+
const normalizedEndpointPath = normalizePath$1(endpointPath);
|
|
2188
|
+
const normalizedCollectionPath = normalizePath$1(`/${collectionSlug}`);
|
|
2189
|
+
if (!normalizedEndpointPath.startsWith(`${normalizedCollectionPath}/`)) return null;
|
|
2190
|
+
const relative = normalizedEndpointPath.slice(normalizedCollectionPath.length);
|
|
2191
|
+
return relative.startsWith("/") ? relative : `/${relative}`;
|
|
2192
|
+
};
|
|
2193
|
+
const ensureCouponCollectionEndpoints = ({ collection, pluginConfig }) => {
|
|
2194
|
+
const couponsSlug = pluginConfig.collections.couponsSlug;
|
|
2195
|
+
const applyPath = toCollectionEndpointPath({
|
|
2196
|
+
endpointPath: pluginConfig.endpoints.applyCoupon,
|
|
2197
|
+
collectionSlug: couponsSlug
|
|
2198
|
+
});
|
|
2199
|
+
const validatePath = toCollectionEndpointPath({
|
|
2200
|
+
endpointPath: pluginConfig.endpoints.validateCoupon,
|
|
2201
|
+
collectionSlug: couponsSlug
|
|
2202
|
+
});
|
|
2203
|
+
if (!applyPath && !validatePath) return collection;
|
|
2204
|
+
const endpoints = asArray(collection.endpoints);
|
|
2205
|
+
const endpointKeys = new Set(endpoints.map((e) => `${(e?.method || "get").toLowerCase()}:${e?.path || ""}`));
|
|
2206
|
+
if (validatePath) {
|
|
2207
|
+
const validateKey = `post:${validatePath}`;
|
|
2208
|
+
if (!endpointKeys.has(validateKey)) {
|
|
2209
|
+
endpointKeys.add(validateKey);
|
|
2210
|
+
endpoints.push({
|
|
2211
|
+
path: validatePath,
|
|
2212
|
+
method: "post",
|
|
2213
|
+
handler: validateCouponEndpoint({ pluginConfig }).handler
|
|
2214
|
+
});
|
|
2215
|
+
}
|
|
2216
|
+
}
|
|
2217
|
+
if (applyPath) {
|
|
2218
|
+
const applyKey = `post:${applyPath}`;
|
|
2219
|
+
if (!endpointKeys.has(applyKey)) {
|
|
2220
|
+
endpointKeys.add(applyKey);
|
|
2221
|
+
endpoints.push({
|
|
2222
|
+
path: applyPath,
|
|
2223
|
+
method: "post",
|
|
2224
|
+
handler: applyCouponEndpoint({ pluginConfig }).handler
|
|
2225
|
+
});
|
|
2226
|
+
}
|
|
2227
|
+
}
|
|
2228
|
+
collection.endpoints = endpoints;
|
|
2229
|
+
return collection;
|
|
2230
|
+
};
|
|
2146
2231
|
const addFieldsToCollection = (config, targetSlug, newFields) => {
|
|
2147
2232
|
const collections = asArray(config.collections);
|
|
2148
2233
|
const idx = collections.findIndex((c) => c.slug === targetSlug);
|
|
@@ -2227,11 +2312,19 @@ const payloadEcommerceCouponPlugin = (pluginOptions = {}) => async (incomingConf
|
|
|
2227
2312
|
if (pluginConfig.referralConfig.allowBothSystems) {
|
|
2228
2313
|
let couponsCollection = createCouponsCollection(pluginConfig);
|
|
2229
2314
|
if (pluginOptions.collections?.couponsCollectionOverride) couponsCollection = await pluginOptions.collections.couponsCollectionOverride({ defaultCollection: couponsCollection });
|
|
2315
|
+
couponsCollection = ensureCouponCollectionEndpoints({
|
|
2316
|
+
collection: couponsCollection,
|
|
2317
|
+
pluginConfig
|
|
2318
|
+
});
|
|
2230
2319
|
collectionsToAdd.push(couponsCollection);
|
|
2231
2320
|
}
|
|
2232
2321
|
} else {
|
|
2233
2322
|
let couponsCollection = createCouponsCollection(pluginConfig);
|
|
2234
2323
|
if (pluginOptions.collections?.couponsCollectionOverride) couponsCollection = await pluginOptions.collections.couponsCollectionOverride({ defaultCollection: couponsCollection });
|
|
2324
|
+
couponsCollection = ensureCouponCollectionEndpoints({
|
|
2325
|
+
collection: couponsCollection,
|
|
2326
|
+
pluginConfig
|
|
2327
|
+
});
|
|
2235
2328
|
collectionsToAdd.push(couponsCollection);
|
|
2236
2329
|
}
|
|
2237
2330
|
const existingSlugs = new Set(asArray(incomingConfig.collections).map((c) => c.slug));
|