Skip to content

Commit 5663900

Browse files
authored
Merge branch 'master' into feat/events-multiselect-bulk-actions
2 parents 9d77538 + 8ac4394 commit 5663900

3 files changed

Lines changed: 54 additions & 1 deletion

File tree

src/models/eventsFactory.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { getMidnightWithTimezoneOffset, getUTCMidnight } from '../utils/dates';
21
import safe from 'safe-regex';
32
import { createProjectEventsByIdLoader } from '../dataLoaders';
43
import RedisHelper from '../redisHelper';
54
import ChartDataService from '../services/chartDataService';
5+
import { getMidnightWithTimezoneOffset, getUTCMidnight } from '../utils/dates';
66

77
const Factory = require('./modelFactory');
88
const mongo = require('../mongo');
@@ -992,6 +992,37 @@ class EventsFactory extends Factory {
992992
},
993993
{ $set: { assignee: normalizedAssignee } }
994994
);
995+
* Remove a single event and all related data (repetitions, daily events)
996+
*
997+
* @param {string|ObjectId} eventId - id of the original event to remove
998+
* @return {Promise<boolean>}
999+
*/
1000+
async removeEvent(eventId) {
1001+
const eventsCollection = this.getCollection(this.TYPES.EVENTS);
1002+
1003+
const event = await eventsCollection.findOne({ _id: new ObjectId(eventId) });
1004+
1005+
// If event is not found, throw error
1006+
if (!event) {
1007+
throw new Error(`Event not found for eventId: ${eventId}`);
1008+
}
1009+
1010+
const { groupHash } = event;
1011+
1012+
// Delete original event
1013+
const result = await eventsCollection.deleteOne({ _id: new ObjectId(eventId) });
1014+
1015+
// Delete all repetitions with same groupHash
1016+
if (await this.isCollectionExists(this.TYPES.REPETITIONS)) {
1017+
await this.getCollection(this.TYPES.REPETITIONS).deleteMany({ groupHash });
1018+
}
1019+
1020+
// Delete all daily event records with same groupHash
1021+
if (await this.isCollectionExists(this.TYPES.DAILY_EVENTS)) {
1022+
await this.getCollection(this.TYPES.DAILY_EVENTS).deleteMany({ groupHash });
1023+
}
1024+
1025+
return result.acknowledged && result.deletedCount > 0;
9951026
}
9961027

9971028
/**

src/resolvers/event.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,19 @@ module.exports = {
199199
success: !!result.acknowledged,
200200
modifiedCount: result.modifiedCount || 0,
201201
};
202+
* Remove event and all related data (repetitions, daily events)
203+
*
204+
* @param {ResolverObj} _obj - resolver context
205+
* @param {string} projectId - project id
206+
* @param {string} eventId - event id to remove
207+
* @return {Promise<boolean>}
208+
*/
209+
async removeEvent(_obj, { projectId, eventId }, context) {
210+
const factory = getEventsFactory(context, projectId);
211+
212+
const result = await factory.removeEvent(eventId);
213+
214+
return result;
202215
},
203216

204217
/**

src/typeDefs/event.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,11 @@ extend type Mutation {
591591
bulkSetEventMarks(
592592
"""
593593
Project id
594+
Remove event and all related data (repetitions, daily events)
595+
"""
596+
removeEvent(
597+
"""
598+
ID of project event is related to
594599
"""
595600
projectId: ID!
596601
@@ -609,6 +614,10 @@ extend type Mutation {
609614
"""
610615
enabled: Boolean!
611616
): BulkSetEventMarksResponse! @requireUserInWorkspace
617+
ID of the event to remove
618+
"""
619+
eventId: ID!
620+
): Boolean! @requireAdmin
612621
613622
"""
614623
Namespace that contains only mutations related to the events

0 commit comments

Comments
 (0)