fix(utils): auto generated update method return (#13225)
* fix(utils): auto generated update method return * tests * cleanup * rm unused function * Update .changeset/young-mangos-suffer.md --------- Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
2f1c39e17e
commit
fd4e791428
5
.changeset/young-mangos-suffer.md
Normal file
5
.changeset/young-mangos-suffer.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@medusajs/utils": minor
|
||||
---
|
||||
|
||||
fix(utils): auto generated update method return
|
||||
122
integration-tests/modules/__tests__/modules/crud.methods.spec.ts
Normal file
122
integration-tests/modules/__tests__/modules/crud.methods.spec.ts
Normal file
@@ -0,0 +1,122 @@
|
||||
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
|
||||
|
||||
jest.setTimeout(100000)
|
||||
|
||||
process.env.ENABLE_INDEX_MODULE = "true"
|
||||
|
||||
medusaIntegrationTestRunner({
|
||||
testSuite: ({ getContainer, dbConnection, api, dbConfig }) => {
|
||||
let appContainer
|
||||
|
||||
beforeAll(() => {
|
||||
appContainer = getContainer()
|
||||
})
|
||||
|
||||
afterAll(() => {
|
||||
process.env.ENABLE_INDEX_MODULE = "false"
|
||||
})
|
||||
|
||||
describe("auto-generated CRUD methods", () => {
|
||||
it("should create brands", async () => {
|
||||
const brandModule = appContainer.resolve("brand")
|
||||
|
||||
const brand = await brandModule.createBrands({
|
||||
name: "Medusa Brand",
|
||||
})
|
||||
|
||||
expect(brand).toEqual(
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
name: "Medusa Brand",
|
||||
})
|
||||
)
|
||||
|
||||
const multipleBrands = await brandModule.createBrands([
|
||||
{
|
||||
name: "Medusa Brand 2",
|
||||
},
|
||||
{
|
||||
name: "Medusa Brand 3",
|
||||
},
|
||||
])
|
||||
|
||||
expect(multipleBrands).toEqual([
|
||||
expect.objectContaining({
|
||||
id: expect.stringMatching("brand_"),
|
||||
name: "Medusa Brand 2",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: expect.stringMatching("brand_"),
|
||||
name: "Medusa Brand 3",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should update brands", async () => {
|
||||
const brandModule = appContainer.resolve("brand")
|
||||
|
||||
const multipleBrands = await brandModule.createBrands([
|
||||
{
|
||||
name: "Medusa Brand 2",
|
||||
},
|
||||
{
|
||||
name: "Medusa Brand 3",
|
||||
},
|
||||
])
|
||||
|
||||
const brand = await brandModule.updateBrands({
|
||||
id: multipleBrands[0].id,
|
||||
name: "Medusa Brand",
|
||||
})
|
||||
|
||||
expect(brand).toEqual(
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
name: "Medusa Brand",
|
||||
})
|
||||
)
|
||||
|
||||
const multipleBrandsUpdated = await brandModule.updateBrands([
|
||||
{
|
||||
id: multipleBrands[0].id,
|
||||
name: "Medusa Brand 22",
|
||||
},
|
||||
{
|
||||
id: multipleBrands[1].id,
|
||||
name: "Medusa Brand 33",
|
||||
},
|
||||
])
|
||||
|
||||
expect(multipleBrandsUpdated).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
name: "Medusa Brand 22",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
name: "Medusa Brand 33",
|
||||
}),
|
||||
])
|
||||
)
|
||||
|
||||
const multipleBrandsUpdatedWithSelector =
|
||||
await brandModule.updateBrands({
|
||||
selector: {
|
||||
name: { $like: "Medusa Brand 22" },
|
||||
},
|
||||
data: {
|
||||
name: "Medusa Brand **",
|
||||
},
|
||||
})
|
||||
|
||||
expect(multipleBrandsUpdatedWithSelector).toEqual([
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
name: "Medusa Brand **",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
@@ -219,19 +219,17 @@ export function MedusaService<
|
||||
data = [],
|
||||
sharedContext: Context = {}
|
||||
): Promise<T | T[]> {
|
||||
const serviceData = Array.isArray(data) ? data : [data]
|
||||
const service = this.__container__[serviceRegistrationName]
|
||||
const models = await service.create(serviceData, sharedContext)
|
||||
const response = Array.isArray(data) ? models : models[0]
|
||||
const models = await service.create(data, sharedContext)
|
||||
|
||||
klassPrototype.aggregatedEvents.bind(this)({
|
||||
action: CommonEvents.CREATED,
|
||||
object: camelToSnakeCase(modelName).toLowerCase(),
|
||||
data: response,
|
||||
data: models,
|
||||
context: sharedContext,
|
||||
})
|
||||
|
||||
return await this.baseRepository_.serialize<T | T[]>(response)
|
||||
return await this.baseRepository_.serialize<T | T[]>(models)
|
||||
}
|
||||
|
||||
applyMethod(methodImplementation, 1)
|
||||
@@ -243,9 +241,8 @@ export function MedusaService<
|
||||
data = [],
|
||||
sharedContext: Context = {}
|
||||
): Promise<T | T[]> {
|
||||
const serviceData = Array.isArray(data) ? data : [data]
|
||||
const service = this.__container__[serviceRegistrationName]
|
||||
const response = await service.update(serviceData, sharedContext)
|
||||
const response = await service.update(data, sharedContext)
|
||||
|
||||
klassPrototype.aggregatedEvents.bind(this)({
|
||||
action: CommonEvents.UPDATED,
|
||||
|
||||
Reference in New Issue
Block a user