diff --git a/node/services/updateRequestStatusService.ts b/node/services/updateRequestStatusService.ts index 893b56dd..b86c7756 100644 --- a/node/services/updateRequestStatusService.ts +++ b/node/services/updateRequestStatusService.ts @@ -15,7 +15,10 @@ import { validateStatusUpdate } from '../utils/validateStatusUpdate' import { createOrUpdateStatusPayload } from '../utils/createOrUpdateStatusPayload' import { createRefundData } from '../utils/createRefundData' import { handleRefund } from '../utils/handleRefund' -import { OMS_RETURN_REQUEST_STATUS_UPDATE } from '../utils/constants' +import { + OMS_RETURN_REQUEST_STATUS_UPDATE, + SETTINGS_PATH, +} from '../utils/constants' import { OMS_RETURN_REQUEST_STATUS_UPDATE_TEMPLATE } from '../utils/templates' import type { StatusUpdateMailData } from '../typings/mailClient' @@ -102,6 +105,18 @@ export const updateRequestStatusService = async ( vtex: { logger }, } = ctx + const settingsPromise = appSettings.get(SETTINGS_PATH, true) + + let settingsData: ReturnAppSettingsCustom | undefined + + try { + settingsData = await settingsPromise + } catch (error) { + throw new ResolverError('Return App settings is not configured', 500) + } + + logger.info(appSettings) + const { status, requestId, comment, refundData } = args const { role, firstName, lastName, email, userId } = userProfile ?? {} @@ -181,7 +196,7 @@ export const updateRequestStatusService = async ( refundData, requestItems: returnRequest.items, refundableShipping: maxRefundableShipping, - appSettings, + settingsData, }) : returnRequest.refundData diff --git a/node/utils/createRefundData.ts b/node/utils/createRefundData.ts index 3a1961c6..9bc1910c 100644 --- a/node/utils/createRefundData.ts +++ b/node/utils/createRefundData.ts @@ -6,13 +6,13 @@ export const createRefundData = ({ refundData, requestItems, refundableShipping, - appSettings, + settingsData, }: { requestId: string refundData?: Maybe requestItems: ReturnRequest['items'] refundableShipping: number - appSettings: ReturnAppSettingsCustom + settingsData: ReturnAppSettingsCustom }): ReturnRequest['refundData'] => { const requestItemsMap = new Map() @@ -21,6 +21,9 @@ export const createRefundData = ({ } const items = [] + let refundableTaxes = 0 + let invoiceValue = 0 + let refundedItemsValue = 0 for (const refundItem of refundData?.items ?? []) { if (refundItem.quantity === 0) continue @@ -46,18 +49,20 @@ export const createRefundData = ({ ) } + refundableTaxes += Number(tax) ?? 0 + items.push({ orderItemIndex, id, - price: appSettings.options.disableTaxRefund + price: settingsData?.options?.disableTaxRefund ? Number(sellingPrice) ?? 0 - : (Number(sellingPrice) ?? 0) + (Number(tax) ?? 0), + : Number(sellingPrice) + Number(tax) ?? 0, quantity: refundItem.quantity, restockFee: refundItem.restockFee, }) } - const refundedItemsValue = items.reduce( + refundedItemsValue = items.reduce( (total, item) => total + (item.price * item.quantity - item.restockFee), 0 ) @@ -76,11 +81,19 @@ export const createRefundData = ({ ) } + invoiceValue = settingsData?.options?.disableTaxRefund + ? refundedItemsValue + refundableTaxes + refundedShippingValue + : refundedItemsValue + refundedShippingValue + + refundedItemsValue = settingsData?.options?.disableTaxRefund + ? refundedItemsValue + refundableTaxes + : refundedItemsValue + return { // invoiceNumber has to match the requestId. // This values is used to filter the invoices created via Return app when calculating the items available to be returned. invoiceNumber: requestId, - invoiceValue: refundedItemsValue + refundedShippingValue, + invoiceValue, refundedItemsValue, refundedShippingValue, items,