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

This commit is contained in:
Carlos R. L. Rodrigues authored and GitHub committed 2022-12-22 13:07:41 -03:00
1 parent 233d6904f8
commit 726858d847
1 file changed
+27 -28
@@ -1,10 +1,6 @@
import { Request, Response } from "express" import { Request, Response } from "express"
import { EntityManager } from "typeorm" import { EntityManager } from "typeorm"
import { import { OrderEditService, PaymentProviderService } from "../../../../services"
OrderEditService,
OrderService,
PaymentProviderService,
} from "../../../../services"
import { import {
defaultStoreOrderEditFields, defaultStoreOrderEditFields,
defaultStoreOrderEditRelations, defaultStoreOrderEditRelations,
@@ -60,8 +56,6 @@ export default async (req: Request, res: Response) => {
const orderEditService: OrderEditService = const orderEditService: OrderEditService =
req.scope.resolve("orderEditService") req.scope.resolve("orderEditService")
const orderService: OrderService = req.scope.resolve("orderService")
const paymentProviderService: PaymentProviderService = req.scope.resolve( const paymentProviderService: PaymentProviderService = req.scope.resolve(
"paymentProviderService" "paymentProviderService"
) )
@@ -72,7 +66,6 @@ export default async (req: Request, res: Response) => {
await manager.transaction(async (manager) => { await manager.transaction(async (manager) => {
const orderEditServiceTx = orderEditService.withTransaction(manager) const orderEditServiceTx = orderEditService.withTransaction(manager)
const orderServiceTx = orderService.withTransaction(manager)
const paymentProviderServiceTx = const paymentProviderServiceTx =
paymentProviderService.withTransaction(manager) paymentProviderService.withTransaction(manager)
@@ -80,6 +73,32 @@ export default async (req: Request, res: Response) => {
relations: ["payment_collection", "payment_collection.payments"], 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) { if (orderEdit.status === OrderEditStatus.CONFIRMED) {
return orderEdit 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, { const returned = await orderEditServiceTx.confirm(id, {
loggedInUserId: userId, 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 return returned
}) })