chore(medusa): Add transactions in mutating actions 2 (#1853)
* chore(medusa): Add transaction on mutation actions * chore(medusa): continue refactoring * chore(medusa): continue refactoring * chore(medusa): continue refactoring * feat(medusa): update invite service mock to provide a withTransaction * feat(medusa): Include pr feedback * feat(medusa): Cleanup idempotent places * feat(medusa): Cleanup idempotent places * feat(medusa): Better Cleanup idempotent places * feat(meudsa): cleanup transaction * fix(medusa): Create cart transaction usage * fix(medusa): Use the right variable * fix(medusa): Use the right variable * fix(medusa): Transaction usage in cart creation flow
This commit is contained in:
@@ -20,6 +20,7 @@ import { DraftOrder } from "../../../.."
|
||||
import { DraftOrderService } from "../../../../services"
|
||||
import { AddressPayload } from "../../../../types/common"
|
||||
import { validator } from "../../../../utils/validator"
|
||||
import { EntityManager } from "typeorm"
|
||||
/**
|
||||
* @oas [post] /draft-orders
|
||||
* operationId: "PostDraftOrders"
|
||||
@@ -127,7 +128,15 @@ export default async (req, res) => {
|
||||
|
||||
const draftOrderService: DraftOrderService =
|
||||
req.scope.resolve("draftOrderService")
|
||||
let draftOrder: DraftOrder = await draftOrderService.create(value)
|
||||
|
||||
const manager: EntityManager = req.scope.resolve("manager")
|
||||
let draftOrder: DraftOrder = await manager.transaction(
|
||||
async (transactionManager) => {
|
||||
return await draftOrderService
|
||||
.withTransaction(transactionManager)
|
||||
.create(value)
|
||||
}
|
||||
)
|
||||
|
||||
draftOrder = await draftOrderService.retrieve(draftOrder.id, {
|
||||
relations: defaultAdminDraftOrdersRelations,
|
||||
|
||||
@@ -90,12 +90,14 @@ export default async (req, res) => {
|
||||
}
|
||||
|
||||
if (validated.variant_id) {
|
||||
const line = await lineItemService.generate(
|
||||
validated.variant_id,
|
||||
draftOrder.cart.region_id,
|
||||
validated.quantity,
|
||||
{ metadata: validated.metadata, unit_price: validated.unit_price }
|
||||
)
|
||||
const line = await lineItemService
|
||||
.withTransaction(manager)
|
||||
.generate(
|
||||
validated.variant_id,
|
||||
draftOrder.cart.region_id,
|
||||
validated.quantity,
|
||||
{ metadata: validated.metadata, unit_price: validated.unit_price }
|
||||
)
|
||||
|
||||
await cartService
|
||||
.withTransaction(manager)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { DraftOrderService } from "../../../../services"
|
||||
import { EntityManager } from "typeorm"
|
||||
/**
|
||||
* @oas [delete] /draft-orders/{id}
|
||||
* operationId: DeleteDraftOrdersDraftOrder
|
||||
@@ -30,7 +31,13 @@ export default async (req, res) => {
|
||||
|
||||
const draftOrderService: DraftOrderService =
|
||||
req.scope.resolve("draftOrderService")
|
||||
await draftOrderService.delete(id)
|
||||
|
||||
const manager: EntityManager = req.scope.resolve("manager")
|
||||
await manager.transaction(async (transactionManager) => {
|
||||
return await draftOrderService
|
||||
.withTransaction(transactionManager)
|
||||
.delete(id)
|
||||
})
|
||||
|
||||
res.json({
|
||||
id,
|
||||
|
||||
@@ -70,7 +70,7 @@ export default async (req, res) => {
|
||||
.withTransaction(manager)
|
||||
.setPaymentSession(cart.id, "system")
|
||||
|
||||
await cartService.createTaxLines(cart.id)
|
||||
await cartService.withTransaction(manager).createTaxLines(cart.id)
|
||||
|
||||
await cartService.withTransaction(manager).authorizePayment(cart.id)
|
||||
|
||||
|
||||
@@ -15,6 +15,9 @@ import { CartService, DraftOrderService } from "../../../../services"
|
||||
import { Type } from "class-transformer"
|
||||
import { AddressPayload } from "../../../../types/common"
|
||||
import { validator } from "../../../../utils/validator"
|
||||
import { EntityManager } from "typeorm"
|
||||
import { DraftOrderStatus } from "../../../../models"
|
||||
|
||||
/**
|
||||
* @oas [post] /admin/draft-orders/{id}
|
||||
* operationId: PostDraftOrdersDraftOrder
|
||||
@@ -80,21 +83,28 @@ export default async (req, res) => {
|
||||
|
||||
const draftOrder = await draftOrderService.retrieve(id)
|
||||
|
||||
if (draftOrder.status === "completed") {
|
||||
if (draftOrder.status === DraftOrderStatus.COMPLETED) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_ALLOWED,
|
||||
"You are only allowed to update open draft orders"
|
||||
)
|
||||
}
|
||||
|
||||
if (validated.no_notification_order !== undefined) {
|
||||
await draftOrderService.update(draftOrder.id, {
|
||||
no_notification_order: validated.no_notification_order,
|
||||
})
|
||||
delete validated.no_notification_order
|
||||
}
|
||||
const manager: EntityManager = req.scope.resolve("manager")
|
||||
await manager.transaction(async (transactionManager) => {
|
||||
if (validated.no_notification_order !== undefined) {
|
||||
await draftOrderService
|
||||
.withTransaction(transactionManager)
|
||||
.update(draftOrder.id, {
|
||||
no_notification_order: validated.no_notification_order,
|
||||
})
|
||||
delete validated.no_notification_order
|
||||
}
|
||||
|
||||
await cartService.update(draftOrder.cart_id, validated)
|
||||
await cartService
|
||||
.withTransaction(transactionManager)
|
||||
.update(draftOrder.cart_id, validated)
|
||||
})
|
||||
|
||||
draftOrder.cart = await cartService.retrieve(draftOrder.cart_id, {
|
||||
relations: defaultAdminDraftOrdersCartRelations,
|
||||
|
||||
Reference in New Issue
Block a user