From d4230f2b5e38a1075a49a96d6bbd38b3325b5906 Mon Sep 17 00:00:00 2001 From: Andy Espagnolo Date: Thu, 28 Sep 2023 13:22:00 -0300 Subject: [PATCH] feat: send notification to voters --- src/back/routes/snapshot.ts | 2 +- src/back/services/notification.ts | 31 ++++++++++++++++++++----------- src/services/ProposalService.ts | 18 ++++++++++++------ 3 files changed, 33 insertions(+), 18 deletions(-) diff --git a/src/back/routes/snapshot.ts b/src/back/routes/snapshot.ts index 635f9fab0..6423bb5f0 100644 --- a/src/back/routes/snapshot.ts +++ b/src/back/routes/snapshot.ts @@ -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() } diff --git a/src/back/services/notification.ts b/src/back/services/notification.ts index d3b1975d5..586e0fd23 100644 --- a/src/back/services/notification.ts +++ b/src/back/services/notification.ts @@ -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, + }) + } } } diff --git a/src/services/ProposalService.ts b/src/services/ProposalService.ts index 03e9eb2c3..053084d3f 100644 --- a/src/services/ProposalService.ts +++ b/src/services/ProposalService.ts @@ -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' @@ -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 = { @@ -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 }) + } + } } } }