|
| 1 | +import { getDb } from './db'; |
| 2 | + |
| 3 | +export type NotificationType = 'new_pledge' | 'campaign_funded' | 'refund_available' | 'creator_update'; |
| 4 | + |
| 5 | +export interface Notification { |
| 6 | + id: number; |
| 7 | + campaignId: string; |
| 8 | + type: NotificationType; |
| 9 | + title: string; |
| 10 | + body: string; |
| 11 | + targetWallet: string; |
| 12 | + actorWallet?: string; |
| 13 | + isRead: boolean; |
| 14 | + createdAt: number; |
| 15 | +} |
| 16 | + |
| 17 | +interface NotificationRow { |
| 18 | + id: number; |
| 19 | + campaign_id: string; |
| 20 | + type: NotificationType; |
| 21 | + title: string; |
| 22 | + body: string; |
| 23 | + target_wallet: string; |
| 24 | + actor_wallet: string | null; |
| 25 | + is_read: number; |
| 26 | + created_at: number; |
| 27 | +} |
| 28 | + |
| 29 | +function rowToNotification(row: NotificationRow): Notification { |
| 30 | + return { |
| 31 | + id: row.id, |
| 32 | + campaignId: row.campaign_id, |
| 33 | + type: row.type, |
| 34 | + title: row.title, |
| 35 | + body: row.body, |
| 36 | + targetWallet: row.target_wallet, |
| 37 | + actorWallet: row.actor_wallet ?? undefined, |
| 38 | + isRead: row.is_read === 1, |
| 39 | + createdAt: row.created_at, |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +export function createNotification(params: { |
| 44 | + campaignId: string; |
| 45 | + type: NotificationType; |
| 46 | + title: string; |
| 47 | + body: string; |
| 48 | + targetWallet: string; |
| 49 | + actorWallet?: string; |
| 50 | +}): Notification { |
| 51 | + const db = getDb(); |
| 52 | + const now = Math.floor(Date.now() / 1000); |
| 53 | + const stmt = db.prepare(` |
| 54 | + INSERT INTO notifications (campaign_id, type, title, body, target_wallet, actor_wallet, created_at) |
| 55 | + VALUES (?, ?, ?, ?, ?, ?, ?) |
| 56 | + `); |
| 57 | + const result = stmt.run( |
| 58 | + params.campaignId, |
| 59 | + params.type, |
| 60 | + params.title, |
| 61 | + params.body, |
| 62 | + params.targetWallet, |
| 63 | + params.actorWallet ?? null, |
| 64 | + now, |
| 65 | + ); |
| 66 | + return { |
| 67 | + id: result.lastInsertRowid as number, |
| 68 | + campaignId: params.campaignId, |
| 69 | + type: params.type, |
| 70 | + title: params.title, |
| 71 | + body: params.body, |
| 72 | + targetWallet: params.targetWallet, |
| 73 | + actorWallet: params.actorWallet, |
| 74 | + isRead: false, |
| 75 | + createdAt: now, |
| 76 | + }; |
| 77 | +} |
| 78 | + |
| 79 | +export function listNotifications( |
| 80 | + wallet: string, |
| 81 | + options: { limit?: number; offset?: number } = {}, |
| 82 | +): { data: Notification[]; total: number } { |
| 83 | + const db = getDb(); |
| 84 | + const limit = options.limit ?? 50; |
| 85 | + const offset = options.offset ?? 0; |
| 86 | + |
| 87 | + const countRow = db |
| 88 | + .prepare('SELECT COUNT(*) as total FROM notifications WHERE target_wallet = ?') |
| 89 | + .get(wallet) as { total: number }; |
| 90 | + |
| 91 | + const rows = db |
| 92 | + .prepare( |
| 93 | + 'SELECT * FROM notifications WHERE target_wallet = ? ORDER BY created_at DESC LIMIT ? OFFSET ?', |
| 94 | + ) |
| 95 | + .all(wallet, limit, offset) as NotificationRow[]; |
| 96 | + |
| 97 | + return { |
| 98 | + data: rows.map(rowToNotification), |
| 99 | + total: countRow.total, |
| 100 | + }; |
| 101 | +} |
| 102 | + |
| 103 | +export function getUnreadCount(wallet: string): number { |
| 104 | + const db = getDb(); |
| 105 | + const row = db |
| 106 | + .prepare( |
| 107 | + 'SELECT COUNT(*) as count FROM notifications WHERE target_wallet = ? AND is_read = 0', |
| 108 | + ) |
| 109 | + .get(wallet) as { count: number }; |
| 110 | + return row.count; |
| 111 | +} |
| 112 | + |
| 113 | +export function markAllRead(wallet: string): void { |
| 114 | + const db = getDb(); |
| 115 | + db.prepare('UPDATE notifications SET is_read = 1 WHERE target_wallet = ? AND is_read = 0').run( |
| 116 | + wallet, |
| 117 | + ); |
| 118 | +} |
| 119 | + |
| 120 | +export function getContributorsForCampaign(campaignId: string): string[] { |
| 121 | + const db = getDb(); |
| 122 | + const rows = db |
| 123 | + .prepare('SELECT DISTINCT contributor FROM pledges WHERE campaign_id = ? AND refunded_at IS NULL') |
| 124 | + .all(campaignId) as { contributor: string }[]; |
| 125 | + return rows.map((r) => r.contributor); |
| 126 | +} |
0 commit comments