Skip to content

Commit 92b902f

Browse files
feat: add api remove event
1 parent 3d9de27 commit 92b902f

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

src/models/eventsFactory.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import safe from 'safe-regex';
33
import { createProjectEventsByIdLoader } from '../dataLoaders';
44
import RedisHelper from '../redisHelper';
55
import ChartDataService from '../services/chartDataService';
6-
76
const Factory = require('./modelFactory');
87
const mongo = require('../mongo');
98
const Event = require('../models/event');
@@ -875,6 +874,39 @@ class EventsFactory extends Factory {
875874
return collection.updateOne(query, update);
876875
}
877876

877+
/**
878+
* Remove a single event and all related data (repetitions, daily events)
879+
*
880+
* @param {string|ObjectId} eventId - id of the original event to remove
881+
* @return {Promise<DeleteResult>}
882+
*/
883+
async removeEvent(eventId) {
884+
const eventsCollection = this.getCollection(this.TYPES.EVENTS);
885+
886+
const event = await eventsCollection.findOne({ _id: new ObjectId(eventId) });
887+
888+
if (!event) {
889+
throw new Error(`Event not found for eventId: ${eventId}`);
890+
}
891+
892+
const { groupHash } = event;
893+
894+
// Delete original event
895+
const result = await eventsCollection.deleteOne({ _id: new ObjectId(eventId) });
896+
897+
// Delete all repetitions with same groupHash
898+
if (await this.isCollectionExists(this.TYPES.REPETITIONS)) {
899+
await this.getCollection(this.TYPES.REPETITIONS).deleteMany({ groupHash });
900+
}
901+
902+
// Delete all daily event records with same groupHash
903+
if (await this.isCollectionExists(this.TYPES.DAILY_EVENTS)) {
904+
await this.getCollection(this.TYPES.DAILY_EVENTS).deleteMany({ groupHash });
905+
}
906+
907+
return result;
908+
}
909+
878910
/**
879911
* Remove all project events
880912
*

src/resolvers/event.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,22 @@ module.exports = {
153153
return !!result.acknowledged;
154154
},
155155

156+
/**
157+
* Remove event and all related data (repetitions, daily events)
158+
*
159+
* @param {ResolverObj} _obj - resolver context
160+
* @param {string} projectId - project id
161+
* @param {string} eventId - event id to remove
162+
* @return {Promise<boolean>}
163+
*/
164+
async removeEvent(_obj, { projectId, eventId }, context) {
165+
const factory = getEventsFactory(context, projectId);
166+
167+
const result = await factory.removeEvent(eventId);
168+
169+
return !!result.acknowledged;
170+
},
171+
156172
/**
157173
* Mutations namespace
158174
*

src/typeDefs/event.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,21 @@ extend type Mutation {
504504
mark: EventMark!
505505
): Boolean!
506506
507+
"""
508+
Remove event and all related data (repetitions, daily events)
509+
"""
510+
removeEvent(
511+
"""
512+
ID of project event is related to
513+
"""
514+
projectId: ID!
515+
516+
"""
517+
ID of the event to remove
518+
"""
519+
eventId: ID!
520+
): Boolean! @requireUserInWorkspace
521+
507522
"""
508523
Namespace that contains only mutations related to the events
509524
"""

0 commit comments

Comments
 (0)