import { MedusaError, isDefined } from "@medusajs/utils" import { ChangeActionType } from "../action-key" import { OrderChangeProcessing } from "../calculate-order-change" OrderChangeProcessing.registerActionType(ChangeActionType.WRITE_OFF_ITEM, { operation({ action, currentOrder }) { const existing = currentOrder.items.find( (item) => item.id === action.details.reference_id )! existing.detail.written_off_quantity ??= 0 existing.detail.written_off_quantity += action.details.quantity }, revert({ action, currentOrder }) { const existing = currentOrder.items.find( (item) => item.id === action.details.reference_id )! existing.detail.written_off_quantity -= action.details.quantity }, validate({ action, currentOrder }) { const refId = action.details?.reference_id if (!isDefined(refId)) { throw new MedusaError( MedusaError.Types.INVALID_DATA, "Details reference ID is required." ) } const existing = currentOrder.items.find((item) => item.id === refId) if (!existing) { throw new MedusaError( MedusaError.Types.INVALID_DATA, `Reference ID "${refId}" not found.` ) } if (!action.details?.quantity) { throw new MedusaError( MedusaError.Types.INVALID_DATA, `Quantity to write-off item ${refId} is required.` ) } const quantityAvailable = existing!.quantity ?? 0 if (action.details?.quantity > quantityAvailable) { throw new MedusaError( MedusaError.Types.INVALID_DATA, "Cannot claim more items than what was ordered." ) } }, })