Skip to content

Commit ada2a24

Browse files
committed
review changes
1 parent d009928 commit ada2a24

File tree

4 files changed

+112
-170
lines changed

4 files changed

+112
-170
lines changed

src/models/eventsFactory.js

Lines changed: 72 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +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');
9+
const { composeEventPayloadWithRepetition } = require('../utils/merge');
1010

1111
/**
1212
* @typedef {Object} RecentEventSchema
@@ -408,36 +408,14 @@ class EventsFactory extends Factory {
408408

409409
const result = {
410410
repetitions: [],
411-
cursor: null,
411+
nextCursor: null,
412412
};
413413

414414
/**
415415
* Get original event
416416
* @type {EventSchema}
417417
*/
418-
let eventOriginal = await this.getCollection(this.TYPES.EVENTS)
419-
.findOne({
420-
_id: new ObjectID(eventId),
421-
});
422-
423-
/**
424-
* If event is not found, try to find it as repetition
425-
*/
426-
if (!eventOriginal) {
427-
const repetition = await this.getCollection(this.TYPES.REPETITIONS)
428-
.findOne({
429-
_id: new ObjectID(eventId),
430-
});
431-
432-
if (!repetition) {
433-
return result;
434-
}
435-
436-
eventOriginal = await this.getCollection(this.TYPES.EVENTS)
437-
.findOne({
438-
groupHash: repetition.groupHash,
439-
});
440-
}
418+
const eventOriginal = await this._findOriginalEvent(eventId);
441419

442420
if (!eventOriginal) {
443421
return result;
@@ -457,21 +435,19 @@ class EventsFactory extends Factory {
457435
.toArray();
458436

459437
if (repetitions.length === limit + 1) {
460-
result.cursor = repetitions.pop()._id;
438+
result.nextCursor = repetitions.pop()._id;
461439
}
462440

463441
for (const repetition of repetitions) {
442+
const event = this._composeEventWithRepetition(eventOriginal, repetition);
443+
464444
result.repetitions.push({
465-
...eventOriginal,
466-
_id: repetition._id,
467-
payload: composeFullRepetitionEvent(eventOriginal, repetition).payload,
468-
timestamp: repetition.timestamp,
469-
firstAppearanceTimestamp: eventOriginal.timestamp,
445+
...event,
470446
projectId: this.projectId,
471447
});
472448
}
473449

474-
const isLastPortion = result.cursor === null;
450+
const isLastPortion = result.nextCursor === null;
475451

476452
/**
477453
* For last portion:
@@ -514,14 +490,10 @@ class EventsFactory extends Factory {
514490
*/
515491
const event = await this.findById(repetitionId);
516492

517-
if (!event) {
518-
return null;
519-
}
520-
521-
return {
493+
return event ? {
522494
...event,
523495
firstAppearanceTimestamp: event.timestamp,
524-
};
496+
} : null;
525497
}
526498

527499
const originalEvent = await this.getCollection(this.TYPES.EVENTS)
@@ -533,13 +505,7 @@ class EventsFactory extends Factory {
533505
return null;
534506
}
535507

536-
return {
537-
...originalEvent,
538-
_id: repetition._id,
539-
payload: composeFullRepetitionEvent(originalEvent, repetition).payload,
540-
timestamp: repetition.timestamp,
541-
firstAppearanceTimestamp: originalEvent.timestamp,
542-
};
508+
return this._composeEventWithRepetition(originalEvent, repetition);
543509
}
544510

