Skip to content

Commit

Permalink
refactor: update how we handle search params for postgrest for playbooks
Browse files Browse the repository at this point in the history
  • Loading branch information
mainawycliffe authored and moshloop committed Oct 16, 2024
1 parent fecfffc commit 59490f0
Showing 1 changed file with 32 additions and 20 deletions.
52 changes: 32 additions & 20 deletions src/api/services/playbooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,34 +161,46 @@ export async function getPlaybookRuns({
starts?: string;
ends?: string;
}) {
const componentParamString = componentId
? `&component_id=eq.${componentId}`
: "";
const searchParams = new URLSearchParams();

const configParamString = configId ? `&config_id=eq.${configId}` : "";
if (componentId) {
searchParams.append("component_id", `eq.${componentId}`);
}

if (configId) {
searchParams.append("config_id", `eq.${configId}`);
}

const statusParamString = status ? `&status=eq.${status}` : "";
if (status) {
searchParams.append("status", `eq.${status}`);
}

searchParams.append("limit", pageSize.toString());
searchParams.append("offset", (pageIndex * pageSize).toString());
searchParams.append("order", "created_at.desc");

if (playbookId) {
searchParams.append("playbook_id", `eq.${playbookId}`);
}

const pagingParams = `&limit=${pageSize}&offset=${pageIndex * pageSize}`;
const select = [
"*",
"playbooks(id,name,title,spec,icon)",
"component:components(id,name,icon)",
"check:checks(id,name,icon)",
"config:config_items(id,name,type,config_class)"
].join(",");

const dateFilter =
starts && ends
? `&and=(start_time.gte.${starts},start_time.lte.${ends})`
: "";
searchParams.append("select", select);

const playbookParamsString = playbookId
? `&playbook_id=eq.${playbookId}`
: "";
const queryString = searchParams.toString();

const res = await resolvePostGrestRequestWithPagination(
ConfigDB.get<PlaybookRun[] | null>(
`/playbook_runs?select=*,playbooks(id,name,title,spec,icon),component:components(id,name,icon),check:checks(id,name,icon),config:config_items(id,name,type,config_class)&&order=created_at.desc${playbookParamsString}${componentParamString}&${configParamString}${pagingParams}${statusParamString}${dateFilter}`,
{
headers: {
Prefer: "count=exact"
}
ConfigDB.get<PlaybookRun[] | null>(`/playbook_runs?${queryString}`, {
headers: {
Prefer: "count=exact"
}
)
})
);
return res;
}

0 comments on commit 59490f0

Please sign in to comment.