Advertisement
rezk2ll

Untitled

Mar 21st, 2023
696
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.    * Compute the Access Information for the workspace folder to be created.
  3.    *
  4.    * @param {Workspace} workspace - the target workspace
  5.    * @param {Company} company - the target company
  6.    * @param {WorkspaceExecutionContext} context - the execution context
  7.    * @returns {Promise<AccessInformation>}
  8.    */
  9.   private getWorkspaceAccess = async (
  10.     workspace: Workspace,
  11.     company: Company,
  12.     context: WorkspaceExecutionContext,
  13.   ): Promise<AccessInformation> => {
  14.     const companyUsersCount = await globalResolver.services.companies.getUsersCount(company.id);
  15.     const workspaceUsersCount = await globalResolver.services.workspaces.getUsersCount(
  16.       workspace.id,
  17.     );
  18.  
  19.     if (companyUsersCount === workspaceUsersCount) {
  20.       return {
  21.         entities: [
  22.           {
  23.             id: "parent",
  24.             type: "folder",
  25.             level: "manage",
  26.           },
  27.           {
  28.             id: company.id,
  29.             type: "company",
  30.             level: "none",
  31.           },
  32.           {
  33.             id: context.user?.id,
  34.             type: "user",
  35.             level: "manage",
  36.           },
  37.         ],
  38.         public: {
  39.           level: "none",
  40.           token: generateAccessToken(),
  41.         },
  42.       };
  43.     }
  44.  
  45.     let workspaceUsers: WorkspaceUser[] = [];
  46.     let wsUsersPagination: Pagination = { limitStr: "100" };
  47.  
  48.     do {
  49.       const wsUsersQuery = await globalResolver.services.workspaces.getUsers(
  50.         { workspaceId: workspace.id },
  51.         wsUsersPagination,
  52.         context,
  53.       );
  54.       wsUsersPagination = wsUsersQuery.nextPage as Pagination;
  55.  
  56.       workspaceUsers = [...workspaceUsers, ...wsUsersQuery.getEntities()];
  57.     } while (wsUsersPagination.page_token);
  58.  
  59.     if (companyUsersCount < 30 || workspaceUsersCount < 30) {
  60.       return {
  61.         entities: [
  62.           {
  63.             id: "parent",
  64.             type: "folder",
  65.             level: "none",
  66.           },
  67.           {
  68.             id: company.id,
  69.             type: "company",
  70.             level: "none",
  71.           },
  72.           {
  73.             id: context.user?.id,
  74.             type: "user",
  75.             level: "manage",
  76.           },
  77.           ...workspaceUsers.reduce((acc, curr) => {
  78.             acc = [
  79.               ...acc,
  80.               {
  81.                 id: curr.userId,
  82.                 type: "user",
  83.                 level: "manage",
  84.               },
  85.             ];
  86.  
  87.             return acc;
  88.           }, []),
  89.         ],
  90.         public: {
  91.           level: "none",
  92.           token: generateAccessToken(),
  93.         },
  94.       };
  95.     }
  96.  
  97.     let companyUsers: CompanyUser[] = [];
  98.     let companyUsersPaginations: Pagination = { limitStr: "100" };
  99.     do {
  100.       const companyUsersQuery = await globalResolver.services.companies.getUsers(
  101.         { group_id: company.id },
  102.         companyUsersPaginations,
  103.         {},
  104.         context,
  105.       );
  106.       companyUsersPaginations = companyUsersQuery.nextPage as Pagination;
  107.       companyUsers = [...companyUsers, ...companyUsersQuery.getEntities()];
  108.     } while (companyUsersPaginations.page_token);
  109.     return {
  110.       entities: [
  111.         {
  112.           id: "parent",
  113.           type: "folder",
  114.           level: "none",
  115.         },
  116.         {
  117.           id: company.id,
  118.           type: "company",
  119.           level: "manage",
  120.         },
  121.         {
  122.           id: context.user?.id,
  123.           type: "user",
  124.           level: "manage",
  125.         },
  126.         ...companyUsers.reduce((acc, curr) => {
  127.           if (workspaceUsers.find(({ userId }) => curr.user_id === userId)) {
  128.             return acc;
  129.           }
  130.  
  131.           acc = [
  132.             ...acc,
  133.             {
  134.               id: curr.user_id,
  135.               type: "user",
  136.               level: "none",
  137.             },
  138.           ];
  139.  
  140.           return acc;
  141.         }, []),
  142.       ],
  143.       public: {
  144.         level: "none",
  145.         token: generateAccessToken(),
  146.       },
  147.     };
  148.   };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement