Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Compute the Access Information for the workspace folder to be created.
- *
- * @param {Workspace} workspace - the target workspace
- * @param {Company} company - the target company
- * @param {WorkspaceExecutionContext} context - the execution context
- * @returns {Promise<AccessInformation>}
- */
- private getWorkspaceAccess = async (
- workspace: Workspace,
- company: Company,
- context: WorkspaceExecutionContext,
- ): Promise<AccessInformation> => {
- const companyUsersCount = await globalResolver.services.companies.getUsersCount(company.id);
- const workspaceUsersCount = await globalResolver.services.workspaces.getUsersCount(
- workspace.id,
- );
- if (companyUsersCount === workspaceUsersCount) {
- return {
- entities: [
- {
- id: "parent",
- type: "folder",
- level: "manage",
- },
- {
- id: company.id,
- type: "company",
- level: "none",
- },
- {
- id: context.user?.id,
- type: "user",
- level: "manage",
- },
- ],
- public: {
- level: "none",
- token: generateAccessToken(),
- },
- };
- }
- let workspaceUsers: WorkspaceUser[] = [];
- let wsUsersPagination: Pagination = { limitStr: "100" };
- do {
- const wsUsersQuery = await globalResolver.services.workspaces.getUsers(
- { workspaceId: workspace.id },
- wsUsersPagination,
- context,
- );
- wsUsersPagination = wsUsersQuery.nextPage as Pagination;
- workspaceUsers = [...workspaceUsers, ...wsUsersQuery.getEntities()];
- } while (wsUsersPagination.page_token);
- if (companyUsersCount < 30 || workspaceUsersCount < 30) {
- return {
- entities: [
- {
- id: "parent",
- type: "folder",
- level: "none",
- },
- {
- id: company.id,
- type: "company",
- level: "none",
- },
- {
- id: context.user?.id,
- type: "user",
- level: "manage",
- },
- ...workspaceUsers.reduce((acc, curr) => {
- acc = [
- ...acc,
- {
- id: curr.userId,
- type: "user",
- level: "manage",
- },
- ];
- return acc;
- }, []),
- ],
- public: {
- level: "none",
- token: generateAccessToken(),
- },
- };
- }
- let companyUsers: CompanyUser[] = [];
- let companyUsersPaginations: Pagination = { limitStr: "100" };
- do {
- const companyUsersQuery = await globalResolver.services.companies.getUsers(
- { group_id: company.id },
- companyUsersPaginations,
- {},
- context,
- );
- companyUsersPaginations = companyUsersQuery.nextPage as Pagination;
- companyUsers = [...companyUsers, ...companyUsersQuery.getEntities()];
- } while (companyUsersPaginations.page_token);
- return {
- entities: [
- {
- id: "parent",
- type: "folder",
- level: "none",
- },
- {
- id: company.id,
- type: "company",
- level: "manage",
- },
- {
- id: context.user?.id,
- type: "user",
- level: "manage",
- },
- ...companyUsers.reduce((acc, curr) => {
- if (workspaceUsers.find(({ userId }) => curr.user_id === userId)) {
- return acc;
- }
- acc = [
- ...acc,
- {
- id: curr.user_id,
- type: "user",
- level: "none",
- },
- ];
- return acc;
- }, []),
- ],
- public: {
- level: "none",
- token: generateAccessToken(),
- },
- };
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement