diff --git a/app/prisma/migrations/20230815084047_make_logged_call_tags_unique_on_logged_call_id_and_name/migration.sql b/app/prisma/migrations/20230815084047_make_logged_call_tags_unique_on_logged_call_id_and_name/migration.sql new file mode 100644 index 0000000..4652e62 --- /dev/null +++ b/app/prisma/migrations/20230815084047_make_logged_call_tags_unique_on_logged_call_id_and_name/migration.sql @@ -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"); diff --git a/app/prisma/schema.prisma b/app/prisma/schema.prisma index c207cbe..cadbb2c 100644 --- a/app/prisma/schema.prisma +++ b/app/prisma/schema.prisma @@ -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]) } diff --git a/app/src/server/api/routers/loggedCalls.router.ts b/app/src/server/api/routers/loggedCalls.router.ts index d23f3c1..9b0efd1 100644 --- a/app/src/server/api/routers/loggedCalls.router.ts +++ b/app/src/server/api/routers/loggedCalls.router.ts @@ -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;