Skip to content

Commit

Permalink
chore: notify finish proposal job errors
Browse files Browse the repository at this point in the history
  • Loading branch information
1emu committed Sep 21, 2023
1 parent 71af0ae commit 0662146
Showing 1 changed file with 31 additions and 27 deletions.
58 changes: 31 additions & 27 deletions src/entities/Proposal/jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,35 +203,39 @@ async function categorizeProposals(
}

export async function finishProposal(context: JobContext) {
const finishableProposals = await ProposalModel.getFinishableProposals()
if (finishableProposals.length === 0) {
return
}
try {
const finishableProposals = await ProposalModel.getFinishableProposals()
if (finishableProposals.length === 0) {
return
}

const currentBudgets = await BudgetService.getBudgetsForProposals(finishableProposals)

context.log(`Updating ${finishableProposals.length} proposals...`)
const { finishedProposals, acceptedProposals, outOfBudgetProposals, rejectedProposals, updatedBudgets } =
await categorizeProposals(finishableProposals, currentBudgets, context)

await updateFinishedProposals(finishedProposals, context)
await updateAcceptedProposals(acceptedProposals, context)
await updateOutOfBudgetProposals(outOfBudgetProposals, context)
await updateRejectedProposals(rejectedProposals, context)
await BudgetService.updateBudgets(updatedBudgets)

const proposals = [...finishedProposals, ...acceptedProposals, ...rejectedProposals]
context.log(`Updating ${proposals.length} proposals in discourse... \n\n`)
for (const { id, title, winnerChoice, outcomeStatus } of proposals) {
ProposalService.commentProposalUpdateInDiscourse(id)
if (outcomeStatus) {
DiscordService.finishProposal(
id,
title,
outcomeStatus,
outcomeStatus === ProposalOutcome.FINISHED ? winnerChoice : undefined
)
const currentBudgets = await BudgetService.getBudgetsForProposals(finishableProposals)

context.log(`Updating ${finishableProposals.length} proposals...`)
const { finishedProposals, acceptedProposals, outOfBudgetProposals, rejectedProposals, updatedBudgets } =
await categorizeProposals(finishableProposals, currentBudgets, context)

await updateFinishedProposals(finishedProposals, context)
await updateAcceptedProposals(acceptedProposals, context)
await updateOutOfBudgetProposals(outOfBudgetProposals, context)
await updateRejectedProposals(rejectedProposals, context)
await BudgetService.updateBudgets(updatedBudgets)

const proposals = [...finishedProposals, ...acceptedProposals, ...rejectedProposals]
context.log(`Updating ${proposals.length} proposals in discourse... \n\n`)
for (const { id, title, winnerChoice, outcomeStatus } of proposals) {
ProposalService.commentProposalUpdateInDiscourse(id)
if (outcomeStatus) {
DiscordService.finishProposal(
id,
title,
outcomeStatus,
outcomeStatus === ProposalOutcome.FINISHED ? winnerChoice : undefined
)
}
}
} catch (error) {
ErrorService.report('Error finishing proposals', { error, category: ErrorCategory.Job })
}
}

Expand Down

0 comments on commit 0662146

Please sign in to comment.