From 02724cba50757b506265c04bb89dbc627d3febd8 Mon Sep 17 00:00:00 2001 From: James Grugett Date: Sat, 30 Apr 2022 12:19:02 -0400 Subject: [PATCH] Update lastCommentTime script --- .../src/scripts/update-last-comment-time.ts | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 functions/src/scripts/update-last-comment-time.ts diff --git a/functions/src/scripts/update-last-comment-time.ts b/functions/src/scripts/update-last-comment-time.ts new file mode 100644 index 00000000..ae950fbe --- /dev/null +++ b/functions/src/scripts/update-last-comment-time.ts @@ -0,0 +1,43 @@ +import * as admin from 'firebase-admin' +import * as _ from 'lodash' + +import { initAdmin } from './script-init' +initAdmin() + +import { Contract } from '../../../common/contract' +import { getValues } from '../utils' +import { Comment } from '../../../common/comment' + +async function updateLastCommentTime() { + const firestore = admin.firestore() + console.log('Updating contracts lastCommentTime') + + const contracts = await getValues(firestore.collection('contracts')) + + console.log('Loaded', contracts.length, 'contracts') + + for (const contract of contracts) { + const contractRef = firestore.doc(`contracts/${contract.id}`) + + const lastComments = await getValues( + contractRef.collection('comments').orderBy('createdTime', 'desc').limit(1) + ) + + if (lastComments.length > 0) { + const lastCommentTime = lastComments[0].createdTime + console.log( + 'Updating lastCommentTime', + contract.question, + lastCommentTime + ) + + await contractRef.update({ + lastCommentTime, + } as Partial) + } + } +} + +if (require.main === module) { + updateLastCommentTime().then(() => process.exit()) +}