@@ -7,13 +7,6 @@ const mongo = require('../mongo');
77const Event = require ( '../models/event' ) ;
88const { ObjectID } = require ( 'mongodb' ) ;
99
10- /**
11- * @typedef {Object } RecentEventSchema
12- * @property {Event } event - event model
13- * @property {Number } count - recent error occurred count
14- * @property {String } data - error occurred date (string)
15- */
16-
1710/**
1811 * @typedef {Object } EventRepetitionSchema
1912 * @property {String } _id — repetition's identifier
@@ -22,6 +15,13 @@ const { ObjectID } = require('mongodb');
2215 * @property {Number } timestamp - repetition's Unix timestamp
2316 */
2417
18+ /**
19+ * @typedef {Object } DaylyEventsSchema
20+ * @property {Event } event - original event of the daily one
21+ * @property {EventRepetitionSchema } repetition - last repetition of the day
22+ * @property {String | null } nextCursor - pointer to the next dailyEvent for pagination
23+ */
24+
2525/**
2626 * @typedef {Object } EventsFilters
2727 * @property {boolean } [starred] - if true, events with 'starred' mark should be included to the output
@@ -148,16 +148,16 @@ class EventsFactory extends Factory {
148148 * Returns events that grouped by day
149149 *
150150 * @param {Number } limit - events count limitations
151- * @param {Number } skip - certain number of documents to skip
151+ * @param {String } paginatoinCursor - pointer to the first daily event to be selected
152152 * @param {'BY_DATE' | 'BY_COUNT' } sort - events sort order
153153 * @param {EventsFilters } filters - marks by which events should be filtered
154154 * @param {String } search - Search query
155155 *
156156 * @return {RecentEventSchema[] }
157157 */
158- async findRecent (
158+ async findRecentDailyEventsWithEventAndRepetition (
159159 limit = 10 ,
160- skip = 0 ,
160+ paginationCursor = '' ,
161161 sort = 'BY_DATE' ,
162162 filters = { } ,
163163 search = ''
@@ -194,6 +194,11 @@ class EventsFactory extends Factory {
194194
195195 const pipeline = [
196196 {
197+ $match : paginationCursor ? {
198+ _id : {
199+ $gte : new ObjectID ( paginationCursor ) ,
200+ }
201+ } : { } ,
197202 $sort : {
198203 groupingTimestamp : - 1 ,
199204 [ sort ] : - 1 ,
@@ -241,6 +246,9 @@ class EventsFactory extends Factory {
241246 : { } ;
242247
243248 pipeline . push (
249+ /**
250+ * Left outer join original event on groupHash field
251+ */
244252 {
245253 $lookup : {
246254 from : 'events:' + this . projectId ,
@@ -249,48 +257,49 @@ class EventsFactory extends Factory {
249257 as : 'event' ,
250258 } ,
251259 } ,
260+ {
261+ $lookup : {
262+ from : 'repetitions:' + this . projectId ,
263+ localField : 'lastRepetitionId' ,
264+ foreignField : '_id' ,
265+ as : 'repetition' ,
266+ }
267+ } ,
268+ /**
269+ * Desctruct event and repetition arrays since there are only one document in both arrays
270+ */
252271 {
253272 $unwind : '$event' ,
254273 } ,
274+ {
275+ $unwind : '$repetition' ,
276+ } ,
255277 {
256278 $match : {
257279 ...matchFilter ,
258280 ...searchFilter ,
259281 } ,
260282 } ,
261- { $skip : skip } ,
262- { $limit : limit } ,
283+ { $limit : limit + 1 } ,
263284 {
264- $group : {
265- _id : null ,
266- dailyInfo : { $push : '$$ROOT' } ,
267- events : { $push : '$event' } ,
268- } ,
269- } ,
270- {
271- $unset : 'dailyInfo.event' ,
285+ $unset : 'groupHash' ,
272286 }
273287 ) ;
274288
275289 const cursor = this . getCollection ( this . TYPES . DAILY_EVENTS ) . aggregate ( pipeline ) ;
276290
277- const result = ( await cursor . toArray ( ) ) . shift ( ) ;
291+ const result = await cursor . toArray ( ) ;
278292
279- /**
280- * aggregation can return empty array so that
281- * result can be undefined
282- *
283- * for that we check result existence
284- *
285- * extra field `projectId` needs to satisfy GraphQL query
286- */
287- if ( result && result . events ) {
288- result . events . forEach ( event => {
289- event . projectId = this . projectId ;
290- } ) ;
293+ let lastEvent ;
294+
295+ if ( result . length === limit + 1 ) {
296+ lastEvent = result . pop ( ) ;
291297 }
292298
293- return result ;
299+ return {
300+ nextCursor : lastEvent ? lastEvent . _id . toString ( ) : null ,
301+ ...result ,
302+ } ;
294303 }
295304
296305 /**
0 commit comments