11const getEventsFactory = require ( './helpers/eventsFactory' ) . default ;
2- const sendPersonalNotification = require ( '../utils/personalNotifications' ) . default ;
2+ const {
3+ parseBulkEventIds,
4+ enqueueAssigneeNotification,
5+ } = require ( './helpers/bulkEventUtils' ) ;
36const { aiService } = require ( '../services/ai' ) ;
7+ const { UserInputError } = require ( 'apollo-server-express' ) ;
8+ const { ObjectId } = require ( 'mongodb' ) ;
49
510/**
611 * See all types and fields here {@see ../typeDefs/event.graphql}
@@ -135,6 +140,26 @@ module.exports = {
135140
136141 return ! ! result . acknowledged ;
137142 } ,
143+ /**
144+ * Mark many original events as visited for current user
145+ *
146+ * @param {ResolverObj } _obj - resolver context
147+ * @param {string } projectId - project id
148+ * @param {string[] } eventIds - original event ids
149+ * @param {UserInContext } user - user context
150+ * @returns {Promise<{ success: boolean, modifiedCount: number }> }
151+ */
152+ async bulkVisitEvents ( _obj , { projectId, eventIds } , { user, ...context } ) {
153+ const validEventIds = parseBulkEventIds ( eventIds ) ;
154+
155+ const factory = getEventsFactory ( context , projectId ) ;
156+ const result = await factory . bulkVisitEvents ( validEventIds , user . id ) ;
157+
158+ return {
159+ success : ! ! result . acknowledged ,
160+ modifiedCount : result . modifiedCount || 0 ,
161+ } ;
162+ } ,
138163
139164 /**
140165 * Mark event with one of the event marks
@@ -153,6 +178,29 @@ module.exports = {
153178 return ! ! result . acknowledged ;
154179 } ,
155180
181+ /**
182+ * Bulk set or clear mark for original events.
183+ *
184+ * @param {ResolverObj } _obj - resolver context
185+ * @param {string } projectId - project id
186+ * @param {string[] } eventIds - original event ids
187+ * @param {string } mark - EventMark enum value
188+ * @param {boolean } enabled - true to set mark, false to remove
189+ * @param {object } context - gql context
190+ * @return {Promise<{ success: boolean, modifiedCount: number }> }
191+ */
192+ async bulkSetEventMarks ( _obj , { projectId, eventIds, mark, enabled } , context ) {
193+ const validEventIds = parseBulkEventIds ( eventIds ) ;
194+
195+ const factory = getEventsFactory ( context , projectId ) ;
196+ const result = await factory . bulkSetEventMarks ( validEventIds , mark , enabled ) ;
197+
198+ return {
199+ success : ! ! result . acknowledged ,
200+ modifiedCount : result . modifiedCount || 0 ,
201+ } ;
202+ } ,
203+
156204 /**
157205 * Remove event and all related data (repetitions, daily events)
158206 *
@@ -212,14 +260,12 @@ module.exports = {
212260
213261 const assigneeData = await factories . usersFactory . dataLoaders . userById . load ( assignee ) ;
214262
215- await sendPersonalNotification ( assigneeData , {
216- type : 'assignee' ,
217- payload : {
218- assigneeId : assignee ,
219- projectId,
220- whoAssignedId : user . id ,
221- eventId,
222- } ,
263+ enqueueAssigneeNotification ( {
264+ assigneeData,
265+ assigneeId : assignee ,
266+ projectId,
267+ whoAssignedId : user . id ,
268+ eventId,
223269 } ) ;
224270
225271 return {
@@ -246,5 +292,62 @@ module.exports = {
246292 success : ! ! result . acknowledged ,
247293 } ;
248294 } ,
295+
296+ /**
297+ * Bulk set/clear assignee for selected original events
298+ *
299+ * @param {ResolverObj } _obj - resolver context
300+ * @param {BulkUpdateAssigneeInput } input - object of arguments
301+ * @param factories - factories for working with models
302+ * @return {Promise<{ success: boolean, modifiedCount: number }> }
303+ */
304+ async bulkUpdateAssignee ( _obj , { input } , { factories, user, ...context } ) {
305+ const { projectId, eventIds, assignee } = input ;
306+ const validEventIds = parseBulkEventIds ( eventIds ) ;
307+ let assigneeData = null ;
308+
309+ const factory = getEventsFactory ( context , projectId ) ;
310+
311+ if ( assignee ) {
312+ if ( ! ObjectId . isValid ( String ( assignee ) ) ) {
313+ throw new UserInputError ( 'assignee must be a valid id or null' ) ;
314+ }
315+
316+ const userExists = await factories . usersFactory . findById ( assignee ) ;
317+
318+ if ( ! userExists ) {
319+ throw new UserInputError ( 'assignee not found' ) ;
320+ }
321+
322+ assigneeData = userExists ;
323+
324+ const project = await factories . projectsFactory . findById ( projectId ) ;
325+ const workspace = await factories . workspacesFactory . findById ( project . workspaceId ) ;
326+ const assigneeExistsInWorkspace = await workspace . getMemberInfo ( assignee ) ;
327+
328+ if ( ! assigneeExistsInWorkspace ) {
329+ throw new UserInputError ( 'assignee is not a workspace member' ) ;
330+ }
331+ }
332+
333+ const result = await factory . bulkUpdateAssignee ( validEventIds , assignee ) ;
334+
335+ if ( assignee && result . modifiedCount > 0 ) {
336+ validEventIds . forEach ( ( eventId ) => {
337+ enqueueAssigneeNotification ( {
338+ assigneeData,
339+ assigneeId : assignee ,
340+ projectId,
341+ whoAssignedId : user . id ,
342+ eventId,
343+ } ) ;
344+ } ) ;
345+ }
346+
347+ return {
348+ success : ! ! result . acknowledged ,
349+ modifiedCount : result . modifiedCount || 0 ,
350+ } ;
351+ } ,
249352 } ,
250353} ;
0 commit comments