diff --git a/packages/api/src/controllers/helpers.test.ts b/packages/api/src/controllers/helpers.test.ts index c5d065b3e..7adc81662 100644 --- a/packages/api/src/controllers/helpers.test.ts +++ b/packages/api/src/controllers/helpers.test.ts @@ -255,6 +255,11 @@ describe("convert w3 storage to object store URL", () => { defaultProjectId: "defaultProject1", }, }; + const mockAsset3 = { + id: "asset3", + userId: "test", + projectId: undefined, + }; const assetList = [mockAsset, mockAsset2]; @@ -281,6 +286,9 @@ describe("convert w3 storage to object store URL", () => { // nonAdmin + multipleAssets result = await addDefaultProjectId(assetList, mockReq, mockRes); expect(result[0].projectId).toBe("defaultProject1"); + // nonAdmin + missing user subitem + result = await addDefaultProjectId(mockAsset3, mockReq, mockRes); + expect(result.projectId).toBe("defaultProject1"); mockReq.user.admin = true; mockReq.user.defaultProjectId = "adminProjectId"; // admin + singleAsset diff --git a/packages/api/src/controllers/helpers.ts b/packages/api/src/controllers/helpers.ts index ebb509d8b..c0815ef82 100644 --- a/packages/api/src/controllers/helpers.ts +++ b/packages/api/src/controllers/helpers.ts @@ -771,45 +771,52 @@ export async function addDefaultProjectId( } }; - const enrichResponseWithUserProjectId = (document) => { - if ("id" in document && "userId" in document && "user" in document) { - if ( - (!document.projectId || document.projectId === "") && - document.user?.defaultProjectId - ) { - document.projectId = document.user.defaultProjectId; + const enrichResponseWithUserProjectId = async (document) => { + if ("id" in document && "userId" in document) { + if (!document.projectId) { + if (document.user) { + document.projectId = document.user.defaultProjectId; + } else { + let user = + document.userId === req.user.id + ? req.user + : await db.user.get(document.userId, { useCache: true }); + if (user.defaultProjectId) { + document.projectId = user.defaultProjectId; + } + } } } }; const clonedBody = deepClone(body); - const processItem = (item) => { + const processItem = async (item) => { if (typeof item === "object" && item !== null) { if (req.user.admin) { - enrichResponseWithUserProjectId(item); + await enrichResponseWithUserProjectId(item); } else { enrichResponse(item); } - Object.values(item).forEach((subItem) => { - if (typeof subItem === "object" && subItem !== null) { - if (req.user.admin) { - enrichResponseWithUserProjectId(subItem); - } else { - enrichResponse(subItem); + await Promise.all( + Object.values(item).map(async (subItem) => { + if (typeof subItem === "object" && subItem !== null) { + if (req.user.admin) { + await enrichResponseWithUserProjectId(subItem); + } else { + enrichResponse(subItem); + } } - } - }); + }), + ); } }; if (Array.isArray(clonedBody)) { - clonedBody.forEach((item) => { - processItem(item); - }); + await Promise.all(clonedBody.map((item) => processItem(item))); } else if (typeof clonedBody === "object" && clonedBody !== null) { - processItem(clonedBody); + await processItem(clonedBody); } return clonedBody; diff --git a/packages/api/src/controllers/stream.test.ts b/packages/api/src/controllers/stream.test.ts index 53992e9ae..a2897bb35 100644 --- a/packages/api/src/controllers/stream.test.ts +++ b/packages/api/src/controllers/stream.test.ts @@ -131,7 +131,8 @@ describe("controllers/stream", () => { } = await setupUsers(server, mockAdminUser, mockNonAdminUser)); client.jwtAuth = adminToken; - projectId = await createProject(client); + let project = await createProject(client); + projectId = project.id; expect(projectId).toBeDefined(); });