Add comment type field to comments (#772)
This commit is contained in:
parent
c2db558b85
commit
c37997bcb7
|
@ -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
|
||||
|
|
31
functions/src/scripts/backfill-comment-types.ts
Normal file
31
functions/src/scripts/backfill-comment-types.ts
Normal 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.`)
|
||||
})
|
||||
}
|
|
@ -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,
|
||||
|
|
Loading…
Reference in New Issue
Block a user