diff --git a/common/comment.ts b/common/comment.ts index a217b292..77b211d3 100644 --- a/common/comment.ts +++ b/common/comment.ts @@ -20,4 +20,6 @@ export type Comment = { userName: string userUsername: string userAvatarUrl?: string + contractSlug?: string + contractQuestion?: string } diff --git a/firestore.indexes.json b/firestore.indexes.json index 12e88033..874344be 100644 --- a/firestore.indexes.json +++ b/firestore.indexes.json @@ -496,6 +496,28 @@ } ] }, + { + "collectionGroup": "comments", + "fieldPath": "contractId", + "indexes": [ + { + "order": "ASCENDING", + "queryScope": "COLLECTION" + }, + { + "order": "DESCENDING", + "queryScope": "COLLECTION" + }, + { + "arrayConfig": "CONTAINS", + "queryScope": "COLLECTION" + }, + { + "order": "ASCENDING", + "queryScope": "COLLECTION_GROUP" + } + ] + }, { "collectionGroup": "comments", "fieldPath": "createdTime", diff --git a/functions/src/on-create-comment-on-contract.ts b/functions/src/on-create-comment-on-contract.ts index d7aa0c5e..3fa0983d 100644 --- a/functions/src/on-create-comment-on-contract.ts +++ b/functions/src/on-create-comment-on-contract.ts @@ -24,6 +24,11 @@ export const onCreateCommentOnContract = functions if (!contract) throw new Error('Could not find contract corresponding with comment') + await change.ref.update({ + contractSlug: contract.slug, + contractQuestion: contract.question, + }) + const comment = change.data() as Comment const lastCommentTime = comment.createdTime diff --git a/functions/src/scripts/denormalize-comment-contract-data.ts b/functions/src/scripts/denormalize-comment-contract-data.ts new file mode 100644 index 00000000..0358c5a1 --- /dev/null +++ b/functions/src/scripts/denormalize-comment-contract-data.ts @@ -0,0 +1,70 @@ +// Filling in the contract-based fields on comments. + +import * as admin from 'firebase-admin' +import { initAdmin } from './script-init' +import { + DocumentCorrespondence, + findDiffs, + describeDiff, + applyDiff, +} from './denormalize' +import { DocumentSnapshot, Transaction } from 'firebase-admin/firestore' + +initAdmin() +const firestore = admin.firestore() + +async function getContractsById(transaction: Transaction) { + const contracts = await transaction.get(firestore.collection('contracts')) + const results = Object.fromEntries(contracts.docs.map((doc) => [doc.id, doc])) + console.log(`Found ${contracts.size} contracts.`) + return results +} + +async function getCommentsByContractId(transaction: Transaction) { + const comments = await transaction.get( + firestore.collectionGroup('comments').where('contractId', '!=', null) + ) + const results = new Map() + comments.forEach((doc) => { + const contractId = doc.get('contractId') + const contractComments = results.get(contractId) || [] + contractComments.push(doc) + results.set(contractId, contractComments) + }) + console.log(`Found ${comments.size} comments on ${results.size} contracts.`) + return results +} + +async function denormalize() { + let hasMore = true + while (hasMore) { + hasMore = await admin.firestore().runTransaction(async (transaction) => { + const [contractsById, commentsByContractId] = await Promise.all([ + getContractsById(transaction), + getCommentsByContractId(transaction), + ]) + const mapping = Object.entries(contractsById).map( + ([id, doc]): DocumentCorrespondence => { + return [doc, commentsByContractId.get(id) || []] + } + ) + const slugDiffs = findDiffs(mapping, 'slug', 'contractSlug') + const qDiffs = findDiffs(mapping, 'question', 'contractQuestion') + console.log(`Found ${slugDiffs.length} comments with mismatched slugs.`) + console.log(`Found ${qDiffs.length} comments with mismatched questions.`) + const diffs = slugDiffs.concat(qDiffs) + diffs.slice(0, 500).forEach((d) => { + console.log(describeDiff(d)) + applyDiff(transaction, d) + }) + if (diffs.length > 500) { + console.log(`Applying first 500 because of Firestore limit...`) + } + return diffs.length > 500 + }) + } +} + +if (require.main === module) { + denormalize().catch((e) => console.error(e)) +}