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

@@ -0,0 +1,8 @@
/*
Warnings:
- A unique constraint covering the columns `[loggedCallId,name]` on the table `LoggedCallTag` will be added. If there are existing duplicate values, this will fail.
*/
-- CreateIndex
CREATE UNIQUE INDEX "LoggedCallTag_loggedCallId_name_key" ON "LoggedCallTag"("loggedCallId", "name");

View File

@@ -331,6 +331,7 @@ model LoggedCallTag {
loggedCallId String @db.Uuid
loggedCall LoggedCall @relation(fields: [loggedCallId], references: [id], onDelete: Cascade)
@@unique([loggedCallId, name])
@@index([projectId, name])
@@index([projectId, name, value])
}

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;