(set, get)
| 41 | }; |
| 42 | |
| 43 | export const createGroupSlice: AppSliceCreator<GroupSlice> = (set, get) => ({ |
| 44 | groupsByName: {}, |
| 45 | groupRequests: {}, |
| 46 | groupErrorsByName: {}, |
| 47 | |
| 48 | listGroups: async ({ pageSize, pageToken, filter }) => { |
| 49 | if (!get().hasWorkspacePermission("bb.groups.list")) { |
| 50 | return { groups: [], nextPageToken: "" }; |
| 51 | } |
| 52 | const response = await groupServiceClientConnect.listGroups( |
| 53 | createProto(ListGroupsRequestSchema, { |
| 54 | pageSize, |
| 55 | pageToken, |
| 56 | filter: buildGroupFilter(filter ?? {}), |
| 57 | }), |
| 58 | { contextValues: createContextValues().set(silentContextKey, true) } |
| 59 | ); |
| 60 | set((state) => ({ |
| 61 | groupsByName: { |
| 62 | ...state.groupsByName, |
| 63 | ...Object.fromEntries( |
| 64 | response.groups.map((group) => [group.name, group]) |
| 65 | ), |
| 66 | }, |
| 67 | })); |
| 68 | return { groups: response.groups, nextPageToken: response.nextPageToken }; |
| 69 | }, |
| 70 | |
| 71 | batchFetchGroups: async (names) => { |
| 72 | const validNames = uniq(names).filter(Boolean).map(ensureGroupIdentifier); |
| 73 | if (validNames.length === 0) return []; |
| 74 | const response = await groupServiceClientConnect.batchGetGroups( |
| 75 | createProto(BatchGetGroupsRequestSchema, { names: validNames }), |
| 76 | { contextValues: createContextValues().set(silentContextKey, true) } |
| 77 | ); |
| 78 | set((state) => ({ |
| 79 | groupsByName: { |
| 80 | ...state.groupsByName, |
| 81 | ...Object.fromEntries( |
| 82 | response.groups.map((group) => [group.name, group]) |
| 83 | ), |
| 84 | }, |
| 85 | })); |
| 86 | return response.groups; |
| 87 | }, |
| 88 | |
| 89 | batchGetOrFetchGroups: async (names) => { |
| 90 | const validNames = uniq(names).filter(Boolean).map(ensureGroupIdentifier); |
| 91 | const missing = validNames.filter((name) => !get().groupsByName[name]); |
| 92 | if (missing.length > 0) { |
| 93 | await get().batchFetchGroups(missing); |
| 94 | } |
| 95 | return validNames.map((name) => get().groupsByName[name]); |
| 96 | }, |
| 97 | |
| 98 | fetchGroup: async (id) => { |
| 99 | if (!get().hasWorkspacePermission("bb.groups.get")) return undefined; |
| 100 | const name = ensureGroupIdentifier(id); |
no test coverage detected