This commit is contained in:
Ian Philips 2022-09-28 13:31:58 -04:00
parent 1d826f7da9
commit b0776afff8

View File

@ -11,18 +11,53 @@ export const onUpdateContract = functions.firestore
.document('contracts/{contractId}') .document('contracts/{contractId}')
.onUpdate(async (change, context) => { .onUpdate(async (change, context) => {
const contract = change.after.data() as Contract const contract = change.after.data() as Contract
const previousContract = change.before.data() as Contract
const { eventId } = context const { eventId } = context
const { openCommentBounties, closeTime, question } = contract
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)
}
})
async function handleUpdatedCloseTime(
previousContract: Contract,
contract: Contract,
eventId: string
) {
const contractUpdater = await getUser(contract.creatorId) const contractUpdater = await getUser(contract.creatorId)
if (!contractUpdater) throw new Error('Could not find contract updater') 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
}
const previousValue = change.before.data() as Contract await createCommentOrAnswerOrUpdatedContractNotification(
contract.id,
// Refund extra unused comment bounties 'contract',
if (!previousValue.isResolved && contract.isResolved) { 'updated',
const bountiesLeft = contract.openCommentBounties ?? 0 contractUpdater,
if (bountiesLeft <= 0) return eventId,
sourceText,
contract
)
}
async function handleUnusedCommentBountyRefunds(contract: Contract) {
const outstandingCommentBounties = await getValues<Txn>( const outstandingCommentBounties = await getValues<Txn>(
firestore.collection('txns').where('category', '==', 'COMMENT_BOUNTY') firestore.collection('txns').where('category', '==', 'COMMENT_BOUNTY')
) )
@ -78,31 +113,6 @@ export const onUpdateContract = functions.firestore
} }
}) })
) )
return }
}
if (
previousValue.closeTime !== contract.closeTime ||
previousValue.question !== contract.question
) {
let sourceText = ''
if (
previousValue.closeTime !== contract.closeTime &&
contract.closeTime
) {
sourceText = contract.closeTime.toString()
} else if (previousValue.question !== contract.question) {
sourceText = contract.question
}
await createCommentOrAnswerOrUpdatedContractNotification(
contract.id,
'contract',
'updated',
contractUpdater,
eventId,
sourceText,
contract
)
}
})
const firestore = admin.firestore() const firestore = admin.firestore()