@@ -6,21 +6,41 @@ const Factory = require('./modelFactory');
66const mongo = require ( '../mongo' ) ;
77const Event = require ( '../models/event' ) ;
88const { ObjectID } = require ( 'mongodb' ) ;
9- const { composeEventPayloadWithRepetition } = require ( '../utils/merge' ) ;
9+ const { composeEventPayloadByRepetition } = require ( '../utils/merge' ) ;
1010
1111/**
1212 * @typedef {Object } EventRepetitionSchema
1313 * @property {String } _id — repetition's identifier
1414 * @property {String } groupHash - event's hash. Generates according to the rule described in EventSchema
1515 * @property {EventPayload } payload - repetition's payload
1616 * @property {Number } timestamp - repetition's Unix timestamp
17+ * @property {Number } originalTimestamp - UNIX timestmap of the original event
18+ * @property {String } originalEventId - id of the original event
19+ * @property {String } projectId - id of the project, which repetition it is
1720 */
1821
1922/**
20- * @typedef {Object } DaylyEventsSchema
21- * @property {Event } event - original event of the daily one
22- * @property {EventRepetitionSchema } repetition - last repetition of the day
23- * @property {String | null } nextCursor - pointer to the next dailyEvent for pagination
23+ * @typedef {Object } EventRepetitionsPortionSchema
24+ * @property {EventRepetitionSchema[] } repetitions - list of repetitions
25+ * @property {String | null } nextCursor - pointer to the first repetition of the next portion, null if there are no repetitions left
26+ */
27+
28+ /**
29+ * @typedef {Object } DailyEventSchema
30+ * @property {String } _id - id of the dailyEvent
31+ * @property {String } groupHash - group hash of the dailyEvent
32+ * @property {Number } groupingTimestamp - UNIX timestamp that represents the day of dailyEvent
33+ * @property {Number } affectedUsers - number of users affected this day
34+ * @property {Number } count - number of events this day
35+ * @property {String } lastRepetitionId - id of the last repetition this day
36+ * @property {Number } lastRepetitionTime - UNIX timestamp that represent time of the last repetition this day
37+ * @property {Event } event - one certain event that represents all of the repetitions this day
38+ */
39+
40+ /**
41+ * @typedef {Object } DaylyEventsPortionSchema
42+ * @property {DailyEventSchema[] } dailyEvents - original event of the daily one
43+ * @property {String | null } nextCursor - pointer to the first dailyEvent of the next portion, null if there are no dailyEvents left
2444 */
2545
2646/**
@@ -154,9 +174,9 @@ class EventsFactory extends Factory {
154174 * @param {EventsFilters } filters - marks by which events should be filtered
155175 * @param {String } search - Search query
156176 *
157- * @return {RecentEventSchema[] }
177+ * @return {DaylyEventsPortionSchema }
158178 */
159- async findRecentDailyEventsWithEventAndRepetition (
179+ async findDailyEventsPortion (
160180 limit = 10 ,
161181 paginationCursor = null ,
162182 sort = 'BY_DATE' ,
@@ -302,13 +322,13 @@ class EventsFactory extends Factory {
302322 const repetition = dailyEvent . repetition ;
303323 const event = dailyEvent . event ;
304324
305- return {
306- ... dailyEvent ,
307- id : dailyEvent . _id . toString ( ) ,
308- _id : undefined ,
309- event : this . _composeEventWithRepetition ( event , repetition ) ,
310- repetition : undefined ,
311- } ;
325+ dailyEvent . event = this . _composeEventWithRepetition ( event , repetition ) ;
326+ dailyEvent . id = dailyEvent . _id . toString ( ) ;
327+
328+ delete dailyEvent . repetition ;
329+ delete dailyEvent . _id ;
330+
331+ return dailyEvent ;
312332 } ) ;
313333
314334 return {
@@ -415,13 +435,11 @@ class EventsFactory extends Factory {
415435 /**
416436 * Returns Event repetitions
417437 *
418- * @param {string|ObjectID } eventId - Event's id (may be repetition id)
438+ * @param {string|ObjectID } eventId - Event's id, could be repetitionId in case when we want to get repetitions portion by one repetition
419439 * @param {Number } limit - count limitations
420440 * @param {Number } cursor - pointer to the next repetition
421441 *
422- * @return {EventRepetitionSchema[] }
423- *
424- * @todo move to Repetitions(?) model
442+ * @return {EventRepetitionsPortionSchema }
425443 */
426444 async getEventRepetitions ( eventId , limit = 10 , cursor = null ) {
427445 limit = this . validateLimit ( limit ) ;
@@ -435,12 +453,12 @@ class EventsFactory extends Factory {
435453
436454 /**
437455 * Get original event
438- * @type {EventSchema }
456+ * @type {Event }
439457 */
440- const eventOriginal = await this . _findOriginalEvent ( eventId ) ;
458+ const eventOriginal = await this . findById ( eventId ) ;
441459
442460 if ( ! eventOriginal ) {
443- return result ;
461+ throw new Error ( `Original event not found for ${ eventId } ` ) ;
444462 }
445463
446464 /**
@@ -450,7 +468,7 @@ class EventsFactory extends Factory {
450468 const repetitions = await this . getCollection ( this . TYPES . REPETITIONS )
451469 . find ( {
452470 groupHash : eventOriginal . groupHash ,
453- _id : cursor ? { $lte : cursor } : { $exists : true } ,
471+ _id : cursor ? { $lte : cursor } : { } ,
454472 } )
455473 . sort ( { _id : - 1 } )
456474 . limit ( limit + 1 )
@@ -482,7 +500,8 @@ class EventsFactory extends Factory {
482500 */
483501 const firstRepetition = {
484502 ...eventOriginal ,
485- firstAppearanceTimestamp : eventOriginal . timestamp ,
503+ originalTimestamp : eventOriginal . timestamp ,
504+ originalEventId : eventOriginal . _id ,
486505 projectId : this . projectId ,
487506 } ;
488507
@@ -493,37 +512,42 @@ class EventsFactory extends Factory {
493512 }
494513
495514 /**
496- * Returns Event concrete repetition
515+ * Returns certain repetition of the original event
497516 *
498517 * @param {String } repetitionId - id of Repetition to find
499- * @return {EventRepetitionSchema|null }
500- *
501- * @todo move to Repetitions(?) model
518+ * @param {String } originalEventId - id of the original event
519+ * @return {Event|null }
502520 */
503- async getEventRepetition ( repetitionId ) {
521+ async getEventRepetition ( repetitionId , originalEventId ) {
522+ /**
523+ * If originalEventId equals repetitionId than user wants to get first repetition which is original event
524+ */
525+ if ( repetitionId === originalEventId ) {
526+ const originalEvent = await this . getCollection ( this . TYPES . EVENTS )
527+ . findOne ( {
528+ _id : ObjectID ( originalEventId ) ,
529+ } ) ;
530+
531+ return originalEvent ? originalEvent : null ;
532+ }
533+
534+ /**
535+ * Otherwise we need to get original event and repetition and merge them
536+ */
504537 const repetition = await this . getCollection ( this . TYPES . REPETITIONS )
505538 . findOne ( {
506539 _id : ObjectID ( repetitionId ) ,
507540 } ) ;
508541
509- if ( ! repetition ) {
510- /**
511- * If repetition is not found, it can mean that client is trying to get original event
512- */
513- const event = await this . findById ( repetitionId ) ;
514-
515- return event ? {
516- ...event ,
517- firstAppearanceTimestamp : event . timestamp ,
518- } : null ;
519- }
520-
521542 const originalEvent = await this . getCollection ( this . TYPES . EVENTS )
522543 . findOne ( {
523- groupHash : repetition . groupHash ,
544+ _id : ObjectID ( originalEventId ) ,
524545 } ) ;
525546
526- if ( ! originalEvent ) {
547+ /**
548+ * If one of the ids are invalid (originalEvent or repetition not found) return null
549+ */
550+ if ( ! originalEvent || ! repetition ) {
527551 return null ;
528552 }
529553
@@ -552,7 +576,7 @@ class EventsFactory extends Factory {
552576 * @returns {Release|null }
553577 */
554578 async getEventRelease ( eventId ) {
555- const eventOriginal = await this . _findOriginalEvent ( eventId ) ;
579+ const eventOriginal = await this . findById ( eventId ) ;
556580
557581 if ( ! eventOriginal ) {
558582 return null ;
@@ -569,13 +593,13 @@ class EventsFactory extends Factory {
569593 /**
570594 * Mark event as visited for passed user
571595 *
572- * @param {string|ObjectId } eventId
573- * @param {string|ObjectId } userId
596+ * @param {string|ObjectId } eventId - id of the original event
597+ * @param {string|ObjectId } userId - id of the user who is visiting the event
574598 *
575599 * @return {Promise<void> }
576600 */
577601 async visitEvent ( eventId , userId ) {
578- const event = await this . _findOriginalEvent ( eventId ) ;
602+ const event = await this . findById ( eventId ) ;
579603
580604 if ( ! event ) {
581605 return null ;
@@ -591,15 +615,15 @@ class EventsFactory extends Factory {
591615 /**
592616 * Mark or unmark event as Resolved, Ignored or Starred
593617 *
594- * @param {string|ObjectId } eventId - event to mark
618+ * @param {string|ObjectId } eventId - id of the original event to mark
595619 * @param {string } mark - mark label
596620 *
597621 * @return {Promise<void> }
598622 */
599623 async toggleEventMark ( eventId , mark ) {
600624 const collection = this . getCollection ( this . TYPES . EVENTS ) ;
601625
602- const event = await this . _findOriginalEvent ( eventId ) ;
626+ const event = await this . findById ( eventId ) ;
603627
604628 if ( ! event ) {
605629 return null ;
@@ -650,14 +674,14 @@ class EventsFactory extends Factory {
650674 /**
651675 * Update assignee to selected event
652676 *
653- * @param {string } eventId - event id
677+ * @param {string } eventId - id of the original event to update
654678 * @param {string } assignee - assignee id for this event
655679 * @return {Promise<void> }
656680 */
657681 async updateAssignee ( eventId , assignee ) {
658682 const collection = this . getCollection ( this . TYPES . EVENTS ) ;
659683
660- const event = await this . _findOriginalEvent ( eventId ) ;
684+ const event = await this . findById ( eventId ) ;
661685
662686 if ( ! event ) {
663687 return null ;
@@ -672,42 +696,6 @@ class EventsFactory extends Factory {
672696 return collection . updateOne ( query , update ) ;
673697 }
674698
675- /**
676- * Find original event by eventId. If event is not found directly,
677- * try to find it as repetition and get original event by groupHash
678- *
679- * @param {string|ObjectID } eventId - event's id, may be repetition id
680- * @returns {Promise<Event|null> } original event or null if not found
681- */
682- async _findOriginalEvent ( eventId ) {
683- let originalEvent ;
684-
685- /**
686- * Try to find it by repetitionId
687- */
688- const repetition = await this . getCollection ( this . TYPES . REPETITIONS )
689- . findOne ( {
690- _id : new ObjectID ( eventId ) ,
691- } ) ;
692-
693- /**
694- * If repetition is not found by eventId, try to find it by eventId
695- */
696- if ( ! repetition ) {
697- originalEvent = await this . getCollection ( this . TYPES . EVENTS )
698- . findOne ( {
699- _id : new ObjectID ( eventId ) ,
700- } ) ;
701- } else {
702- originalEvent = await this . getCollection ( this . TYPES . EVENTS )
703- . findOne ( {
704- groupHash : repetition . groupHash ,
705- } ) ;
706- }
707-
708- return originalEvent ;
709- }
710-
711699 /**
712700 * Compose event with repetition
713701 *
@@ -719,9 +707,10 @@ class EventsFactory extends Factory {
719707 return {
720708 ...event ,
721709 _id : repetition . _id ,
722- firstAppearanceTimestamp : event . timestamp ,
710+ originalTimestamp : event . timestamp ,
711+ originalEventId : event . _id ,
723712 timestamp : repetition . timestamp ,
724- payload : composeEventPayloadWithRepetition ( event . payload , repetition ) ,
713+ payload : composeEventPayloadByRepetition ( event . payload , repetition ) ,
725714 projectId : this . projectId ,
726715 } ;
727716 }
0 commit comments