@@ -86,6 +86,7 @@ import { updateUserTotalDonated } from '../services/userService';
86
86
import { MainCategory } from '../entities/mainCategory' ;
87
87
import { getNotificationAdapter } from '../adapters/adaptersFactory' ;
88
88
import { findProjectUpdatesByProjectId } from '../repositories/projectUpdateRepository' ;
89
+ import { STATUS_CODES } from 'http' ;
89
90
90
91
// use redis for session data instead of in-memory storage
91
92
// tslint:disable-next-line:no-var-requires
@@ -1372,8 +1373,58 @@ const getAdminBroInstance = async () => {
1372
1373
response ,
1373
1374
context : AdminBroContextInterface ,
1374
1375
) => {
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 ;
1377
1428
} ,
1378
1429
after : async (
1379
1430
request : AdminBroRequestInterface ,
@@ -1393,6 +1444,46 @@ const getAdminBroInstance = async () => {
1393
1444
userId : currentAdmin . id ,
1394
1445
description : HISTORY_DESCRIPTIONS . HAS_BEEN_EDITED ,
1395
1446
} ) ;
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
+ } ) ;
1396
1487
}
1397
1488
1398
1489
return request ;
0 commit comments