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:
@@ -227,6 +227,15 @@
|
||||
"name": "stock_location",
|
||||
"schema": "public",
|
||||
"indexes": [
|
||||
{
|
||||
"columnNames": [
|
||||
"address_id"
|
||||
],
|
||||
"composite": false,
|
||||
"keyName": "stock_location_address_id_unique",
|
||||
"primary": false,
|
||||
"unique": true
|
||||
},
|
||||
{
|
||||
"keyName": "IDX_stock_location_address_id",
|
||||
"columnNames": [],
|
||||
|
||||
@@ -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";'
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,7 @@ const StockLocationAddress = model
|
||||
province: model.text().nullable(),
|
||||
postal_code: model.text().nullable(),
|
||||
metadata: model.json().nullable(),
|
||||
stock_locations: model.hasMany(() => StockLocation, {
|
||||
stock_locations: model.hasOne(() => StockLocation, {
|
||||
mappedBy: "address",
|
||||
}),
|
||||
})
|
||||
|
||||
@@ -11,7 +11,9 @@ import {
|
||||
ModulesSdkTypes,
|
||||
StockLocationAddressInput,
|
||||
StockLocationTypes,
|
||||
UpdateStockLocationAddressInput,
|
||||
UpdateStockLocationInput,
|
||||
UpsertStockLocationAddressInput,
|
||||
UpsertStockLocationInput,
|
||||
} from "@medusajs/framework/types"
|
||||
import {
|
||||
@@ -258,4 +260,62 @@ export default class StockLocationModuleService
|
||||
) {
|
||||
return await this.stockLocationAddressService_.update(input, context)
|
||||
}
|
||||
|
||||
async upsertStockLocationAddresses(
|
||||
data: UpsertStockLocationAddressInput,
|
||||
context?: Context
|
||||
): Promise<StockLocationTypes.StockLocationAddressDTO>
|
||||
async upsertStockLocationAddresses(
|
||||
data: UpsertStockLocationAddressInput[],
|
||||
context?: Context
|
||||
): Promise<StockLocationTypes.StockLocationAddressDTO[]>
|
||||
|
||||
@InjectManager()
|
||||
async upsertStockLocationAddresses(
|
||||
data: UpsertStockLocationAddressInput | UpsertStockLocationAddressInput[],
|
||||
@MedusaContext() context: Context = {}
|
||||
): Promise<
|
||||
| StockLocationTypes.StockLocationAddressDTO
|
||||
| StockLocationTypes.StockLocationAddressDTO[]
|
||||
> {
|
||||
const input = Array.isArray(data) ? data : [data]
|
||||
|
||||
const result = await this.upsertStockLocationAddresses_(input, context)
|
||||
|
||||
return await this.baseRepository_.serialize<
|
||||
| StockLocationTypes.StockLocationAddressDTO[]
|
||||
| StockLocationTypes.StockLocationAddressDTO
|
||||
>(Array.isArray(data) ? result : result[0])
|
||||
}
|
||||
|
||||
@InjectTransactionManager()
|
||||
async upsertStockLocationAddresses_(
|
||||
input: UpsertStockLocationAddressInput[],
|
||||
@MedusaContext() context: Context = {}
|
||||
) {
|
||||
const toUpdate = input.filter(
|
||||
(location): location is UpdateStockLocationAddressInput => !!location.id
|
||||
) as UpdateStockLocationAddressInput[]
|
||||
const toCreate = input.filter(
|
||||
(location) => !location.id
|
||||
) as StockLocationAddressInput[]
|
||||
|
||||
const operations: Promise<
|
||||
| InferEntityType<typeof StockLocationAddress>[]
|
||||
| InferEntityType<typeof StockLocationAddress>
|
||||
>[] = []
|
||||
|
||||
if (toCreate.length) {
|
||||
operations.push(
|
||||
this.stockLocationAddressService_.create(toCreate, context)
|
||||
)
|
||||
}
|
||||
if (toUpdate.length) {
|
||||
operations.push(
|
||||
this.stockLocationAddressService_.update(toUpdate, context)
|
||||
)
|
||||
}
|
||||
|
||||
return (await promiseAll(operations)).flat()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user