fix: Correctly list files for all ids that are passed (#12575)

This commit is contained in:
Stevche Radevski authored and GitHub committed 2025-05-22 14:52:05 +02:00
1 parent 3c6aa979a6
commit 98798f305c
2 files changed
+24 -33

No files matched your search

+5 -5
View File
@@ -135,17 +135,17 @@ export interface IFileModuleService extends IModuleService {
): Promise<FileDTO>
/**
* This method is used to retrieve a file by ID, similarly to `retrieve`. It doesn't retrieve multiple files, but it's added to support retrieving files with [Query](https://docs.medusajs.com/learn/fundamentals/module-links/query).
* This method is used to list files. It only supports filtering by ID.
*
* @param {FilterableFileProps} filters - The filters to apply on the retrieved files.
* @param {FindConfig<FileDTO>} config -
* The configurations determining how the files are retrieved. Its properties, such as `select` or `relations`, accept the
* attributes or relations associated with a file.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<FileDTO[]>} The list of files. In this case, it will have at most one file.
* @returns {Promise<FileDTO[]>} The list of files.
*
* @example
* const files = await fileModuleService.listFiles({ id: "file_123" })
* const files = await fileModuleService.listFiles({ id: ["file_123", "file_456"] })
*/
listFiles(
filters?: FilterableFileProps,
@@ -154,14 +154,14 @@ export interface IFileModuleService extends IModuleService {
): Promise<FileDTO[]>
/**
* This method is used to retrieve a file by ID, similarly to `retrieve`. It doesn't retrieve multiple files, but it's added to support retrieving files with [Query](https://docs.medusajs.com/learn/fundamentals/module-links/query).
* This method is used to list files and their count. It only supports filtering by ID.
*
* @param {FilterableFileProps} filters - The filters to apply on the retrieved files.
* @param {FindConfig<FileDTO>} config -
* The configurations determining how the files are retrieved. Its properties, such as `select` or `relations`, accept the
* attributes or relations associated with a file.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<[FileDTO[], number]>} The list of files and their count. In this case, it will have at most one file.
* @returns {Promise<[FileDTO[], number]>} The list of files and their count.
*
* @example
* const [files] = await fileModuleService.listAndCountFiles({ id: "file_123" })
@@ -104,28 +104,25 @@ export default class FileModuleService implements FileTypes.IFileModuleService {
config?: FindConfig<FileDTO>,
sharedContext?: Context
): Promise<FileDTO[]> {
const id = Array.isArray(filters?.id) ? filters?.id?.[0] : filters?.id
if (!id) {
if (!filters?.id) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
"Listing of files is only supported when filtering by ID."
)
}
const ids = Array.isArray(filters?.id) ? filters?.id : [filters?.id]
const res = await Promise.all(
ids.map(async (id) => {
const res = await this.fileProviderService_.getPresignedDownloadUrl({
fileKey: id,
})
return { id, url: res }
})
)
if (!res) {
return []
}
return [
{
id,
url: res,
},
]
return res
}
async listAndCountFiles(
@@ -133,31 +130,25 @@ export default class FileModuleService implements FileTypes.IFileModuleService {
config?: FindConfig<FileDTO>,
sharedContext?: Context
): Promise<[FileDTO[], number]> {
const id = Array.isArray(filters?.id) ? filters?.id?.[0] : filters?.id
if (!id) {
if (!filters?.id) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
"Listing and counting of files is only supported when filtering by ID."
"Listing of files is only supported when filtering by ID."
)
}
const ids = Array.isArray(filters?.id) ? filters?.id : [filters?.id]
const res = await Promise.all(
ids.map(async (id) => {
const res = await this.fileProviderService_.getPresignedDownloadUrl({
fileKey: id,
})
return { id, url: res }
})
)
if (!res) {
return [[], 0]
}
return [
[
{
id,
url: res,
},
],
1,
]
return [res, res.length]
}
/**