update prefs safely

This commit is contained in:
Ian Philips 2022-10-10 07:01:44 -06:00
parent 8d06e4b4d2
commit a310963952

View File

@ -1,25 +1,33 @@
import * as admin from 'firebase-admin' import * as admin from 'firebase-admin'
import { initAdmin } from './script-init' import { initAdmin } from './script-init'
import { getAllPrivateUsers } from 'functions/src/utils' import { filterDefined } from 'common/lib/util/array'
import { getPrivateUser } from '../utils'
initAdmin() initAdmin()
const firestore = admin.firestore() const firestore = admin.firestore()
async function main() { async function main() {
const privateUsers = await getAllPrivateUsers() // const privateUsers = await getAllPrivateUsers()
const privateUsers = filterDefined([
await getPrivateUser('ddSo9ALC15N9FAZdKdA2qE3iIvH3'),
])
await Promise.all( await Promise.all(
privateUsers.map((privateUser) => { privateUsers.map((privateUser) => {
if (!privateUser.id) return Promise.resolve() if (!privateUser.id) return Promise.resolve()
return firestore if (privateUser.notificationPreferences.opt_out_all === undefined) {
.collection('private-users') console.log('updating opt out all', privateUser.id)
.doc(privateUser.id) return firestore
.update({ .collection('private-users')
notificationPreferences: { .doc(privateUser.id)
...privateUser.notificationPreferences, .update({
opt_out_all: [], notificationPreferences: {
}, ...privateUser.notificationPreferences,
}) opt_out_all: [],
},
})
}
return
}) })
) )
} }