Skip to content

Commit 05dd271

Browse files
committed
Merge branch 'refactor-project-overview' into stage
2 parents 58cbef8 + 9e2fa3e commit 05dd271

2 files changed

Lines changed: 42 additions & 36 deletions

File tree

src/models/eventsFactory.js

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ const Event = require('../models/event');
88
const { ObjectID } = require('mongodb');
99
const { composeEventPayloadByRepetition } = require('../utils/merge');
1010

11+
/**
12+
* @typedef {import('mongodb').UpdateWriteOpResult} UpdateWriteOpResult
13+
*/
14+
1115
/**
1216
* @typedef {Object} EventRepetitionSchema
1317
* @property {String} _id — repetition's identifier
@@ -39,9 +43,9 @@ const { composeEventPayloadByRepetition } = require('../utils/merge');
3943

4044
/**
4145
* @typedef {Object} DailyEventsCursor
42-
* @property {Number} groupingTimestampBound - boundary value of groupingTimestamp field of the last event in the portion
43-
* @property {Number} sortValueBound - boundary value of the field by which events are sorted (count/affectedUsers/lastRepetitionTime) of the last event in the portion
44-
* @property {String} idBound - boundary value of _id field of the last event in the portion
46+
* @property {Number} groupingTimestampBoundary - boundary value of groupingTimestamp field of the last event in the portion
47+
* @property {Number} sortValueBoundary - boundary value of the field by which events are sorted (count/affectedUsers/lastRepetitionTime) of the last event in the portion
48+
* @property {String} idBoundary - boundary value of _id field of the last event in the portion
4549
*/
4650

4751
/**
@@ -236,16 +240,16 @@ class EventsFactory extends Factory {
236240
* If groupingTimestamp is less than the cursors one
237241
* - daily events of the next day
238242
*/
239-
groupingTimestamp: { $lt: paginationCursor.groupingTimestampBound },
243+
groupingTimestamp: { $lt: paginationCursor.groupingTimestampBoundary },
240244
},
241245
{
242246
/**
243247
* If groupingTimestamp equals to the cursor one, but [sort] is less than the cursors one
244248
* - daily events of the same day, but with less count/affectedUsers/lastRepetitionTime
245249
*/
246250
$and: [
247-
{ groupingTimestamp: paginationCursor.groupingTimestampBound },
248-
{ [sort]: { $lt: paginationCursor.sortValueBound } },
251+
{ groupingTimestamp: paginationCursor.groupingTimestampBoundary },
252+
{ [sort]: { $lt: paginationCursor.sortValueBoundary } },
249253
],
250254
},
251255
{
@@ -254,9 +258,9 @@ class EventsFactory extends Factory {
254258
* - daily events of the same day with the same count/affectedUsers/lastRepetitionTime, but that were created earlier
255259
*/
256260
$and: [
257-
{ groupingTimestamp: paginationCursor.groupingTimestampBound },
258-
{ [sort]: paginationCursor.sortValueBound },
259-
{ _id: { $lte: new ObjectID(paginationCursor.idBound) } },
261+
{ groupingTimestamp: paginationCursor.groupingTimestampBoundary },
262+
{ [sort]: paginationCursor.sortValueBoundary },
263+
{ _id: { $lte: new ObjectID(paginationCursor.idBoundary) } },
260264
],
261265
},
262266
],
@@ -363,9 +367,9 @@ class EventsFactory extends Factory {
363367
const nextCursorEvent = result.pop();
364368

365369
nextCursor = {
366-
groupingTimestampBound: nextCursorEvent.groupingTimestamp,
367-
sortValueBound: nextCursorEvent[sort],
368-
idBound: nextCursorEvent._id.toString(),
370+
groupingTimestampBoundary: nextCursorEvent.groupingTimestamp,
371+
sortValueBoundary: nextCursorEvent[sort],
372+
idBoundary: nextCursorEvent._id.toString(),
369373
};
370374
}
371375

