feat(medusa): Modules initializer (#3352)

This commit is contained in:
Carlos R. L. Rodrigues
2023-03-17 12:18:52 -03:00
committed by GitHub
parent 8a7421db5b
commit aa690beed7
51 changed files with 1290 additions and 715 deletions
@@ -85,9 +85,6 @@ export default async (req, res) => {
const orderServiceTx = orderService.withTransaction(manager)
const cartServiceTx = cartService.withTransaction(manager)
const productVariantInventoryServiceTx =
productVariantInventoryService.withTransaction(manager)
const draftOrder = await draftOrderServiceTx.retrieve(id)
const cart = await cartServiceTx.retrieveWithTotals(draftOrder.cart_id)
@@ -116,7 +113,7 @@ export default async (req, res) => {
})
await reserveQuantityForDraftOrder(order, {
productVariantInventoryService: productVariantInventoryServiceTx,
productVariantInventoryService,
})
return order
@@ -59,9 +59,7 @@ export default async (req: Request, res: Response) => {
.withTransaction(transactionManager)
.detachInventoryItem(id)
await inventoryService
.withTransaction(transactionManager)
.deleteInventoryItem(id)
await inventoryService.deleteInventoryItem(id)
})
res.status(200).send({
@@ -74,11 +74,7 @@ export default async (req: Request, res: Response) => {
)
}
await manager.transaction(async (transactionManager) => {
await inventoryService
.withTransaction(transactionManager)
.deleteInventoryLevel(id, location_id)
})
await inventoryService.deleteInventoryLevel(id, location_id)
const inventoryItem = await inventoryService.retrieveInventoryItem(
id,
@@ -77,14 +77,10 @@ export default async (req: Request, res: Response) => {
req.scope.resolve("inventoryService")
const manager: EntityManager = req.scope.resolve("manager")
await manager.transaction(async (transactionManager) => {
await inventoryService
.withTransaction(transactionManager)
.updateInventoryItem(
id,
req.validatedBody as AdminPostInventoryItemsInventoryItemReq
)
})
await inventoryService.updateInventoryItem(
id,
req.validatedBody as AdminPostInventoryItemsInventoryItemReq
)
const inventoryItem = await inventoryService.retrieveInventoryItem(
id,
@@ -81,11 +81,7 @@ export default async (req: Request, res: Response) => {
const validatedBody =
req.validatedBody as AdminPostInventoryItemsItemLocationLevelsLevelReq
await manager.transaction(async (transactionManager) => {
await inventoryService
.withTransaction(transactionManager)
.updateInventoryLevel(id, location_id, validatedBody)
})
await inventoryService.updateInventoryLevel(id, location_id, validatedBody)
const inventoryItem = await inventoryService.retrieveInventoryItem(
id,
@@ -1,3 +1,14 @@
import { MedusaError } from "medusa-core-utils"
import { EntityManager } from "typeorm"
import { ulid } from "ulid"
import { IInventoryService } from "../../../../../interfaces"
import { ProductVariant } from "../../../../../models"
import {
ProductVariantInventoryService,
ProductVariantService,
} from "../../../../../services"
import { InventoryItemDTO } from "../../../../../types/inventory"
import { CreateProductVariantInput } from "../../../../../types/product-variant"
import {
DistributedTransaction,
TransactionHandlerType,
@@ -6,17 +17,6 @@ import {
TransactionState,
TransactionStepsDefinition,
} from "../../../../../utils/transaction"
import { ulid } from "ulid"
import { EntityManager } from "typeorm"
import { IInventoryService } from "../../../../../interfaces"
import {
ProductVariantInventoryService,
ProductVariantService,
} from "../../../../../services"
import { CreateProductVariantInput } from "../../../../../types/product-variant"
import { InventoryItemDTO } from "../../../../../types/inventory"
import { ProductVariant } from "../../../../../models"
import { MedusaError } from "medusa-core-utils"
enum actions {
createVariant = "createVariant",
@@ -1,6 +1,5 @@
import { IsNumber, IsObject, IsOptional, IsString } from "class-validator"
import { isDefined } from "medusa-core-utils"
import { EntityManager } from "typeorm"
import { IInventoryService } from "../../../../interfaces"
import { validateUpdateReservationQuantity } from "./utils/validate-reservation-quantity"
@@ -66,8 +65,6 @@ import { validateUpdateReservationQuantity } from "./utils/validate-reservation-
export default async (req, res) => {
const { validatedBody } = req as { validatedBody: AdminPostReservationsReq }
const manager: EntityManager = req.scope.resolve("manager")
const inventoryService: IInventoryService =
req.scope.resolve("inventoryService")
@@ -82,11 +79,9 @@ export default async (req, res) => {
)
}
const reservation = await manager.transaction(async (manager) => {
return await inventoryService
.withTransaction(manager)
.createReservationItem(validatedBody)
})
const reservation = await inventoryService.createReservationItem(
validatedBody
)
res.status(200).json({ reservation })
}
@@ -58,9 +58,7 @@ export default async (req, res) => {
req.scope.resolve("inventoryService")
const manager: EntityManager = req.scope.resolve("manager")
await manager.transaction(async (manager) => {
await inventoryService.withTransaction(manager).deleteReservationItem(id)
})
await inventoryService.deleteReservationItem(id)
res.json({
id,
@@ -91,9 +91,7 @@ export default async (req, res) => {
}
const result = await manager.transaction(async (manager) => {
await inventoryService
.withTransaction(manager)
.updateReservationItem(id, validatedBody)
await inventoryService.updateReservationItem(id, validatedBody)
})
res.status(200).json({ reservation: result })
@@ -79,12 +79,8 @@ export default async (req, res) => {
if (inventoryService) {
await Promise.all([
inventoryService
.withTransaction(transactionManager)
.deleteInventoryItemLevelByLocationId(id),
inventoryService
.withTransaction(transactionManager)
.deleteReservationItemByLocationId(id),
inventoryService.deleteInventoryItemLevelByLocationId(id),
inventoryService.deleteReservationItemByLocationId(id),
])
}
})