fix(utils): medusa internal service returned data should match typings (#12715)

FIXES SUP-1824

**What**
The medusa internal service update should always return the data in the expected shape described by the interface. The medusa service should not have to handle the reshapre
This commit is contained in:
Adrien de Peretti
2025-06-12 17:55:49 +02:00
committed by GitHub
parent ea0b297b44
commit ab634a14ba
9 changed files with 94 additions and 25 deletions

View File

@@ -260,6 +260,13 @@ export function MedusaInternalService<
| InferEntityType<TEntity>[]
}
let shouldReturnArray = false
if (Array.isArray(input)) {
shouldReturnArray = true
} else if (isObject(input) && "selector" in input) {
shouldReturnArray = true
}
const primaryKeys = AbstractService_.retrievePrimaryKeys(model)
const inputArray = Array.isArray(input) ? input : [input]
@@ -352,7 +359,9 @@ export function MedusaInternalService<
}
if (!toUpdateData.length) {
return []
return (shouldReturnArray ? [] : void 0) as
| InferEntityType<TEntity>
| InferEntityType<TEntity>[]
}
// Manage metadata if needed
@@ -372,10 +381,12 @@ export function MedusaInternalService<
}
})
return await this[propertyRepositoryName].update(
const entities = await this[propertyRepositoryName].update(
toUpdateData,
sharedContext
)
return shouldReturnArray ? entities : entities[0]
}
delete(idOrSelector: string, sharedContext?: Context): Promise<string[]>

View File

@@ -245,12 +245,7 @@ export function MedusaService<
): Promise<T | T[]> {
const serviceData = Array.isArray(data) ? data : [data]
const service = this.__container__[serviceRegistrationName]
const models = await service.update(serviceData, sharedContext)
const response = models.length
? Array.isArray(data)
? models
: models[0]
: []
const response = await service.update(serviceData, sharedContext)
klassPrototype.aggregatedEvents.bind(this)({
action: CommonEvents.UPDATED,