Skip to content

Commit 9e2fa3e

Browse files
committed
imp(): remove redundant db calls
1 parent a65dcd4 commit 9e2fa3e

1 file changed

Lines changed: 24 additions & 18 deletions

File tree

src/models/eventsFactory.js

Lines changed: 24 additions & 18 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
@@ -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
/**

0 commit comments

Comments
 (0)