( client: Client, code: string, )
| 123 | |
| 124 | // A code is redeemable when it exists, is unused, and is unexpired. |
| 125 | export const findRedeemableCode = async ( |
| 126 | client: Client, |
| 127 | code: string, |
| 128 | ): Promise<InviteCodeRow | null> => { |
| 129 | const result = await client.execute({ |
| 130 | sql: "SELECT * FROM invite_code WHERE code = ? AND used_at IS NULL", |
| 131 | args: [code.trim().toUpperCase()], |
| 132 | }); |
| 133 | const raw = result.rows[0]; |
| 134 | if (!raw) return null; |
| 135 | const row = toRow(raw); |
| 136 | if (row.expiresAt && Date.parse(row.expiresAt) < Date.now()) return null; |
| 137 | return row; |
| 138 | }; |
| 139 | |
| 140 | // Mark a code consumed. The `used_at IS NULL` guard makes this the single-use |
| 141 | // gate even under a race: rowsAffected === 0 means someone redeemed it first. |
no test coverage detected