-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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 (#10832) * fix(stock-location,core-flows,types): updates existing address when updating stock location address * chore: use hasOne instead of hasMany
- Loading branch information
Showing
10 changed files
with
336 additions
and
7 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
15 changes: 15 additions & 0 deletions
15
packages/modules/stock-location/src/migrations/Migration20250106142624.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,15 @@ | ||
import { Migration } from "@mikro-orm/migrations" | ||
|
||
export class Migration20250106142624 extends Migration { | ||
async up(): Promise<void> { | ||
this.addSql( | ||
'alter table if exists "stock_location" add constraint "stock_location_address_id_unique" unique ("address_id");' | ||
) | ||
} | ||
|
||
async down(): Promise<void> { | ||
this.addSql( | ||
'alter table if exists "stock_location" drop constraint if exists "stock_location_address_id_unique";' | ||
) | ||
} | ||
} |
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
Oops, something went wrong.