diff --git a/api/src/components/notification/bulletin.ts b/api/src/components/notification/bulletin.ts index 9e483e82b..d28a5125d 100644 --- a/api/src/components/notification/bulletin.ts +++ b/api/src/components/notification/bulletin.ts @@ -398,12 +398,7 @@ export const createBulletin = async ( } case I.NotificationActionTypeEnum.PUBLICATION_VERSION_LINKED_PREDECESSOR: { - // We use the previous version because this is the one with the link - if (!previousPublishedVersion) { - break; - } - - const usersToBeNotified = await userService.getUsersWithDirectLinkFromVersion(previousPublishedVersion.id); + const usersToBeNotified = await userService.getUsersWithDirectLinkFromVersion(currentPublishedVersion.id); entries = usersToBeNotified.map((user) => ({ userId: user.id, diff --git a/api/src/components/publicationVersion/controller.ts b/api/src/components/publicationVersion/controller.ts index 72f137a37..9d445ec7a 100644 --- a/api/src/components/publicationVersion/controller.ts +++ b/api/src/components/publicationVersion/controller.ts @@ -2,7 +2,6 @@ import * as I from 'interface'; import * as response from 'lib/response'; import * as publicationVersionService from 'publicationVersion/service'; import * as publicationService from 'publication/service'; -import * as notificationBulletin from 'notification/bulletin'; import * as coAuthorService from 'coAuthor/service'; import * as userService from 'user/service'; import * as Helpers from 'lib/helpers'; @@ -359,53 +358,6 @@ export const updateStatus = async ( event.queryStringParameters.ariContactConsent ); - const excludedUserIds = publicationVersion.coAuthors - .map((coAuthor) => coAuthor.linkedUser) - .filter((i): i is string => i !== null); - - const previousPublishedVersion = await publicationVersionService.getPreviousPublishedVersion( - publicationVersion.versionOf - ); - - await Promise.all([ - // Notifies all users that bookmarked this publication version that a new version is now LIVE. - notificationBulletin.createBulletin( - I.NotificationActionTypeEnum.PUBLICATION_BOOKMARK_VERSION_CREATED, - publicationVersion, - previousPublishedVersion - ), - - // Notify all users that red-flagged this publication version that a new version is now LIVE. - notificationBulletin.createBulletin( - I.NotificationActionTypeEnum.PUBLICATION_VERSION_RED_FLAG_RAISED, - publicationVersion, - previousPublishedVersion - ), - - // Notifies authors that peer-reviewed this publication version that a new version is now LIVE. - notificationBulletin.createBulletin( - I.NotificationActionTypeEnum.PUBLICATION_VERSION_PEER_REVIEWED, - publicationVersion, - previousPublishedVersion - ), - - // Notifies authors of child publications (that link FROM this publication) - notificationBulletin.createBulletin( - I.NotificationActionTypeEnum.PUBLICATION_VERSION_LINKED_PREDECESSOR, - publicationVersion, - previousPublishedVersion, - { excludedUserIds } - ), - - // Notifies authors of parent publications (that this publication links TO) - notificationBulletin.createBulletin( - I.NotificationActionTypeEnum.PUBLICATION_VERSION_LINKED_SUCCESSOR, - publicationVersion, - previousPublishedVersion, - { excludedUserIds } - ) - ]); - return response.json(200, { message: 'Publication is now LIVE.' }); } catch (err) { console.log(err); diff --git a/api/src/components/publicationVersion/service.ts b/api/src/components/publicationVersion/service.ts index 69e68e60f..f06322f3a 100644 --- a/api/src/components/publicationVersion/service.ts +++ b/api/src/components/publicationVersion/service.ts @@ -2,7 +2,7 @@ import nodemailer from 'nodemailer'; import { convert } from 'html-to-text'; import { createId } from '@paralleldrive/cuid2'; import { Prisma } from '@prisma/client'; - +import * as notificationBulletin from 'notification/bulletin'; import * as client from 'lib/client'; import * as doi from 'lib/doi'; import * as email from 'lib/email'; @@ -700,6 +700,54 @@ export const transferOwnership = (publicationVersionId: string, requesterId: str } }); +const createBulletinNotifications = async ( + publicationVersion: I.PublicationVersion, + previousVersion: I.PublicationVersion | null +) => { + const excludedUserIds = publicationVersion.coAuthors + .map((coAuthor) => coAuthor.linkedUser) + .filter((i): i is string => i !== null); + + await Promise.all([ + // Notifies all users that bookmarked this publication version that a new version is now LIVE. + notificationBulletin.createBulletin( + I.NotificationActionTypeEnum.PUBLICATION_BOOKMARK_VERSION_CREATED, + publicationVersion, + previousVersion + ), + + // Notify all users that red-flagged the previous publication version that a new version is now LIVE. + notificationBulletin.createBulletin( + I.NotificationActionTypeEnum.PUBLICATION_VERSION_RED_FLAG_RAISED, + publicationVersion, + previousVersion + ), + + // Notifies authors that peer-reviewed the previous publication version that a new version is now LIVE. + notificationBulletin.createBulletin( + I.NotificationActionTypeEnum.PUBLICATION_VERSION_PEER_REVIEWED, + publicationVersion, + previousVersion + ), + + // Notifies authors of PARENT publications (that link from this publication) + notificationBulletin.createBulletin( + I.NotificationActionTypeEnum.PUBLICATION_VERSION_LINKED_PREDECESSOR, + publicationVersion, + previousVersion, + { excludedUserIds } + ), + + // Notifies authors of CHILD publications (that link to the previous version of this publication) + notificationBulletin.createBulletin( + I.NotificationActionTypeEnum.PUBLICATION_VERSION_LINKED_SUCCESSOR, + publicationVersion, + previousVersion, + { excludedUserIds } + ) + ]); +}; + // Actions that run after a version is published (changes status to LIVE). // Pulled out to a separate function because things may need to run when something is // published immediately (i.e. not going through full drafting workflow) and bypasses the updateStatus function. @@ -822,6 +870,9 @@ export const postPublishHook = async (publicationVersion: I.PublicationVersion, // Complete remaining tasks in parallel. const postDBUpdatePromises: Array> = []; + // Notifications + postDBUpdatePromises.push(createBulletinNotifications(publicationVersion, previousVersion)); + // (Re)index publication in opensearch. postDBUpdatePromises.push( new Promise((resolve) => {