@@ -575,7 +579,7 @@ class EventsFactory extends Factory {
575579
*
576580
* @param {String} repetitionId - id of Repetition to find
577581
* @param {String} originalEventId - id of the original event
578-
* @return {Event|null}
582+
* @return {EventRepetitionSchema|null}
579583
*/
580584
async getEventRepetition(repetitionId, originalEventId) {
581585
/**
@@ -591,6 +595,8 @@ class EventsFactory extends Factory {
591595
* All events have same type with originalEvent id
592596
*/
593597
originalEvent.originalEventId = originalEventId;
598+
originalEvent.originalTimestamp = originalEvent.timestamp;
599+
originalEvent.projectId = this.projectId;
594600

595601
return originalEvent || null;
596602
}
@@ -660,20 +666,20 @@ class EventsFactory extends Factory {
660666
* @param {string|ObjectId} eventId - id of the original event
661667
* @param {string|ObjectId} userId - id of the user who is visiting the event
662668
*
663-
* @return {Promise<void>}
669+
* @return {Promise<UpdateWriteOpResult>}
664670
*/
665671
async visitEvent(eventId, userId) {
666-
const event = await this.findById(eventId);
672+
const result = await this.getCollection(this.TYPES.EVENTS)
673+
.updateOne(
674+
{ _id: new ObjectID(eventId) },
675+
{ $addToSet: { visitedBy: new ObjectID(userId) } }
676+
);
667677

668-
if (!event) {
678+
if (result.matchedCount === 0) {
669679
throw new Error(`Event not found for eventId: ${eventId}`);
670680
}
671681

672-
return this.getCollection(this.TYPES.EVENTS)
673-
.updateOne(
674-
{ _id: new ObjectID(event._id) },
675-
{ $addToSet: { visitedBy: new ObjectID(userId) } }
676-
);
682+
return result;
677683
}
678684

679685
/**
@@ -682,7 +688,7 @@ class EventsFactory extends Factory {
682688
* @param {string|ObjectId} eventId - id of the original event to mark
683689
* @param {string} mark - mark label
684690
*
685-
* @return {Promise<void>}
691+
* @return {Promise<UpdateWriteOpResult>}
686692
*/
687693
async toggleEventMark(eventId, mark) {
688694
const collection = this.getCollection(this.TYPES.EVENTS);
@@ -745,19 +751,19 @@ class EventsFactory extends Factory {
745751
async updateAssignee(eventId, assignee) {
746752
const collection = this.getCollection(this.TYPES.EVENTS);
747753

748-
const event = await this.findById(eventId);
749-
750-
if (!event) {
751-
throw new Error(`Event not found for eventId: ${eventId}`);
752-
}
753-
754-
const query = { _id: new ObjectID(event._id) };
754+
const query = { _id: new ObjectID(eventId) };
755755

756756
const update = {
757757
$set: { assignee: assignee },
758758
};
759759

760-
return collection.updateOne(query, update);
760+
const result = await collection.updateOne(query, update);
761+
762+
if (result.updatedCount === 0) {
763+
throw new Error(`Event not found for eventId: ${eventId}`);
764+
}
765+
766+
return result;
761767
}
762768

763769
/**

src/typeDefs/project.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,23 @@ type DailyEventsCursor {
5959
"""
6060
Grouping timestamp of the first event in the next portion
6161
"""
62-
groupingTimestampBound: Int!
62+
groupingTimestampBoundary: Int!
6363
6464
"""
6565
Sort key value of the first event in the next portion
6666
"""
67-
sortValueBound: Int!
67+
sortValueBoundary: Int!
6868
6969
"""
7070
ID of the first event of in the next portion
7171
"""
72-
idBound: ID!
72+
idBoundary: ID!
7373
}
7474
7575
input DailyEventsCursorInput {
76-
groupingTimestampBound: Int!
77-
sortValueBound: Int!
78-
idBound: ID!
76+
groupingTimestampBoundary: Int!
77+
sortValueBoundary: Int!
78+
idBoundary: ID!
7979
}
8080
8181
"""

0 commit comments

Comments
 (0)