chore(order): dml (#10292)

* ../../core/types/src/dml/index.ts

* ../../core/types/src/dml/index.ts

* fix: relationships mapping

* handle nullable foreign keys types

* handle nullable foreign keys types

* handle nullable foreign keys types

* continue to update product category repository

* fix all product category repositories issues

* fix product category service types

* fix product module service types

* fix product module service types

* fix repository template type

* refactor: use a singleton DMLToMikroORM factory instance

Since the MikroORM MetadataStorage is global, we will also have to turn DML
to MikroORM entities conversion use a global bucket as well

* refactor: update product module to use DML in tests

* wip: tests

* WIP product linkable fixes

* continue type fixing and start test fixing

* test: fix more tests

* fix repository

* fix pivot table computaion + fix mikro orm repository

* fix many to many management and configuration

* fix many to many management and configuration

* fix many to many management and configuration

* update product tag relation configuration

* Introduce experimental dml hooks to fix some issues with categories

* more fixes

* fix product tests

* add missing id prefixes

* fix product category handle management

* test: fix more failing tests

* test: make it all green

* test: fix breaking tests

* fix: build issues

* fix: build issues

* fix: more breaking tests

* refactor: fix issues after merge

* refactor: fix issues after merge

* refactor: surpress types error

* test: fix DML failing tests

* improve many to many inference + tests

* Wip fix columns from product entity

* remove product model before create hook and manage handle validation and transformation at the service level

* test: fix breaking unit tests

* fix: product module service to not update handle on product update

* fix define link and joiner config

* test: fix joiner config test

* test: fix joiner config test

* fix joiner config primary keys

* Fix joiner config builder

* Fix joiner config builder

* test: remove only modifier from test

* refactor: remove hooks usage from product collection

* refactor: remove hooks usage from product-option

* refactor: remove hooks usage for computing category handle

* refactor: remove hooks usage from productCategory model

* refactor: remove hooks from DML

* refactor: remove cruft

* order dml

* cleanup

* re add foerign key indexes

* wip

* chore: remove unused types

* wip

* changes

* rm raw

* autoincrement

* wip

* rel

* refactor: cleanup

* migration and models configuration adjustments

* cleanup

* number searchable

* fix random ordering

* fix

* test: fix product-category tests

* test: update breaking DML tests

* test: array assertion to not care about ordering

* fix: temporarily apply id ordering for products

* types

* wip

* WIP type improvements

* update order models

* partially fix types temporarely

* rel

* fix: recursive type issue

* improve type inference breaks

* improve type inference breaks

* update models

* rm nullable

* default value

* repository

* update default value handling

* fix unit tests

* WIP

* toMikroORM

* fix relations

* cascades

* fix

* experimental dml hooks

* rm migration

* serial

* nullable autoincrement

* fix model

* model changes

* fix one to one DML

* order test

* fix addresses

* fix unit tests

* Re align dml entity name inference

* update model table name config

* update model table name config

* revert

* update return relation

* WIP

* hasOne

* models

* fix

* model

* initial commit

* cart service

* order module

* utils unit test

* index engine

* changeset

* merge

* fix hasOne with fk

* update

* free text filter per entity

* tests

* prod category

* property string many to many

* fix big number

* link modules migration set names

* merge

* shipping option rules

* serializer

* unit test

* fix test mikro orm init

* fix test mikro orm init

* Maintain merge object properties

* fix test mikro orm init

* prevent unit test from connecting to db

* wip

* fix test

* fix test

* link test

* schema

* models

* auto increment

* hook

* model hooks

* order

* wip

* orm version

* request return field

* fix return configuration on order model

* workflows

* core flows

* unit test

* test

* base repo

* test

* base repo

* test fix

* inventory move

* locking inventory

* test

* free text fix

* rm timeout mock

* migrate fulfillment values

* v6.4.3

* cleanup

* link-modules update sql

* revert test

* remove fake timers

---------

Co-authored-by: adrien2p <adrien.deperetti@gmail.com>
Co-authored-by: Harminder Virk <virk.officials@gmail.com>
Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2025-01-21 08:04:47 -05:00
1 parent 05c8a67d8e
commit cc73802ab3
178 files changed
+5731 -7120

No files matched your search

+4 -4
View File
@@ -27,10 +27,10 @@
"license": "MIT",
"devDependencies": {
"@medusajs/framework": "^2.3.1",
"@mikro-orm/core": "5.9.7",
"@mikro-orm/knex": "5.9.7",
"@mikro-orm/migrations": "5.9.7",
"@mikro-orm/postgresql": "5.9.7",
"@mikro-orm/core": "6.4.3",
"@mikro-orm/knex": "6.4.3",
"@mikro-orm/migrations": "6.4.3",
"@mikro-orm/postgresql": "6.4.3",
"@swc/core": "^1.7.28",
"@swc/jest": "^0.2.36",
"awilix": "^8.0.1",
@@ -1,4 +1,4 @@
import { IInventoryService, InventoryTypes } from "@medusajs/framework/types"
import { InventoryTypes } from "@medusajs/framework/types"
import { StepResponse, createStep } from "@medusajs/framework/workflows-sdk"
import { MathBN, Modules } from "@medusajs/framework/utils"
@@ -29,20 +29,22 @@ export const adjustInventoryLevelsStep = createStep(
input: AdjustInventoryLevelsStepInput,
{ container }
) => {
const inventoryService: IInventoryService = container.resolve(
Modules.INVENTORY
)
const inventoryService = container.resolve(Modules.INVENTORY)
const locking = container.resolve(Modules.LOCKING)
const inventoryItemIds = input.map((item) => item.inventory_item_id)
const adjustedLevels: InventoryTypes.InventoryLevelDTO[] =
await inventoryService.adjustInventory(
input.map((item) => {
return {
inventoryItemId: item.inventory_item_id,
locationId: item.location_id,
adjustment: item.adjustment,
}
})
)
await locking.execute(inventoryItemIds, async () => {
return await inventoryService.adjustInventory(
input.map((item) => {
return {
inventoryItemId: item.inventory_item_id,
locationId: item.location_id,
adjustment: item.adjustment,
}
})
)
})
return new StepResponse(
adjustedLevels,
@@ -55,11 +57,15 @@ export const adjustInventoryLevelsStep = createStep(
)
},
async (adjustedLevels, { container }) => {
if (!adjustedLevels) {
if (!adjustedLevels?.length) {
return
}
const inventoryService = container.resolve(Modules.INVENTORY)
const locking = container.resolve(Modules.LOCKING)
const inventoryItemIds = adjustedLevels.map(
(item) => item.inventory_item_id
)
/**
* @todo
@@ -67,14 +73,16 @@ export const adjustInventoryLevelsStep = createStep(
* "inventoryItemId" and "locationId" as snake case, whereas
* the expected object needed these properties as camelCase
*/
await inventoryService.adjustInventory(
adjustedLevels.map((level) => {
return {
inventoryItemId: level.inventory_item_id,
locationId: level.location_id,
adjustment: level.adjustment,
}
})
)
await locking.execute(inventoryItemIds, async () => {
await inventoryService.adjustInventory(
adjustedLevels.map((level) => {
return {
inventoryItemId: level.inventory_item_id,
locationId: level.location_id,
adjustment: level.adjustment,
}
})
)
})
}
)
@@ -20,10 +20,10 @@ export const updateOrderClaimsStep = createStep(
const { selects, relations } = getSelectsAndRelationsFromObjectArray(data, {
objectFields: ["metadata"],
})
const dataBeforeUpdate = await service.listOrderClaims(
const dataBeforeUpdate = (await service.listOrderClaims(
{ id: data.map((d) => d.id) },
{ relations, select: selects }
)
)) as UpdateOrderClaimDTO[]
const updated = await service.updateOrderClaims(
data.map((dt) => {
@@ -26,10 +26,10 @@ export const updateOrderExchangesStep = createStep(
const { selects, relations } = getSelectsAndRelationsFromObjectArray(data, {
objectFields: ["metadata"],
})
const dataBeforeUpdate = await service.listOrderExchanges(
const dataBeforeUpdate = (await service.listOrderExchanges(
{ id: data.map((d) => d.id) },
{ relations, select: selects }
)
)) as UpdateOrderExchangeDTO[]
const updated = await service.updateOrderExchanges(
data.map((dt) => {
@@ -22,6 +22,7 @@ export * from "./exchange/create-exchange-items-from-actions"
export * from "./exchange/delete-exchanges"
export * from "./preview-order-change"
export * from "./register-fulfillment"
export * from "./register-order-changes"
export * from "./register-shipment"
export * from "./return/cancel-return"
export * from "./return/create-complete-return"
@@ -32,7 +33,5 @@ export * from "./return/update-returns"
export * from "./set-tax-lines-for-items"
export * from "./update-order-change-actions"
export * from "./update-order-changes"
export * from "./update-order-exchanges"
export * from "./update-shipping-methods"
export * from "./update-orders"
export * from "./register-order-changes"
export * from "./update-shipping-methods"
@@ -13,7 +13,7 @@ import { StepResponse, createStep } from "@medusajs/framework/workflows-sdk"
*/
export type UpdateOrderChangesStepInput = UpdateOrderChangeDTO[]
export const updateOrderChangesStepId = "update-order-shopping-methods"
export const updateOrderChangesStepId = "update-order-changes"
/**
* This step updates order change.
*/
@@ -1,62 +0,0 @@
import {
IOrderModuleService,
UpdateOrderExchangeDTO,
} from "@medusajs/framework/types"
import {
Modules,
getSelectsAndRelationsFromObjectArray,
} from "@medusajs/framework/utils"
import { StepResponse, createStep } from "@medusajs/framework/workflows-sdk"
/**
* The order exchanges to update.
*/
export type UpdateOrderExchangesStepInput = UpdateOrderExchangeDTO[]
export const updateOrderExchangesStepId = "update-order-exchange"
/**
* This step update order changes.
*/
export const updateOrderExchangesStep = createStep(
updateOrderExchangesStepId,
async (data: UpdateOrderExchangesStepInput, { container }) => {
const service = container.resolve<IOrderModuleService>(Modules.ORDER)
const { selects, relations } = getSelectsAndRelationsFromObjectArray(data, {
objectFields: ["metadata"],
})
const dataBeforeUpdate = await service.listOrderExchanges(
{ id: data.map((d) => d.id) },
{ relations, select: selects }
)
const updated = await service.updateOrderExchanges(
data.map((dt) => {
const { id, ...rest } = dt
return {
selector: { id },
data: rest,
}
})
)
return new StepResponse(updated, dataBeforeUpdate)
},
async (dataBeforeUpdate, { container }) => {
if (!dataBeforeUpdate?.length) {
return
}
const service = container.resolve<IOrderModuleService>(Modules.ORDER)
await service.updateOrderExchanges(
dataBeforeUpdate.map((dt) => {
const { id, ...rest } = dt
return {
selector: { id },
data: rest,
}
})
)
}
)
@@ -143,13 +143,15 @@ function prepareInventoryUpdate({
}[] = []
for (const item of fulfillment.items) {
// if this is `null` this means that item is from variant that has `manage_inventory` false
if (item.inventory_item_id) {
inventoryAdjustment.push({
inventory_item_id: item.inventory_item_id as string,
location_id: fulfillment.location_id,
adjustment: item.quantity,
})
if (!item.inventory_item_id) {
continue
}
inventoryAdjustment.push({
inventory_item_id: item.inventory_item_id as string,
location_id: fulfillment.location_id,
adjustment: item.quantity,
})
}
return {
@@ -223,16 +223,10 @@ export const orderClaimRequestItemReturnWorkflow = createWorkflow(
when({ orderClaim }, ({ orderClaim }) => {
return !orderClaim.return_id
}).then(() => {
const createdReturnId = transform(
{ createdReturn },
({ createdReturn }) => {
return createdReturn?.[0]!.id
}
)
updateOrderClaimsStep([
{
id: orderClaim.id,
return_id: createdReturnId,
return: createdReturn?.[0]!.id,
},
])
})
@@ -327,6 +327,8 @@ export const confirmClaimRequestWorkflow = createWorkflow(
fields: [
"id",
"status",
"claim_id",
"return_id",
"actions.id",
"actions.claim_id",
"actions.return_id",
@@ -225,16 +225,10 @@ export const orderExchangeRequestItemReturnWorkflow = createWorkflow(
when({ orderExchange }, ({ orderExchange }) => {
return !orderExchange.return_id
}).then(() => {
const createdReturnId = transform(
{ createdReturn },
({ createdReturn }) => {
return createdReturn?.[0]!.id
}
)
updateOrderExchangesStep([
{
id: orderExchange.id,
return_id: createdReturnId,
return: createdReturn?.[0]!.id,
},
])
})
@@ -186,7 +186,17 @@ export const removeItemReturnActionWorkflow = createWorkflow(
const updatedOrderChange: OrderChangeDTO = useRemoteQueryStep({
entry_point: "order_change",
fields: ["actions.action", "actions.return_id", "actions.id"],
fields: [
"actions.id",
"actions.action",
"actions.order_id",
"actions.return_id",
"actions.claim_id",
"actions.exchange_id",
"actions.reference",
"actions.reference_id",
"actions.order_change_id",
],
variables: {
filters: {
order_id: orderReturn.order_id,