@@ -392,15 +392,15 @@ class EventsFactory extends Factory {
392392 /**
393393 * Returns Event repetitions
394394 *
395- * @param {string|ObjectID } groupHash - Event's group hash
395+ * @param {string|ObjectID } eventId - Event's id (may be repetition id)
396396 * @param {Number } limit - count limitations
397397 * @param {Number } cursor - selection offset
398398 *
399399 * @return {EventRepetitionSchema[] }
400400 *
401401 * @todo move to Repetitions(?) model
402402 */
403- async getEventRepetitionsByGroupHash ( groupHash , limit = 10 , cursor = undefined ) {
403+ async getEventRepetitions ( eventId , limit = 10 , cursor = undefined ) {
404404 limit = this . validateLimit ( limit ) ;
405405
406406 cursor = cursor ? new ObjectID ( cursor ) : undefined ;
@@ -414,11 +414,30 @@ class EventsFactory extends Factory {
414414 * Get original event
415415 * @type {EventSchema }
416416 */
417- const eventOriginal = await this . getCollection ( this . TYPES . EVENTS )
417+ let eventOriginal = await this . getCollection ( this . TYPES . EVENTS )
418418 . findOne ( {
419- groupHash : groupHash ,
419+ _id : new ObjectID ( eventId ) ,
420420 } ) ;
421421
422+ /**
423+ * If event is not found, try to find it as repetition
424+ */
425+ if ( ! eventOriginal ) {
426+ const repetition = await this . getCollection ( this . TYPES . REPETITIONS )
427+ . findOne ( {
428+ _id : new ObjectID ( eventId ) ,
429+ } ) ;
430+
431+ if ( ! repetition ) {
432+ return result ;
433+ }
434+
435+ eventOriginal = await this . getCollection ( this . TYPES . EVENTS )
436+ . findOne ( {
437+ _id : repetition . eventId ,
438+ } ) ;
439+ }
440+
422441 if ( ! eventOriginal ) {
423442 return result ;
424443 }
@@ -538,12 +557,35 @@ class EventsFactory extends Factory {
538557 * @param {string } groupHash - hash of event to get the release
539558 * @returns {Release|null }
540559 */
541- async getEventRelease ( groupHash ) {
560+ async getEventRelease ( eventId ) {
542561 const eventOriginal = await this . getCollection ( this . TYPES . EVENTS )
543562 . findOne ( {
544- groupHash : groupHash ,
563+ _id : new ObjectID ( eventId ) ,
545564 } ) ;
546565
566+ /**
567+ * If event is not found, try to find it as repetition
568+ */
569+ if ( ! eventOriginal ) {
570+ const repetition = await this . getCollection ( this . TYPES . REPETITIONS )
571+ . findOne ( {
572+ _id : new ObjectID ( eventId ) ,
573+ } ) ;
574+
575+ if ( ! repetition ) {
576+ return null ;
577+ }
578+
579+ eventOriginal = await this . getCollection ( this . TYPES . EVENTS )
580+ . findOne ( {
581+ _id : repetition . eventId ,
582+ } ) ;
583+
584+ if ( ! eventOriginal ) {
585+ return null ;
586+ }
587+ }
588+
547589 const release = await mongo . databases . events . collection ( this . TYPES . RELEASES ) . findOne ( {
548590 release : eventOriginal . payload . release ,
549591 projectId : this . projectId . toString ( ) ,
@@ -555,15 +597,43 @@ class EventsFactory extends Factory {
555597 /**
556598 * Mark event as visited for passed user
557599 *
558- * @param {string|ObjectId } groupHash
600+ * @param {string|ObjectId } eventId
559601 * @param {string|ObjectId } userId
560602 *
561603 * @return {Promise<void> }
562604 */
563- async visitEvent ( groupHash , userId ) {
605+ async visitEvent ( eventId , userId ) {
606+ let event = await this . getCollection ( this . TYPES . EVENTS )
607+ . findOne ( {
608+ _id : new ObjectID ( eventId ) ,
609+ } ) ;
610+
611+ /**
612+ * If event is not found, try to find it as repetition
613+ */
614+ if ( ! event ) {
615+ const repetition = await this . getCollection ( this . TYPES . REPETITIONS )
616+ . findOne ( {
617+ _id : new ObjectID ( eventId ) ,
618+ } ) ;
619+
620+ if ( ! repetition ) {
621+ return null ;
622+ }
623+
624+ event = await this . getCollection ( this . TYPES . EVENTS )
625+ . findOne ( {
626+ _id : repetition . eventId ,
627+ } ) ;
628+
629+ if ( ! event ) {
630+ return null ;
631+ }
632+ }
633+
564634 return this . getCollection ( this . TYPES . EVENTS )
565635 . updateOne (
566- { groupHash : groupHash } ,
636+ { _id : new ObjectID ( eventId ) } ,
567637 { $addToSet : { visitedBy : new ObjectID ( userId ) } }
568638 ) ;
569639 }
@@ -576,11 +646,32 @@ class EventsFactory extends Factory {
576646 *
577647 * @return {Promise<void> }
578648 */
579- async toggleEventMark ( groupHash , mark ) {
649+ async toggleEventMark ( eventId , mark ) {
580650 const collection = this . getCollection ( this . TYPES . EVENTS ) ;
581- const query = { groupHash : groupHash } ;
651+ const query = { _id : new ObjectID ( eventId ) } ;
582652
583653 const event = await collection . findOne ( query ) ;
654+
655+
656+ /**
657+ * If event is not found, try to find it as repetition
658+ */
659+ if ( ! event ) {
660+ const repetition = await this . getCollection ( this . TYPES . REPETITIONS )
661+ . findOne ( {
662+ _id : new ObjectID ( eventId ) ,
663+ } ) ;
664+
665+ if ( repetition ) {
666+ event = await this . getCollection ( this . TYPES . EVENTS )
667+ . findOne ( {
668+ _id : repetition . eventId ,
669+ } ) ;
670+
671+ query . _id = new ObjectID ( repetition . eventId ) ;
672+ }
673+ }
674+
584675 const markKey = `marks.${ mark } ` ;
585676
586677 let update ;
@@ -624,13 +715,35 @@ class EventsFactory extends Factory {
624715 /**
625716 * Update assignee to selected event
626717 *
627- * @param {string } groupHash - event id
718+ * @param {string } eventId - event id
628719 * @param {string } assignee - assignee id for this event
629720 * @return {Promise<void> }
630721 */
631- async updateAssignee ( groupHash , assignee ) {
722+ async updateAssignee ( eventId , assignee ) {
632723 const collection = this . getCollection ( this . TYPES . EVENTS ) ;
633- const query = { groupHash : groupHash } ;
724+ const query = { _id : new ObjectID ( eventId ) } ;
725+
726+ const event = await collection . findOne ( query ) ;
727+
728+ /**
729+ * If event is not found, try to find it as repetition
730+ */
731+ if ( ! event ) {
732+ const repetition = await this . getCollection ( this . TYPES . REPETITIONS )
733+ . findOne ( {
734+ _id : new ObjectID ( eventId ) ,
735+ } ) ;
736+
737+ if ( repetition ) {
738+ event = await this . getCollection ( this . TYPES . EVENTS )
739+ . findOne ( {
740+ _id : repetition . eventId ,
741+ } ) ;
742+
743+ query . _id = new ObjectID ( repetition . eventId ) ;
744+ }
745+ }
746+
634747 const update = {
635748 $set : { assignee : assignee } ,
636749 } ;
0 commit comments