Skip to content

Commit

Permalink
feat: send notification to voters
Browse files Browse the repository at this point in the history
  • Loading branch information
andyesp committed Sep 28, 2023
1 parent 6e4b8ad commit d4230f2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/back/routes/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default routes((router) => {
router.get('/snapshot/proposal-scores/:proposalSnapshotId', handleAPI(getProposalScores))
})

async function getStatus(req: Request) {
async function getStatus() {
return await SnapshotService.getStatus()
}

Expand Down
31 changes: 20 additions & 11 deletions src/back/services/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,16 +197,25 @@ export class NotificationService {
}
}

static async votingEndedVoters(proposal: ProposalAttributes, voters: string[]) {
/*
TYPE SUBSET
Trigger: A proposal ended
Target recipient: Users who voted on the proposal
Copy: Title: "Voting Ended on a Proposal You Voted On"
Description: "Discover the results of the proposal you participated in as a voter. Your input matters!"
Target URL: Proposal URL
*/

return true
static async votingEndedVoters(proposal: ProposalAttributes, addresses: string[]) {
try {
if (!areValidAddresses(addresses)) {
throw new Error('Invalid addresses')
}

return await this.sendNotification({
title: 'Voting Ended on a Proposal You Voted On',
body: 'Discover the results of the proposal you participated in as a voter. Your input matters!',
recipient: addresses,
url: proposalUrl(proposal.id),
customType: NotificationCustomType.Proposal,
})
} catch (error) {
ErrorService.report('Error sending voting ended notification to voters', {
error,
category: ErrorCategory.Notifications,
proposal,
})
}
}
}
18 changes: 12 additions & 6 deletions src/services/ProposalService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { NotificationService } from '../back/services/notification'
import { VoteService } from '../back/services/vote'
import { Discourse, DiscourseComment, DiscoursePost } from '../clients/Discourse'
import { SnapshotProposalContent } from '../clients/SnapshotTypes'
import { NOTIFICATIONS_SERVICE_ENABLED } from '../constants'
import CoauthorModel from '../entities/Coauthor/model'
import isDAOCommittee from '../entities/Committee/isDAOCommittee'
import ProposalModel from '../entities/Proposal/model'
Expand All @@ -19,12 +20,10 @@ import VotesModel from '../entities/Votes/model'
import { inBackground } from '../helpers'
import { getProfile } from '../utils/Catalyst'
import Time from '../utils/date/Time'
import { ErrorCategory } from '../utils/errorCategories'
import { getEnvironmentChainId } from '../utils/votes/utils'

import { DiscordService } from './DiscordService'
import { DiscourseService } from './DiscourseService'
import { ErrorService } from './ErrorService'
import { SnapshotService } from './SnapshotService'

export type ProposalInCreation = {
Expand Down Expand Up @@ -241,10 +240,17 @@ export class ProposalService {
status
)

for (const proposal of proposals) {
NotificationService.votingEndedAuthors(proposal)
// TODO: Get voters from proposalIds
// TODO: NotificationService.votingEndedVoters(proposal, voters)
if (NOTIFICATIONS_SERVICE_ENABLED) {
for (const proposal of proposals) {
try {
NotificationService.votingEndedAuthors(proposal)
const votes = await VoteService.getVotes(proposal.id)
const voters = Object.keys(votes)
NotificationService.votingEndedVoters(proposal, voters)
} catch (error) {
logger.log('Error sending notifications on proposal finish', { proposalId: proposal.id })
}
}
}
}
}

0 comments on commit d4230f2

Please sign in to comment.