Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hawk.api",
"version": "1.4.12",
"version": "1.4.13",
"main": "index.ts",
"license": "BUSL-1.1",
"scripts": {
Expand Down
33 changes: 33 additions & 0 deletions src/models/eventsFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,39 @@ class EventsFactory extends Factory {
return collection.updateOne(query, update);
}

/**
* Remove a single event and all related data (repetitions, daily events)
*
* @param {string|ObjectId} eventId - id of the original event to remove
* @return {Promise<boolean>}
*/
async removeEvent(eventId) {
const eventsCollection = this.getCollection(this.TYPES.EVENTS);

const event = await eventsCollection.findOne({ _id: new ObjectId(eventId) });

if (!event) {
throw new Error(`Event not found for eventId: ${eventId}`);
}

const { groupHash } = event;

// Delete original event
const result = await eventsCollection.deleteOne({ _id: new ObjectId(eventId) });

Comment thread
quangtuanitmo18 marked this conversation as resolved.
// Delete all repetitions with same groupHash
if (await this.isCollectionExists(this.TYPES.REPETITIONS)) {
await this.getCollection(this.TYPES.REPETITIONS).deleteMany({ groupHash });
}

// Delete all daily event records with same groupHash
if (await this.isCollectionExists(this.TYPES.DAILY_EVENTS)) {
await this.getCollection(this.TYPES.DAILY_EVENTS).deleteMany({ groupHash });
}
Comment thread
quangtuanitmo18 marked this conversation as resolved.

return result.acknowledged && result.deletedCount > 0;
}

/**
* Remove all project events
*
Expand Down
16 changes: 16 additions & 0 deletions src/resolvers/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,22 @@ module.exports = {
return !!result.acknowledged;
},

/**
* Remove event and all related data (repetitions, daily events)
*
* @param {ResolverObj} _obj - resolver context
* @param {string} projectId - project id
* @param {string} eventId - event id to remove
* @return {Promise<boolean>}
*/
async removeEvent(_obj, { projectId, eventId }, context) {
const factory = getEventsFactory(context, projectId);

const result = await factory.removeEvent(eventId);

return result;
},

/**
* Mutations namespace
*
Expand Down
15 changes: 15 additions & 0 deletions src/typeDefs/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,21 @@ extend type Mutation {
mark: EventMark!
): Boolean!

"""
Remove event and all related data (repetitions, daily events)
"""
removeEvent(
"""
ID of project event is related to
"""
projectId: ID!

"""
ID of the event to remove
"""
eventId: ID!
): Boolean! @requireAdmin

"""
Namespace that contains only mutations related to the events
"""
Expand Down