@@ -6,7 +6,7 @@ const Factory = require('./modelFactory');
66const mongo = require ( '../mongo' ) ;
77const Event = require ( '../models/event' ) ;
88const { ObjectID } = require ( 'mongodb' ) ;
9- const { composeFullRepetitionEvent } = require ( '../utils/merge' ) ;
9+ const { composeEventPayloadWithRepetition } = require ( '../utils/merge' ) ;
1010
1111/**
1212 * @typedef {Object } RecentEventSchema
@@ -408,36 +408,14 @@ class EventsFactory extends Factory {
408408
409409 const result = {
410410 repetitions : [ ] ,
411- cursor : null ,
411+ nextCursor : null ,
412412 } ;
413413
414414 /**
415415 * Get original event
416416 * @type {EventSchema }
417417 */
418- let eventOriginal = await this . getCollection ( this . TYPES . EVENTS )
419- . findOne ( {
420- _id : new ObjectID ( eventId ) ,
421- } ) ;
422-
423- /**
424- * If event is not found, try to find it as repetition
425- */
426- if ( ! eventOriginal ) {
427- const repetition = await this . getCollection ( this . TYPES . REPETITIONS )
428- . findOne ( {
429- _id : new ObjectID ( eventId ) ,
430- } ) ;
431-
432- if ( ! repetition ) {
433- return result ;
434- }
435-
436- eventOriginal = await this . getCollection ( this . TYPES . EVENTS )
437- . findOne ( {
438- groupHash : repetition . groupHash ,
439- } ) ;
440- }
418+ const eventOriginal = await this . _findOriginalEvent ( eventId ) ;
441419
442420 if ( ! eventOriginal ) {
443421 return result ;
@@ -457,21 +435,19 @@ class EventsFactory extends Factory {
457435 . toArray ( ) ;
458436
459437 if ( repetitions . length === limit + 1 ) {
460- result . cursor = repetitions . pop ( ) . _id ;
438+ result . nextCursor = repetitions . pop ( ) . _id ;
461439 }
462440
463441 for ( const repetition of repetitions ) {
442+ const event = this . _composeEventWithRepetition ( eventOriginal , repetition ) ;
443+
464444 result . repetitions . push ( {
465- ...eventOriginal ,
466- _id : repetition . _id ,
467- payload : composeFullRepetitionEvent ( eventOriginal , repetition ) . payload ,
468- timestamp : repetition . timestamp ,
469- firstAppearanceTimestamp : eventOriginal . timestamp ,
445+ ...event ,
470446 projectId : this . projectId ,
471447 } ) ;
472448 }
473449
474- const isLastPortion = result . cursor === null ;
450+ const isLastPortion = result . nextCursor === null ;
475451
476452 /**
477453 * For last portion:
@@ -514,14 +490,10 @@ class EventsFactory extends Factory {
514490 */
515491 const event = await this . findById ( repetitionId ) ;
516492
517- if ( ! event ) {
518- return null ;
519- }
520-
521- return {
493+ return event ? {
522494 ...event ,
523495 firstAppearanceTimestamp : event . timestamp ,
524- } ;
496+ } : null ;
525497 }
526498
527499 const originalEvent = await this . getCollection ( this . TYPES . EVENTS )
@@ -533,13 +505,7 @@ class EventsFactory extends Factory {
533505 return null ;
534506 }
535507
536- return {
537- ...originalEvent ,
538- _id : repetition . _id ,
539- payload : composeFullRepetitionEvent ( originalEvent , repetition ) . payload ,
540- timestamp : repetition . timestamp ,
541- firstAppearanceTimestamp : originalEvent . timestamp ,
542- } ;
508+ return this . _composeEventWithRepetition ( originalEvent , repetition ) ;
543509 }
544510
545511 /**
@@ -564,32 +530,10 @@ class EventsFactory extends Factory {
564530 * @returns {Release|null }
565531 */
566532 async getEventRelease ( eventId ) {
567- let eventOriginal = await this . getCollection ( this . TYPES . EVENTS )
568- . findOne ( {
569- _id : new ObjectID ( eventId ) ,
570- } ) ;
533+ const eventOriginal = await this . _findOriginalEvent ( eventId ) ;
571534
572- /**
573- * If event is not found, try to find it as repetition
574- */
575535 if ( ! eventOriginal ) {
576- const repetition = await this . getCollection ( this . TYPES . REPETITIONS )
577- . findOne ( {
578- _id : new ObjectID ( eventId ) ,
579- } ) ;
580-
581- if ( ! repetition ) {
582- return null ;
583- }
584-
585- eventOriginal = await this . getCollection ( this . TYPES . EVENTS )
586- . findOne ( {
587- groupHash : repetition . groupHash ,
588- } ) ;
589-
590- if ( ! eventOriginal ) {
591- return null ;
592- }
536+ return null ;
593537 }
594538
595539 const release = await mongo . databases . events . collection ( this . TYPES . RELEASES ) . findOne ( {
@@ -609,37 +553,15 @@ class EventsFactory extends Factory {
609553 * @return {Promise<void> }
610554 */
611555 async visitEvent ( eventId , userId ) {
612- let event = await this . getCollection ( this . TYPES . EVENTS )
613- . findOne ( {
614- _id : new ObjectID ( eventId ) ,
615- } ) ;
556+ const event = await this . _findOriginalEvent ( eventId ) ;
616557
617- /**
618- * If event is not found, try to find it as repetition
619- */
620558 if ( ! event ) {
621- const repetition = await this . getCollection ( this . TYPES . REPETITIONS )
622- . findOne ( {
623- _id : new ObjectID ( eventId ) ,
624- } ) ;
625-
626- if ( ! repetition ) {
627- return null ;
628- }
629-
630- event = await this . getCollection ( this . TYPES . EVENTS )
631- . findOne ( {
632- groupHash : repetition . groupHash ,
633- } ) ;
634-
635- if ( ! event ) {
636- return null ;
637- }
559+ return null ;
638560 }
639561
640562 return this . getCollection ( this . TYPES . EVENTS )
641563 . updateOne (
642- { _id : new ObjectID ( eventId ) } ,
564+ { _id : new ObjectID ( event . _id ) } ,
643565 { $addToSet : { visitedBy : new ObjectID ( userId ) } }
644566 ) ;
645567 }
@@ -654,29 +576,15 @@ class EventsFactory extends Factory {
654576 */
655577 async toggleEventMark ( eventId , mark ) {
656578 const collection = this . getCollection ( this . TYPES . EVENTS ) ;
657- const query = { _id : new ObjectID ( eventId ) } ;
658579
659- let event = await collection . findOne ( query ) ;
580+ const event = await this . _findOriginalEvent ( eventId ) ;
660581
661- /**
662- * If event is not found, try to find it as repetition
663- */
664582 if ( ! event ) {
665- const repetition = await this . getCollection ( this . TYPES . REPETITIONS )
666- . findOne ( {
667- _id : new ObjectID ( eventId ) ,
668- } ) ;
669-
670- if ( repetition ) {
671- event = await this . getCollection ( this . TYPES . EVENTS )
672- . findOne ( {
673- groupHash : repetition . groupHash ,
674- } ) ;
675-
676- query . _id = new ObjectID ( event . _id ) ;
677- }
583+ return null ;
678584 }
679585
586+ const query = { _id : new ObjectID ( event . _id ) } ;
587+
680588 const markKey = `marks.${ mark } ` ;
681589
682590 let update ;
@@ -726,34 +634,72 @@ class EventsFactory extends Factory {
726634 */
727635 async updateAssignee ( eventId , assignee ) {
728636 const collection = this . getCollection ( this . TYPES . EVENTS ) ;
729- const query = { _id : new ObjectID ( eventId ) } ;
730637
731- let event = await collection . findOne ( query ) ;
638+ const event = await this . _findOriginalEvent ( eventId ) ;
639+
640+ if ( ! event ) {
641+ return null ;
642+ }
643+
644+ const query = { _id : new ObjectID ( event . _id ) } ;
645+
646+ const update = {
647+ $set : { assignee : assignee } ,
648+ } ;
649+
650+ return collection . updateOne ( query , update ) ;
651+ }
652+
653+ /**
654+ * Find original event by eventId. If event is not found directly,
655+ * try to find it as repetition and get original event by groupHash
656+ *
657+ * @param {string|ObjectID } eventId - event's id, may be repetition id
658+ * @returns {Promise<Event|null> } original event or null if not found
659+ */
660+ async _findOriginalEvent ( eventId ) {
661+ let eventOriginal = await this . getCollection ( this . TYPES . EVENTS )
662+ . findOne ( {
663+ _id : new ObjectID ( eventId ) ,
664+ } ) ;
732665
733666 /**
734667 * If event is not found, try to find it as repetition
735668 */
736- if ( ! event ) {
669+ if ( ! eventOriginal ) {
737670 const repetition = await this . getCollection ( this . TYPES . REPETITIONS )
738671 . findOne ( {
739672 _id : new ObjectID ( eventId ) ,
740673 } ) ;
741674
742- if ( repetition ) {
743- event = await this . getCollection ( this . TYPES . EVENTS )
744- . findOne ( {
745- groupHash : repetition . groupHash ,
746- } ) ;
747-
748- query . _id = new ObjectID ( event . _id ) ;
675+ if ( ! repetition ) {
676+ return null ;
749677 }
678+
679+ eventOriginal = await this . getCollection ( this . TYPES . EVENTS )
680+ . findOne ( {
681+ groupHash : repetition . groupHash ,
682+ } ) ;
750683 }
751684
752- const update = {
753- $set : { assignee : assignee } ,
754- } ;
685+ return eventOriginal ;
686+ }
755687
756- return collection . updateOne ( query , update ) ;
688+ /**
689+ * Compose event with repetition
690+ *
691+ * @param {Event } event - event
692+ * @param {Repetition } repetition - repetition
693+ * @returns {Event } event merged with repetition
694+ */
695+ _composeEventWithRepetition ( event , repetition ) {
696+ return {
697+ ...event ,
698+ _id : repetition . _id ,
699+ firstAppearanceTimestamp : event . timestamp ,
700+ timestamp : repetition . timestamp ,
701+ payload : composeEventPayloadWithRepetition ( event . payload , repetition ) ,
702+ } ;
757703 }
758704}
759705
0 commit comments