Skip to content

Commit 8ac4394

Browse files
feat: add api remove event (#633)
* feat: add api remove event * Bump version up to 1.4.8 * address review comments * chore: hide Mongo-specific delete result behind boolean * Bump version up to 1.4.13 --------- Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
1 parent afaf103 commit 8ac4394

4 files changed

Lines changed: 67 additions & 2 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "hawk.api",
3-
"version": "1.4.12",
3+
"version": "1.4.13",
44
"main": "index.ts",
55
"license": "BUSL-1.1",
66
"scripts": {

src/models/eventsFactory.js

Lines changed: 35 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');
@@ -918,6 +918,40 @@ class EventsFactory extends Factory {
918918
return collection.updateOne(query, update);
919919
}
920920

921+
/**
922+
* Remove a single event and all related data (repetitions, daily events)
923+
*
924+
* @param {string|ObjectId} eventId - id of the original event to remove
925+
* @return {Promise<boolean>}
926+
*/
927+
async removeEvent(eventId) {
928+
const eventsCollection = this.getCollection(this.TYPES.EVENTS);
929+
930+
const event = await eventsCollection.findOne({ _id: new ObjectId(eventId) });
931+
932+
// If event is not found, throw error
933+
if (!event) {
934+
throw new Error(`Event not found for eventId: ${eventId}`);
935+
}
936+
937+
const { groupHash } = event;
938+
939+
// Delete original event
940+
const result = await eventsCollection.deleteOne({ _id: new ObjectId(eventId) });
941+
942+
// Delete all repetitions with same groupHash
943+
if (await this.isCollectionExists(this.TYPES.REPETITIONS)) {
944+
await this.getCollection(this.TYPES.REPETITIONS).deleteMany({ groupHash });
945+
}
946+
947+
// Delete all daily event records with same groupHash
948+
if (await this.isCollectionExists(this.TYPES.DAILY_EVENTS)) {
949+
await this.getCollection(this.TYPES.DAILY_EVENTS).deleteMany({ groupHash });
950+
}
951+
952+
return result.acknowledged && result.deletedCount > 0;
953+
}
954+
921955
/**
922956
* Remove all project events
923957
*

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;
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! @requireAdmin
521+
507522
"""
508523
Namespace that contains only mutations related to the events
509524
"""

0 commit comments

Comments
 (0)