@@ -6,7 +6,7 @@ import Raven from 'shared/raven';
66import { getCommunityById } from '../models/community' ;
77import { storeNotification } from '../models/notification' ;
88import { storeUsersNotifications } from 'shared/db/queries/usersNotifications' ;
9- import { getUsers } from 'shared/db/queries/user' ;
9+ import { getUserById } from 'shared/db/queries/user' ;
1010import { fetchPayload } from '../utils/payloads' ;
1111import isEmail from 'validator/lib/isEmail' ;
1212import { sendPrivateCommunityRequestApprovedEmailQueue } from 'shared/bull/queues' ;
@@ -41,25 +41,26 @@ export default async (job: Job<PrivateCommunityRequestApprovedJobData>) => {
4141 const updatedNotification = await storeNotification ( nextNotificationRecord ) ;
4242
4343 const community = await getCommunityById ( communityId ) ;
44- const recipients = await getUsers ( [ userId ] ) ;
45- const filteredRecipients = recipients . filter (
46- user => user && isEmail ( user . email )
47- ) ;
48- const usersNotificationPromises = filteredRecipients . map ( recipient =>
49- storeUsersNotifications ( updatedNotification . id , recipient . id )
44+ const recipient = await getUserById ( userId ) ;
45+
46+ const canSendEmail = recipient && recipient . email && isEmail ( recipient . email ) ;
47+
48+ const notificationPromise = storeUsersNotifications (
49+ updatedNotification . id ,
50+ recipient . id
5051 ) ;
5152
52- const usersEmailPromises = filteredRecipients . map ( recipient =>
53+ const emailPromise =
54+ canSendEmail &&
5355 sendPrivateCommunityRequestApprovedEmailQueue . add ( {
5456 // $FlowIssue
5557 recipient,
5658 community,
57- } )
58- ) ;
59+ } ) ;
5960
6061 return await Promise . all ( [
61- ... usersEmailPromises , // handle emails separately
62- ... usersNotificationPromises , // update or store usersNotifications in-app
62+ emailPromise , // handle emails separately
63+ notificationPromise , // update or store usersNotifications in-app
6364 ] ) . catch ( err => {
6465 console . error ( '❌ Error in job:\n' ) ;
6566 console . error ( err ) ;
0 commit comments