feat(): Backport metadata management (#11469)

FIXES FRMW-2915

**What**
Backport metadata management. all the metadata get preserved unless a specific empty string is provided for the key which in turn would remove that key from the metadata
This commit is contained in:
Adrien de Peretti
2025-02-17 10:14:53 +01:00
committed by GitHub
parent a5ff1b92ce
commit 63f0774569
7 changed files with 171 additions and 4 deletions

View File

@@ -45,10 +45,13 @@ describe("Internal Module Service Factory", () => {
let instance
beforeEach(() => {
jest.clearAllMocks()
instance = new IMedusaInternalService(containerMock)
})
afterEach(() => {
jest.clearAllMocks()
})
it("should throw model id undefined error on retrieve if id is not defined", async () => {
const err = await instance.retrieve().catch((e) => e)
expect(err.message).toBe("model - id must be defined")
@@ -201,7 +204,7 @@ describe("Internal Module Service Factory", () => {
updateData,
])
const result = await instance.update(updateData)
const result = await instance.update({ selector: {}, data: updateData })
expect(result).toEqual([updateData])
})
@@ -223,6 +226,32 @@ describe("Internal Module Service Factory", () => {
])
})
it("should update entities metadata successfully", async () => {
const updateData = {
id: "1",
name: "UpdatedItem",
metadata: { key1: "", key2: "key2" },
}
const entitiesToUpdate = [
{ id: "1", name: "Item", metadata: { key1: "value1" } },
]
containerMock[modelRepositoryName].find.mockClear()
containerMock[modelRepositoryName].find.mockResolvedValueOnce(
entitiesToUpdate
)
await instance.update({ selector: {}, data: updateData })
expect(
containerMock[modelRepositoryName].update.mock.calls[0][0][0].update
).toEqual({
...updateData,
metadata: {
key2: "key2",
},
})
})
it("should delete entity successfully", async () => {
await instance.delete("1")
expect(containerMock[modelRepositoryName].delete).toHaveBeenCalledWith(

View File

@@ -17,6 +17,7 @@ import {
isString,
lowerCaseFirst,
MedusaError,
mergeMetadata,
} from "../common"
import { FreeTextSearchFilterKeyPrefix } from "../dal"
import { DmlEntity, toMikroORMEntity } from "../dml"
@@ -350,6 +351,16 @@ export function MedusaInternalService<
return []
}
// Manage metadata if needed
toUpdateData.forEach(({ entity, update }) => {
if (isPresent(update.metadata)) {
entity.metadata = update.metadata = mergeMetadata(
entity.metadata ?? {},
update.metadata
)
}
})
return await this[propertyRepositoryName].update(
toUpdateData,
sharedContext