Skip to content

Commit d4bc87f

Browse files
committed
merging repetitions on api side
1 parent 055f47e commit d4bc87f

File tree

4 files changed

+110
-161
lines changed

4 files changed

+110
-161
lines changed

src/models/eventsFactory.js

Lines changed: 84 additions & 24 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
@@ -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
};

src/resolvers/event.js

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,6 @@ module.exports = {
1818
},
1919
},
2020
Event: {
21-
/**
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-
},
3821

3922
/**
4023
* Returns repetitions list of the event
@@ -47,10 +30,10 @@ module.exports = {
4730
*
4831
* @return {EventRepetitionSchema[]}
4932
*/
50-
async repetitions({ _id: eventId, projectId }, { limit, skip }) {
33+
async repetitions({ groupHash, projectId }, { limit, skip }) {
5134
const factory = new EventsFactory(projectId);
5235

53-
return factory.getEventRepetitions(eventId, limit, skip);
36+
return factory.getEventRepetitions(groupHash, limit, skip);
5437
},
5538

5639
/**
@@ -115,9 +98,9 @@ module.exports = {
11598
* @param {String} eventId - event id
11699
* @returns {Promise<Release>}
117100
*/
118-
async release({ projectId, id: eventId }) {
101+
async release({ projectId, groupHash }) {
119102
const factory = new EventsFactory(new ObjectID(projectId));
120-
const release = await factory.getEventRelease(eventId);
103+
const release = await factory.getEventRelease(groupHash);
121104

122105
return release;
123106
},
@@ -132,10 +115,10 @@ module.exports = {
132115
* @param {UserInContext} user - user context
133116
* @return {Promise<boolean>}
134117
*/
135-
async visitEvent(_obj, { project, id }, { user }) {
118+
async visitEvent(_obj, { project, groupHash }, { user }) {
136119
const factory = new EventsFactory(project);
137120

138-
const { result } = await factory.visitEvent(id, user.id);
121+
const { result } = await factory.visitEvent(groupHash, user.id);
139122

140123
return !!result.ok;
141124
},
@@ -145,14 +128,14 @@ module.exports = {
145128
*
146129
* @param {ResolverObj} _obj - resolver context
147130
* @param {string} project - project id
148-
* @param {string} id - event id
131+
* @param {string} groupHash - event id
149132
* @param {string} mark - mark to set
150133
* @return {Promise<boolean>}
151134
*/
152-
async toggleEventMark(_obj, { project, eventId, mark }) {
135+
async toggleEventMark(_obj, { project, groupHash, mark }) {
153136
const factory = new EventsFactory(project);
154137

155-
const { result } = await factory.toggleEventMark(eventId, mark);
138+
const { result } = await factory.toggleEventMark(groupHash, mark);
156139

157140
return !!result.ok;
158141
},
@@ -174,7 +157,7 @@ module.exports = {
174157
* @return {Promise<boolean>}
175158
*/
176159
async updateAssignee(_obj, { input }, { factories, user }) {
177-
const { projectId, eventId, assignee } = input;
160+
const { projectId, groupHash, assignee } = input;
178161
const factory = new EventsFactory(projectId);
179162

180163
const userExists = await factories.usersFactory.findById(assignee);
@@ -196,7 +179,7 @@ module.exports = {
196179
};
197180
}
198181

199-
const { result } = await factory.updateAssignee(eventId, assignee);
182+
const { result } = await factory.updateAssignee(groupHash, assignee);
200183

201184
const assigneeData = await factories.usersFactory.dataLoaders.userById.load(assignee);
202185

@@ -206,7 +189,7 @@ module.exports = {
206189
assigneeId: assignee,
207190
projectId,
208191
whoAssignedId: user.id,
209-
eventId,
192+
groupHash,
210193
},
211194
});
212195

@@ -225,10 +208,10 @@ module.exports = {
225208
* @return {Promise<boolean>}
226209
*/
227210
async removeAssignee(_obj, { input }) {
228-
const { projectId, eventId } = input;
211+
const { projectId, groupHash } = input;
229212
const factory = new EventsFactory(projectId);
230213

231-
const { result } = await factory.updateAssignee(eventId, '');
214+
const { result } = await factory.updateAssignee(groupHash, '');
232215

233216
return {
234217
success: !!result.ok,

src/resolvers/project.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -287,17 +287,17 @@ module.exports = {
287287
*
288288
* @returns {Event}
289289
*/
290-
async event(project, { id: eventId }) {
290+
async event(project, { id: repetitionId }) {
291291
const factory = new EventsFactory(project._id);
292-
const event = await factory.findById(eventId);
293-
294-
if (!event) {
292+
const repetition = await factory.getEventRepetition(repetitionId);
293+
294+
if (!repetition) {
295295
return null;
296296
}
297297

298-
event.projectId = project._id;
298+
repetition.projectId = project._id;
299299

300-
return event;
300+
return repetition;
301301
},
302302

303303
/**

0 commit comments

Comments
 (0)