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 emails for ownership changes #1750

Open
wants to merge 7 commits into
base: 1722-transferOwnerShipOfProject
Choose a base branch
from
10 changes: 10 additions & 0 deletions src/adapters/notifications/MockNotificationAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
BroadCastNotificationInputParams,
NotificationAdapterInterface,
OrttoPerson,
ProjectOwnershipChangedParams,
ProjectsHaveNewRankingInputParam,
} from './NotificationAdapterInterface';
import { Donation } from '../../entities/donation';
Expand Down Expand Up @@ -267,4 +268,13 @@ export class MockNotificationAdapter implements NotificationAdapterInterface {
});
return Promise.resolve(undefined);
}

notifyProjectOwnershipChange(
params: ProjectOwnershipChangedParams,
): Promise<void> {
logger.debug('MockNotificationAdapter notifyProjectOwnershipChange', {
params,
});
return Promise.resolve(undefined);
}
}
9 changes: 9 additions & 0 deletions src/adapters/notifications/NotificationAdapterInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ export interface ProjectsHaveNewRankingInputParam {
newBottomRank: number;
}

export interface ProjectOwnershipChangedParams {
previousOwnerUser: User | null;
newOwnerUser: User | null;
projectName: string;
}

export interface OrttoPerson {
fields: {
'str::first': string;
Expand Down Expand Up @@ -114,4 +120,7 @@ export interface NotificationAdapterInterface {
params: BroadCastNotificationInputParams,
): Promise<void>;
projectsHaveNewRank(params: ProjectsHaveNewRankingInputParam): Promise<void>;
notifyProjectOwnershipChange(
params: ProjectOwnershipChangedParams,
): Promise<void>;
}
41 changes: 41 additions & 0 deletions src/adapters/notifications/NotificationCenterAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
BroadCastNotificationInputParams,
NotificationAdapterInterface,
OrttoPerson,
ProjectOwnershipChangedParams,
ProjectsHaveNewRankingInputParam,
} from './NotificationAdapterInterface';
import { Donation } from '../../entities/donation';
Expand Down Expand Up @@ -989,6 +990,46 @@ export class NotificationCenterAdapter implements NotificationAdapterInterface {
});
}
}

async notifyProjectOwnershipChange(
params: ProjectOwnershipChangedParams,
): Promise<void> {
try {
const newOwnerPayload = {
email: params.newOwnerUser?.email,
userId: params.newOwnerUser?.id || 0,
ownerName: params.newOwnerUser?.name || '',
projectName: params.projectName,
};

if (newOwnerPayload.email) {
await callSendNotification({
eventName: NOTIFICATIONS_EVENT_NAMES.PROJECT_OWNERSHIP_CHANGED_TO,
segment: {
payload: newOwnerPayload,
},
});
}

const oldOwnerPayload = {
email: params.previousOwnerUser?.email,
userId: params.previousOwnerUser?.id || 0,
ownerName: params.previousOwnerUser?.name || '',
projectName: params.projectName,
};

if (oldOwnerPayload.email) {
await callSendNotification({
eventName: NOTIFICATIONS_EVENT_NAMES.PROJECT_OWNERSHIP_CHANGED_FROM,
segment: {
payload: oldOwnerPayload,
},
});
}
} catch (e) {
logger.error('notifyProjectOwnershipChange >> error', e);
}
}
}

const getEmailDataDonationAttributes = async (params: {
Expand Down
2 changes: 2 additions & 0 deletions src/analytics/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,6 @@ export enum NOTIFICATIONS_EVENT_NAMES {
SUBSCRIBE_ONBOARDING = 'Subscribe onboarding',
CREATE_ORTTO_PROFILE = 'Create Ortto profile',
SEND_EMAIL_CONFIRMATION = 'Send email confirmation',
PROJECT_OWNERSHIP_CHANGED_TO = 'Project ownership changed to',
PROJECT_OWNERSHIP_CHANGED_FROM = 'Project ownership changed from',
}
27 changes: 25 additions & 2 deletions src/server/adminJs/tabs/projectsTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import { User } from '../../../entities/user';
import { refreshProjectEstimatedMatchingView } from '../../../services/projectViewsService';
import { extractAdminJsReferrerUrlParams } from '../adminJs';
import { relateManyProjectsToQfRound } from '../../../repositories/qfRoundRepository2';
import { NotificationCenterAdapter } from '../../../adapters/notifications/NotificationCenterAdapter';

// add queries depending on which filters were selected
export const buildProjectsQuery = (
Expand Down Expand Up @@ -658,6 +659,22 @@ export const exportProjectsWithFiltersToCsv = async (
};
}
};

async function sendOwnershipChangeEmails(
project: Project,
previousOwnerUser: User | null,
newOwnerUser: User | null,
) {
const notificationCenter = new NotificationCenterAdapter();

const params = {
previousOwnerUser,
newOwnerUser,
projectName: project.title,
};
await notificationCenter.notifyProjectOwnershipChange(params);
}

export const projectsTab = {
resource: Project,
options: {
Expand Down Expand Up @@ -1082,9 +1099,10 @@ export const projectsTab = {
});
if (project) {
if (request?.record?.params?.adminChanged) {
const adminUser = await User.findOne({
const newAdminUser = await User.findOne({
where: { id: request?.record?.params?.newAdminId },
});
const previousAdminUser = project.adminUser;
const previousAdminAddress = project.adminUser?.walletAddress;
if (previousAdminAddress) {
if (project.adminAddressHistory) {
Expand All @@ -1093,8 +1111,13 @@ export const projectsTab = {
project.adminAddressHistory = [previousAdminAddress];
}
}
project.adminUser = adminUser!;
project.adminUser = newAdminUser!;
await project.save();
await sendOwnershipChangeEmails(
project,
previousAdminUser,
newAdminUser,
);
}
// Not required for now
// Project.notifySegment(project, SegmentEvents.PROJECT_EDITED);
Expand Down