Skip to content

Commit ae60716

Browse files
jchrisclaude
andcommitted
fix: queryCondition now matches by email OR userId
Previously when existingUserId was provided, it only searched by userId, ignoring email. This meant invites created by email weren't found when the invited user already existed. Now we OR together all conditions. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent f60369e commit ae60716

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

dashboard/backend/sql/sql-helper.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Queryable, QueryUser } from "@fireproof/core-protocols-dashboard";
2-
import { and, eq, or } from "drizzle-orm/sql/expressions";
2+
import { eq, or } from "drizzle-orm/sql/expressions";
33
import { SQLiteColumn } from "drizzle-orm/sqlite-core";
44

55
export function toUndef(v: string | null | undefined): string | undefined {
@@ -28,42 +28,42 @@ export function queryCondition(
2828
readonly queryProvider: SQLiteColumn;
2929
},
3030
) {
31+
// Build conditions for matching by userId, email, or nick
32+
// We OR together all possible matches so invites can be found by email even when userId is provided
33+
const conditions = [] as ReturnType<typeof or>[];
34+
3135
if (query.existingUserId) {
32-
return eq(table.userId, query.existingUserId);
36+
conditions.push(eq(table.userId, query.existingUserId));
3337
}
3438

3539
const str = query.byString?.trim();
3640
if (str) {
3741
const byEmail = queryEmail(str);
3842
const byNick = queryNick(str);
39-
const conditions = [] as ReturnType<typeof or>[];
4043
if (byEmail) {
4144
conditions.push(eq(table.queryEmail, byEmail));
4245
}
4346
if (byNick) {
4447
conditions.push(eq(table.queryNick, byNick));
4548
}
4649
conditions.push(eq(table.userId, str));
47-
return or(...conditions);
4850
}
4951

5052
const byEmail = queryEmail(query.byEmail);
5153
const byNick = queryNick(query.byNick);
52-
let where: ReturnType<typeof and> = eq(table.userId, Math.random() + "");
53-
if (byEmail && byNick && query.andProvider) {
54-
where = and(eq(table.queryEmail, byEmail), eq(table.queryNick, byNick), eq(table.queryProvider, query.andProvider));
55-
} else if (byEmail && byNick) {
56-
where = and(eq(table.queryEmail, byEmail), eq(table.queryNick, byNick));
57-
} else if (byEmail && query.andProvider) {
58-
where = and(eq(table.queryEmail, byEmail), eq(table.queryProvider, query.andProvider));
59-
} else if (byNick && query.andProvider) {
60-
where = and(eq(table.queryNick, byNick), eq(table.queryProvider, query.andProvider));
61-
} else if (byEmail) {
62-
where = eq(table.queryEmail, byEmail);
63-
} else if (byNick) {
64-
where = eq(table.queryNick, byNick);
54+
if (byEmail) {
55+
conditions.push(eq(table.queryEmail, byEmail));
56+
}
57+
if (byNick) {
58+
conditions.push(eq(table.queryNick, byNick));
6559
}
66-
return where;
60+
61+
if (conditions.length === 0) {
62+
// No valid conditions - return a condition that matches nothing
63+
return eq(table.userId, Math.random() + "");
64+
}
65+
66+
return or(...conditions);
6767
}
6868

6969
export function queryNick(nick?: string): string | undefined {

0 commit comments

Comments
 (0)