Skip to content

Commit 864b97e

Browse files
Send notifications to project
related to #729
1 parent 9cf7368 commit 864b97e

File tree

1 file changed

+93
-2
lines changed

1 file changed

+93
-2
lines changed

src/server/adminBro.ts

+93-2
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ import { updateUserTotalDonated } from '../services/userService';
8686
import { MainCategory } from '../entities/mainCategory';
8787
import { getNotificationAdapter } from '../adapters/adaptersFactory';
8888
import { findProjectUpdatesByProjectId } from '../repositories/projectUpdateRepository';
89+
import { STATUS_CODES } from 'http';
8990

9091
// use redis for session data instead of in-memory storage
9192
// tslint:disable-next-line:no-var-requires
@@ -1372,8 +1373,58 @@ const getAdminBroInstance = async () => {
13721373
response,
13731374
context: AdminBroContextInterface,
13741375
) => {
1375-
const { currentAdmin } = context;
1376-
logger.info('Edit project ', request.payload);
1376+
const { verified, listed } = request.payload;
1377+
const statusChanges: string[] = [];
1378+
if (request?.payload?.id) {
1379+
const project = await findProjectById(
1380+
Number(request.payload.id),
1381+
);
1382+
if (
1383+
Number(request?.payload?.statusId) !== project?.status?.id
1384+
) {
1385+
switch (Number(request?.payload?.statusId)) {
1386+
case ProjStatus.active:
1387+
statusChanges.push(
1388+
NOTIFICATIONS_EVENT_NAMES.PROJECT_ACTIVATED,
1389+
);
1390+
break;
1391+
case ProjStatus.deactive:
1392+
statusChanges.push(
1393+
NOTIFICATIONS_EVENT_NAMES.PROJECT_DEACTIVATED,
1394+
);
1395+
break;
1396+
case ProjStatus.cancelled:
1397+
statusChanges.push(
1398+
NOTIFICATIONS_EVENT_NAMES.PROJECT_CANCELLED,
1399+
);
1400+
break;
1401+
}
1402+
}
1403+
if (project?.verified && !verified) {
1404+
statusChanges.push(
1405+
NOTIFICATIONS_EVENT_NAMES.PROJECT_UNVERIFIED,
1406+
);
1407+
}
1408+
if (!project?.verified && verified) {
1409+
statusChanges.push(
1410+
NOTIFICATIONS_EVENT_NAMES.PROJECT_VERIFIED,
1411+
);
1412+
}
1413+
if (project?.listed && !listed) {
1414+
statusChanges.push(
1415+
NOTIFICATIONS_EVENT_NAMES.PROJECT_UNLISTED,
1416+
);
1417+
}
1418+
if (!project?.listed && listed) {
1419+
statusChanges.push(
1420+
NOTIFICATIONS_EVENT_NAMES.PROJECT_LISTED,
1421+
);
1422+
}
1423+
1424+
// We put these status changes in payload, so in after hook we would know to send notification for users
1425+
request.payload.statusChanges = statusChanges.join(',');
1426+
}
1427+
return request;
13771428
},
13781429
after: async (
13791430
request: AdminBroRequestInterface,
@@ -1393,6 +1444,46 @@ const getAdminBroInstance = async () => {
13931444
userId: currentAdmin.id,
13941445
description: HISTORY_DESCRIPTIONS.HAS_BEEN_EDITED,
13951446
});
1447+
const statusChanges =
1448+
request?.record?.params?.statusChanges?.split(',') || [];
1449+
1450+
const eventAndHandlers = [
1451+
{
1452+
event: NOTIFICATIONS_EVENT_NAMES.PROJECT_VERIFIED,
1453+
handler: getNotificationAdapter().projectVerified,
1454+
},
1455+
{
1456+
event: NOTIFICATIONS_EVENT_NAMES.PROJECT_UNVERIFIED,
1457+
handler: getNotificationAdapter().projectUnVerified,
1458+
},
1459+
{
1460+
event: NOTIFICATIONS_EVENT_NAMES.PROJECT_LISTED,
1461+
handler: getNotificationAdapter().projectListed,
1462+
},
1463+
{
1464+
event: NOTIFICATIONS_EVENT_NAMES.PROJECT_UNLISTED,
1465+
handler: getNotificationAdapter().projectDeListed,
1466+
},
1467+
{
1468+
event: NOTIFICATIONS_EVENT_NAMES.PROJECT_ACTIVATED,
1469+
handler: getNotificationAdapter().projectReactivated,
1470+
},
1471+
{
1472+
event: NOTIFICATIONS_EVENT_NAMES.PROJECT_DEACTIVATED,
1473+
handler: getNotificationAdapter().projectDeactivated,
1474+
},
1475+
{
1476+
event: NOTIFICATIONS_EVENT_NAMES.PROJECT_CANCELLED,
1477+
handler: getNotificationAdapter().projectCancelled,
1478+
},
1479+
];
1480+
1481+
eventAndHandlers.forEach(eventHandler => {
1482+
if (statusChanges?.includes(eventHandler.event)) {
1483+
// Dont put await before that intentionally to not block admin panel response with that
1484+
eventHandler.handler({ project });
1485+
}
1486+
});
13961487
}
13971488

13981489
return request;

0 commit comments

Comments
 (0)