|
| 1 | +import { randomBytes } from 'crypto' |
| 2 | + |
| 3 | +import { DatabaseClient, Pubkey } from '../@types/base' |
| 4 | +import { DBInviteCode, InviteCode } from '../@types/invite-code' |
| 5 | +import { IInviteCodeRepository } from '../@types/repositories' |
| 6 | +import { createLogger } from '../factories/logger-factory' |
| 7 | +import { toBuffer } from '../utils/transform' |
| 8 | + |
| 9 | +const logger = createLogger('invite-code-repository') |
| 10 | + |
| 11 | +export function generateInviteCode(): string { |
| 12 | + return randomBytes(16).toString('hex') |
| 13 | +} |
| 14 | + |
| 15 | +function fromDBInviteCode(row: DBInviteCode): InviteCode { |
| 16 | + return { |
| 17 | + code: row.code, |
| 18 | + createdBy: row.created_by ? row.created_by.toString('hex') : null, |
| 19 | + claimedBy: row.claimed_by ? row.claimed_by.toString('hex') : null, |
| 20 | + expiresAt: row.expires_at, |
| 21 | + maxUses: row.max_uses, |
| 22 | + useCount: row.use_count, |
| 23 | + createdAt: row.created_at, |
| 24 | + updatedAt: row.updated_at, |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +function affectedRows(result: unknown): number { |
| 29 | + if (typeof result === 'number') { return result } |
| 30 | + if (result && typeof (result as any).rowCount === 'number') { return (result as any).rowCount } |
| 31 | + return 0 |
| 32 | +} |
| 33 | + |
| 34 | +export class InviteCodeRepository implements IInviteCodeRepository { |
| 35 | + public constructor(private readonly dbClient: DatabaseClient) {} |
| 36 | + |
| 37 | + public async create( |
| 38 | + code: string, |
| 39 | + expiresAt?: Date, |
| 40 | + maxUses: number = 1, |
| 41 | + client: DatabaseClient = this.dbClient, |
| 42 | + ): Promise<InviteCode> { |
| 43 | + logger('create invite code: %s (expires: %s, maxUses: %d)', code, expiresAt ?? 'never', maxUses) |
| 44 | + |
| 45 | + const now = new Date() |
| 46 | + const row: DBInviteCode = { |
| 47 | + code, |
| 48 | + created_by: null, |
| 49 | + claimed_by: null, |
| 50 | + expires_at: expiresAt ?? null, |
| 51 | + max_uses: maxUses, |
| 52 | + use_count: 0, |
| 53 | + created_at: now, |
| 54 | + updated_at: now, |
| 55 | + } |
| 56 | + |
| 57 | + await client<DBInviteCode>('invite_codes').insert(row) |
| 58 | + |
| 59 | + return fromDBInviteCode(row) |
| 60 | + } |
| 61 | + |
| 62 | + public async findByCode( |
| 63 | + code: string, |
| 64 | + client: DatabaseClient = this.dbClient, |
| 65 | + ): Promise<InviteCode | undefined> { |
| 66 | + logger('find invite code: %s', code) |
| 67 | + |
| 68 | + const [row] = await client<DBInviteCode>('invite_codes') |
| 69 | + .where('code', code) |
| 70 | + .select() |
| 71 | + |
| 72 | + if (!row) { |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + return fromDBInviteCode(row) |
| 77 | + } |
| 78 | + |
| 79 | + // Atomic claim: single UPDATE ensures only one caller wins on a single-use code |
| 80 | + public async claimCode( |
| 81 | + code: string, |
| 82 | + pubkey: Pubkey, |
| 83 | + client: DatabaseClient = this.dbClient, |
| 84 | + ): Promise<boolean> { |
| 85 | + logger('claim invite code %s for %s', code, pubkey) |
| 86 | + |
| 87 | + const now = new Date() |
| 88 | + |
| 89 | + const result = await client<DBInviteCode>('invite_codes') |
| 90 | + .where('code', code) |
| 91 | + .where(function () { |
| 92 | + this.where('max_uses', 0) // 0 = unlimited uses |
| 93 | + .orWhereRaw('use_count < max_uses') |
| 94 | + }) |
| 95 | + .where(function () { |
| 96 | + this.whereNull('expires_at') |
| 97 | + .orWhere('expires_at', '>', now) |
| 98 | + }) |
| 99 | + .update({ |
| 100 | + use_count: client.raw('use_count + 1'), |
| 101 | + claimed_by: toBuffer(pubkey), |
| 102 | + updated_at: now, |
| 103 | + } as any) |
| 104 | + |
| 105 | + return affectedRows(result) > 0 |
| 106 | + } |
| 107 | + |
| 108 | + public async findActiveCodes( |
| 109 | + limit: number = 100, |
| 110 | + client: DatabaseClient = this.dbClient, |
| 111 | + ): Promise<InviteCode[]> { |
| 112 | + logger('find active invite codes (limit %d)', limit) |
| 113 | + |
| 114 | + const now = new Date() |
| 115 | + |
| 116 | + const rows = await client<DBInviteCode>('invite_codes') |
| 117 | + .where(function () { |
| 118 | + this.whereNull('expires_at') |
| 119 | + .orWhere('expires_at', '>', now) |
| 120 | + }) |
| 121 | + .where(function () { |
| 122 | + this.where('max_uses', 0) |
| 123 | + .orWhereRaw('use_count < max_uses') |
| 124 | + }) |
| 125 | + .orderBy('created_at', 'desc') |
| 126 | + .limit(limit) |
| 127 | + .select() |
| 128 | + |
| 129 | + return rows.map(fromDBInviteCode) |
| 130 | + } |
| 131 | + |
| 132 | + public async deleteExpiredCodes( |
| 133 | + client: DatabaseClient = this.dbClient, |
| 134 | + ): Promise<number> { |
| 135 | + logger('delete expired invite codes') |
| 136 | + |
| 137 | + const now = new Date() |
| 138 | + |
| 139 | + const result = await client<DBInviteCode>('invite_codes') |
| 140 | + .whereNotNull('expires_at') |
| 141 | + .where('expires_at', '<=', now) |
| 142 | + .delete() |
| 143 | + |
| 144 | + const count = affectedRows(result) |
| 145 | + logger('deleted %d expired invite codes', count) |
| 146 | + |
| 147 | + return count |
| 148 | + } |
| 149 | +} |
0 commit comments