-
-
Save peleteiro/556d1f6ed9e9555f365c696d7363e1c3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const admin = require('firebase-admin') | |
const {DateTime} = require('luxon') | |
const _ = require('lodash') | |
const fbSource = admin.initializeApp( | |
{ | |
credential: admin.credential.cert(require(`./serviceAccountKey.production.json`)), | |
databaseURL: 'https://biblebox-prod.firebaseio.com', | |
}, | |
'source', | |
) | |
const fbTarget = admin.initializeApp( | |
{ | |
name: 'target', | |
credential: admin.credential.cert(require(`./serviceAccountKey.push.json`)), | |
databaseURL: 'https://biblebox-push.firebaseio.com', | |
}, | |
'target', | |
) | |
const fsSource = fbSource.firestore() | |
const main = async () => { | |
const lastOne = await (async () => { | |
const q = await fsTarget.collection('notification_subscribers').orderBy('createdAt', 'desc').limit(1).get() | |
if (q.size < 1) return | |
const i = await fsSource.collection('notification_subscribers').doc(q.docs[0].id) | |
return await i.get() | |
})() | |
const query = fsSource.collection('notification_subscribers').orderBy('createdAt', 'asc').limit(100) | |
let s = await (lastOne ? query.startAfter(lastOne).get() : query.get()) | |
while (!s.empty) { | |
await messaging.subscribeToTopic(s.docs.map(d => d.id), 'daily') | |
const batch = fsTarget.batch() | |
for (doc of s.docs) { | |
await exportSubscriber(batch, doc.data()) | |
} | |
await batch.commit() | |
s = await query.startAfter(_.last(s.docs)).get() | |
} | |
} | |
const fsTarget = fbTarget.firestore() | |
const messaging = fbTarget.messaging() | |
const increment = admin.firestore.FieldValue.increment(1) | |
const exportSubscriber = async (batch, {uid, token, platform, createdAt, updatedAt}) => { | |
const ref = fsTarget.collection('notification_subscribers').doc(token) | |
// const doc = await ref.get() | |
// if (doc.exists) return | |
const now = DateTime.fromJSDate(createdAt.toDate()) | |
await batch.create(ref, {token, platform, uid, subscribed: true, createdAt, updatedAt}) | |
batch.set( | |
fsTarget.collection('notification_subscribers__counter').doc(now.toFormat('yyyy-MM-dd')), | |
{date: admin.firestore.Timestamp.fromDate(now.startOf('day').toJSDate()), created: increment}, | |
{merge: true}, | |
) | |
batch.set(fsTarget.collection('notification_subscribers__counter').doc('total'), {created: increment}, {merge: true}) | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment