Skip to content

Commit 692682b

Browse files
committed
event -> repetition
1 parent 9f6df54 commit 692682b

File tree

7 files changed

+171
-196
lines changed

7 files changed

+171
-196
lines changed

src/models/eventsFactory.js

Lines changed: 97 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const Factory = require('./modelFactory');
66
const mongo = require('../mongo');
77
const Event = require('../models/event');
88
const { ObjectID } = require('mongodb');
9+
const { composeFullRepetitionEvent } = require('../utils/merge');
910

1011
/**
1112
* @typedef {Object} RecentEventSchema
@@ -393,21 +394,43 @@ class EventsFactory extends Factory {
393394
*
394395
* @param {string|ObjectID} eventId - Event's id
395396
* @param {Number} limit - count limitations
396-
* @param {Number} skip - selection offset
397+
* @param {Number} cursor - cursor for pagination
397398
*
398399
* @return {EventRepetitionSchema[]}
399400
*
400401
* @todo move to Repetitions(?) model
401402
*/
402-
async getEventRepetitions(eventId, limit = 10, skip = 0) {
403+
async getEventRepetitions(repetitionId, limit = 10, cursor = undefined) {
403404
limit = this.validateLimit(limit);
404-
skip = this.validateSkip(skip);
405+
cursor = cursor ? new ObjectID(cursor) : undefined;
406+
407+
const result = {
408+
repetitions: [],
409+
cursor: undefined,
410+
};
405411

406412
/**
407413
* Get original event
408414
* @type {EventSchema}
409415
*/
410-
const eventOriginal = await this.findById(eventId);
416+
let eventOriginal = await this.findById(repetitionId);
417+
418+
/**
419+
* If original event is not found, it can mean that client is trying to get repetitions of original event
420+
*/
421+
if (!eventOriginal) {
422+
const repetition = await this.getEventRepetition(repetitionId);
423+
424+
if (!repetition) {
425+
return result;
426+
}
427+
428+
eventOriginal = await this.findById(repetition.eventId);
429+
}
430+
431+
if (!eventOriginal) {
432+
return result;
433+
}
411434

412435
/**
413436
* Collect repetitions
@@ -416,13 +439,27 @@ class EventsFactory extends Factory {
416439
const repetitions = await this.getCollection(this.TYPES.REPETITIONS)
417440
.find({
418441
groupHash: eventOriginal.groupHash,
442+
_id: cursor ? { $lte: cursor } : {},
419443
})
420444
.sort({ _id: -1 })
421-
.limit(limit)
422-
.skip(skip)
445+
.limit(limit + 1)
423446
.toArray();
424447

425-
const isLastPortion = repetitions.length < limit && skip === 0;
448+
if (repetitions.length === limit + 1) {
449+
result.cursor = repetitions.pop()._id;
450+
}
451+
452+
for (const repetition of repetitions) {
453+
result.repetitions.push({
454+
...eventOriginal,
455+
_id: repetition._id,
456+
payload: composeFullRepetitionEvent(eventOriginal, repetition).payload,
457+
timestamp: repetition.timestamp,
458+
firstAppearanceTimestamp: eventOriginal.timestamp,
459+
});
460+
}
461+
462+
const isLastPortion = repetitions.length < limit;
426463

427464
/**
428465
* For last portion:
@@ -434,16 +471,14 @@ class EventsFactory extends Factory {
434471
* @type {EventRepetitionSchema}
435472
*/
436473
const firstRepetition = {
437-
_id: eventOriginal._id,
438-
payload: eventOriginal.payload,
439-
groupHash: eventOriginal.groupHash,
440-
timestamp: eventOriginal.timestamp,
474+
...eventOriginal,
475+
firstAppearanceTimestamp: eventOriginal.timestamp,
441476
};
442477

443-
repetitions.push(firstRepetition);
478+
result.repetitions.push(firstRepetition);
444479
}
445480

446-
return repetitions;
481+
return result;
447482
}
448483

449484
/**
@@ -455,10 +490,40 @@ class EventsFactory extends Factory {
455490
* @todo move to Repetitions(?) model
456491
*/
457492
async getEventRepetition(repetitionId) {
458-
return this.getCollection(this.TYPES.REPETITIONS)
493+
const repetition = await this.getCollection(this.TYPES.REPETITIONS)
459494
.findOne({
460495
_id: ObjectID(repetitionId),
461496
});
497+
498+
if (!repetition) {
499+
/**
500+
* If repetition is not found, it can mean that client is trying to get original event
501+
*/
502+
const event = await this.findById(repetitionId);
503+
504+
if (!event) {
505+
return null;
506+
}
507+
508+
return {
509+
...event,
510+
firstAppearanceTimestamp: event.timestamp,
511+
};
512+
}
513+
514+
const originalEvent = await this.findById(repetition.eventId);
515+
516+
if (!originalEvent) {
517+
return null;
518+
}
519+
520+
return {
521+
...originalEvent,
522+
_id: repetition._id,
523+
payload: composeFullRepetitionEvent(originalEvent, repetition).payload,
524+
timestamp: repetition.timestamp,
525+
firstAppearanceTimestamp: originalEvent.timestamp,
526+
};
462527
}
463528

464529
/**
@@ -501,7 +566,15 @@ class EventsFactory extends Factory {
501566
*
502567
* @return {Promise<void>}
503568
*/
504-
async visitEvent(eventId, userId) {
569+
async visitEvent(repetitionId, userId) {
570+
const repetition = await this.getCollection(this.TYPES.REPETITIONS)
571+
.findOne({ _id: new ObjectID(repetitionId) });
572+
573+
/**
574+
* If repetition is not found, it can mean that client is trying to work with original event
575+
*/
576+
const eventId = repetition ? repetition.eventId : repetitionId;
577+
505578
return this.getCollection(this.TYPES.EVENTS)
506579
.updateOne(
507580
{ _id: new ObjectID(eventId) },
@@ -517,7 +590,15 @@ class EventsFactory extends Factory {
517590
*
518591
* @return {Promise<void>}
519592
*/
520-
async toggleEventMark(eventId, mark) {
593+
async toggleEventMark(repetitionId, mark) {
594+
const repetition = await this.getCollection(this.TYPES.REPETITIONS)
595+
.findOne({ _id: new ObjectID(repetitionId) });
596+
597+
/**
598+
* If repetition is not found, it can mean that client is trying to work with original event
599+
*/
600+
const eventId = repetition ? repetition.eventId : repetitionId;
601+
521602
const collection = this.getCollection(this.TYPES.EVENTS);
522603
const query = { _id: new ObjectID(eventId) };
523604

src/resolvers/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const {
1010
const user = require('./user').default;
1111
const workspace = require('./workspace');
1212
const project = require('./project');
13-
const event = require('./event');
13+
const repetition = require('./repetition');
1414
const plans = require('./plans').default;
1515
const projectNotifications = require('./projectNotifications').default;
1616
const projectPatterns = require('./projectPatterns').default;
@@ -70,7 +70,7 @@ const resolvers = [
7070
user,
7171
workspace,
7272
project,
73-
event,
73+
repetition,
7474
projectNotifications,
7575
projectPatterns,
7676
userNotifications,

src/resolvers/project.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -280,24 +280,26 @@ module.exports = {
280280
},
281281

282282
/**
283-
* Find project's event
283+
* Find project's repetition
284284
*
285285
* @param {ProjectDBScheme} project - result of parent resolver
286-
* @param {String} eventId - event's identifier
286+
* @param {String} repetitionId - repetition's identifier
287287
*
288-
* @returns {Event}
288+
* @returns {Repetition}
289289
*/
290-
async event(project, { id: eventId }) {
290+
async repetition(project, { id: repetitionId }) {
291291
const factory = new EventsFactory(project._id);
292-
const event = await factory.findById(eventId);
293292

294-
if (!event) {
295-
return null;
296-
}
293+
const repetition = await factory.getEventRepetition(repetitionId);
297294

298-
event.projectId = project._id;
295+
/**
296+
* If repetition is not found, it can mean that client is trying to get original event
297+
*/
298+
if (!repetition) {
299+
return factory.getEventLastRepetition(repetitionId);
300+
}
299301

300-
return event;
302+
return repetition;
301303
},
302304

303305
/**
Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ const { ObjectID } = require('mongodb');
33
const sendPersonalNotification = require('../utils/personalNotifications').default;
44

55
/**
6-
* See all types and fields here {@see ../typeDefs/event.graphql}
6+
* See all types and fields here {@see ../typeDefs/repetition.graphql}
77
*/
88
module.exports = {
9-
EventMarks: {
9+
RepetitionMarks: {
1010
starred(marks) {
1111
return 'starred' in marks;
1212
},
@@ -17,40 +17,22 @@ module.exports = {
1717
return 'resolved' in marks;
1818
},
1919
},
20-
Event: {
20+
Repetition: {
2121
/**
22-
* Returns Event with concrete repetition
23-
*
24-
* @param {string} eventId - id of Event of which repetition requested
25-
* @param {string} projectId - projectId of Event of which repetition requested
26-
* @param {string|null} [repetitionId] - if not specified, last repetition will returned
27-
* @return {Promise<EventRepetitionSchema>}
28-
*/
29-
async repetition({ id: eventId, projectId }, { id: repetitionId }) {
30-
const factory = new EventsFactory(projectId);
31-
32-
if (!repetitionId) {
33-
return factory.getEventLastRepetition(eventId);
34-
}
35-
36-
return factory.getEventRepetition(repetitionId);
37-
},
38-
39-
/**
40-
* Returns repetitions list of the event
22+
* Returns repetitions list of the repetition
4123
*
4224
* @param {ResolverObj} _obj
43-
* @param {String} eventId
25+
* @param {String} repetitionId
4426
* @param {String} projectId
4527
* @param {Number} limit
4628
* @param {Number} skip
4729
*
4830
* @return {EventRepetitionSchema[]}
4931
*/
50-
async repetitions({ _id: eventId, projectId }, { limit, skip }) {
32+
async repetitions({ _id: repetitionId, projectId }, { limit, cursor }) {
5133
const factory = new EventsFactory(projectId);
5234

53-
return factory.getEventRepetitions(eventId, limit, skip);
35+
return factory.getEventRepetitions(repetitionId, limit, cursor);
5436
},
5537

5638
/**
@@ -124,15 +106,15 @@ module.exports = {
124106
},
125107
Mutation: {
126108
/**
127-
* Mark event as visited for current user
109+
* Mark repetition as visited for current user
128110
*
129111
* @param {ResolverObj} _obj - resolver context
130112
* @param {string} project - project id
131113
* @param {string} id - event id
132114
* @param {UserInContext} user - user context
133115
* @return {Promise<boolean>}
134116
*/
135-
async visitEvent(_obj, { project, id }, { user }) {
117+
async visitRepetition(_obj, { project, id }, { user }) {
136118
const factory = new EventsFactory(project);
137119

138120
const { result } = await factory.visitEvent(id, user.id);
@@ -149,10 +131,10 @@ module.exports = {
149131
* @param {string} mark - mark to set
150132
* @return {Promise<boolean>}
151133
*/
152-
async toggleEventMark(_obj, { project, eventId, mark }) {
134+
async toggleRepetitionMark(_obj, { project, repetitionId, mark }) {
153135
const factory = new EventsFactory(project);
154136

155-
const { result } = await factory.toggleEventMark(eventId, mark);
137+
const { result } = await factory.toggleEventMark(repetitionId, mark);
156138

157139
return !!result.ok;
158140
},
@@ -162,9 +144,9 @@ module.exports = {
162144
*
163145
* @return {Function()}
164146
*/
165-
events: () => ({}),
147+
repetitions: () => ({}),
166148
},
167-
EventsMutations: {
149+
RepetitionMutations: {
168150
/**
169151
* Update assignee to selected event
170152
*

src/typeDefs/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { gql } from 'apollo-server-express';
22

33
import billing from './billing';
4-
import event from './event';
4+
import repetition from './repetition';
55
import notifications from './notifications';
66
import notificationsInput from './notificationsInput';
77
import projectNotifications from './projectNotifications';
@@ -87,7 +87,7 @@ const rootSchema = gql`
8787
const typeDefinitions = [
8888
rootSchema,
8989
billing,
90-
event,
90+
repetition,
9191
notifications,
9292
notificationsInput,
9393
projectNotifications,

src/typeDefs/project.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ type Project {
9494
uidAdded: User!
9595
9696
"""
97-
Project's Event
97+
Project repetitions
9898
"""
99-
event(id: ID!): Event
99+
repetition: Repetition!
100100
101101
"""
102102
Project events
@@ -107,7 +107,7 @@ type Project {
107107
108108
"Certain number of documents to skip"
109109
skip: Int = 0
110-
): [Event!]
110+
): [Repetition!]
111111
112112
"""
113113
Returns recent events grouped by day

0 commit comments

Comments
 (0)