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.
|
// They're uniquely identified by the pair contractId/betId.
|
||||||
export type Comment = {
|
export type Comment = {
|
||||||
id: string
|
id: string
|
||||||
|
commentType: 'contract' | 'group'
|
||||||
|
|
||||||
contractId?: string
|
contractId?: string
|
||||||
groupId?: string
|
groupId?: string
|
||||||
betId?: 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))
|
: doc(getCommentsCollection(contractId))
|
||||||
const comment: Comment = removeUndefinedProps({
|
const comment: Comment = removeUndefinedProps({
|
||||||
id: ref.id,
|
id: ref.id,
|
||||||
|
commentType: 'contract',
|
||||||
contractId,
|
contractId,
|
||||||
userId: commenter.id,
|
userId: commenter.id,
|
||||||
content: content,
|
content: content,
|
||||||
|
@ -61,6 +62,7 @@ export async function createCommentOnGroup(
|
||||||
const ref = doc(getCommentsOnGroupCollection(groupId))
|
const ref = doc(getCommentsOnGroupCollection(groupId))
|
||||||
const comment: Comment = removeUndefinedProps({
|
const comment: Comment = removeUndefinedProps({
|
||||||
id: ref.id,
|
id: ref.id,
|
||||||
|
commentType: 'group',
|
||||||
groupId,
|
groupId,
|
||||||
userId: user.id,
|
userId: user.id,
|
||||||
content: content,
|
content: content,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user