@unchainedshop/plugins 4.0.0-rc.2 → 4.0.0-rc.21
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/lib/accounts/google-oauth.d.ts +4 -0
- package/lib/accounts/google-oauth.d.ts.map +1 -0
- package/lib/accounts/google-oauth.js +145 -0
- package/lib/accounts/google-oauth.js.map +1 -0
- package/lib/accounts/linkedin-oauth.d.ts +4 -0
- package/lib/accounts/linkedin-oauth.d.ts.map +1 -0
- package/lib/accounts/linkedin-oauth.js +82 -0
- package/lib/accounts/linkedin-oauth.js.map +1 -0
- package/lib/delivery/send-message.js.map +1 -1
- package/lib/enrollments/licensed.js +15 -2
- package/lib/enrollments/licensed.js.map +1 -1
- package/lib/files/gridfs/adapter.d.ts.map +1 -1
- package/lib/files/gridfs/adapter.js +19 -11
- package/lib/files/gridfs/adapter.js.map +1 -1
- package/lib/files/gridfs/gridfs-adapter.d.ts +3 -0
- package/lib/files/gridfs/gridfs-adapter.d.ts.map +1 -0
- package/lib/files/gridfs/gridfs-adapter.js +97 -0
- package/lib/files/gridfs/gridfs-adapter.js.map +1 -0
- package/lib/files/gridfs/gridfs-webhook.d.ts +11 -0
- package/lib/files/gridfs/gridfs-webhook.d.ts.map +1 -0
- package/lib/files/gridfs/gridfs-webhook.js +84 -0
- package/lib/files/gridfs/gridfs-webhook.js.map +1 -0
- package/lib/files/gridfs/handler-express.d.ts.map +1 -1
- package/lib/files/gridfs/handler-express.js +27 -24
- package/lib/files/gridfs/handler-express.js.map +1 -1
- package/lib/files/gridfs/handler-fastify.d.ts.map +1 -1
- package/lib/files/gridfs/handler-fastify.js +57 -14
- package/lib/files/gridfs/handler-fastify.js.map +1 -1
- package/lib/files/gridfs/module.d.ts +4 -4
- package/lib/files/gridfs/module.d.ts.map +1 -1
- package/lib/files/gridfs/module.js +2 -2
- package/lib/files/gridfs/module.js.map +1 -1
- package/lib/files/minio/handler-express.d.ts.map +1 -1
- package/lib/files/minio/handler-express.js +4 -7
- package/lib/files/minio/handler-express.js.map +1 -1
- package/lib/files/minio/handler-fastify.js +2 -2
- package/lib/files/minio/handler-fastify.js.map +1 -1
- package/lib/files/minio/index.d.ts +1 -1
- package/lib/files/minio/index.d.ts.map +1 -1
- package/lib/files/minio/index.js +7 -7
- package/lib/files/minio/index.js.map +1 -1
- package/lib/files/minio/minio-adapter.d.ts +6 -0
- package/lib/files/minio/minio-adapter.d.ts.map +1 -0
- package/lib/files/minio/minio-adapter.js +181 -0
- package/lib/files/minio/minio-adapter.js.map +1 -0
- package/lib/files/minio/minio-webhook.d.ts +2 -0
- package/lib/files/minio/minio-webhook.d.ts.map +1 -0
- package/lib/files/minio/minio-webhook.js +33 -0
- package/lib/files/minio/minio-webhook.js.map +1 -0
- package/lib/filters/local-search.js +1 -1
- package/lib/filters/local-search.js.map +1 -1
- package/lib/payment/apple-iap/adapter.js +12 -10
- package/lib/payment/apple-iap/adapter.js.map +1 -1
- package/lib/payment/apple-iap/db/AppleTransactionsCollection.d.ts +1 -1
- package/lib/payment/apple-iap/db/AppleTransactionsCollection.d.ts.map +1 -1
- package/lib/payment/apple-iap/handler-express.d.ts.map +1 -1
- package/lib/payment/apple-iap/handler-express.js +14 -10
- package/lib/payment/apple-iap/handler-express.js.map +1 -1
- package/lib/payment/apple-iap/handler-fastify.d.ts.map +1 -1
- package/lib/payment/apple-iap/handler-fastify.js +15 -8
- package/lib/payment/apple-iap/handler-fastify.js.map +1 -1
- package/lib/payment/apple-iap/module/configureAppleTransactionsModule.d.ts +9 -0
- package/lib/payment/apple-iap/module/configureAppleTransactionsModule.d.ts.map +1 -0
- package/lib/payment/apple-iap/module/configureAppleTransactionsModule.js +17 -0
- package/lib/payment/apple-iap/module/configureAppleTransactionsModule.js.map +1 -0
- package/lib/payment/apple-iap/module.d.ts +4 -4
- package/lib/payment/apple-iap/module.d.ts.map +1 -1
- package/lib/payment/apple-iap/module.js +4 -2
- package/lib/payment/apple-iap/module.js.map +1 -1
- package/lib/payment/braintree.js +3 -1
- package/lib/payment/braintree.js.map +1 -1
- package/lib/payment/cryptopay/db/CryptopayTransactions.d.ts +1 -1
- package/lib/payment/cryptopay/db/CryptopayTransactions.d.ts.map +1 -1
- package/lib/payment/cryptopay/derive-btc-address.d.ts.map +1 -1
- package/lib/payment/cryptopay/derive-btc-address.js +2 -3
- package/lib/payment/cryptopay/derive-btc-address.js.map +1 -1
- package/lib/payment/cryptopay/derive-eth-address.d.ts.map +1 -1
- package/lib/payment/cryptopay/derive-eth-address.js +5 -3
- package/lib/payment/cryptopay/derive-eth-address.js.map +1 -1
- package/lib/payment/cryptopay/handle-webhook.js +2 -2
- package/lib/payment/cryptopay/handle-webhook.js.map +1 -1
- package/lib/payment/cryptopay/handler-express.d.ts.map +1 -1
- package/lib/payment/cryptopay/handler-express.js +4 -7
- package/lib/payment/cryptopay/handler-express.js.map +1 -1
- package/lib/payment/cryptopay/handler-fastify.d.ts.map +1 -1
- package/lib/payment/cryptopay/handler-fastify.js +1 -1
- package/lib/payment/cryptopay/handler-fastify.js.map +1 -1
- package/lib/payment/cryptopay/middleware.d.ts +2 -0
- package/lib/payment/cryptopay/middleware.d.ts.map +1 -0
- package/lib/payment/cryptopay/middleware.js +75 -0
- package/lib/payment/cryptopay/middleware.js.map +1 -0
- package/lib/payment/cryptopay/module/configureCryptopayModule.d.ts +25 -0
- package/lib/payment/cryptopay/module/configureCryptopayModule.d.ts.map +1 -0
- package/lib/payment/cryptopay/module/configureCryptopayModule.js +75 -0
- package/lib/payment/cryptopay/module/configureCryptopayModule.js.map +1 -0
- package/lib/payment/cryptopay/module.d.ts +2 -2
- package/lib/payment/cryptopay/module.d.ts.map +1 -1
- package/lib/payment/cryptopay/module.js +4 -4
- package/lib/payment/cryptopay/module.js.map +1 -1
- package/lib/payment/cryptopay/plugin.d.ts.map +1 -1
- package/lib/payment/cryptopay/plugin.js +18 -4
- package/lib/payment/cryptopay/plugin.js.map +1 -1
- package/lib/payment/datatrans-v2/handler-express.d.ts.map +1 -1
- package/lib/payment/datatrans-v2/handler-express.js +13 -11
- package/lib/payment/datatrans-v2/handler-express.js.map +1 -1
- package/lib/payment/datatrans-v2/handler-fastify.d.ts.map +1 -1
- package/lib/payment/datatrans-v2/handler-fastify.js +11 -5
- package/lib/payment/datatrans-v2/handler-fastify.js.map +1 -1
- package/lib/payment/datatrans-v2/index.js +63 -37
- package/lib/payment/datatrans-v2/index.js.map +1 -1
- package/lib/payment/datatrans-v2/middleware.d.ts +2 -0
- package/lib/payment/datatrans-v2/middleware.d.ts.map +1 -0
- package/lib/payment/datatrans-v2/middleware.js +68 -0
- package/lib/payment/datatrans-v2/middleware.js.map +1 -0
- package/lib/payment/datatrans-v2/parseRegistrationData.d.ts +1 -1
- package/lib/payment/datatrans-v2/parseRegistrationData.d.ts.map +1 -1
- package/lib/payment/paypal-checkout.js +8 -8
- package/lib/payment/paypal-checkout.js.map +1 -1
- package/lib/payment/payrexx/api/index.d.ts +1 -1
- package/lib/payment/payrexx/api/index.d.ts.map +1 -1
- package/lib/payment/payrexx/api/makeFetcher.d.ts +1 -1
- package/lib/payment/payrexx/api/makeFetcher.d.ts.map +1 -1
- package/lib/payment/payrexx/api/makeFetcher.js +1 -1
- package/lib/payment/payrexx/api/makeFetcher.js.map +1 -1
- package/lib/payment/payrexx/handler-express.d.ts.map +1 -1
- package/lib/payment/payrexx/handler-express.js +15 -15
- package/lib/payment/payrexx/handler-express.js.map +1 -1
- package/lib/payment/payrexx/handler-fastify.js +23 -15
- package/lib/payment/payrexx/handler-fastify.js.map +1 -1
- package/lib/payment/payrexx/index.js +21 -15
- package/lib/payment/payrexx/index.js.map +1 -1
- package/lib/payment/payrexx/middleware.d.ts +2 -0
- package/lib/payment/payrexx/middleware.d.ts.map +1 -0
- package/lib/payment/payrexx/middleware.js +86 -0
- package/lib/payment/payrexx/middleware.js.map +1 -0
- package/lib/payment/postfinance-checkout/api-client.d.ts +16 -0
- package/lib/payment/postfinance-checkout/api-client.d.ts.map +1 -0
- package/lib/payment/postfinance-checkout/api-client.js +75 -0
- package/lib/payment/postfinance-checkout/api-client.js.map +1 -0
- package/lib/payment/postfinance-checkout/api-types.d.ts +98 -0
- package/lib/payment/postfinance-checkout/api-types.d.ts.map +1 -0
- package/lib/payment/postfinance-checkout/api-types.js +51 -0
- package/lib/payment/postfinance-checkout/api-types.js.map +1 -0
- package/lib/payment/postfinance-checkout/api.d.ts +5 -5
- package/lib/payment/postfinance-checkout/api.d.ts.map +1 -1
- package/lib/payment/postfinance-checkout/api.js +38 -62
- package/lib/payment/postfinance-checkout/api.js.map +1 -1
- package/lib/payment/postfinance-checkout/handler-express.d.ts.map +1 -1
- package/lib/payment/postfinance-checkout/handler-express.js +10 -11
- package/lib/payment/postfinance-checkout/handler-express.js.map +1 -1
- package/lib/payment/postfinance-checkout/handler-fastify.d.ts.map +1 -1
- package/lib/payment/postfinance-checkout/handler-fastify.js +10 -8
- package/lib/payment/postfinance-checkout/handler-fastify.js.map +1 -1
- package/lib/payment/postfinance-checkout/index.d.ts +2 -1
- package/lib/payment/postfinance-checkout/index.d.ts.map +1 -1
- package/lib/payment/postfinance-checkout/index.js +44 -32
- package/lib/payment/postfinance-checkout/index.js.map +1 -1
- package/lib/payment/postfinance-checkout/middleware.d.ts +2 -0
- package/lib/payment/postfinance-checkout/middleware.d.ts.map +1 -0
- package/lib/payment/postfinance-checkout/middleware.js +38 -0
- package/lib/payment/postfinance-checkout/middleware.js.map +1 -0
- package/lib/payment/postfinance-checkout/utils.d.ts +3 -3
- package/lib/payment/postfinance-checkout/utils.d.ts.map +1 -1
- package/lib/payment/postfinance-checkout/utils.js +3 -4
- package/lib/payment/postfinance-checkout/utils.js.map +1 -1
- package/lib/payment/saferpay/adapter.d.ts.map +1 -1
- package/lib/payment/saferpay/adapter.js +9 -0
- package/lib/payment/saferpay/adapter.js.map +1 -1
- package/lib/payment/saferpay/handler-express.d.ts.map +1 -1
- package/lib/payment/saferpay/handler-express.js +10 -8
- package/lib/payment/saferpay/handler-express.js.map +1 -1
- package/lib/payment/saferpay/handler-fastify.d.ts.map +1 -1
- package/lib/payment/saferpay/handler-fastify.js +11 -5
- package/lib/payment/saferpay/handler-fastify.js.map +1 -1
- package/lib/payment/saferpay/middleware.d.ts +2 -0
- package/lib/payment/saferpay/middleware.d.ts.map +1 -0
- package/lib/payment/saferpay/middleware.js +57 -0
- package/lib/payment/saferpay/middleware.js.map +1 -0
- package/lib/payment/saferpay/module/configureSaferpayTransactionsModule.d.ts +10 -0
- package/lib/payment/saferpay/module/configureSaferpayTransactionsModule.d.ts.map +1 -0
- package/lib/payment/saferpay/module/configureSaferpayTransactionsModule.js +24 -0
- package/lib/payment/saferpay/module/configureSaferpayTransactionsModule.js.map +1 -0
- package/lib/payment/saferpay/module.d.ts +2 -2
- package/lib/payment/stripe/handler-express.js +25 -17
- package/lib/payment/stripe/handler-express.js.map +1 -1
- package/lib/payment/stripe/handler-fastify.d.ts.map +1 -1
- package/lib/payment/stripe/handler-fastify.js +33 -12
- package/lib/payment/stripe/handler-fastify.js.map +1 -1
- package/lib/payment/stripe/index.js +19 -13
- package/lib/payment/stripe/index.js.map +1 -1
- package/lib/payment/stripe/middleware.d.ts +6 -0
- package/lib/payment/stripe/middleware.d.ts.map +1 -0
- package/lib/payment/stripe/middleware.js +114 -0
- package/lib/payment/stripe/middleware.js.map +1 -0
- package/lib/payment/stripe/stripe.d.ts +2 -2
- package/lib/payment/stripe/stripe.d.ts.map +1 -1
- package/lib/payment/stripe/stripe.js +14 -11
- package/lib/payment/stripe/stripe.js.map +1 -1
- package/lib/plugins-index.d.ts +48 -0
- package/lib/plugins-index.d.ts.map +1 -0
- package/lib/plugins-index.js +107 -0
- package/lib/plugins-index.js.map +1 -0
- package/lib/presets/all.d.ts +9 -7
- package/lib/presets/all.d.ts.map +1 -1
- package/lib/presets/all.js +3 -1
- package/lib/presets/all.js.map +1 -1
- package/lib/pricing/delivery-swiss-tax.d.ts.map +1 -1
- package/lib/pricing/delivery-swiss-tax.js +13 -20
- package/lib/pricing/delivery-swiss-tax.js.map +1 -1
- package/lib/pricing/order-discount.d.ts.map +1 -1
- package/lib/pricing/order-discount.js +5 -3
- package/lib/pricing/order-discount.js.map +1 -1
- package/lib/pricing/order-round.d.ts.map +1 -1
- package/lib/pricing/order-round.js +4 -3
- package/lib/pricing/order-round.js.map +1 -1
- package/lib/pricing/product-catalog-price.d.ts.map +1 -1
- package/lib/pricing/product-catalog-price.js +26 -2
- package/lib/pricing/product-catalog-price.js.map +1 -1
- package/lib/pricing/product-price-rateconversion.js +3 -3
- package/lib/pricing/product-price-rateconversion.js.map +1 -1
- package/lib/pricing/product-swiss-tax.d.ts +1 -5
- package/lib/pricing/product-swiss-tax.d.ts.map +1 -1
- package/lib/pricing/product-swiss-tax.js +26 -22
- package/lib/pricing/product-swiss-tax.js.map +1 -1
- package/lib/pricing/tax/ch.d.ts +7 -2
- package/lib/pricing/tax/ch.d.ts.map +1 -1
- package/lib/pricing/tax/ch.js +16 -1
- package/lib/pricing/tax/ch.js.map +1 -1
- package/lib/pricing/utils/isDeliveryAddressInCountry.d.ts +7 -0
- package/lib/pricing/utils/isDeliveryAddressInCountry.d.ts.map +1 -0
- package/lib/pricing/utils/isDeliveryAddressInCountry.js +13 -0
- package/lib/pricing/utils/isDeliveryAddressInCountry.js.map +1 -0
- package/lib/warehousing/eth-minter.js +23 -11
- package/lib/warehousing/eth-minter.js.map +1 -1
- package/lib/worker/budgetsms.d.ts +10 -0
- package/lib/worker/budgetsms.d.ts.map +1 -0
- package/lib/worker/budgetsms.js +217 -0
- package/lib/worker/budgetsms.js.map +1 -0
- package/lib/worker/bulk-import.d.ts +1 -1
- package/lib/worker/bulk-import.d.ts.map +1 -1
- package/lib/worker/bulk-import.js +11 -33
- package/lib/worker/bulk-import.js.map +1 -1
- package/lib/worker/bulkgate.d.ts +2 -0
- package/lib/worker/bulkgate.d.ts.map +1 -0
- package/lib/worker/bulkgate.js +104 -0
- package/lib/worker/bulkgate.js.map +1 -0
- package/lib/worker/email.d.ts.map +1 -1
- package/lib/worker/email.js +25 -7
- package/lib/worker/email.js.map +1 -1
- package/lib/worker/enrollment-order-generator.d.ts.map +1 -1
- package/lib/worker/enrollment-order-generator.js +7 -49
- package/lib/worker/enrollment-order-generator.js.map +1 -1
- package/lib/worker/error-notifications.js +1 -1
- package/lib/worker/error-notifications.js.map +1 -1
- package/lib/worker/export-token.d.ts.map +1 -1
- package/lib/worker/export-token.js +0 -1
- package/lib/worker/export-token.js.map +1 -1
- package/lib/worker/heartbeat.js +2 -8
- package/lib/worker/heartbeat.js.map +1 -1
- package/lib/worker/message.d.ts +1 -1
- package/lib/worker/message.d.ts.map +1 -1
- package/lib/worker/message.js +4 -2
- package/lib/worker/message.js.map +1 -1
- package/lib/worker/sms.js +16 -16
- package/lib/worker/sms.js.map +1 -1
- package/lib/worker/twilio.d.ts +2 -0
- package/lib/worker/twilio.d.ts.map +1 -0
- package/lib/worker/twilio.js +60 -0
- package/lib/worker/twilio.js.map +1 -0
- package/package.json +29 -45
- package/src/delivery/send-message.ts +1 -1
- package/src/enrollments/licensed.ts +15 -2
- package/src/files/gridfs/adapter.ts +24 -10
- package/src/files/gridfs/handler-express.ts +32 -24
- package/src/files/gridfs/handler-fastify.ts +63 -15
- package/src/files/gridfs/module.ts +7 -2
- package/src/files/minio/handler-express.ts +4 -7
- package/src/files/minio/handler-fastify.ts +2 -2
- package/src/files/minio/index.ts +16 -16
- package/src/filters/local-search.ts +1 -1
- package/src/payment/apple-iap/adapter.ts +12 -14
- package/src/payment/apple-iap/db/AppleTransactionsCollection.ts +1 -1
- package/src/payment/apple-iap/handler-express.ts +17 -11
- package/src/payment/apple-iap/handler-fastify.ts +19 -9
- package/src/payment/apple-iap/module.ts +9 -4
- package/src/payment/braintree.ts +4 -1
- package/src/payment/cryptopay/db/CryptopayTransactions.ts +1 -1
- package/src/payment/cryptopay/derive-btc-address.ts +1 -3
- package/src/payment/cryptopay/derive-eth-address.ts +5 -3
- package/src/payment/cryptopay/handle-webhook.ts +6 -6
- package/src/payment/cryptopay/handler-express.ts +4 -7
- package/src/payment/cryptopay/handler-fastify.ts +2 -2
- package/src/payment/cryptopay/module.ts +8 -5
- package/src/payment/cryptopay/plugin.ts +21 -4
- package/src/payment/datatrans-v2/handler-express.ts +17 -12
- package/src/payment/datatrans-v2/handler-fastify.ts +15 -6
- package/src/payment/datatrans-v2/index.ts +82 -50
- package/src/payment/paypal-checkout.ts +8 -11
- package/src/payment/payrexx/api/index.ts +1 -1
- package/src/payment/payrexx/api/makeFetcher.ts +2 -2
- package/src/payment/payrexx/handler-express.ts +24 -32
- package/src/payment/payrexx/handler-fastify.ts +33 -33
- package/src/payment/payrexx/index.ts +26 -16
- package/src/payment/postfinance-checkout/api-client.ts +111 -0
- package/src/payment/postfinance-checkout/api-types.ts +111 -0
- package/src/payment/postfinance-checkout/api.ts +58 -85
- package/src/payment/postfinance-checkout/handler-express.ts +11 -13
- package/src/payment/postfinance-checkout/handler-fastify.ts +11 -10
- package/src/payment/postfinance-checkout/index.ts +60 -38
- package/src/payment/postfinance-checkout/utils.ts +5 -10
- package/src/payment/saferpay/adapter.ts +14 -2
- package/src/payment/saferpay/handler-express.ts +14 -13
- package/src/payment/saferpay/handler-fastify.ts +14 -9
- package/src/payment/stripe/handler-express.ts +33 -33
- package/src/payment/stripe/handler-fastify.ts +40 -27
- package/src/payment/stripe/index.ts +17 -13
- package/src/payment/stripe/stripe.ts +17 -12
- package/src/presets/all.ts +3 -1
- package/src/pricing/delivery-swiss-tax.ts +16 -31
- package/src/pricing/order-discount.ts +6 -4
- package/src/pricing/order-round.ts +5 -4
- package/src/pricing/product-catalog-price.ts +25 -2
- package/src/pricing/product-price-rateconversion.ts +3 -3
- package/src/pricing/product-swiss-tax.ts +39 -40
- package/src/pricing/tax/ch.ts +33 -7
- package/src/pricing/utils/isDeliveryAddressInCountry.test.ts +106 -0
- package/src/pricing/utils/isDeliveryAddressInCountry.ts +26 -0
- package/src/warehousing/eth-minter.ts +30 -11
- package/src/worker/budgetsms.ts +268 -0
- package/src/worker/bulk-import.ts +12 -53
- package/src/worker/bulkgate.ts +138 -0
- package/src/worker/email.ts +30 -8
- package/src/worker/enrollment-order-generator.ts +14 -72
- package/src/worker/error-notifications.ts +2 -2
- package/src/worker/export-token.ts +0 -1
- package/src/worker/heartbeat.ts +2 -9
- package/src/worker/message.ts +5 -3
- package/src/worker/{sms.ts → twilio.ts} +15 -26
- package/lib/payment/cryptopay/de.d.ts +0 -2
- package/lib/payment/cryptopay/de.d.ts.map +0 -1
- package/lib/payment/cryptopay/de.js +0 -10
- package/lib/payment/cryptopay/de.js.map +0 -1
- package/lib/payment/cryptopay/derive-btc-address.test copy.d.ts +0 -2
- package/lib/payment/cryptopay/derive-btc-address.test copy.d.ts.map +0 -1
- package/lib/payment/cryptopay/derive-btc-address.test copy.js +0 -10
- package/lib/payment/cryptopay/derive-btc-address.test copy.js.map +0 -1
- package/src/pricing/product-swiss-tax.test.ts +0 -139
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { describe, it } from 'node:test';
|
|
2
|
+
import assert from 'node:assert';
|
|
3
|
+
import isDeliveryAddressInCountry from './isDeliveryAddressInCountry.js';
|
|
4
|
+
|
|
5
|
+
describe('isDeliveryAddressInCountry', () => {
|
|
6
|
+
const context = {
|
|
7
|
+
modules: {
|
|
8
|
+
orders: {
|
|
9
|
+
deliveries: {
|
|
10
|
+
findDelivery: async ({ orderDeliveryId }: any) => {
|
|
11
|
+
if (orderDeliveryId === 'CH' || orderDeliveryId === 'LI')
|
|
12
|
+
return { context: { address: { countryCode: 'CH' } } };
|
|
13
|
+
if (orderDeliveryId === null) return { context: { address: null } };
|
|
14
|
+
return { context: { address: { countryCode: 'RT' } } };
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
order: {
|
|
20
|
+
deliveryId: 'CH',
|
|
21
|
+
billingAddress: { countryCode: 'LI' },
|
|
22
|
+
},
|
|
23
|
+
countryCode: 'CH',
|
|
24
|
+
};
|
|
25
|
+
it('Should return true when passed country code CH', async () => {
|
|
26
|
+
assert.strictEqual(
|
|
27
|
+
await isDeliveryAddressInCountry({ ...context, countryCode: 'ch', order: null } as any, [
|
|
28
|
+
'CH',
|
|
29
|
+
'LI',
|
|
30
|
+
]),
|
|
31
|
+
true,
|
|
32
|
+
);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('Should return true when passed country code LI', async () => {
|
|
36
|
+
assert.strictEqual(
|
|
37
|
+
await isDeliveryAddressInCountry({ ...context, countryCode: 'LI', order: null } as any, [
|
|
38
|
+
'CH',
|
|
39
|
+
'LI',
|
|
40
|
+
]),
|
|
41
|
+
true,
|
|
42
|
+
);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('Should return false when country is neither CH nor LI and order is null', async () => {
|
|
46
|
+
assert.strictEqual(
|
|
47
|
+
await isDeliveryAddressInCountry({ ...context, countryCode: 'ET', order: null } as any, [
|
|
48
|
+
'CH',
|
|
49
|
+
'LI',
|
|
50
|
+
]),
|
|
51
|
+
false,
|
|
52
|
+
);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('Order should take precedence over country parameter', async () => {
|
|
56
|
+
// not CH or LI order so returns false
|
|
57
|
+
assert.strictEqual(
|
|
58
|
+
await isDeliveryAddressInCountry(
|
|
59
|
+
{
|
|
60
|
+
...context,
|
|
61
|
+
countryCode: 'CH',
|
|
62
|
+
order: {
|
|
63
|
+
deliveryId: 'ET',
|
|
64
|
+
billingAddress: { countryCode: 'ET' },
|
|
65
|
+
},
|
|
66
|
+
} as any,
|
|
67
|
+
['CH', 'LI'],
|
|
68
|
+
),
|
|
69
|
+
false,
|
|
70
|
+
);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('If order is not from CH and LI but billingAddress is, it should take precedence over country ', async () => {
|
|
74
|
+
assert.strictEqual(
|
|
75
|
+
await isDeliveryAddressInCountry(
|
|
76
|
+
{
|
|
77
|
+
...context,
|
|
78
|
+
countryCode: 'IT',
|
|
79
|
+
order: {
|
|
80
|
+
deliveryId: null,
|
|
81
|
+
billingAddress: { countryCode: 'CH' },
|
|
82
|
+
},
|
|
83
|
+
} as any,
|
|
84
|
+
['CH', 'LI'],
|
|
85
|
+
),
|
|
86
|
+
true,
|
|
87
|
+
);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('Should return false if billingAddress is neither CH or LI and order does not have address', async () => {
|
|
91
|
+
assert.strictEqual(
|
|
92
|
+
await isDeliveryAddressInCountry(
|
|
93
|
+
{
|
|
94
|
+
...context,
|
|
95
|
+
country: null,
|
|
96
|
+
order: {
|
|
97
|
+
deliveryId: null,
|
|
98
|
+
billingAddress: { countryCode: 'HH' },
|
|
99
|
+
},
|
|
100
|
+
} as any,
|
|
101
|
+
['CH', 'LI'],
|
|
102
|
+
),
|
|
103
|
+
false,
|
|
104
|
+
);
|
|
105
|
+
});
|
|
106
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Order, OrderDelivery } from '@unchainedshop/core-orders';
|
|
2
|
+
|
|
3
|
+
export default function isDeliveryAddressInCountry(
|
|
4
|
+
{
|
|
5
|
+
orderDelivery,
|
|
6
|
+
order,
|
|
7
|
+
countryCode: forceCountryCode = null,
|
|
8
|
+
}: {
|
|
9
|
+
orderDelivery?: OrderDelivery | null;
|
|
10
|
+
order?: Order | null;
|
|
11
|
+
countryCode?: string | null;
|
|
12
|
+
},
|
|
13
|
+
allowedCountryCodes: string[],
|
|
14
|
+
) {
|
|
15
|
+
let countryCode = forceCountryCode?.toUpperCase().trim() || order?.countryCode;
|
|
16
|
+
|
|
17
|
+
if (orderDelivery || order) {
|
|
18
|
+
const address = orderDelivery?.context?.address || order?.billingAddress;
|
|
19
|
+
if (address?.countryCode > '') {
|
|
20
|
+
countryCode = address.countryCode?.toUpperCase().trim();
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (!countryCode) return false;
|
|
25
|
+
return allowedCountryCodes.includes(countryCode);
|
|
26
|
+
}
|
|
@@ -11,9 +11,12 @@ import { ProductContractStandard, ProductTypes } from '@unchainedshop/core-produ
|
|
|
11
11
|
import { systemLocale } from '@unchainedshop/utils';
|
|
12
12
|
import { generateDbObjectId } from '@unchainedshop/mongodb';
|
|
13
13
|
import { getFileAdapter } from '@unchainedshop/core-files';
|
|
14
|
+
import { createLogger } from '@unchainedshop/logger';
|
|
14
15
|
|
|
15
16
|
const { MINTER_TOKEN_OFFSET = '0', ROOT_URL = 'http://localhost:4010' } = process.env;
|
|
16
17
|
|
|
18
|
+
const logger = createLogger('unchained:eth-minter');
|
|
19
|
+
|
|
17
20
|
const ETHMinter: IWarehousingAdapter = {
|
|
18
21
|
...WarehousingAdapter,
|
|
19
22
|
|
|
@@ -40,7 +43,7 @@ const ETHMinter: IWarehousingAdapter = {
|
|
|
40
43
|
? { contractAddress: contractStandard }
|
|
41
44
|
: {
|
|
42
45
|
contractAddress: contractStandard,
|
|
43
|
-
|
|
46
|
+
tokenSerialNumber: tokenId,
|
|
44
47
|
},
|
|
45
48
|
);
|
|
46
49
|
const tokensCreated = existingTokens.reduce((acc, curToken) => {
|
|
@@ -57,7 +60,13 @@ const ETHMinter: IWarehousingAdapter = {
|
|
|
57
60
|
},
|
|
58
61
|
|
|
59
62
|
configurationError() {
|
|
60
|
-
|
|
63
|
+
if (contractStandard === 'ERC1155' && !tokenId) {
|
|
64
|
+
logger.error(
|
|
65
|
+
`Token ID is required for ERC1155 contract standard (productId: ${product!._id})`,
|
|
66
|
+
);
|
|
67
|
+
return WarehousingError.INCOMPLETE_CONFIGURATION;
|
|
68
|
+
}
|
|
69
|
+
return null;
|
|
61
70
|
},
|
|
62
71
|
|
|
63
72
|
stock: async () => {
|
|
@@ -74,11 +83,17 @@ const ETHMinter: IWarehousingAdapter = {
|
|
|
74
83
|
const meta = { contractStandard };
|
|
75
84
|
const tokensCreated = await getTokensCreated();
|
|
76
85
|
|
|
86
|
+
if (!orderPosition) {
|
|
87
|
+
throw new Error('Order position not found in context');
|
|
88
|
+
}
|
|
89
|
+
|
|
77
90
|
if (contractStandard === 'ERC721') {
|
|
78
|
-
// ERC721 is non-fungible, thus every
|
|
79
|
-
const items = new Array(orderPosition
|
|
91
|
+
// ERC721 is non-fungible, thus every tokenSerialNumber unique!
|
|
92
|
+
const items = new Array(orderPosition.quantity).fill(null).map((_, i) => ({
|
|
80
93
|
_id: generateDbObjectId(),
|
|
81
|
-
|
|
94
|
+
tokenSerialNumber: Number(
|
|
95
|
+
parseInt(MINTER_TOKEN_OFFSET, 10) + tokensCreated + (i + 1),
|
|
96
|
+
).toString(),
|
|
82
97
|
contractAddress,
|
|
83
98
|
quantity: 1,
|
|
84
99
|
chainId,
|
|
@@ -90,20 +105,24 @@ const ETHMinter: IWarehousingAdapter = {
|
|
|
90
105
|
return [
|
|
91
106
|
{
|
|
92
107
|
_id: generateDbObjectId(),
|
|
93
|
-
|
|
108
|
+
tokenSerialNumber: tokenId!,
|
|
94
109
|
contractAddress,
|
|
95
|
-
quantity: orderPosition
|
|
110
|
+
quantity: orderPosition.quantity,
|
|
96
111
|
chainId,
|
|
97
112
|
meta,
|
|
98
113
|
},
|
|
99
114
|
];
|
|
100
115
|
},
|
|
101
116
|
|
|
102
|
-
tokenMetadata: async (
|
|
117
|
+
tokenMetadata: async (tokenSerialNumber) => {
|
|
103
118
|
// Metadata standards supported
|
|
104
119
|
// https://eips.ethereum.org/EIPS/eip-1155 (backward compatible with 721)
|
|
105
120
|
// https://eips.ethereum.org/EIPS/eip-721
|
|
106
121
|
|
|
122
|
+
if (!product) {
|
|
123
|
+
throw new Error('Product not found in context');
|
|
124
|
+
}
|
|
125
|
+
|
|
107
126
|
const allLanguages = await modules.languages.findLanguages({
|
|
108
127
|
includeInactive: false,
|
|
109
128
|
});
|
|
@@ -114,15 +133,15 @@ const ETHMinter: IWarehousingAdapter = {
|
|
|
114
133
|
});
|
|
115
134
|
const file = firstMedia && (await modules.files.findFile({ fileId: firstMedia.mediaId }));
|
|
116
135
|
|
|
117
|
-
const
|
|
118
|
-
const signedUrl = await
|
|
136
|
+
const fileAdapter = file && getFileAdapter();
|
|
137
|
+
const signedUrl = await fileAdapter?.createDownloadURL(file!);
|
|
119
138
|
const url = signedUrl && (await modules.files.normalizeUrl(signedUrl, {}));
|
|
120
139
|
const text = await modules.products.texts.findLocalizedText({
|
|
121
140
|
productId: product._id,
|
|
122
141
|
locale: locale || systemLocale,
|
|
123
142
|
});
|
|
124
143
|
|
|
125
|
-
const name = `${text.title} #${
|
|
144
|
+
const name = `${text.title} #${tokenSerialNumber}`;
|
|
126
145
|
|
|
127
146
|
const isDefaultLanguageActive = locale ? locale.language === systemLocale.language : true;
|
|
128
147
|
const localization = isDefaultLanguageActive
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import { IWorkerAdapter, WorkerAdapter, WorkerDirector } from '@unchainedshop/core';
|
|
2
|
+
|
|
3
|
+
const { BUDGETSMS_USERNAME, BUDGETSMS_USERID, BUDGETSMS_HANDLE } = process.env;
|
|
4
|
+
|
|
5
|
+
// BudgetSMS Plugin - Verified against official API documentation
|
|
6
|
+
// Note: BudgetSMS API has a quirk where it returns error 1001 (authentication failed)
|
|
7
|
+
// when you have insufficient credit, instead of the correct error 1003.
|
|
8
|
+
const BudgetSMSWorker: IWorkerAdapter<
|
|
9
|
+
{
|
|
10
|
+
from?: string;
|
|
11
|
+
to: string;
|
|
12
|
+
text?: string;
|
|
13
|
+
test?: boolean; // If true, uses test endpoint (no credit deducted)
|
|
14
|
+
customid?: string;
|
|
15
|
+
price?: boolean; // Include price info in response
|
|
16
|
+
mccmnc?: boolean; // Include carrier info in response
|
|
17
|
+
credit?: boolean; // Include remaining credit in response
|
|
18
|
+
[key: string]: any;
|
|
19
|
+
},
|
|
20
|
+
any
|
|
21
|
+
> = {
|
|
22
|
+
...WorkerAdapter,
|
|
23
|
+
|
|
24
|
+
key: 'shop.unchained.worker-plugin.budgetsms',
|
|
25
|
+
label: 'Send Messages through BudgetSMS',
|
|
26
|
+
version: '1.0.0',
|
|
27
|
+
|
|
28
|
+
type: 'BUDGETSMS',
|
|
29
|
+
|
|
30
|
+
doWork: async ({
|
|
31
|
+
from,
|
|
32
|
+
to,
|
|
33
|
+
text,
|
|
34
|
+
test = false,
|
|
35
|
+
customid,
|
|
36
|
+
price = false,
|
|
37
|
+
mccmnc = false,
|
|
38
|
+
credit = false,
|
|
39
|
+
...params
|
|
40
|
+
}) => {
|
|
41
|
+
try {
|
|
42
|
+
// Validate credentials
|
|
43
|
+
if (!BUDGETSMS_USERNAME || !BUDGETSMS_USERID || !BUDGETSMS_HANDLE) {
|
|
44
|
+
throw new Error(
|
|
45
|
+
'Missing BudgetSMS credentials. Please set BUDGETSMS_USERNAME, BUDGETSMS_USERID, and BUDGETSMS_HANDLE',
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Trim credentials to remove any whitespace
|
|
50
|
+
const username = BUDGETSMS_USERNAME.trim();
|
|
51
|
+
const userid = BUDGETSMS_USERID.trim();
|
|
52
|
+
const handle = BUDGETSMS_HANDLE.trim();
|
|
53
|
+
|
|
54
|
+
// Validate userid is numeric only
|
|
55
|
+
if (!/^\d+$/.test(userid)) {
|
|
56
|
+
throw new Error('BUDGETSMS_USERID must be numeric only. Found: ' + userid);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Choose endpoint based on test mode
|
|
60
|
+
const url = test ? 'https://api.budgetsms.net/testsms/' : 'https://api.budgetsms.net/sendsms/';
|
|
61
|
+
|
|
62
|
+
// Format phone number according to E.164 (no + or 00, no spaces/dashes)
|
|
63
|
+
const formattedTo = to.replace(/^\+|^00/, '').replace(/[\s-]/g, '');
|
|
64
|
+
|
|
65
|
+
// Build query parameters
|
|
66
|
+
const queryParams = new URLSearchParams({
|
|
67
|
+
username: username,
|
|
68
|
+
userid: userid,
|
|
69
|
+
handle: handle,
|
|
70
|
+
msg: text || '', // Note: parameter is 'msg' not 'text'
|
|
71
|
+
from: from || '',
|
|
72
|
+
to: formattedTo,
|
|
73
|
+
...(customid && { customid }),
|
|
74
|
+
...(!test && price && { price: '1' }), // Price info only for real sends
|
|
75
|
+
...(!test && mccmnc && { mccmnc: '1' }), // Carrier info only for real sends
|
|
76
|
+
...(!test && credit && { credit: '1' }), // Credit info only for real sends
|
|
77
|
+
...params,
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
// Make GET request
|
|
81
|
+
const fullUrl = `${url}?${queryParams.toString()}`;
|
|
82
|
+
|
|
83
|
+
const response = await fetch(fullUrl, {
|
|
84
|
+
method: 'GET',
|
|
85
|
+
headers: {
|
|
86
|
+
Accept: 'text/plain',
|
|
87
|
+
'User-Agent': 'Unchained/BudgetSMS-Plugin',
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
const responseText = await response.text().then((text) => text.trim());
|
|
92
|
+
|
|
93
|
+
// Parse response: "OK <smsid> [price] [parts] [mccmnc] [credit]" or "ERR <errorcode>"
|
|
94
|
+
if (responseText.startsWith('OK')) {
|
|
95
|
+
const parts = responseText.split(' ');
|
|
96
|
+
const result: any = {
|
|
97
|
+
sms_id: parts[1] || null,
|
|
98
|
+
status: test ? 'test_successful' : 'sent',
|
|
99
|
+
test_mode: test,
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
if (test) {
|
|
103
|
+
result.message = 'Test SMS validated successfully (no credit deducted)';
|
|
104
|
+
} else {
|
|
105
|
+
// Parse additional info based on parameters used
|
|
106
|
+
let index = 2;
|
|
107
|
+
if (price && parts[index] && parts[index + 1]) {
|
|
108
|
+
result.price = parseFloat(parts[index]);
|
|
109
|
+
result.parts = parseInt(parts[index + 1]);
|
|
110
|
+
index += 2;
|
|
111
|
+
}
|
|
112
|
+
if (mccmnc && parts[index]) {
|
|
113
|
+
result.mccmnc = parts[index];
|
|
114
|
+
index += 1;
|
|
115
|
+
}
|
|
116
|
+
if (credit && parts[index]) {
|
|
117
|
+
result.remaining_credit = parseFloat(parts[index]);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return {
|
|
122
|
+
success: true,
|
|
123
|
+
result,
|
|
124
|
+
error: null,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (responseText.startsWith('ERR')) {
|
|
129
|
+
const parts = responseText.split(' ');
|
|
130
|
+
const errorCode = parts[1] || 'UNKNOWN';
|
|
131
|
+
|
|
132
|
+
// Error code mappings from documentation
|
|
133
|
+
const errorMessages: Record<string, string> = {
|
|
134
|
+
'1001': 'Authentication failed OR insufficient credit (BudgetSMS returns 1001 for both)',
|
|
135
|
+
'1002': 'Account not active',
|
|
136
|
+
'1003': 'Insufficient credit',
|
|
137
|
+
'1004': 'No access to this API',
|
|
138
|
+
'1005': 'No handle provided',
|
|
139
|
+
'1006': 'No UserID provided',
|
|
140
|
+
'1007': 'No Username provided',
|
|
141
|
+
'2001': 'SMS message text is empty',
|
|
142
|
+
'2002': 'SMS numeric senderid can be max. 16 numbers',
|
|
143
|
+
'2003': 'SMS alphanumeric sender can be max. 11 characters',
|
|
144
|
+
'2004': 'SMS senderid is empty or invalid',
|
|
145
|
+
'2005': 'Destination number is too short',
|
|
146
|
+
'2006': 'Destination is not numeric',
|
|
147
|
+
'2007': 'Destination is empty',
|
|
148
|
+
'2008': 'SMS text is not OK (check encoding?)',
|
|
149
|
+
'2009': 'Parameter issue',
|
|
150
|
+
'2010': 'Destination number is invalidly formatted',
|
|
151
|
+
'2011': 'Destination is invalid',
|
|
152
|
+
'2012': 'SMS message text is too long',
|
|
153
|
+
'2013': 'SMS message is invalid',
|
|
154
|
+
'2014': 'SMS CustomID is used before',
|
|
155
|
+
'2015': 'Charset problem',
|
|
156
|
+
'2016': 'Invalid UTF-8 encoding',
|
|
157
|
+
'3001': 'No route to destination',
|
|
158
|
+
'3002': 'No routes are setup',
|
|
159
|
+
'3003': 'Invalid destination',
|
|
160
|
+
'4001': 'System error, related to customid',
|
|
161
|
+
'4002': 'System error, temporary issue',
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
// Special handling for 1001 error
|
|
165
|
+
let message = errorMessages[errorCode] || `Unknown error code: ${errorCode}`;
|
|
166
|
+
if (errorCode === '1001') {
|
|
167
|
+
message += '. Note: Check your account balance - BudgetSMS may return 1001 when credit is 0.';
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return {
|
|
171
|
+
success: false,
|
|
172
|
+
error: {
|
|
173
|
+
name: 'BUDGETSMS_ERROR',
|
|
174
|
+
code: errorCode,
|
|
175
|
+
message,
|
|
176
|
+
},
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// Unexpected response
|
|
181
|
+
return {
|
|
182
|
+
success: false,
|
|
183
|
+
error: {
|
|
184
|
+
name: 'BUDGETSMS_ERROR',
|
|
185
|
+
message: 'Unexpected response format from BudgetSMS API',
|
|
186
|
+
response: responseText,
|
|
187
|
+
},
|
|
188
|
+
};
|
|
189
|
+
} catch (err) {
|
|
190
|
+
return {
|
|
191
|
+
success: false,
|
|
192
|
+
error: {
|
|
193
|
+
name: err.name,
|
|
194
|
+
message: err.message,
|
|
195
|
+
stack: err.stack,
|
|
196
|
+
},
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
},
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
WorkerDirector.registerAdapter(BudgetSMSWorker);
|
|
203
|
+
|
|
204
|
+
// Environment variables needed:
|
|
205
|
+
// BUDGETSMS_USERNAME=your_username (alphanumeric)
|
|
206
|
+
// BUDGETSMS_USERID=your_userid (NUMERIC ONLY - found in BudgetSMS control panel)
|
|
207
|
+
// BUDGETSMS_HANDLE=your_api_handle (alphanumeric - found in BudgetSMS control panel)
|
|
208
|
+
|
|
209
|
+
// IMPORTANT: The userid MUST be numeric only. You can find it in the BudgetSMS control panel after login.
|
|
210
|
+
// Example: BUDGETSMS_USERID=12345 (NOT "user12345" or any non-numeric value)
|
|
211
|
+
|
|
212
|
+
// Usage examples:
|
|
213
|
+
//
|
|
214
|
+
// Send real SMS (requires credits):
|
|
215
|
+
// await WorkerDirector.doWork({
|
|
216
|
+
// type: 'BUDGETSMS',
|
|
217
|
+
// from: 'YourCompany',
|
|
218
|
+
// to: '+41791234567',
|
|
219
|
+
// text: 'Your message here',
|
|
220
|
+
// price: true, // Get price info
|
|
221
|
+
// credit: true, // Get remaining credit
|
|
222
|
+
// });
|
|
223
|
+
//
|
|
224
|
+
// Test SMS (no credit deducted):
|
|
225
|
+
// await WorkerDirector.doWork({
|
|
226
|
+
// type: 'BUDGETSMS',
|
|
227
|
+
// test: true,
|
|
228
|
+
// from: 'YourCompany',
|
|
229
|
+
// to: '+41791234567',
|
|
230
|
+
// text: 'Test message',
|
|
231
|
+
// });
|
|
232
|
+
//
|
|
233
|
+
// IMPORTANT: If you get error 1001, it could mean either:
|
|
234
|
+
// 1. Authentication failed (wrong credentials)
|
|
235
|
+
// 2. Insufficient credit (0 balance)
|
|
236
|
+
// Use the checkBudgetSmsCredentials() helper to verify which one it is.
|
|
237
|
+
|
|
238
|
+
// Helper function to check credentials and balance
|
|
239
|
+
export async function checkBudgetSmsCredentials() {
|
|
240
|
+
const url = 'https://api.budgetsms.net/checkcredit/';
|
|
241
|
+
const username = process.env.BUDGETSMS_USERNAME?.trim();
|
|
242
|
+
const userid = process.env.BUDGETSMS_USERID?.trim();
|
|
243
|
+
const handle = process.env.BUDGETSMS_HANDLE?.trim();
|
|
244
|
+
|
|
245
|
+
if (!username || !userid || !handle) {
|
|
246
|
+
return { error: 'Missing credentials' };
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
if (!/^\d+$/.test(userid)) {
|
|
250
|
+
return { error: 'UserID must be numeric only' };
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
const queryParams = new URLSearchParams({ username, userid, handle });
|
|
254
|
+
|
|
255
|
+
try {
|
|
256
|
+
const response = await fetch(`${url}?${queryParams.toString()}`, { method: 'GET' });
|
|
257
|
+
const text = await response.text();
|
|
258
|
+
|
|
259
|
+
if (text.startsWith('OK')) {
|
|
260
|
+
const credit = parseFloat(text.split(' ')[1]) || 0;
|
|
261
|
+
return { success: true, credit };
|
|
262
|
+
} else {
|
|
263
|
+
return { error: text };
|
|
264
|
+
}
|
|
265
|
+
} catch (err) {
|
|
266
|
+
return { error: err.message };
|
|
267
|
+
}
|
|
268
|
+
}
|
|
@@ -1,57 +1,9 @@
|
|
|
1
1
|
import { WorkerDirector, WorkerAdapter, IWorkerAdapter } from '@unchainedshop/core';
|
|
2
2
|
import { createLogger } from '@unchainedshop/logger';
|
|
3
|
-
import JSONStream from 'JSONStream';
|
|
4
|
-
import { EventIterator } from 'event-iterator';
|
|
5
|
-
import { UnchainedCore } from '@unchainedshop/core';
|
|
6
3
|
|
|
7
|
-
const logger = createLogger('unchained:
|
|
4
|
+
const logger = createLogger('unchained:bulk-import');
|
|
8
5
|
|
|
9
|
-
const
|
|
10
|
-
bulkImporter,
|
|
11
|
-
payloadId,
|
|
12
|
-
unchainedAPI: Pick<UnchainedCore, 'bulkImporter' | 'modules' | 'services'>,
|
|
13
|
-
) => {
|
|
14
|
-
logger.trace(`parseAsync start`);
|
|
15
|
-
|
|
16
|
-
const readStream = await unchainedAPI.services.files.createDownloadStream({ fileId: payloadId });
|
|
17
|
-
|
|
18
|
-
if (!readStream) {
|
|
19
|
-
throw new Error(
|
|
20
|
-
'The current file adapter does not support streams when downloading required for streamed events. Please use a different file adapter.',
|
|
21
|
-
);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const eventIterator = new EventIterator(
|
|
25
|
-
(queue) => {
|
|
26
|
-
const jsonStream = JSONStream.parse('events.*'); // rows, ANYTHING, doc
|
|
27
|
-
jsonStream.on('data', queue.push);
|
|
28
|
-
jsonStream.on('close', queue.stop);
|
|
29
|
-
jsonStream.on('error', queue.fail);
|
|
30
|
-
|
|
31
|
-
queue.on('highWater', () => readStream.pause());
|
|
32
|
-
queue.on('lowWater', () => readStream.resume());
|
|
33
|
-
|
|
34
|
-
readStream.pipe(jsonStream);
|
|
35
|
-
readStream.on('error', queue.fail);
|
|
36
|
-
|
|
37
|
-
return () => {
|
|
38
|
-
jsonStream.removeListener('data', queue.push);
|
|
39
|
-
jsonStream.removeListener('close', queue.stop);
|
|
40
|
-
jsonStream.removeListener('error', queue.fail);
|
|
41
|
-
jsonStream.destroy();
|
|
42
|
-
};
|
|
43
|
-
},
|
|
44
|
-
{ highWaterMark: 100, lowWaterMark: 5 },
|
|
45
|
-
);
|
|
46
|
-
|
|
47
|
-
for await (const event of eventIterator) {
|
|
48
|
-
await bulkImporter.prepare(event, unchainedAPI);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
logger.trace(`parseAsync done`);
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
export const BulkImportWorker: IWorkerAdapter<any, Record<string, unknown>> = {
|
|
6
|
+
export const BulkImportWorker: IWorkerAdapter<any, Record<string, any>> = {
|
|
55
7
|
...WorkerAdapter,
|
|
56
8
|
|
|
57
9
|
key: 'shop.unchained.worker-plugin.bulk-import',
|
|
@@ -76,13 +28,20 @@ export const BulkImportWorker: IWorkerAdapter<any, Record<string, unknown>> = {
|
|
|
76
28
|
|
|
77
29
|
if (rawPayload.payloadId) {
|
|
78
30
|
// stream payload from gridfs
|
|
79
|
-
|
|
31
|
+
const readStream = await unchainedAPI.services.files.createDownloadStream({
|
|
32
|
+
fileId: rawPayload.payloadId,
|
|
33
|
+
});
|
|
34
|
+
if (!readStream) {
|
|
35
|
+
throw new Error('Could not create download stream from uploaded file');
|
|
36
|
+
}
|
|
37
|
+
await unchainedAPI.bulkImporter.pipeEventStream(readStream, bulkImporter, unchainedAPI);
|
|
80
38
|
} else {
|
|
81
39
|
const { events } = rawPayload;
|
|
82
40
|
if (!events?.length) throw new Error('No events submitted');
|
|
83
41
|
|
|
84
42
|
await events.reduce(async (currentEventPromise, nextEvent) => {
|
|
85
43
|
await currentEventPromise;
|
|
44
|
+
await bulkImporter.validate(nextEvent);
|
|
86
45
|
return bulkImporter.prepare(nextEvent, unchainedAPI);
|
|
87
46
|
}, Promise.resolve());
|
|
88
47
|
}
|
|
@@ -93,13 +52,13 @@ export const BulkImportWorker: IWorkerAdapter<any, Record<string, unknown>> = {
|
|
|
93
52
|
if (error) {
|
|
94
53
|
return {
|
|
95
54
|
success: false,
|
|
96
|
-
result
|
|
55
|
+
result: result!,
|
|
97
56
|
error,
|
|
98
57
|
};
|
|
99
58
|
}
|
|
100
59
|
return {
|
|
101
60
|
success: true,
|
|
102
|
-
result
|
|
61
|
+
result: result!,
|
|
103
62
|
};
|
|
104
63
|
} catch (err) {
|
|
105
64
|
logger.error(err);
|