From 3e25faf74c8418b740adc7d03ebb009e4f12a614 Mon Sep 17 00:00:00 2001 From: Marshall Polaris Date: Thu, 26 May 2022 22:07:56 -0700 Subject: [PATCH] Add script to backfill contract `collectedFees` --- functions/src/scripts/backfill-fees.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 functions/src/scripts/backfill-fees.ts diff --git a/functions/src/scripts/backfill-fees.ts b/functions/src/scripts/backfill-fees.ts new file mode 100644 index 00000000..9ca068d2 --- /dev/null +++ b/functions/src/scripts/backfill-fees.ts @@ -0,0 +1,25 @@ +// We have many old contracts without a collectedFees data structure. Let's fill them in. + +import * as admin from 'firebase-admin' +import { initAdmin } from './script-init' +import { noFees } from '../../../common/fees' + +initAdmin() +const firestore = admin.firestore() + +if (require.main === module) { + const contractsRef = firestore.collection('contracts') + contractsRef.get().then((contractsSnaps) => { + let n = 0 + console.log(`Loaded ${contractsSnaps.size} contracts.`) + contractsSnaps.forEach((ct) => { + const data = ct.data() + if (!('collectedFees' in data)) { + n += 1 + console.log(`Filling in missing fees on contract ${data.id}...`) + ct.ref.update({ collectedFees: noFees }) + } + }) + console.log(`Updated ${n} contracts.`) + }) +}