-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core-flows, types, medusa): Add Update location level endpoint f…
…or api-v2 (#6743) * initialize update-location-level * update middlewares * readd middleware * pr feedback
- Loading branch information
Showing
11 changed files
with
267 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
packages/core-flows/src/inventory/steps/update-inventory-levels.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { IInventoryServiceNext, InventoryNext } from "@medusajs/types" | ||
import { StepResponse, createStep } from "@medusajs/workflows-sdk" | ||
import { | ||
convertItemResponseToUpdateRequest, | ||
getSelectsAndRelationsFromObjectArray, | ||
} from "@medusajs/utils" | ||
|
||
import { ModuleRegistrationName } from "@medusajs/modules-sdk" | ||
|
||
export const updateInventoryLevelsStepId = "update-inventory-levels-step" | ||
export const updateInventoryLevelsStep = createStep( | ||
updateInventoryLevelsStepId, | ||
async ( | ||
input: InventoryNext.BulkUpdateInventoryLevelInput[], | ||
{ container } | ||
) => { | ||
const inventoryService: IInventoryServiceNext = container.resolve( | ||
ModuleRegistrationName.INVENTORY | ||
) | ||
|
||
const { selects, relations } = getSelectsAndRelationsFromObjectArray(input) | ||
|
||
const dataBeforeUpdate = await inventoryService.listInventoryLevels( | ||
{ | ||
$or: input.map(({ inventory_item_id, location_id }) => ({ | ||
inventory_item_id, | ||
location_id, | ||
})), | ||
}, | ||
{} | ||
) | ||
|
||
const updatedLevels: InventoryNext.InventoryLevelDTO[] = | ||
await inventoryService.updateInventoryLevels(input) | ||
|
||
return new StepResponse(updatedLevels, { | ||
dataBeforeUpdate, | ||
selects, | ||
relations, | ||
}) | ||
}, | ||
async (revertInput, { container }) => { | ||
if (!revertInput?.dataBeforeUpdate?.length) { | ||
return | ||
} | ||
|
||
const { dataBeforeUpdate, selects, relations } = revertInput | ||
|
||
const inventoryService = container.resolve(ModuleRegistrationName.INVENTORY) | ||
|
||
await inventoryService.updateInventoryLevels( | ||
dataBeforeUpdate.map((data) => | ||
convertItemResponseToUpdateRequest(data, selects, relations) | ||
) as InventoryNext.BulkUpdateInventoryLevelInput[] | ||
) | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
packages/core-flows/src/inventory/workflows/update-inventory-levels.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { InventoryLevelDTO, InventoryNext } from "@medusajs/types" | ||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk" | ||
|
||
import { updateInventoryLevelsStep } from "../steps/update-inventory-levels" | ||
|
||
interface WorkflowInput { | ||
updates: InventoryNext.BulkUpdateInventoryLevelInput[] | ||
} | ||
export const updateInventoryLevelsWorkflowId = | ||
"update-inventory-levels-workflow" | ||
export const updateInventoryLevelsWorkflow = createWorkflow( | ||
updateInventoryLevelsWorkflowId, | ||
(input: WorkflowData<WorkflowInput>): WorkflowData<InventoryLevelDTO[]> => { | ||
return updateInventoryLevelsStep(input.updates) | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters