@ns8/ns8-shopify-switches
Advanced tools
Comparing version 1.0.192 to 1.0.193
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const getOrder = async (shopifyClient, orderId) => (await shopifyClient.order.get(orderId, { fields: 'id,total_price_set,financial_status,fulfillments,fulfillment_status' }).catch((error) => new Error(`Could not fetch order. ${JSON.stringify(error)}`))); | ||
const getTransactions = async (shopifyClient, orderId) => (await shopifyClient.transaction.list(orderId, { fields: 'order_id,authorization,kind' }).catch((error) => new Error(`Could not fetch transaction list. ${JSON.stringify(error)}`))); | ||
const voidTransaction = async (shopifyClient, transaction) => { | ||
const { authorization, order_id, } = transaction; | ||
return await shopifyClient.transaction.create(order_id, { authorization, kind: 'void' }) | ||
.catch((error) => new Error(`Unable to void transaction. ${JSON.stringify(error)}`)); | ||
const refundOrder_1 = require("./refundOrder"); | ||
const getOrder = async (shopifyClient, orderId) => (shopifyClient.order.get(orderId, { fields: 'id,total_price_set,financial_status,fulfillments,fulfillment_status,line_items' }).catch((error) => { throw new Error(`Could not fetch order. ${JSON.stringify(error)}`); })); | ||
const getTransactions = async (shopifyClient, orderId) => (shopifyClient.transaction.list(orderId, { fields: 'order_id,authorization,kind' }).catch((error) => { throw new Error(`Could not fetch transaction list. ${JSON.stringify(error)}`); })); | ||
const errorHandler = (error) => { | ||
throw error; | ||
}; | ||
const orderCanBeCanceled = (order) => { | ||
const { financial_status, fulfillments, fulfillment_status, } = order; | ||
return ((fulfillments && fulfillments.length === 0) | ||
&& fulfillment_status === null | ||
&& financial_status !== 'partially_paid' | ||
&& financial_status !== 'partially_refunded'); | ||
}; | ||
const orderCanBeCanceled = (order) => { var _a, _b, _c; return (((_b = (_a = order) === null || _a === void 0 ? void 0 : _a.fulfillments) === null || _b === void 0 ? void 0 : _b.length) === 0 && ((_c = order) === null || _c === void 0 ? void 0 : _c.fulfillment_status) === null); }; | ||
exports.orderCanBeCanceled = orderCanBeCanceled; | ||
const cancelFullyPaidOrder = async (shopifyClient, order, email = false) => { | ||
const { id, total_price_set: { presentment_money: { amount, currency_code: currency, }, }, } = order; | ||
const params = { | ||
amount, | ||
currency, | ||
const orderCanBeVoided = (order) => order.financial_status === 'authorized'; | ||
const cancelUnpaidOrder = async (shopifyClient, orderId, email = false) => { | ||
const cancelOrderParams = { | ||
email, | ||
@@ -27,13 +18,10 @@ restock: true, | ||
}; | ||
const results = await shopifyClient.order.cancel(id, Object.assign({}, params)) | ||
.catch((error) => new Error(`Unable to cancel fully paid order. ${JSON.stringify(error)}`)); | ||
return results instanceof Error ? results : 'Paid order refunded and canceled.'; | ||
return shopifyClient.order.cancel(orderId, cancelOrderParams) | ||
.catch((error) => { throw new Error(`Unable to cancel unpaid order. ${JSON.stringify(error)}`); }); | ||
}; | ||
const cancelRefundedOrder = async (shopifyClient, orderId, email = false) => { | ||
const results = await shopifyClient.order.cancel(orderId, { email, restock: true, reason: 'fraud' }) | ||
.catch((error) => new Error(`Unable to cancel refunded order. ${JSON.stringify(error)}`)); | ||
return results instanceof Error ? results : 'Refunded order canceled.'; | ||
const voidTransaction = async (shopifyClient, transaction) => { | ||
const { authorization, order_id, } = transaction; | ||
return shopifyClient.transaction.create(order_id, { authorization, kind: 'void' }) | ||
.catch((error) => { throw new Error(`Unable to void transaction. ${JSON.stringify(error)}`); }); | ||
}; | ||
const cancelUnpaidOrder = async (shopifyClient, orderId, email = false) => (await shopifyClient.order.cancel(orderId, { email, restock: true, reason: 'fraud' }) | ||
.catch((error) => new Error(`Unable to cancel unpaid order. ${JSON.stringify(error)}`))); | ||
const voidAuthorizedOrder = async (shopifyClient, order, email = false) => { | ||
@@ -43,14 +31,10 @@ let returnMessage; | ||
const { id } = order; | ||
const cancelOrderResults = await cancelUnpaidOrder(shopifyClient, id, email); | ||
const transactions = await getTransactions(shopifyClient, id); | ||
if (transactions instanceof Error) { | ||
return transactions; | ||
} | ||
const transactions = await getTransactions(shopifyClient, id).catch(errorHandler); | ||
const transaction = transactions.find((tx) => tx.kind === 'authorization'); | ||
if (transaction) { | ||
voidTransactionResults = await voidTransaction(shopifyClient, transaction); | ||
if (voidTransactionResults instanceof Error) { | ||
returnMessage = voidTransactionResults; | ||
} | ||
else { | ||
voidTransactionResults = await voidTransaction(shopifyClient, transaction).catch((error) => { | ||
returnMessage = error.message; | ||
return undefined; | ||
}); | ||
if (voidTransactionResults) { | ||
returnMessage = `Transaction voided successfully ${JSON.stringify(voidTransactionResults)}`; | ||
@@ -62,6 +46,7 @@ } | ||
} | ||
if (cancelOrderResults instanceof Error) { | ||
returnMessage = `${returnMessage} --- ${cancelOrderResults}`; | ||
} | ||
else { | ||
const cancelOrderResults = await cancelUnpaidOrder(shopifyClient, id, email).catch((error) => { | ||
returnMessage = `${returnMessage} --- ${error.message}`; | ||
return undefined; | ||
}); | ||
if (cancelOrderResults) { | ||
returnMessage = `${returnMessage} --- Order canceled successfully ${JSON.stringify(cancelOrderResults)}`; | ||
@@ -71,18 +56,31 @@ } | ||
}; | ||
const getCancelOrderParameters = async (shopifyClient, order, email = false) => { | ||
let cancelOrderParams = { | ||
email, | ||
restock: true, | ||
reason: 'fraud', | ||
}; | ||
if (refundOrder_1.orderCanBeRefunded(order)) { | ||
const partialCancelParams = await refundOrder_1.default(shopifyClient, order).catch(errorHandler); | ||
cancelOrderParams = Object.assign(Object.assign({}, cancelOrderParams), partialCancelParams); | ||
} | ||
return cancelOrderParams; | ||
}; | ||
const cancelOrder = async (shopifyClient, order, emailOnCancel = false) => { | ||
const cancelOrderParams = await getCancelOrderParameters(shopifyClient, order, emailOnCancel).catch(errorHandler); | ||
await shopifyClient.order.cancel(order.id, cancelOrderParams) | ||
.catch((error) => { throw new Error(`Unable to cancel order. ${JSON.stringify(error)}`); }); | ||
const returnMessage = `Order with financial_status ${order.financial_status}` | ||
+ ` canceled${(cancelOrderParams.amount || cancelOrderParams.refund) ? ' and refunded' : ''}.`; | ||
return returnMessage; | ||
}; | ||
const cancelShopifyOrder = async (shopifyClient, orderId, emailOnCancel = false) => { | ||
const order = await getOrder(shopifyClient, orderId).catch(errorHandler); | ||
let returnMessage; | ||
const order = await getOrder(shopifyClient, orderId); | ||
if (order instanceof Error) { | ||
return order; | ||
} | ||
if (orderCanBeCanceled(order)) { | ||
const { financial_status } = order; | ||
if (financial_status === 'paid') { | ||
returnMessage = await cancelFullyPaidOrder(shopifyClient, order, emailOnCancel); | ||
if (orderCanBeVoided(order)) { | ||
returnMessage = await voidAuthorizedOrder(shopifyClient, order, emailOnCancel).catch(errorHandler); | ||
} | ||
else if (financial_status === 'refunded') { | ||
returnMessage = await cancelRefundedOrder(shopifyClient, orderId, emailOnCancel); | ||
} | ||
else { | ||
returnMessage = await voidAuthorizedOrder(shopifyClient, order, emailOnCancel); | ||
returnMessage = await cancelOrder(shopifyClient, order, emailOnCancel).catch(errorHandler); | ||
} | ||
@@ -89,0 +87,0 @@ } |
@@ -25,3 +25,4 @@ "use strict"; | ||
if (status === ns8_protect_models_1.Status.CANCELLED) { | ||
cancelShopifyOrderResults = await cancelShopifyOrder_1.default(client, orderId, profile.emailOnCancel); | ||
cancelShopifyOrderResults = await cancelShopifyOrder_1.default(client, orderId, profile.emailOnCancel) | ||
.catch((error) => error); | ||
console.log('cancelShopifyOrderResults', cancelShopifyOrderResults.toString()); | ||
@@ -28,0 +29,0 @@ } |
{ | ||
"name": "@ns8/ns8-shopify-switches", | ||
"version": "1.0.192", | ||
"version": "1.0.193", | ||
"description": "Custom switches for the shopify integration", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
104657
50
1281