@@ -6,6 +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' ) ;
910
1011/**
1112 * @typedef {Object } RecentEventSchema
@@ -399,15 +400,29 @@ class EventsFactory extends Factory {
399400 *
400401 * @todo move to Repetitions(?) model
401402 */
402- async getEventRepetitions ( eventId , limit = 10 , skip = 0 ) {
403+ async getEventRepetitionsByGroupHash ( groupHash , limit = 10 , cursor = undefined ) {
403404 limit = this . validateLimit ( limit ) ;
404405 skip = this . validateSkip ( skip ) ;
405406
407+ cursor = cursor ? new ObjectID ( cursor ) : undefined ;
408+
409+ const result = {
410+ repetitions : [ ] ,
411+ cursor : undefined ,
412+ } ;
413+
406414 /**
407415 * Get original event
408416 * @type {EventSchema }
409417 */
410- const eventOriginal = await this . findById ( eventId ) ;
418+ const eventOriginal = await this . getCollection ( this . TYPES . EVENTS )
419+ . findOne ( {
420+ groupHash : groupHash ,
421+ } ) ;
422+
423+ if ( ! eventOriginal ) {
424+ return result ;
425+ }
411426
412427 /**
413428 * Collect repetitions
@@ -416,13 +431,27 @@ class EventsFactory extends Factory {
416431 const repetitions = await this . getCollection ( this . TYPES . REPETITIONS )
417432 . find ( {
418433 groupHash : eventOriginal . groupHash ,
434+ _id : cursor ? { $lte : cursor } : { } ,
419435 } )
420436 . sort ( { _id : - 1 } )
421- . limit ( limit )
422- . skip ( skip )
437+ . limit ( limit + 1 )
423438 . toArray ( ) ;
424439
425- const isLastPortion = repetitions . length < limit && skip === 0 ;
440+ if ( repetitions . length === limit + 1 ) {
441+ result . cursor = repetitions . pop ( ) . _id ;
442+ }
443+
444+ for ( const repetition of repetitions ) {
445+ result . repetitions . push ( {
446+ ...eventOriginal ,
447+ _id : repetition . _id ,
448+ payload : composeFullRepetitionEvent ( eventOriginal , repetition ) . payload ,
449+ timestamp : repetition . timestamp ,
450+ firstAppearanceTimestamp : eventOriginal . timestamp ,
451+ } ) ;
452+ }
453+
454+ const isLastPortion = repetitions . length < limit ;
426455
427456 /**
428457 * For last portion:
@@ -434,16 +463,14 @@ class EventsFactory extends Factory {
434463 * @type {EventRepetitionSchema }
435464 */
436465 const firstRepetition = {
437- _id : eventOriginal . _id ,
438- payload : eventOriginal . payload ,
439- groupHash : eventOriginal . groupHash ,
440- timestamp : eventOriginal . timestamp ,
466+ ...eventOriginal ,
467+ firstAppearanceTimestamp : eventOriginal . timestamp ,
441468 } ;
442469
443- repetitions . push ( firstRepetition ) ;
470+ result . repetitions . push ( firstRepetition ) ;
444471 }
445472
446- return repetitions ;
473+ return result ;
447474 }
448475
449476 /**
@@ -455,10 +482,40 @@ class EventsFactory extends Factory {
455482 * @todo move to Repetitions(?) model
456483 */
457484 async getEventRepetition ( repetitionId ) {
458- return this . getCollection ( this . TYPES . REPETITIONS )
485+ const repetition = await this . getCollection ( this . TYPES . REPETITIONS )
459486 . findOne ( {
460487 _id : ObjectID ( repetitionId ) ,
461488 } ) ;
489+
490+ if ( ! repetition ) {
491+ /**
492+ * If repetition is not found, it can mean that client is trying to get original event
493+ */
494+ const event = await this . findById ( repetitionId ) ;
495+
496+ if ( ! event ) {
497+ return null ;
498+ }
499+
500+ return {
501+ ...event ,
502+ firstAppearanceTimestamp : event . timestamp ,
503+ } ;
504+ }
505+
506+ const originalEvent = await this . findById ( repetition . eventId ) ;
507+
508+ if ( ! originalEvent ) {
509+ return null ;
510+ }
511+
512+ return {
513+ ...originalEvent ,
514+ _id : repetition . _id ,
515+ payload : composeFullRepetitionEvent ( originalEvent , repetition ) . payload ,
516+ timestamp : repetition . timestamp ,
517+ firstAppearanceTimestamp : originalEvent . timestamp ,
518+ } ;
462519 }
463520
464521 /**
@@ -479,11 +536,14 @@ class EventsFactory extends Factory {
479536 /**
480537 * Get a release from corresponding to this event
481538 *
482- * @param {string } eventId - id of event to get the release
539+ * @param {string } groupHash - hash of event to get the release
483540 * @returns {Release|null }
484541 */
485- async getEventRelease ( eventId ) {
486- const eventOriginal = await this . findById ( eventId ) ;
542+ async getEventRelease ( groupHash ) {
543+ const eventOriginal = await this . getCollection ( this . TYPES . EVENTS )
544+ . findOne ( {
545+ groupHash : groupHash ,
546+ } ) ;
487547
488548 const release = await mongo . databases . events . collection ( this . TYPES . RELEASES ) . findOne ( {
489549 release : eventOriginal . payload . release ,
@@ -496,30 +556,30 @@ class EventsFactory extends Factory {
496556 /**
497557 * Mark event as visited for passed user
498558 *
499- * @param {string|ObjectId } eventId
559+ * @param {string|ObjectId } groupHash
500560 * @param {string|ObjectId } userId
501561 *
502562 * @return {Promise<void> }
503563 */
504- async visitEvent ( eventId , userId ) {
564+ async visitEvent ( groupHash , userId ) {
505565 return this . getCollection ( this . TYPES . EVENTS )
506566 . updateOne (
507- { _id : new ObjectID ( eventId ) } ,
567+ { groupHash : groupHash } ,
508568 { $addToSet : { visitedBy : new ObjectID ( userId ) } }
509569 ) ;
510570 }
511571
512572 /**
513573 * Mark or unmark event as Resolved, Ignored or Starred
514574 *
515- * @param {string|ObjectId } eventId - event to mark
575+ * @param {string|ObjectId } groupHash - event to mark
516576 * @param {string } mark - mark label
517577 *
518578 * @return {Promise<void> }
519579 */
520- async toggleEventMark ( eventId , mark ) {
580+ async toggleEventMark ( groupHash , mark ) {
521581 const collection = this . getCollection ( this . TYPES . EVENTS ) ;
522- const query = { _id : new ObjectID ( eventId ) } ;
582+ const query = { groupHash : groupHash } ;
523583
524584 const event = await collection . findOne ( query ) ;
525585 const markKey = `marks.${ mark } ` ;
@@ -565,13 +625,13 @@ class EventsFactory extends Factory {
565625 /**
566626 * Update assignee to selected event
567627 *
568- * @param {string } eventId - event id
628+ * @param {string } groupHash - event id
569629 * @param {string } assignee - assignee id for this event
570630 * @return {Promise<void> }
571631 */
572- async updateAssignee ( eventId , assignee ) {
632+ async updateAssignee ( groupHash , assignee ) {
573633 const collection = this . getCollection ( this . TYPES . EVENTS ) ;
574- const query = { _id : new ObjectID ( eventId ) } ;
634+ const query = { groupHash : groupHash } ;
575635 const update = {
576636 $set : { assignee : assignee } ,
577637 } ;
0 commit comments