Skip to content

Commit

Permalink
api: projects: fetch user if missing subitem (#2239)
Browse files Browse the repository at this point in the history
* api: projects: fetch user if missing subitem

* address comments

* address commet

* added test case

* fix tests
  • Loading branch information
gioelecerati authored Jul 1, 2024
1 parent 878bece commit 398fa30
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
8 changes: 8 additions & 0 deletions packages/api/src/controllers/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand All @@ -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
Expand Down
49 changes: 28 additions & 21 deletions packages/api/src/controllers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion packages/api/src/controllers/stream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand Down

0 comments on commit 398fa30

Please sign in to comment.