Skip to content

Commit

Permalink
fix how i get the attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasayb committed Feb 6, 2023
1 parent f05de81 commit 40b88c1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
19 changes: 17 additions & 2 deletions node/services/updateRequestStatusService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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 ?? {}
Expand Down Expand Up @@ -181,7 +196,7 @@ export const updateRequestStatusService = async (
refundData,
requestItems: returnRequest.items,
refundableShipping: maxRefundableShipping,
appSettings,
settingsData,
})
: returnRequest.refundData

Expand Down
25 changes: 19 additions & 6 deletions node/utils/createRefundData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ export const createRefundData = ({
refundData,
requestItems,
refundableShipping,
appSettings,
settingsData,
}: {
requestId: string
refundData?: Maybe<RefundDataInput>
requestItems: ReturnRequest['items']
refundableShipping: number
appSettings: ReturnAppSettingsCustom
settingsData: ReturnAppSettingsCustom
}): ReturnRequest['refundData'] => {
const requestItemsMap = new Map<number, ReturnRequest['items'][number]>()

Expand All @@ -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
Expand All @@ -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
)
Expand All @@ -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,
Expand Down

0 comments on commit 40b88c1

Please sign in to comment.