Add comment type field to comments (#772)

This commit is contained in:
Marshall Polaris 2022-08-18 12:47:35 -07:00 committed by GitHub
parent c2db558b85
commit c37997bcb7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 0 deletions

View File

@ -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

View File

@ -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.`)
})
}

View File

@ -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,