chore: complete payment collection on order edit even if confirmed (#2877)

This commit is contained in:
Carlos R. L. Rodrigues
2022-12-22 13:07:41 -03:00
committed by GitHub
parent 233d6904f8
commit 726858d847
@@ -1,10 +1,6 @@
import { Request, Response } from "express"
import { EntityManager } from "typeorm"
import {
OrderEditService,
OrderService,
PaymentProviderService,
} from "../../../../services"
import { OrderEditService, PaymentProviderService } from "../../../../services"
import {
defaultStoreOrderEditFields,
defaultStoreOrderEditRelations,
@@ -60,8 +56,6 @@ export default async (req: Request, res: Response) => {
const orderEditService: OrderEditService =
req.scope.resolve("orderEditService")
const orderService: OrderService = req.scope.resolve("orderService")
const paymentProviderService: PaymentProviderService = req.scope.resolve(
"paymentProviderService"
)
@@ -72,7 +66,6 @@ export default async (req: Request, res: Response) => {
await manager.transaction(async (manager) => {
const orderEditServiceTx = orderEditService.withTransaction(manager)
const orderServiceTx = orderService.withTransaction(manager)
const paymentProviderServiceTx =
paymentProviderService.withTransaction(manager)
@@ -80,6 +73,32 @@ export default async (req: Request, res: Response) => {
relations: ["payment_collection", "payment_collection.payments"],
})
const allowedStatus = [OrderEditStatus.REQUESTED, OrderEditStatus.CONFIRMED]
if (
orderEdit.payment_collection &&
allowedStatus.includes(orderEdit.status)
) {
if (
orderEdit.payment_collection.status !==
PaymentCollectionStatus.AUTHORIZED
) {
throw new MedusaError(
MedusaError.Types.NOT_ALLOWED,
"Unable to complete an order edit if the payment is not authorized"
)
}
if (orderEdit.payment_collection) {
for (const payment of orderEdit.payment_collection.payments) {
if (payment.order_id !== orderEdit.order_id) {
await paymentProviderServiceTx.updatePayment(payment.id, {
order_id: orderEdit.order_id,
})
}
}
}
}
if (orderEdit.status === OrderEditStatus.CONFIRMED) {
return orderEdit
}
@@ -91,30 +110,10 @@ export default async (req: Request, res: Response) => {
)
}
if (orderEdit.payment_collection) {
if (
orderEdit.payment_collection.status !==
PaymentCollectionStatus.AUTHORIZED
) {
throw new MedusaError(
MedusaError.Types.NOT_ALLOWED,
"Unable to complete an order edit if the payment is not authorized"
)
}
}
const returned = await orderEditServiceTx.confirm(id, {
loggedInUserId: userId,
})
if (orderEdit.payment_collection) {
for (const payment of orderEdit.payment_collection.payments) {
await paymentProviderServiceTx.updatePayment(payment.id, {
order_id: orderEdit.order_id,
})
}
}
return returned
})