fix(stock-location,core-flows,types): updates existing address when updating stock location (#10832)

* fix(stock-location,core-flows,types): updates existing address when updating stock location address

* chore: use hasOne instead of hasMany
This commit is contained in:
Riqwan Thamir
2025-01-07 07:55:28 +01:00
committed by GitHub
parent fde73dbfae
commit 99a06102a2
10 changed files with 336 additions and 7 deletions
@@ -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)
}
)
@@ -1,15 +1,19 @@
import {
FilterableStockLocationProps,
StockLocationDTO,
UpdateStockLocationInput,
FilterableStockLocationProps,
UpsertStockLocationAddressInput,
} from "@medusajs/framework/types"
import {
WorkflowData,
WorkflowResponse,
createWorkflow,
transform,
} from "@medusajs/framework/workflows-sdk"
import { useQueryGraphStep } from "../../common"
import { updateStockLocationsStep } from "../steps"
import { upsertStockLocationAddressesStep } from "../steps/upsert-stock-location-addresses"
export interface UpdateStockLocationsWorkflowInput {
selector: FilterableStockLocationProps
@@ -24,6 +28,50 @@ export const updateStockLocationsWorkflow = createWorkflow(
(
input: WorkflowData<UpdateStockLocationsWorkflowInput>
): WorkflowResponse<StockLocationDTO[]> => {
return new WorkflowResponse(updateStockLocationsStep(input))
const stockLocationsQuery = useQueryGraphStep({
entity: "stock_location",
filters: input.selector,
fields: ["id", "address.id"],
}).config({ name: "get-stock-location" })
const stockLocations = transform(
{ stockLocationsQuery },
({ stockLocationsQuery }) => stockLocationsQuery.data
)
const normalizedData = transform(
{ input, stockLocations },
({ input, stockLocations }) => {
const { address, address_id, ...stockLocationInput } = input.update
const addressesInput: UpsertStockLocationAddressInput[] = []
if (address) {
for (const stockLocation of stockLocations) {
if (stockLocation.address?.id) {
addressesInput.push({
id: stockLocation.address?.id!,
...address,
})
} else {
addressesInput.push(address)
}
}
}
return {
stockLocationInput: {
selector: input.selector,
update: stockLocationInput,
},
addressesInput,
}
}
)
upsertStockLocationAddressesStep(normalizedData.addressesInput)
return new WorkflowResponse(
updateStockLocationsStep(normalizedData.stockLocationInput)
)
}
)