|
1 | | -import { getMidnightWithTimezoneOffset, getUTCMidnight } from '../utils/dates'; |
2 | 1 | import safe from 'safe-regex'; |
3 | 2 | import { createProjectEventsByIdLoader } from '../dataLoaders'; |
4 | 3 | import RedisHelper from '../redisHelper'; |
5 | 4 | import ChartDataService from '../services/chartDataService'; |
| 5 | +import { getMidnightWithTimezoneOffset, getUTCMidnight } from '../utils/dates'; |
6 | 6 |
|
7 | 7 | const Factory = require('./modelFactory'); |
8 | 8 | const mongo = require('../mongo'); |
@@ -992,6 +992,37 @@ class EventsFactory extends Factory { |
992 | 992 | }, |
993 | 993 | { $set: { assignee: normalizedAssignee } } |
994 | 994 | ); |
| 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; |
995 | 1026 | } |
996 | 1027 |
|
997 | 1028 | /** |
|
0 commit comments