From a31096395231e28a0af26e9589e69405c3837b94 Mon Sep 17 00:00:00 2001 From: Ian Philips Date: Mon, 10 Oct 2022 07:01:44 -0600 Subject: [PATCH] update prefs safely --- .../add-new-notification-preference.ts | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/functions/src/scripts/add-new-notification-preference.ts b/functions/src/scripts/add-new-notification-preference.ts index d7e7072b..a9d3baef 100644 --- a/functions/src/scripts/add-new-notification-preference.ts +++ b/functions/src/scripts/add-new-notification-preference.ts @@ -1,25 +1,33 @@ import * as admin from 'firebase-admin' import { initAdmin } from './script-init' -import { getAllPrivateUsers } from 'functions/src/utils' +import { filterDefined } from 'common/lib/util/array' +import { getPrivateUser } from '../utils' initAdmin() const firestore = admin.firestore() async function main() { - const privateUsers = await getAllPrivateUsers() + // const privateUsers = await getAllPrivateUsers() + const privateUsers = filterDefined([ + await getPrivateUser('ddSo9ALC15N9FAZdKdA2qE3iIvH3'), + ]) await Promise.all( privateUsers.map((privateUser) => { if (!privateUser.id) return Promise.resolve() - return firestore - .collection('private-users') - .doc(privateUser.id) - .update({ - notificationPreferences: { - ...privateUser.notificationPreferences, - opt_out_all: [], - }, - }) + if (privateUser.notificationPreferences.opt_out_all === undefined) { + console.log('updating opt out all', privateUser.id) + return firestore + .collection('private-users') + .doc(privateUser.id) + .update({ + notificationPreferences: { + ...privateUser.notificationPreferences, + opt_out_all: [], + }, + }) + } + return }) ) }