feat(order,core-flows): added order change create workflow (#8033)

what:

- adds anorder change create workflow
- remove order change service, brings validation to entry point service
This commit is contained in:
Riqwan Thamir
2024-07-09 14:12:05 +02:00
committed by GitHub
parent 9516890bb3
commit b6fd82e31e
13 changed files with 498 additions and 441 deletions

View File

@@ -1,3 +1,2 @@
export { default as OrderChangeService } from "./order-change-service"
export { default as OrderModuleService } from "./order-module-service"
export { default as OrderService } from "./order-service"

View File

@@ -1,138 +0,0 @@
import {
Context,
DAL,
FindConfig,
OrderTypes,
RepositoryService,
} from "@medusajs/types"
import {
deduplicate,
InjectManager,
InjectTransactionManager,
MedusaContext,
MedusaError,
ModulesSdkUtils,
} from "@medusajs/utils"
import { OrderChange } from "@models"
import { OrderChangeStatus } from "@types"
type InjectedDependencies = {
orderChangeRepository: DAL.RepositoryService
}
export default class OrderChangeService extends ModulesSdkUtils.MedusaInternalService<
InjectedDependencies,
OrderChange
>(OrderChange) {
protected readonly orderChangeRepository_: RepositoryService<OrderChange>
constructor(container: InjectedDependencies) {
// @ts-ignore
super(...arguments)
this.orderChangeRepository_ = container.orderChangeRepository
}
@InjectManager("orderChangeRepository_")
async listCurrentOrderChange<TEntityMethod = OrderTypes.OrderDTO>(
orderId: string | string[],
config: FindConfig<TEntityMethod> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<OrderChange[]> {
const allChanges = await super.list(
{ order_id: orderId },
config ?? {
select: ["order_id", "status", "version"],
order: {
order_id: "ASC",
version: "DESC",
},
}
)
if (!allChanges.length) {
return []
}
const lastChanges: string[] = []
const seen = new Set()
for (let i = 0; i < allChanges.length; i++) {
if (seen.has(allChanges[i].order_id)) {
continue
}
seen.add(allChanges[i].order_id)
if (this.isActive(allChanges[i])) {
lastChanges.push(allChanges[i].id)
}
}
let orderChange!: OrderChange
if (allChanges?.length > 0) {
if (this.isActive(allChanges[0])) {
orderChange = allChanges[0]
}
}
if (!orderChange) {
return []
}
const relations = deduplicate([...(config.relations ?? []), "actions"])
config.relations = relations
const queryConfig = ModulesSdkUtils.buildQuery<OrderChange>(
{
id: lastChanges,
order: {
items: {
version: orderChange.version,
},
},
},
config
)
return await this.orderChangeRepository_.find(queryConfig, sharedContext)
}
isActive(orderChange: OrderChange): boolean {
return (
orderChange.status === OrderChangeStatus.PENDING ||
orderChange.status === OrderChangeStatus.REQUESTED
)
}
async create(
data: Partial<OrderChange>[],
sharedContext?: Context
): Promise<OrderChange[]>
async create(
data: Partial<OrderChange>,
sharedContext?: Context
): Promise<OrderChange>
@InjectTransactionManager("orderChangeRepository_")
async create(
data: Partial<OrderChange>[] | Partial<OrderChange>,
@MedusaContext() sharedContext: Context = {}
): Promise<OrderChange[] | OrderChange> {
const dataArr = Array.isArray(data) ? data : [data]
const activeOrderEdit = await this.listCurrentOrderChange(
dataArr.map((d) => d.order_id!),
{},
sharedContext
)
if (activeOrderEdit.length > 0) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`An active order change already exists for the order(s) ${activeOrderEdit
.map((a) => a.order_id)
.join(",")}`
)
}
return await super.create(dataArr, sharedContext)
}
}

View File

@@ -71,7 +71,6 @@ import {
formatOrder,
} from "../utils"
import * as BundledActions from "./actions"
import OrderChangeService from "./order-change-service"
import OrderService from "./order-service"
type InjectedDependencies = {
@@ -85,7 +84,7 @@ type InjectedDependencies = {
lineItemTaxLineService: ModulesSdkTypes.IMedusaInternalService<any>
shippingMethodTaxLineService: ModulesSdkTypes.IMedusaInternalService<any>
transactionService: ModulesSdkTypes.IMedusaInternalService<any>
orderChangeService: OrderChangeService
orderChangeService: ModulesSdkTypes.IMedusaInternalService<any>
orderChangeActionService: ModulesSdkTypes.IMedusaInternalService<any>
orderItemService: ModulesSdkTypes.IMedusaInternalService<any>
orderSummaryService: ModulesSdkTypes.IMedusaInternalService<any>
@@ -176,7 +175,7 @@ export default class OrderModuleService<
protected lineItemTaxLineService_: ModulesSdkTypes.IMedusaInternalService<TLineItemTaxLine>
protected shippingMethodTaxLineService_: ModulesSdkTypes.IMedusaInternalService<TShippingMethodTaxLine>
protected transactionService_: ModulesSdkTypes.IMedusaInternalService<TTransaction>
protected orderChangeService_: OrderChangeService
protected orderChangeService_: ModulesSdkTypes.IMedusaInternalService<TOrderChange>
protected orderChangeActionService_: ModulesSdkTypes.IMedusaInternalService<TOrderChangeAction>
protected orderItemService_: ModulesSdkTypes.IMedusaInternalService<TOrderItem>
protected orderSummaryService_: ModulesSdkTypes.IMedusaInternalService<TOrderSummary>
@@ -1741,21 +1740,30 @@ export default class OrderModuleService<
@MedusaContext() sharedContext?: Context
): Promise<OrderChange[]> {
const dataArr = Array.isArray(data) ? data : [data]
const orderIds: string[] = []
const dataMap: Record<string, object> = {}
const orderChanges = await this.listOrderChanges(
{
order_id: dataArr.map((data) => data.order_id),
status: [OrderChangeStatus.PENDING, OrderChangeStatus.REQUESTED],
},
{},
sharedContext
)
const orderChangesMap = new Map<string, OrderTypes.OrderChangeDTO>(
orderChanges.map((item) => [item.order_id, item])
)
for (const change of dataArr) {
orderIds.push(change.order_id)
dataMap[change.order_id] = change
}
const orders = await this.listOrders(
{
id: orderIds,
},
{
select: ["id", "version"],
},
{ id: orderIds },
{ select: ["id", "version"] },
sharedContext
)
@@ -1769,6 +1777,15 @@ export default class OrderModuleService<
}
const input = orders.map((order) => {
const existingOrderChange = orderChangesMap.get(order.id)
if (existingOrderChange) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`Order (${order.id}) already has an existing active order change`
)
}
return {
...dataMap[order.id],
version: order.version + 1,