11const getEventsFactory = require ( './helpers/eventsFactory' ) . default ;
2- const sendPersonalNotification = require ( '../utils/personalNotifications' ) . default ;
2+ const {
3+ fireAndForgetAssigneeNotifications,
4+ parseBulkEventIds,
5+ mergeFailedEventIds,
6+ } = require ( './helpers/bulkEvents' ) ;
37const { aiService } = require ( '../services/ai' ) ;
48const { UserInputError } = require ( 'apollo-server-express' ) ;
59
6- /**
7- * Enqueue assignee notifications in background (do not block resolver response)
8- *
9- * @param {object } args - notification args
10- * @param {object } args.assigneeData - assigned user data
11- * @param {string[] } args.eventIds - original event ids
12- * @param {string } args.projectId - project id
13- * @param {string } args.assigneeId - assignee id
14- * @param {string } args.whoAssignedId - user id who performed assignment
15- * @returns {void }
16- */
17- function fireAndForgetAssigneeNotifications ( {
18- assigneeData,
19- eventIds,
20- projectId,
21- assigneeId,
22- whoAssignedId,
23- } ) {
24- void Promise . allSettled ( eventIds . map ( eventId => sendPersonalNotification ( assigneeData , {
25- type : 'assignee' ,
26- payload : {
27- assigneeId,
28- projectId,
29- whoAssignedId,
30- eventId,
31- } ,
32- } ) ) ) . catch ( ( error ) => {
33- console . error ( 'Failed to enqueue assignee notifications' , error ) ;
34- } ) ;
35- }
36-
3710/**
3811 * See all types and fields here {@see ../typeDefs/event.graphql}
3912 */
@@ -201,13 +174,23 @@ module.exports = {
201174 throw new UserInputError ( 'bulkToggleEventMarks supports only resolved, ignored and starred marks' ) ;
202175 }
203176
204- if ( ! eventIds || ! eventIds . length ) {
205- throw new UserInputError ( 'eventIds must contain at least one id' ) ;
177+ const { validEventIds, invalidEventIds } = parseBulkEventIds ( eventIds ) ;
178+
179+ if ( validEventIds . length === 0 ) {
180+ return {
181+ updatedCount : 0 ,
182+ updatedEventIds : [ ] ,
183+ failedEventIds : invalidEventIds ,
184+ } ;
206185 }
207186
208187 const factory = getEventsFactory ( context , projectId ) ;
188+ const result = await factory . bulkToggleEventMark ( validEventIds , mark ) ;
209189
210- return factory . bulkToggleEventMark ( eventIds , mark ) ;
190+ return {
191+ ...result ,
192+ failedEventIds : mergeFailedEventIds ( result , invalidEventIds ) ,
193+ } ;
211194 } ,
212195
213196 /**
@@ -296,12 +279,18 @@ module.exports = {
296279 */
297280 async bulkUpdateAssignee ( _obj , { input } , { factories, user, ...context } ) {
298281 const { projectId, eventIds, assignee } = input ;
299- const factory = getEventsFactory ( context , projectId ) ;
282+ const { validEventIds , invalidEventIds } = parseBulkEventIds ( eventIds ) ;
300283
301- if ( ! eventIds || ! eventIds . length ) {
302- throw new UserInputError ( 'eventIds must contain at least one id' ) ;
284+ if ( validEventIds . length === 0 ) {
285+ return {
286+ updatedCount : 0 ,
287+ updatedEventIds : [ ] ,
288+ failedEventIds : invalidEventIds ,
289+ } ;
303290 }
304291
292+ const factory = getEventsFactory ( context , projectId ) ;
293+
305294 if ( assignee ) {
306295 const userExists = await factories . usersFactory . findById ( assignee ) ;
307296
@@ -318,14 +307,18 @@ module.exports = {
318307 }
319308 }
320309
321- const result = await factory . bulkUpdateAssignee ( eventIds , assignee ) ;
310+ const result = await factory . bulkUpdateAssignee ( validEventIds , assignee ) ;
311+ const resultWithInvalid = {
312+ ...result ,
313+ failedEventIds : mergeFailedEventIds ( result , invalidEventIds ) ,
314+ } ;
322315
323- if ( assignee && result . updatedEventIds . length > 0 ) {
316+ if ( assignee && resultWithInvalid . updatedEventIds . length > 0 ) {
324317 void factories . usersFactory . dataLoaders . userById . load ( assignee )
325318 . then ( ( assigneeData ) => {
326319 fireAndForgetAssigneeNotifications ( {
327320 assigneeData,
328- eventIds : result . updatedEventIds ,
321+ eventIds : resultWithInvalid . updatedEventIds ,
329322 projectId,
330323 assigneeId : assignee ,
331324 whoAssignedId : user . id ,
@@ -336,7 +329,7 @@ module.exports = {
336329 } ) ;
337330 }
338331
339- return result ;
332+ return resultWithInvalid ;
340333 } ,
341334 } ,
342335} ;
0 commit comments