|
1 | 1 | 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"; |
3 | 3 | import { SQLiteColumn } from "drizzle-orm/sqlite-core"; |
4 | 4 |
|
5 | 5 | export function toUndef(v: string | null | undefined): string | undefined { |
@@ -28,42 +28,42 @@ export function queryCondition( |
28 | 28 | readonly queryProvider: SQLiteColumn; |
29 | 29 | }, |
30 | 30 | ) { |
| 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 | + |
31 | 35 | if (query.existingUserId) { |
32 | | - return eq(table.userId, query.existingUserId); |
| 36 | + conditions.push(eq(table.userId, query.existingUserId)); |
33 | 37 | } |
34 | 38 |
|
35 | 39 | const str = query.byString?.trim(); |
36 | 40 | if (str) { |
37 | 41 | const byEmail = queryEmail(str); |
38 | 42 | const byNick = queryNick(str); |
39 | | - const conditions = [] as ReturnType<typeof or>[]; |
40 | 43 | if (byEmail) { |
41 | 44 | conditions.push(eq(table.queryEmail, byEmail)); |
42 | 45 | } |
43 | 46 | if (byNick) { |
44 | 47 | conditions.push(eq(table.queryNick, byNick)); |
45 | 48 | } |
46 | 49 | conditions.push(eq(table.userId, str)); |
47 | | - return or(...conditions); |
48 | 50 | } |
49 | 51 |
|
50 | 52 | const byEmail = queryEmail(query.byEmail); |
51 | 53 | 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)); |
65 | 59 | } |
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); |
67 | 67 | } |
68 | 68 |
|
69 | 69 | export function queryNick(nick?: string): string | undefined { |
|
0 commit comments