|
| 1 | +import { AVATAR_INFO } from "@flanksource-ui/constants"; |
| 2 | +import { IncidentCommander } from "../axios"; |
| 3 | +import { resolvePostGrestRequestWithPagination } from "../resolve"; |
| 4 | +import { PermissionAPIResponse } from "../types/permissions"; |
| 5 | + |
| 6 | +export type FetchPermissionsInput = { |
| 7 | + componentId?: string; |
| 8 | + personId?: string; |
| 9 | + teamId?: string; |
| 10 | + configId?: string; |
| 11 | + checkId?: string; |
| 12 | + canaryId?: string; |
| 13 | + playbookId?: string; |
| 14 | +}; |
| 15 | + |
| 16 | +function composeQueryParamForFetchPermissions({ |
| 17 | + componentId, |
| 18 | + personId, |
| 19 | + teamId, |
| 20 | + configId, |
| 21 | + checkId, |
| 22 | + canaryId, |
| 23 | + playbookId |
| 24 | +}: FetchPermissionsInput) { |
| 25 | + if (componentId) { |
| 26 | + return `component_id=eq.${componentId}`; |
| 27 | + } |
| 28 | + if (personId) { |
| 29 | + return `person_id=eq.${personId}`; |
| 30 | + } |
| 31 | + if (teamId) { |
| 32 | + return `team_id=eq.${teamId}`; |
| 33 | + } |
| 34 | + if (configId) { |
| 35 | + return `config_id=eq.${configId}`; |
| 36 | + } |
| 37 | + if (checkId) { |
| 38 | + return `check_id=eq.${checkId}`; |
| 39 | + } |
| 40 | + if (canaryId) { |
| 41 | + return `canary_id=eq.${canaryId}`; |
| 42 | + } |
| 43 | + if (playbookId) { |
| 44 | + return `playbook_id=eq.${playbookId}`; |
| 45 | + } |
| 46 | + return undefined; |
| 47 | +} |
| 48 | + |
| 49 | +export function fetchPermissions( |
| 50 | + input: FetchPermissionsInput, |
| 51 | + pagination: { |
| 52 | + pageSize: number; |
| 53 | + pageIndex: number; |
| 54 | + } |
| 55 | +) { |
| 56 | + const queryParam = composeQueryParamForFetchPermissions(input); |
| 57 | + const selectFields = [ |
| 58 | + "*", |
| 59 | + "checks:check_id(id, name, status, type)", |
| 60 | + "catalog:config_id(id, name, type, config_class)", |
| 61 | + "component:component_id(id, name, icon)", |
| 62 | + "canary:canary_id(id, name, icon)", |
| 63 | + "playbook:playbook_id(id, title, name, icon)", |
| 64 | + "team:team_id(id, name, icon)", |
| 65 | + `person:person_id(${AVATAR_INFO})`, |
| 66 | + `createdBy:created_by(${AVATAR_INFO})` |
| 67 | + ]; |
| 68 | + |
| 69 | + const { pageSize, pageIndex } = pagination; |
| 70 | + |
| 71 | + const url = `/permissions?${queryParam}&select=${selectFields.join(",")}&limit=${pageSize}&offset=${pageIndex * pageSize}`; |
| 72 | + return resolvePostGrestRequestWithPagination( |
| 73 | + IncidentCommander.get<PermissionAPIResponse[]>(url) |
| 74 | + ); |
| 75 | +} |
0 commit comments