Filter by tags

This commit is contained in:
David Corbitt
2023-08-15 01:50:48 -07:00
parent 7003595e76
commit 890a738568
3 changed files with 32 additions and 2 deletions

View File

@@ -90,7 +90,28 @@ export const loggedCallsRouter = createTRPCRouter({
return eb.and(wheres);
});
const rawCalls = await baseQuery
const tagFilters = input.filters.filter(
(filter) => !defaultFilterableFields.includes(filter.field),
);
let updatedBaseQuery = baseQuery;
for (let i = 0; i < tagFilters.length; i++) {
const filter = tagFilters[i];
if (!filter?.value) continue;
const tableAlias = `lct${i}`;
updatedBaseQuery = updatedBaseQuery
.leftJoin(`LoggedCallTag as ${tableAlias}`, (join) =>
join
.onRef("lc.id", "=", `${tableAlias}.loggedCallId`)
.on(`${tableAlias}.name`, "=", filter.field),
)
.where(
sql.raw(`${tableAlias}.value ${comparatorToSqlValue(filter.comparator, filter.value)}`),
) as unknown as typeof baseQuery;
}
const rawCalls = await updatedBaseQuery
.select([
"lc.id as id",
"lc.requestedAt as requestedAt",
@@ -129,7 +150,7 @@ export const loggedCallsRouter = createTRPCRouter({
},
}));
const matchingLogIds = await baseQuery.select(["lc.id"]).execute();
const matchingLogIds = await updatedBaseQuery.select(["lc.id"]).execute();
const count = matchingLogIds.length;