fix(): Event group id propagation and event managements (#12157)

This commit is contained in:
Adrien de Peretti
2025-04-14 15:57:52 -03:00
committed by GitHub
parent 3a481290ea
commit 2f6963a5fb
22 changed files with 777 additions and 592 deletions
@@ -113,13 +113,21 @@ describe("Remote Link", function () {
},
])
expect(ProductInventoryLinkModule.create).toBeCalledWith([
["var_123", "inv_123"],
["var_abc", "inv_abc"],
])
expect(InventoryStockLocationLink.create).toBeCalledWith([
["ilev_123", "loc_123"],
])
expect(ProductInventoryLinkModule.create).toBeCalledWith(
[
["var_123", "inv_123"],
["var_abc", "inv_abc"],
],
undefined,
undefined,
{}
)
expect(InventoryStockLocationLink.create).toBeCalledWith(
[["ilev_123", "loc_123"]],
undefined,
undefined,
{}
)
})
it("Should call delete in cascade all the modules involved in the link", async function () {
@@ -167,18 +175,21 @@ describe("Remote Link", function () {
expect(ProductInventoryLinkModule.softDelete).toHaveBeenNthCalledWith(
1,
{ variant_id: ["var_123"] },
{ returnLinkableKeys: ["variant_id", "inventory_item_id"] }
{ returnLinkableKeys: ["variant_id", "inventory_item_id"] },
{}
)
expect(ProductInventoryLinkModule.softDelete).toHaveBeenNthCalledWith(
2,
{ variant_id: ["var_abc"] },
{ returnLinkableKeys: ["variant_id", "inventory_item_id"] }
{ returnLinkableKeys: ["variant_id", "inventory_item_id"] },
{}
)
expect(ProductModule.softDelete).toBeCalledWith(
{ id: ["var_123"] },
{ returnLinkableKeys: ["product_id", "variant_id"] }
{ returnLinkableKeys: ["product_id", "variant_id"] },
{}
)
expect(InventoryModule.softDelete).toBeCalledWith(
@@ -189,14 +200,16 @@ describe("Remote Link", function () {
"inventory_level_id",
"reservation_item_id",
],
}
},
{}
)
expect(InventoryStockLocationLink.softDelete).toBeCalledWith(
{
inventory_level_id: ["ilev_123"],
},
{ returnLinkableKeys: ["inventory_level_id", "stock_location_id"] }
{ returnLinkableKeys: ["inventory_level_id", "stock_location_id"] },
{}
)
})
})
+42 -15
View File
@@ -1,4 +1,5 @@
import {
Context,
ILinkModule,
LinkDefinition,
LoadedModule,
@@ -7,7 +8,9 @@ import {
import {
isObject,
MedusaContext,
MedusaError,
MedusaModuleType,
Modules,
promiseAll,
toPascalCase,
@@ -55,6 +58,9 @@ type LinkDataConfig = {
}
export class Link {
// To not lose the context chain, we need to set the type to MedusaModuleType
static __type = MedusaModuleType
private modulesMap: Map<string, LoadedLinkModule> = new Map()
private relationsPairs: Map<string, LoadedLinkModule> = new Map()
private relations: Map<string, Map<string, RemoteRelationship[]>> = new Map()
@@ -171,7 +177,8 @@ export class Link {
private async executeCascade(
removedServices: DeleteEntityInput,
executionMethod: "softDelete" | "restore"
executionMethod: "softDelete" | "restore",
@MedusaContext() sharedContext: Context = {}
): Promise<[CascadeError[] | null, RemovedIds]> {
const removedIds: RemovedIds = {}
const returnIdsList: RemovedIds = {}
@@ -254,9 +261,13 @@ export class Link {
method += toPascalCase(args.methodSuffix)
}
const removed = await service[method](cascadeDelKeys, {
returnLinkableKeys: returnFields,
})
const removed = await service[method](
cascadeDelKeys,
{
returnLinkableKeys: returnFields,
},
sharedContext
)
deletedEntities = removed as Record<string, string[]>
} catch (error) {
@@ -382,7 +393,10 @@ export class Link {
}
}
async create(link: LinkDefinition | LinkDefinition[]): Promise<unknown[]> {
async create(
link: LinkDefinition | LinkDefinition[],
@MedusaContext() sharedContext: Context = {}
): Promise<unknown[]> {
const allLinks = Array.isArray(link) ? link : [link]
const serviceLinks = new Map<
string,
@@ -489,7 +503,8 @@ export class Link {
},
{
take: 1,
}
},
sharedContext
)
if (existingLinks.length > 0) {
@@ -507,13 +522,18 @@ export class Link {
const promises: Promise<unknown[]>[] = []
for (const [serviceName, data] of serviceLinks) {
const service = this.modulesMap.get(serviceName)!
promises.push(service.create(data.linksToCreate))
promises.push(
service.create(data.linksToCreate, undefined, undefined, sharedContext)
)
}
return (await promiseAll(promises)).flat()
}
async dismiss(link: LinkDefinition | LinkDefinition[]): Promise<unknown[]> {
async dismiss(
link: LinkDefinition | LinkDefinition[],
@MedusaContext() sharedContext: Context = {}
): Promise<unknown[]> {
const allLinks = Array.isArray(link) ? link : [link]
const serviceLinks = new Map<string, [string | string[], string][]>()
@@ -541,27 +561,34 @@ export class Link {
for (const [serviceName, links] of serviceLinks) {
const service = this.modulesMap.get(serviceName)!
promises.push(service.dismiss(links))
promises.push(service.dismiss(links, undefined, sharedContext))
}
return (await promiseAll(promises)).flat()
}
async delete(
removedServices: DeleteEntityInput
removedServices: DeleteEntityInput,
@MedusaContext() sharedContext: Context = {}
): Promise<[CascadeError[] | null, RemovedIds]> {
return await this.executeCascade(removedServices, "softDelete")
return await this.executeCascade(
removedServices,
"softDelete",
sharedContext
)
}
async restore(
removedServices: DeleteEntityInput
removedServices: DeleteEntityInput,
@MedusaContext() sharedContext: Context = {}
): Promise<[CascadeError[] | null, RestoredIds]> {
return await this.executeCascade(removedServices, "restore")
return await this.executeCascade(removedServices, "restore", sharedContext)
}
async list(
link: LinkDefinition | LinkDefinition[],
options?: { asLinkDefinition?: boolean }
options?: { asLinkDefinition?: boolean },
@MedusaContext() sharedContext: Context = {}
): Promise<(object | LinkDefinition)[]> {
const allLinks = Array.isArray(link) ? link : [link]
const serviceLinks = new Map<string, object[]>()
@@ -587,7 +614,7 @@ export class Link {
promises.push(
service
.list({ $or: filters })
.list({ $or: filters }, {}, sharedContext)
.then((links: any[]) =>
options?.asLinkDefinition
? convertRecordsToLinkDefinition(links, service)