fix(core-flows,utils,medusa): fix bug where payment collection across orders were getting updated (#8401)
This took embarrassingly long to debug. :| what: - fixes a bug where the payment collection of other orders were getting updated - adds order status to payments section <img width="1069" alt="Screenshot 2024-08-02 at 08 37 38" src="https://github.com/user-attachments/assets/31776bd3-e6b9-4d23-8be6-f972f7316cf3"> <img width="1072" alt="Screenshot 2024-08-02 at 08 37 48" src="https://github.com/user-attachments/assets/38cdd8a1-9f31-4920-91bf-a3554e298960">
This commit is contained in:
+8
-2
@@ -24,6 +24,7 @@ import {
|
||||
getLocaleAmount,
|
||||
getStylizedAmount,
|
||||
} from "../../../../../lib/money-amount-helpers"
|
||||
import { getOrderPaymentStatus } from "../../../../../lib/order-helpers"
|
||||
|
||||
type OrderPaymentSectionProps = {
|
||||
order: HttpTypes.AdminOrder
|
||||
@@ -46,7 +47,7 @@ export const OrderPaymentSection = ({ order }: OrderPaymentSectionProps) => {
|
||||
|
||||
return (
|
||||
<Container className="divide-y divide-dashed p-0">
|
||||
<Header />
|
||||
<Header order={order} />
|
||||
|
||||
<PaymentBreakdown
|
||||
order={order}
|
||||
@@ -60,12 +61,17 @@ export const OrderPaymentSection = ({ order }: OrderPaymentSectionProps) => {
|
||||
)
|
||||
}
|
||||
|
||||
const Header = () => {
|
||||
const Header = ({ order }) => {
|
||||
const { t } = useTranslation()
|
||||
const { label, color } = getOrderPaymentStatus(t, order.payment_status)
|
||||
|
||||
return (
|
||||
<div className="flex items-center justify-between px-6 py-4">
|
||||
<Heading level="h2">{t("orders.payment.title")}</Heading>
|
||||
|
||||
<StatusBadge color={color} className="text-nowrap">
|
||||
{label}
|
||||
</StatusBadge>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
+21
-10
@@ -1,3 +1,4 @@
|
||||
import { isPresent } from "@medusajs/utils"
|
||||
import {
|
||||
StepResponse,
|
||||
WorkflowData,
|
||||
@@ -39,7 +40,7 @@ export const refreshPaymentCollectionForCartWorkflowId =
|
||||
export const refreshPaymentCollectionForCartWorkflow = createWorkflow(
|
||||
refreshPaymentCollectionForCartWorkflowId,
|
||||
(input: WorkflowData<WorklowInput>): WorkflowData<void> => {
|
||||
const carts = useRemoteQueryStep({
|
||||
const cart = useRemoteQueryStep({
|
||||
entry_point: "cart",
|
||||
fields: [
|
||||
"id",
|
||||
@@ -50,30 +51,40 @@ export const refreshPaymentCollectionForCartWorkflow = createWorkflow(
|
||||
],
|
||||
variables: { id: input.cart_id },
|
||||
throw_if_key_not_found: true,
|
||||
list: false,
|
||||
})
|
||||
|
||||
const cart = transform({ carts }, (data) => data.carts[0])
|
||||
const deletePaymentSessionInput = transform(
|
||||
{ paymentCollection: cart.payment_collection },
|
||||
(data) => {
|
||||
return {
|
||||
ids:
|
||||
data.paymentCollection?.payment_sessions?.map((ps) => ps.id) || [],
|
||||
data.paymentCollection?.payment_sessions
|
||||
?.map((ps) => ps.id)
|
||||
?.flat(1) || [],
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
const updatePaymentCollectionInput = transform({ cart }, (data) => {
|
||||
if (!isPresent(data.cart?.payment_collection?.id)) {
|
||||
return
|
||||
}
|
||||
|
||||
return {
|
||||
selector: { id: data.cart.payment_collection.id },
|
||||
update: {
|
||||
amount: data.cart.total,
|
||||
currency_code: data.cart.currency_code,
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
parallelize(
|
||||
deletePaymentSessionsWorkflow.runAsStep({
|
||||
input: deletePaymentSessionInput,
|
||||
}),
|
||||
updatePaymentCollectionStep({
|
||||
selector: { id: cart.payment_collection.id },
|
||||
update: {
|
||||
amount: cart.total,
|
||||
currency_code: cart.currency_code,
|
||||
},
|
||||
})
|
||||
updatePaymentCollectionStep(updatePaymentCollectionInput)
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
import {
|
||||
ModuleRegistrationName,
|
||||
getSelectsAndRelationsFromObjectArray,
|
||||
isPresent,
|
||||
} from "@medusajs/utils"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
@@ -18,6 +19,10 @@ export const updatePaymentCollectionStepId = "update-payment-collection"
|
||||
export const updatePaymentCollectionStep = createStep(
|
||||
updatePaymentCollectionStepId,
|
||||
async (data: StepInput, { container }) => {
|
||||
if (!isPresent(data) || !isPresent(data.selector)) {
|
||||
return new StepResponse([], [])
|
||||
}
|
||||
|
||||
const paymentModuleService = container.resolve<IPaymentModuleService>(
|
||||
ModuleRegistrationName.PAYMENT
|
||||
)
|
||||
@@ -42,7 +47,7 @@ export const updatePaymentCollectionStep = createStep(
|
||||
return new StepResponse(updated, prevData)
|
||||
},
|
||||
async (prevData, { container }) => {
|
||||
if (!prevData) {
|
||||
if (!prevData?.length) {
|
||||
return
|
||||
}
|
||||
const paymentModuleService = container.resolve<IPaymentModuleService>(
|
||||
|
||||
@@ -48,6 +48,8 @@ export const defaultAdminRetrieveOrderFields = [
|
||||
"*shipping_methods.tax_lines",
|
||||
"*shipping_methods.adjustments",
|
||||
"*payment_collections",
|
||||
"*payment_collections.payments",
|
||||
"*payment_collections.payments.refunds",
|
||||
]
|
||||
|
||||
export const retrieveTransformQueryConfig = {
|
||||
|
||||
Reference in New Issue
Block a user