Skip to content

Commit

Permalink
feat: send notification on coauthor request
Browse files Browse the repository at this point in the history
  • Loading branch information
andyesp committed Sep 25, 2023
1 parent 9a52aed commit 29516e1
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions src/back/services/notification.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ethers } from 'ethers'
import isEthereumAddress from 'validator/lib/isEthereumAddress'

import { NOTIFICATIONS_SERVICE_ENABLED } from '../../constants'
import { ProposalAttributes } from '../../entities/Proposal/types'
Expand All @@ -24,6 +25,9 @@ const NotificationIdentityType = {
DIRECT_PAYLOAD: 2,
}

const areValidAddresses = (addresses: string[]) =>
Array.isArray(addresses) && addresses.every((item) => isEthereumAddress(item))

export class NotificationService {
static async sendNotification({
type,
Expand Down Expand Up @@ -76,23 +80,27 @@ export class NotificationService {
return NotificationType.TARGET
}

private static getRecipients(address: string | string[] | undefined) {
if (!address) {
private static getRecipients(recipient: string | string[] | undefined) {
if (!recipient) {
return undefined
}

if (Array.isArray(address)) {
return address.map((item: string) => getCaipAddress(item, CHAIN_ID))
if (Array.isArray(recipient)) {
return recipient.map((item: string) => getCaipAddress(item, CHAIN_ID))
}

return getCaipAddress(address, CHAIN_ID)
return getCaipAddress(recipient, CHAIN_ID)
}

static async proposalEnacted(proposal: ProposalAttributes) {
const coauthors = await CoauthorService.getAllFromProposalId(proposal.id)
const coauthorsAddresses = coauthors.length > 0 ? coauthors.map((coauthor) => coauthor.address) : []
const addresses = [proposal.user, ...coauthorsAddresses]

if (!areValidAddresses(addresses)) {
return
}

return await this.sendNotification({
title: 'Grant Proposal Enacted',
body: 'Congratulations! Your Grant Proposal has been successfully enacted and a Vesting Contract was added',
Expand All @@ -102,15 +110,16 @@ export class NotificationService {
}

static async coAuthorRequested(proposal: ProposalAttributes, coAuthors: string[]) {
/*
Trigger: A proposal got published and the user has been added as a coauthor.
Target recipient: Proposed co author
Copy: Title: "Co-author Request Received"
Description: "You've been invited to collaborate as a co-author on a published proposal. Accept it or reject it here"
Target URL: Proposal URL
*/
if (!areValidAddresses(coAuthors)) {
return
}

return true
return await this.sendNotification({
title: 'Co-author Request Received',
body: "You've been invited to collaborate as a co-author on a published proposal. Accept it or reject it here",
recipient: coAuthors,
url: proposalUrl(proposal.id),
})
}

static async votingEndedAuthors(proposal: ProposalAttributes, authors: string[]) {
Expand Down

0 comments on commit 29516e1

Please sign in to comment.