|
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'); |
@@ -918,6 +918,40 @@ class EventsFactory extends Factory { |
918 | 918 | return collection.updateOne(query, update); |
919 | 919 | } |
920 | 920 |
|
| 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 | + |
921 | 955 | /** |
922 | 956 | * Remove all project events |
923 | 957 | * |
|
0 commit comments