545511
/**
@@ -564,32 +530,10 @@ class EventsFactory extends Factory {
564530
* @returns {Release|null}
565531
*/
566532
async getEventRelease(eventId) {
567-
let eventOriginal = await this.getCollection(this.TYPES.EVENTS)
568-
.findOne({
569-
_id: new ObjectID(eventId),
570-
});
533+
const eventOriginal = await this._findOriginalEvent(eventId);
571534

572-
/**
573-
* If event is not found, try to find it as repetition
574-
*/
575535
if (!eventOriginal) {
576-
const repetition = await this.getCollection(this.TYPES.REPETITIONS)
577-
.findOne({
578-
_id: new ObjectID(eventId),
579-
});
580-
581-
if (!repetition) {
582-
return null;
583-
}
584-
585-
eventOriginal = await this.getCollection(this.TYPES.EVENTS)
586-
.findOne({
587-
groupHash: repetition.groupHash,
588-
});
589-
590-
if (!eventOriginal) {
591-
return null;
592-
}
536+
return null;
593537
}
594538

595539
const release = await mongo.databases.events.collection(this.TYPES.RELEASES).findOne({
@@ -609,37 +553,15 @@ class EventsFactory extends Factory {
609553
* @return {Promise<void>}
610554
*/
611555
async visitEvent(eventId, userId) {
612-
let event = await this.getCollection(this.TYPES.EVENTS)
613-
.findOne({
614-
_id: new ObjectID(eventId),
615-
});
556+
const event = await this._findOriginalEvent(eventId);
616557

617-
/**
618-
* If event is not found, try to find it as repetition
619-
*/
620558
if (!event) {
621-
const repetition = await this.getCollection(this.TYPES.REPETITIONS)
622-
.findOne({
623-
_id: new ObjectID(eventId),
624-
});
625-
626-
if (!repetition) {
627-
return null;
628-
}
629-
630-
event = await this.getCollection(this.TYPES.EVENTS)
631-
.findOne({
632-
groupHash: repetition.groupHash,
633-
});
634-
635-
if (!event) {
636-
return null;
637-
}
559+
return null;
638560
}
639561

640562
return this.getCollection(this.TYPES.EVENTS)
641563
.updateOne(
642-
{ _id: new ObjectID(eventId) },
564+
{ _id: new ObjectID(event._id) },
643565
{ $addToSet: { visitedBy: new ObjectID(userId) } }
644566
);
645567
}
@@ -654,29 +576,15 @@ class EventsFactory extends Factory {
654576
*/
655577
async toggleEventMark(eventId, mark) {
656578
const collection = this.getCollection(this.TYPES.EVENTS);
657-
const query = { _id: new ObjectID(eventId) };
658579

659-
let event = await collection.findOne(query);
580+
const event = await this._findOriginalEvent(eventId);
660581

661-
/**
662-
* If event is not found, try to find it as repetition
663-
*/
664582
if (!event) {
665-
const repetition = await this.getCollection(this.TYPES.REPETITIONS)
666-
.findOne({
667-
_id: new ObjectID(eventId),
668-
});
669-
670-
if (repetition) {
671-
event = await this.getCollection(this.TYPES.EVENTS)
672-
.findOne({
673-
groupHash: repetition.groupHash,
674-
});
675-
676-
query._id = new ObjectID(event._id);
677-
}
583+
return null;
678584
}
679585

586+
const query = { _id: new ObjectID(event._id) };
587+
680588
const markKey = `marks.${mark}`;
681589

682590
let update;
@@ -726,34 +634,72 @@ class EventsFactory extends Factory {
726634
*/
727635
async updateAssignee(eventId, assignee) {
728636
const collection = this.getCollection(this.TYPES.EVENTS);
729-
const query = { _id: new ObjectID(eventId) };
730637

731-
let event = await collection.findOne(query);
638+
const event = await this._findOriginalEvent(eventId);
639+
640+
if (!event) {
641+
return null;
642+
}
643+
644+
const query = { _id: new ObjectID(event._id) };
645+
646+
const update = {
647+
$set: { assignee: assignee },
648+
};
649+
650+
return collection.updateOne(query, update);
651+
}
652+
653+
/**
654+
* Find original event by eventId. If event is not found directly,
655+
* try to find it as repetition and get original event by groupHash
656+
*
657+
* @param {string|ObjectID} eventId - event's id, may be repetition id
658+
* @returns {Promise<Event|null>} original event or null if not found
659+
*/
660+
async _findOriginalEvent(eventId) {
661+
let eventOriginal = await this.getCollection(this.TYPES.EVENTS)
662+
.findOne({
663+
_id: new ObjectID(eventId),
664+
});
732665

733666
/**
734667
* If event is not found, try to find it as repetition
735668
*/
736-
if (!event) {
669+
if (!eventOriginal) {
737670
const repetition = await this.getCollection(this.TYPES.REPETITIONS)
738671
.findOne({
739672
_id: new ObjectID(eventId),
740673
});
741674

742-
if (repetition) {
743-
event = await this.getCollection(this.TYPES.EVENTS)
744-
.findOne({
745-
groupHash: repetition.groupHash,
746-
});
747-
748-
query._id = new ObjectID(event._id);
675+
if (!repetition) {
676+
return null;
749677
}
678+
679+
eventOriginal = await this.getCollection(this.TYPES.EVENTS)
680+
.findOne({
681+
groupHash: repetition.groupHash,
682+
});
750683
}
751684

752-
const update = {
753-
$set: { assignee: assignee },
754-
};
685+
return eventOriginal;
686+
}
755687

756-
return collection.updateOne(query, update);
688+
/**
689+
* Compose event with repetition
690+
*
691+
* @param {Event} event - event
692+
* @param {Repetition} repetition - repetition
693+
* @returns {Event} event merged with repetition
694+
*/
695+
_composeEventWithRepetition(event, repetition) {
696+
return {
697+
...event,
698+
_id: repetition._id,
699+
firstAppearanceTimestamp: event.timestamp,
700+
timestamp: repetition.timestamp,
701+
payload: composeEventPayloadWithRepetition(event.payload, repetition),
702+
};
757703
}
758704
}
759705

src/typeDefs/event.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,9 @@ type EventMarks {
198198
"""
199199
Object returned in repetitions property of event object
200200
"""
201-
type RepetitionsResponse {
201+
type RepetitionsPortion {
202202
repetitions: [Event!]
203-
cursor: String
203+
nextCursor: String
204204
}
205205
206206
"""
@@ -255,7 +255,7 @@ type Event {
255255
"""
256256
Event repetitions
257257
"""
258-
repetitions(cursor: String = null, limit: Int = 10): RepetitionsResponse!
258+
repetitions(cursor: String = null, limit: Int = 10): RepetitionsPortion!
259259
260260
"""
261261
Array of users who visited event

src/utils/merge.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,21 @@ function stringifyPayloadField(payload: GroupedEventDBScheme['payload'], field:
7171
}
7272

7373
/**
74-
* Helps to merge original event and repetition due to delta format,
74+
* Helps to merge original event payload and repetition due to delta format,
7575
* in case of old delta format, we need to patch the payload
7676
* in case of new delta format, we need to assemble the payload
7777
*
78-
* @param originalEvent {HawkEvent} - The original event
79-
* @param repetition {HawkEventRepetition} - The repetition to process
80-
* @returns {HawkEvent} Updated event with processed repetition payload
78+
* @param originalEventPayload {GroupedEventDBScheme['payload']} - The original event payload
79+
* @param repetition {RepetitionDBScheme} - The repetition to process
80+
* @returns {GroupedEventDBScheme['payload']} Updated event with processed repetition payload
8181
*/
82-
export function composeFullRepetitionEvent(originalEvent: GroupedEventDBScheme, repetition: RepetitionDBScheme | undefined): GroupedEventDBScheme {
82+
export function composeEventPayloadWithRepetition(originalEventPayload: GroupedEventDBScheme['payload'], repetition: RepetitionDBScheme | undefined): GroupedEventDBScheme['payload'] {
8383
/**
8484
* Make a deep copy of the original event, because we need to avoid mutating the original event
8585
*/
86-
const event = cloneDeep(originalEvent);
8786

8887
if (!repetition) {
89-
return event;
88+
return originalEventPayload;
9089
}
9190

9291
/**
@@ -96,35 +95,35 @@ export function composeFullRepetitionEvent(originalEvent: GroupedEventDBScheme,
9695
/**
9796
* Parse addons and context fields from string to object before patching
9897
*/
99-
event.payload = parsePayloadField(event.payload, 'addons');
100-
event.payload = parsePayloadField(event.payload, 'context');
98+
originalEventPayload = parsePayloadField(originalEventPayload, 'addons');
99+
originalEventPayload = parsePayloadField(originalEventPayload, 'context');
101100

102-
event.payload = patch({
103-
left: event.payload,
101+
originalEventPayload = patch({
102+
left: originalEventPayload,
104103
delta: JSON.parse(repetition.delta),
105104
});
106105

107106
/**
108107
* Stringify addons and context fields from object to string after patching
109108
*/
110-
event.payload = stringifyPayloadField(event.payload, 'addons');
111-
event.payload = stringifyPayloadField(event.payload, 'context');
109+
originalEventPayload = stringifyPayloadField(originalEventPayload, 'addons');
110+
originalEventPayload = stringifyPayloadField(originalEventPayload, 'context');
112111

113-
return event;
112+
return originalEventPayload;
114113
}
115114

116115
/**
117116
* New delta format (repetition.payload is null) and repetition.delta is null (there is no delta between original and repetition)
118117
*/
119118
if (!repetition.payload) {
120-
return event;
119+
return originalEventPayload;
121120
}
122121

123122
/**
124123
* Old delta format (repetition.payload is not null)
125124
* @todo remove after 6 september 2025
126125
*/
127-
event.payload = repetitionAssembler(event.payload, repetition.payload);
126+
originalEventPayload = repetitionAssembler(originalEventPayload, repetition.payload);
128127

129-
return event;
128+
return originalEventPayload;
130129
}

0 commit comments

Comments
 (0)