diff --git a/common/comment.ts b/common/comment.ts index 77b211d3..c1b721cc 100644 --- a/common/comment.ts +++ b/common/comment.ts @@ -4,6 +4,8 @@ import type { JSONContent } from '@tiptap/core' // They're uniquely identified by the pair contractId/betId. export type Comment = { id: string + commentType: 'contract' | 'group' + contractId?: string groupId?: string betId?: string diff --git a/functions/src/scripts/backfill-comment-types.ts b/functions/src/scripts/backfill-comment-types.ts new file mode 100644 index 00000000..cce1c85d --- /dev/null +++ b/functions/src/scripts/backfill-comment-types.ts @@ -0,0 +1,31 @@ +// Comment types were introduced in August 2022. + +import { initAdmin } from './script-init' +import { log, writeAsync } from '../utils' + +if (require.main === module) { + const app = initAdmin() + const firestore = app.firestore() + const commentsRef = firestore.collectionGroup('comments') + commentsRef.get().then(async (commentsSnaps) => { + log(`Loaded ${commentsSnaps.size} contracts.`) + const needsFilling = commentsSnaps.docs.filter((ct) => { + return !('commentType' in ct.data()) + }) + log(`Found ${needsFilling.length} comments to update.`) + const updates = needsFilling.map((d) => { + const comment = d.data() + const fields: { [k: string]: unknown } = {} + if (comment.contractId != null && comment.groupId == null) { + fields.commentType = 'contract' + } else if (comment.groupId != null && comment.contractId == null) { + fields.commentType = 'group' + } else { + log(`Invalid comment ${comment}; not touching it.`) + } + return { doc: d.ref, fields, info: comment } + }) + await writeAsync(firestore, updates) + log(`Updated all comments.`) + }) +} diff --git a/web/lib/firebase/comments.ts b/web/lib/firebase/comments.ts index e82c6d45..f0678367 100644 --- a/web/lib/firebase/comments.ts +++ b/web/lib/firebase/comments.ts @@ -33,6 +33,7 @@ export async function createCommentOnContract( : doc(getCommentsCollection(contractId)) const comment: Comment = removeUndefinedProps({ id: ref.id, + commentType: 'contract', contractId, userId: commenter.id, content: content, @@ -61,6 +62,7 @@ export async function createCommentOnGroup( const ref = doc(getCommentsOnGroupCollection(groupId)) const comment: Comment = removeUndefinedProps({ id: ref.id, + commentType: 'group', groupId, userId: user.id, content: content,