|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * One-off script: subscribes all device tokens stored in deviceTopics to their |
| 4 | + * respective FCM topics via the Firebase Admin SDK. |
| 5 | + * |
| 6 | + * Run AFTER the migration `20260317125624-subscribe-device-tokens-to-broadcast` has |
| 7 | + * been applied to populate the deviceTopics field. |
| 8 | + * |
| 9 | + * Required env vars: |
| 10 | + * MONGODB_CON e.g. mongodb://localhost/galoy |
| 11 | + * GOOGLE_APPLICATION_CREDENTIALS path to Firebase service account JSON |
| 12 | + * FCM_TOPIC_PREFIX (optional) e.g. "test" → topic becomes "test-broadcast" |
| 13 | + * omit on prod → topic is "broadcast" |
| 14 | + */ |
| 15 | + |
| 16 | +const { MongoClient } = require("mongodb") |
| 17 | +const admin = require("firebase-admin") |
| 18 | + |
| 19 | +const BATCH_SIZE = 1000 |
| 20 | + |
| 21 | +const MONGODB_CON = process.env.MONGODB_CON |
| 22 | +if (!MONGODB_CON) { |
| 23 | + console.error("Error: MONGODB_CON environment variable is required") |
| 24 | + process.exit(1) |
| 25 | +} |
| 26 | + |
| 27 | +if (!process.env.GOOGLE_APPLICATION_CREDENTIALS) { |
| 28 | + console.error("Error: GOOGLE_APPLICATION_CREDENTIALS environment variable is required") |
| 29 | + process.exit(1) |
| 30 | +} |
| 31 | + |
| 32 | +admin.initializeApp({ credential: admin.credential.applicationDefault() }) |
| 33 | +const messaging = admin.messaging() |
| 34 | + |
| 35 | +async function subscribeInBatches(tokens, topic) { |
| 36 | + let successCount = 0 |
| 37 | + let failureCount = 0 |
| 38 | + |
| 39 | + for (let i = 0; i < tokens.length; i += BATCH_SIZE) { |
| 40 | + const batch = tokens.slice(i, i + BATCH_SIZE) |
| 41 | + const batchNum = Math.floor(i / BATCH_SIZE) + 1 |
| 42 | + console.log( |
| 43 | + `Subscribing batch ${batchNum} (tokens ${i + 1}–${i + batch.length}) to topic "${topic}"`, |
| 44 | + ) |
| 45 | + |
| 46 | + const response = await messaging.subscribeToTopic(batch, topic) |
| 47 | + successCount += response.successCount |
| 48 | + failureCount += response.failureCount |
| 49 | + |
| 50 | + if (response.errors.length > 0) { |
| 51 | + response.errors.forEach(({ index, error }) => { |
| 52 | + console.warn(` Token[${index}] failed: ${error.message}`) |
| 53 | + }) |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + return { successCount, failureCount } |
| 58 | +} |
| 59 | + |
| 60 | +async function main() { |
| 61 | + const client = new MongoClient(MONGODB_CON) |
| 62 | + |
| 63 | + try { |
| 64 | + await client.connect() |
| 65 | + const db = client.db() |
| 66 | + |
| 67 | + const users = await db |
| 68 | + .collection("users") |
| 69 | + .find( |
| 70 | + { deviceTopics: { $exists: true } }, |
| 71 | + { projection: { _id: 0, deviceTopics: 1 } }, |
| 72 | + ) |
| 73 | + .toArray() |
| 74 | + |
| 75 | + if (users.length === 0) { |
| 76 | + console.log("No users with deviceTopics found — run the migration first") |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + // Group tokens by topic |
| 81 | + const tokensByTopic = {} |
| 82 | + for (const user of users) { |
| 83 | + for (const [token, topics] of Object.entries(user.deviceTopics)) { |
| 84 | + for (const topic of topics) { |
| 85 | + if (!tokensByTopic[topic]) tokensByTopic[topic] = [] |
| 86 | + tokensByTopic[topic].push(token) |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + for (const [topic, tokens] of Object.entries(tokensByTopic)) { |
| 92 | + console.log(`\nSubscribing ${tokens.length} tokens to topic "${topic}"`) |
| 93 | + const { successCount, failureCount } = await subscribeInBatches(tokens, topic) |
| 94 | + console.log(`Done — success: ${successCount}, failures: ${failureCount}`) |
| 95 | + } |
| 96 | + } finally { |
| 97 | + await client.close() |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +main().catch((err) => { |
| 102 | + console.error(err) |
| 103 | + process.exit(1) |
| 104 | +}) |
0 commit comments