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

This commit is contained in:
Stevche Radevski
2025-05-22 14:52:05 +02:00
committed by GitHub
parent 3c6aa979a6
commit 98798f305c
2 changed files with 34 additions and 43 deletions
+5 -5
View File
@@ -135,17 +135,17 @@ export interface IFileModuleService extends IModuleService {
): Promise<FileDTO> ): 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 {FilterableFileProps} filters - The filters to apply on the retrieved files.
* @param {FindConfig<FileDTO>} config - * @param {FindConfig<FileDTO>} config -
* The configurations determining how the files are retrieved. Its properties, such as `select` or `relations`, accept the * The configurations determining how the files are retrieved. Its properties, such as `select` or `relations`, accept the
* attributes or relations associated with a file. * 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. * @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 * @example
* const files = await fileModuleService.listFiles({ id: "file_123" }) * const files = await fileModuleService.listFiles({ id: ["file_123", "file_456"] })
*/ */
listFiles( listFiles(
filters?: FilterableFileProps, filters?: FilterableFileProps,
@@ -154,14 +154,14 @@ export interface IFileModuleService extends IModuleService {
): Promise<FileDTO[]> ): 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 {FilterableFileProps} filters - The filters to apply on the retrieved files.
* @param {FindConfig<FileDTO>} config - * @param {FindConfig<FileDTO>} config -
* The configurations determining how the files are retrieved. Its properties, such as `select` or `relations`, accept the * The configurations determining how the files are retrieved. Its properties, such as `select` or `relations`, accept the
* attributes or relations associated with a file. * 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. * @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 * @example
* const [files] = await fileModuleService.listAndCountFiles({ id: "file_123" }) * const [files] = await fileModuleService.listAndCountFiles({ id: "file_123" })
@@ -104,28 +104,25 @@ export default class FileModuleService implements FileTypes.IFileModuleService {
config?: FindConfig<FileDTO>, config?: FindConfig<FileDTO>,
sharedContext?: Context sharedContext?: Context
): Promise<FileDTO[]> { ): Promise<FileDTO[]> {
const id = Array.isArray(filters?.id) ? filters?.id?.[0] : filters?.id if (!filters?.id) {
if (!id) {
throw new MedusaError( throw new MedusaError(
MedusaError.Types.INVALID_DATA, MedusaError.Types.INVALID_DATA,
"Listing of files is only supported when filtering by ID." "Listing of files is only supported when filtering by ID."
) )
} }
const res = await this.fileProviderService_.getPresignedDownloadUrl({ const ids = Array.isArray(filters?.id) ? filters?.id : [filters?.id]
fileKey: id,
})
if (!res) { const res = await Promise.all(
return [] ids.map(async (id) => {
} const res = await this.fileProviderService_.getPresignedDownloadUrl({
fileKey: id,
})
return { id, url: res }
})
)
return [ return res
{
id,
url: res,
},
]
} }
async listAndCountFiles( async listAndCountFiles(
@@ -133,31 +130,25 @@ export default class FileModuleService implements FileTypes.IFileModuleService {
config?: FindConfig<FileDTO>, config?: FindConfig<FileDTO>,
sharedContext?: Context sharedContext?: Context
): Promise<[FileDTO[], number]> { ): Promise<[FileDTO[], number]> {
const id = Array.isArray(filters?.id) ? filters?.id?.[0] : filters?.id if (!filters?.id) {
if (!id) {
throw new MedusaError( throw new MedusaError(
MedusaError.Types.INVALID_DATA, 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 res = await this.fileProviderService_.getPresignedDownloadUrl({ const ids = Array.isArray(filters?.id) ? filters?.id : [filters?.id]
fileKey: id,
})
if (!res) { const res = await Promise.all(
return [[], 0] ids.map(async (id) => {
} const res = await this.fileProviderService_.getPresignedDownloadUrl({
fileKey: id,
})
return { id, url: res }
})
)
return [ return [res, res.length]
[
{
id,
url: res,
},
],
1,
]
} }
/** /**