-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(stock-location,core-flows,types): updates existing address when u…
…pdating stock location address
- Loading branch information
Showing
7 changed files
with
311 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@medusajs/stock-location": patch | ||
"@medusajs/core-flows": patch | ||
"@medusajs/types": patch | ||
--- | ||
|
||
fix(stock-location,core-flows,types): update existing address when updating stock location address |
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
73 changes: 73 additions & 0 deletions
73
packages/core/core-flows/src/stock-location/steps/upsert-stock-location-addresses.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,73 @@ | ||
import { | ||
IStockLocationService, | ||
UpsertStockLocationAddressInput, | ||
} from "@medusajs/framework/types" | ||
import { | ||
getSelectsAndRelationsFromObjectArray, | ||
promiseAll, | ||
} from "@medusajs/framework/utils" | ||
import { StepResponse, createStep } from "@medusajs/framework/workflows-sdk" | ||
|
||
import { Modules } from "@medusajs/framework/utils" | ||
|
||
export const upsertStockLocationAddressesStepId = | ||
"upsert-stock-location-addresses-step" | ||
/** | ||
* This step upserts stock location addresses matching the specified filters. | ||
*/ | ||
export const upsertStockLocationAddressesStep = createStep( | ||
upsertStockLocationAddressesStepId, | ||
async (input: UpsertStockLocationAddressInput[], { container }) => { | ||
const stockLocationService = container.resolve<IStockLocationService>( | ||
Modules.STOCK_LOCATION | ||
) | ||
|
||
const stockLocationAddressIds = input.map((i) => i.id!).filter(Boolean) | ||
const { selects, relations } = getSelectsAndRelationsFromObjectArray(input) | ||
|
||
const dataToUpdate = await stockLocationService.listStockLocationAddresses( | ||
{ id: stockLocationAddressIds }, | ||
{ select: selects, relations } | ||
) | ||
|
||
const updateIds = dataToUpdate.map((du) => du.id) | ||
|
||
const updatedAddresses = | ||
await stockLocationService.upsertStockLocationAddresses(input) | ||
|
||
const dataToDelete = updatedAddresses.filter( | ||
(address) => !updateIds.includes(address.id) | ||
) | ||
|
||
return new StepResponse(updatedAddresses, { dataToUpdate, dataToDelete }) | ||
}, | ||
async (revertData, { container }) => { | ||
if (!revertData) { | ||
return | ||
} | ||
|
||
const stockLocationService = container.resolve<IStockLocationService>( | ||
Modules.STOCK_LOCATION | ||
) | ||
|
||
const promises: any[] = [] | ||
|
||
if (revertData.dataToDelete) { | ||
promises.push( | ||
stockLocationService.deleteStockLocationAddresses( | ||
revertData.dataToDelete.map((d) => d.id!) | ||
) | ||
) | ||
} | ||
|
||
if (revertData.dataToUpdate) { | ||
promises.push( | ||
stockLocationService.upsertStockLocationAddresses( | ||
revertData.dataToUpdate | ||
) | ||
) | ||
} | ||
|
||
await promiseAll(promises) | ||
} | ||
) |
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