Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send notification to project supporters when a project update has been added #814

Merged
merged 4 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 52 additions & 109 deletions src/adapters/notifications/NotificationCenterAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@ import {
import { Donation } from '../../entities/donation';
import { Project } from '../../entities/project';
import { User } from '../../entities/user';
import {
createBasicAuthentication,
getTimestampInSeconds,
} from '../../utils/utils';
import { createBasicAuthentication } from '../../utils/utils';
import { logger } from '../../utils/logger';
import { NOTIFICATIONS_EVENT_NAMES } from '../../analytics/analytics';
import Bull from 'bull';
import { redisConfig } from '../../redis';
import config from '../../config';
import { findUsersWhoDonatedToProjectExcludeWhoLiked } from '../../repositories/donationRepository';
import { findUsersWhoLikedProjectExcludeProjectOwner } from '../../repositories/reactionRepository';
import { findUsersWhoBoostedProject } from '../../repositories/powerBoostingRepository';

import { findProjectById } from '../../repositories/projectRepository';
import { findAllUsers } from '../../repositories/userRepository';
import {
findAllUsers,
findUsersWhoDonatedToProjectExcludeWhoLiked,
findUsersWhoBoostedProject,
findUsersWhoLikedProjectExcludeProjectOwner,
findUsersWhoSupportProject,
} from '../../repositories/userRepository';
const notificationCenterUsername = process.env.NOTIFICATION_CENTER_USERNAME;
const notificationCenterPassword = process.env.NOTIFICATION_CENTER_PASSWORD;
const notificationCenterBaseUrl = process.env.NOTIFICATION_CENTER_BASE_URL;
Expand Down Expand Up @@ -146,28 +147,14 @@ export class NotificationCenterAdapter implements NotificationAdapterInterface {
const { project } = params;
const projectOwner = project.adminUser as User;

const donors = await findUsersWhoDonatedToProjectExcludeWhoLiked(
project.id,
);
donors.map(user =>
const supporters = await findUsersWhoSupportProject(project.id);
supporters.map(user =>
sendProjectRelatedNotificationsQueue.add({
project,
eventName: NOTIFICATIONS_EVENT_NAMES.PROJECT_VERIFIED_DONORS,
eventName: NOTIFICATIONS_EVENT_NAMES.PROJECT_VERIFIED_USERS_WHO_SUPPORT,
user,
}),
);

const usersWhoLiked = await findUsersWhoLikedProjectExcludeProjectOwner(
project.id,
);
usersWhoLiked.map(user =>
sendProjectRelatedNotificationsQueue.add({
project,
eventName: NOTIFICATIONS_EVENT_NAMES.PROJECT_VERIFIED_USERS_WHO_LIKED,
user,
}),
);

return sendProjectRelatedNotification({
project,
eventName: NOTIFICATIONS_EVENT_NAMES.PROJECT_VERIFIED,
Expand Down Expand Up @@ -218,6 +205,15 @@ export class NotificationCenterAdapter implements NotificationAdapterInterface {
async projectBadgeRevoked(params: { project: Project }): Promise<void> {
const { project } = params;
const user = project.adminUser as User;
const supporters = await findUsersWhoSupportProject(project.id);
supporters.map(u =>
sendProjectRelatedNotificationsQueue.add({
project,
eventName:
NOTIFICATIONS_EVENT_NAMES.PROJECT_UNVERIFIED_USERS_WHO_SUPPORT,
user: u,
}),
);
return sendProjectRelatedNotification({
project,
eventName: NOTIFICATIONS_EVENT_NAMES.PROJECT_BADGE_REVOKED,
Expand Down Expand Up @@ -308,12 +304,12 @@ export class NotificationCenterAdapter implements NotificationAdapterInterface {
const { project } = params;
const user = project.adminUser as User;

const usersWhoBoosted = await findUsersWhoBoostedProject(project.id);
usersWhoBoosted.map(u =>
const supporters = await findUsersWhoSupportProject(project.id);
supporters.map(u =>
sendProjectRelatedNotificationsQueue.add({
project,
eventName:
NOTIFICATIONS_EVENT_NAMES.PROJECT_UNVERIFIED_USERS_WHO_BOOSTED,
NOTIFICATIONS_EVENT_NAMES.PROJECT_UNVERIFIED_USERS_WHO_SUPPORT,
user: u,
}),
);
Expand Down Expand Up @@ -361,24 +357,12 @@ export class NotificationCenterAdapter implements NotificationAdapterInterface {
async projectCancelled(params: { project: Project }): Promise<void> {
const { project } = params;

const donors = await findUsersWhoDonatedToProjectExcludeWhoLiked(
project.id,
);
donors.map(user =>
sendProjectRelatedNotificationsQueue.add({
project,
eventName: NOTIFICATIONS_EVENT_NAMES.PROJECT_CANCELLED_DONORS,
user,
}),
);

const usersWhoLiked = await findUsersWhoLikedProjectExcludeProjectOwner(
project.id,
);
usersWhoLiked.map(user =>
const supporters = await findUsersWhoSupportProject(project.id);
supporters.map(user =>
sendProjectRelatedNotificationsQueue.add({
project,
eventName: NOTIFICATIONS_EVENT_NAMES.PROJECT_CANCELLED_USERS_WHO_LIKED,
eventName:
NOTIFICATIONS_EVENT_NAMES.PROJECT_CANCELLED_USERS_WHO_SUPPORT,
user,
}),
);
Expand All @@ -404,32 +388,20 @@ export class NotificationCenterAdapter implements NotificationAdapterInterface {
}): Promise<void> {
const { project, update } = params;

const donors = await findUsersWhoDonatedToProjectExcludeWhoLiked(
project.id,
);
donors.map(user =>
const supporters = await findUsersWhoSupportProject(project.id);
supporters.map(user =>
sendProjectRelatedNotificationsQueue.add({
project,
eventName: NOTIFICATIONS_EVENT_NAMES.PROJECT_UPDATED_DONOR,
user,
}),
);

const usersWhoLiked = await findUsersWhoLikedProjectExcludeProjectOwner(
project.id,
);
usersWhoLiked.map(user =>
sendProjectRelatedNotificationsQueue.add({
project,
eventName: NOTIFICATIONS_EVENT_NAMES.PROJECT_UPDATED_USERS_WHO_LIKED,
eventName:
NOTIFICATIONS_EVENT_NAMES.PROJECT_ADD_AN_UPDATE_USERS_WHO_SUPPORT,
user,
}),
);

const projectOwner = project?.adminUser as User;
await sendProjectRelatedNotification({
project,
eventName: NOTIFICATIONS_EVENT_NAMES.PROJECT_UPDATED_OWNER,
eventName: NOTIFICATIONS_EVENT_NAMES.PROJECT_UPDATE_ADDED_OWNER,
sendEmail: true,
segment: {
analyticsUserId: projectOwner.segmentUserId(),
Expand All @@ -447,24 +419,11 @@ export class NotificationCenterAdapter implements NotificationAdapterInterface {
async projectDeListed(params: { project: Project }): Promise<void> {
const { project } = params;

const donors = await findUsersWhoDonatedToProjectExcludeWhoLiked(
project.id,
);
donors.map(user =>
const supporters = await findUsersWhoSupportProject(project.id);
supporters.map(user =>
sendProjectRelatedNotificationsQueue.add({
project,
eventName: NOTIFICATIONS_EVENT_NAMES.PROJECT_UNLISTED_DONORS,
user,
}),
);

const usersWhoLiked = await findUsersWhoLikedProjectExcludeProjectOwner(
project.id,
);
usersWhoLiked.map(user =>
sendProjectRelatedNotificationsQueue.add({
project,
eventName: NOTIFICATIONS_EVENT_NAMES.PROJECT_UNLISTED_USERS_WHO_LIKED,
eventName: NOTIFICATIONS_EVENT_NAMES.PROJECT_UNLISTED_SUPPORTED,
user,
}),
);
Expand Down Expand Up @@ -493,26 +452,12 @@ export class NotificationCenterAdapter implements NotificationAdapterInterface {
const metadata = {
reason,
};
const donors = await findUsersWhoDonatedToProjectExcludeWhoLiked(
project.id,
);
donors.map(user =>
sendProjectRelatedNotificationsQueue.add({
project,
eventName: NOTIFICATIONS_EVENT_NAMES.PROJECT_DEACTIVATED_DONORS,
user,
metadata,
}),
);

const usersWhoLiked = await findUsersWhoLikedProjectExcludeProjectOwner(
project.id,
);
usersWhoLiked.map(user =>
const supporters = await findUsersWhoSupportProject(project.id);
supporters.map(user =>
sendProjectRelatedNotificationsQueue.add({
project,
eventName:
NOTIFICATIONS_EVENT_NAMES.PROJECT_DEACTIVATED_USERS_WHO_LIKED,
NOTIFICATIONS_EVENT_NAMES.PROJECT_DEACTIVATED_USERS_WHO_SUPPORT,
user,
metadata,
}),
Expand All @@ -539,6 +484,15 @@ export class NotificationCenterAdapter implements NotificationAdapterInterface {
const { project } = params;
const projectOwner = project?.adminUser as User;

const supporters = await findUsersWhoSupportProject(project.id);
supporters.map(user =>
sendProjectRelatedNotificationsQueue.add({
project,
eventName: NOTIFICATIONS_EVENT_NAMES.PROJECT_LISTED_SUPPORTED,
user,
}),
);

await sendProjectRelatedNotification({
project,
eventName: NOTIFICATIONS_EVENT_NAMES.PROJECT_LISTED,
Expand Down Expand Up @@ -611,27 +565,16 @@ export class NotificationCenterAdapter implements NotificationAdapterInterface {
async projectReactivated(params: { project: Project }): Promise<void> {
const { project } = params;
const projectOwner = project?.adminUser as User;
const donors = await findUsersWhoDonatedToProjectExcludeWhoLiked(
project.id,
);
donors.map(user =>
const supporters = await findUsersWhoSupportProject(project.id);
supporters.map(user =>
sendProjectRelatedNotificationsQueue.add({
project,
eventName: NOTIFICATIONS_EVENT_NAMES.PROJECT_ACTIVATED_DONORS,
eventName:
NOTIFICATIONS_EVENT_NAMES.PROJECT_ACTIVATED_USERS_WHO_SUPPORT,
user,
}),
);

const usersWhoLiked = await findUsersWhoLikedProjectExcludeProjectOwner(
project.id,
);
usersWhoLiked.map(user =>
sendProjectRelatedNotificationsQueue.add({
project,
eventName: NOTIFICATIONS_EVENT_NAMES.PROJECT_ACTIVATED_USERS_WHO_LIKED,
user,
}),
);
return sendProjectRelatedNotification({
project,
eventName: NOTIFICATIONS_EVENT_NAMES.PROJECT_ACTIVATED,
Expand Down
23 changes: 9 additions & 14 deletions src/analytics/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ export enum NOTIFICATIONS_EVENT_NAMES {
DRAFTED_PROJECT_ACTIVATED = 'Draft published',
PROJECT_LISTED = 'Project listed',
PROJECT_UNLISTED = 'Project unlisted',
PROJECT_UNLISTED_DONORS = 'Project unlisted - Donors',
PROJECT_UNLISTED_USERS_WHO_LIKED = 'Project unlisted - Users Who Liked',
PROJECT_UNLISTED_SUPPORTED = 'Project unlisted - Users who supported',
PROJECT_LISTED_SUPPORTED = 'Project listed - Users who supported',
PROJECT_EDITED = 'Project edited',
PROJECT_BADGE_REVOKED = 'Project badge revoked',
PROJECT_BADGE_REVOKE_REMINDER = 'Project badge revoke reminder',
Expand All @@ -12,34 +12,29 @@ export enum NOTIFICATIONS_EVENT_NAMES {
PROJECT_BADGE_UP_FOR_REVOKING = 'Project badge up for revoking',
PROJECT_BOOSTED = 'Project boosted',
PROJECT_VERIFIED = 'Project verified',
PROJECT_VERIFIED_DONORS = 'Project verified - Donors',
PROJECT_VERIFIED_USERS_WHO_LIKED = 'Project verified - Users Who Liked',
PROJECT_VERIFIED_USERS_WHO_SUPPORT = 'Project verified - Users who supported',

// https://github.com/Giveth/impact-graph/issues/624#issuecomment-1240364389
PROJECT_REJECTED = 'Project unverified',

PROJECT_UNVERIFIED = 'Project unverified',
PROJECT_UNVERIFIED_USERS_WHO_BOOSTED = 'Project unverified - Users Who Boosted',
PROJECT_UNVERIFIED_USERS_WHO_SUPPORT = 'Project unverified - Users who supported',
PROJECT_ACTIVATED = 'Project activated',
PROJECT_ACTIVATED_DONORS = 'Project activated - Donors',
PROJECT_ACTIVATED_USERS_WHO_LIKED = 'Project activated - Users Who Liked',
PROJECT_ACTIVATED_USERS_WHO_SUPPORT = 'Project activated - Users who supported',
PROJECT_DEACTIVATED = 'Project deactivated',
PROJECT_DEACTIVATED_DONORS = 'Project deactivated - Donors',
PROJECT_DEACTIVATED_USERS_WHO_LIKED = 'Project deactivated - Users Who Liked',
PROJECT_DEACTIVATED_USERS_WHO_SUPPORT = 'Project deactivated - Users who supported',

PROJECT_CANCELLED = 'Project cancelled',
PROJECT_CANCELLED_DONORS = 'Project cancelled - Donors',
PROJECT_CANCELLED_USERS_WHO_LIKED = 'Project cancelled - Users Who Liked',
PROJECT_CANCELLED_USERS_WHO_SUPPORT = 'Project cancelled - Users who supported',
MADE_DONATION = 'Made donation',
DONATION_RECEIVED = 'Donation received',
DONATION_GET_PRICE_FAILED = 'Donation get price failed',
PROJECT_RECEIVED_HEART = 'project liked',
PROJECT_UPDATED_DONOR = 'Project updated - donor',
PROJECT_UPDATED_OWNER = 'Project updated - owner',
PROJECT_UPDATED_USERS_WHO_LIKED = 'Project update - Users Who Liked',
PROJECT_UPDATE_ADDED_OWNER = 'Project update added - owner',
PROJECT_CREATED = 'The project saved as draft',
UPDATED_PROFILE = 'Updated profile',
GET_DONATION_PRICE_FAILED = 'Get Donation Price Failed',
VERIFICATION_FORM_GOT_DRAFT_BY_ADMIN = 'Verification form got draft by admin',
RAW_HTML_BROADCAST = 'Raw HTML Broadcast',
PROJECT_ADD_AN_UPDATE_USERS_WHO_SUPPORT = 'Project update added - Users who supported',
}
Loading