2022-06-01 13:11:25 +00:00
|
|
|
import * as functions from 'firebase-functions'
|
2022-09-30 15:27:42 +00:00
|
|
|
import { getUser, getValues, log } from './utils'
|
2022-08-24 16:49:53 +00:00
|
|
|
import { createCommentOrAnswerOrUpdatedContractNotification } from './create-notification'
|
2022-06-01 13:11:25 +00:00
|
|
|
import { Contract } from '../../common/contract'
|
2022-09-30 15:27:42 +00:00
|
|
|
import { Txn } from '../../common/txn'
|
|
|
|
import { partition, sortBy } from 'lodash'
|
|
|
|
import { runTxn, TxnData } from './transact'
|
|
|
|
import * as admin from 'firebase-admin'
|
2022-06-01 13:11:25 +00:00
|
|
|
|
|
|
|
export const onUpdateContract = functions.firestore
|
|
|
|
.document('contracts/{contractId}')
|
|
|
|
.onUpdate(async (change, context) => {
|
|
|
|
const contract = change.after.data() as Contract
|
2022-09-30 15:27:42 +00:00
|
|
|
const previousContract = change.before.data() as Contract
|
2022-06-01 13:11:25 +00:00
|
|
|
const { eventId } = context
|
2022-09-30 15:27:42 +00:00
|
|
|
const { openCommentBounties, closeTime, question } = contract
|
2022-06-01 13:11:25 +00:00
|
|
|
|
2022-09-30 15:27:42 +00:00
|
|
|
if (
|
|
|
|
!previousContract.isResolved &&
|
|
|
|
contract.isResolved &&
|
|
|
|
(openCommentBounties ?? 0) > 0
|
|
|
|
) {
|
|
|
|
await handleUnusedCommentBountyRefunds(contract)
|
|
|
|
// No need to notify users of resolution, that's handled in resolve-market
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
previousContract.closeTime !== closeTime ||
|
|
|
|
previousContract.question !== question
|
|
|
|
) {
|
|
|
|
await handleUpdatedCloseTime(previousContract, contract, eventId)
|
|
|
|
}
|
|
|
|
})
|
2022-06-01 13:11:25 +00:00
|
|
|
|
2022-09-30 15:27:42 +00:00
|
|
|
async function handleUpdatedCloseTime(
|
|
|
|
previousContract: Contract,
|
|
|
|
contract: Contract,
|
|
|
|
eventId: string
|
|
|
|
) {
|
|
|
|
const contractUpdater = await getUser(contract.creatorId)
|
|
|
|
if (!contractUpdater) throw new Error('Could not find contract updater')
|
|
|
|
let sourceText = ''
|
|
|
|
if (previousContract.closeTime !== contract.closeTime && contract.closeTime) {
|
|
|
|
sourceText = contract.closeTime.toString()
|
|
|
|
} else if (previousContract.question !== contract.question) {
|
|
|
|
sourceText = contract.question
|
|
|
|
}
|
2022-09-16 14:15:16 +00:00
|
|
|
|
2022-09-30 15:27:42 +00:00
|
|
|
await createCommentOrAnswerOrUpdatedContractNotification(
|
|
|
|
contract.id,
|
|
|
|
'contract',
|
|
|
|
'updated',
|
|
|
|
contractUpdater,
|
|
|
|
eventId,
|
|
|
|
sourceText,
|
|
|
|
contract
|
|
|
|
)
|
|
|
|
}
|
2022-09-16 14:15:16 +00:00
|
|
|
|
2022-09-30 15:27:42 +00:00
|
|
|
async function handleUnusedCommentBountyRefunds(contract: Contract) {
|
|
|
|
const outstandingCommentBounties = await getValues<Txn>(
|
|
|
|
firestore.collection('txns').where('category', '==', 'COMMENT_BOUNTY')
|
|
|
|
)
|
|
|
|
|
|
|
|
const commentBountiesOnThisContract = sortBy(
|
|
|
|
outstandingCommentBounties.filter(
|
|
|
|
(bounty) => bounty.data?.contractId === contract.id
|
|
|
|
),
|
|
|
|
(bounty) => bounty.createdTime
|
|
|
|
)
|
|
|
|
|
|
|
|
const [toBank, fromBank] = partition(
|
|
|
|
commentBountiesOnThisContract,
|
|
|
|
(bounty) => bounty.toType === 'BANK'
|
|
|
|
)
|
|
|
|
if (toBank.length <= fromBank.length) return
|
|
|
|
|
|
|
|
await firestore
|
|
|
|
.collection('contracts')
|
|
|
|
.doc(contract.id)
|
|
|
|
.update({ openCommentBounties: 0 })
|
|
|
|
|
|
|
|
const refunds = toBank.slice(fromBank.length)
|
|
|
|
await Promise.all(
|
|
|
|
refunds.map(async (extraBountyTxn) => {
|
|
|
|
const result = await firestore.runTransaction(async (trans) => {
|
|
|
|
const bonusTxn: TxnData = {
|
|
|
|
fromId: extraBountyTxn.toId,
|
|
|
|
fromType: 'BANK',
|
|
|
|
toId: extraBountyTxn.fromId,
|
|
|
|
toType: 'USER',
|
|
|
|
amount: extraBountyTxn.amount,
|
|
|
|
token: 'M$',
|
|
|
|
category: 'REFUND_COMMENT_BOUNTY',
|
|
|
|
data: {
|
|
|
|
contractId: contract.id,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return await runTxn(trans, bonusTxn)
|
|
|
|
})
|
|
|
|
|
|
|
|
if (result.status != 'success' || !result.txn) {
|
|
|
|
log(
|
|
|
|
`Couldn't refund bonus for user: ${extraBountyTxn.fromId} - status:`,
|
|
|
|
result.status
|
|
|
|
)
|
|
|
|
log('message:', result.message)
|
|
|
|
} else {
|
|
|
|
log(
|
|
|
|
`Refund bonus txn for user: ${extraBountyTxn.fromId} completed:`,
|
|
|
|
result.txn?.id
|
|
|
|
)
|
2022-06-10 22:48:28 +00:00
|
|
|
}
|
2022-09-30 15:27:42 +00:00
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
2022-06-10 22:48:28 +00:00
|
|
|
|
2022-09-30 15:27:42 +00:00
|
|
|
const firestore = admin.firestore()
|