Skip to content

Commit 1963544

Browse files
committed
🐛 fix(recurring-events): update event util checks
1 parent 8380a1e commit 1963544

File tree

12 files changed

+32
-197
lines changed

12 files changed

+32
-197
lines changed

packages/backend/src/event/classes/gcal.event.parser.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { faker } from "@faker-js/faker";
33
import { Categories_Recurrence } from "@core/types/event.types";
44
import { WithGcalId, gSchema$Event } from "@core/types/gcal";
55
import dayjs from "@core/util/date/dayjs";
6-
import { isBase, isExistingInstance } from "@core/util/event/event.util";
6+
import { isBase, isInstance } from "@core/util/event/event.util";
77
import { UserDriver } from "@backend/__tests__/drivers/user.driver";
88
import {
99
cleanupCollections,
@@ -546,7 +546,7 @@ describe("GcalEventParser", () => {
546546
.toArray();
547547

548548
const dbSeriesBase = dbSeries.find(isBase);
549-
const dbSeriesInstance = dbSeries.find(isExistingInstance);
549+
const dbSeriesInstance = dbSeries.find(isInstance);
550550

551551
expect(changes).toHaveLength(1);
552552

packages/backend/src/event/classes/gcal.event.parser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
import { WithGcalId, gSchema$Event, gSchema$EventBase } from "@core/types/gcal";
1212
import {
1313
isBase,
14-
isExistingInstance,
14+
isInstance,
1515
isRegularEvent,
1616
} from "@core/util/event/event.util";
1717
import {
@@ -125,7 +125,7 @@ export class GcalEventParser {
125125
this.#compassEvent = cEvent ?? null;
126126

127127
this.#isInstance = isInstanceGCalEvent(this.#event);
128-
this.#isCompassInstance = cEvent ? isExistingInstance(cEvent) : false;
128+
this.#isCompassInstance = cEvent ? isInstance(cEvent) : false;
129129

130130
this.#isBase = isBaseGCalEvent(this.#event);
131131
this.#isCompassBase = cEvent ? isBase(cEvent) : false;

packages/backend/src/event/classes/gcal.event.rrule.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { recurring } from "@core/__mocks__/v1/events/gcal/gcal.recurring";
55
import { GCAL_MAX_RECURRENCES } from "@core/constants/core.constants";
66
import { gSchema$EventBase } from "@core/types/gcal";
77
import dayjs from "@core/util/date/dayjs";
8-
import { isExistingInstance } from "@core/util/event/event.util";
8+
import { isInstance } from "@core/util/event/event.util";
99
import {
1010
isBaseGCalEvent,
1111
isInstanceGCalEvent,
@@ -281,7 +281,7 @@ describe("GcalEventRRule: ", () => {
281281
expect(instance.user).toEqual(userId);
282282
expect(instance.startDate).toBeDefined();
283283
expect(instance.endDate).toBeDefined();
284-
expect(isExistingInstance(instance)).toEqual(true);
284+
expect(isInstance(instance)).toEqual(true);
285285

286286
expect(instance.startDate).toEqual(
287287
startDate.add(index, "day").format(dateFormat),
@@ -313,7 +313,7 @@ describe("GcalEventRRule: ", () => {
313313
expect(instance.user).toEqual(userId);
314314
expect(instance.startDate).toBeDefined();
315315
expect(instance.endDate).toBeDefined();
316-
expect(isExistingInstance(instance)).toEqual(true);
316+
expect(isInstance(instance)).toEqual(true);
317317

318318
expect(instance.startDate).toEqual(
319319
startDate.add(index, "day").format(dateFormat),

packages/backend/src/event/services/event.find.test.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ import {
99
RecurringEventUpdateScope,
1010
Schema_Event,
1111
} from "@core/types/event.types";
12-
import {
13-
isBase,
14-
isExistingInstance,
15-
isInstance,
16-
} from "@core/util/event/event.util";
12+
import { isBase, isInstance } from "@core/util/event/event.util";
1713
import { createMockBaseEvent } from "@core/util/test/ccal.event.factory";
1814
import { UtilDriver } from "@backend/__tests__/drivers/util.driver";
1915
import {
@@ -99,7 +95,7 @@ describe("Jan 2022: Many Formats", () => {
9995
const baseEvents = result.filter(isBase);
10096
expect(baseEvents).toHaveLength(0);
10197

102-
const instances = result.filter(isExistingInstance);
98+
const instances = result.filter(isInstance);
10399
expect(instances).toHaveLength(gInstances.length);
104100
});
105101

packages/backend/src/event/services/event.service.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ import { gSchema$Event } from "@core/types/gcal";
3131
import { IDSchema } from "@core/types/type.utils";
3232
import { getCurrentRangeDates } from "@core/util/date/date.util";
3333
import { CompassEventRRule } from "@core/util/event/compass.event.rrule";
34-
import {
35-
isExistingInstance,
36-
parseCompassEventDate,
37-
} from "@core/util/event/event.util";
34+
import { isInstance, parseCompassEventDate } from "@core/util/event/event.util";
3835
import { getGcalClient } from "@backend/auth/services/google.auth.service";
3936
import { Collections } from "@backend/common/constants/collections";
4037
import { EventError } from "@backend/common/errors/event/event.errors";
@@ -183,7 +180,7 @@ class EventService {
183180
}
184181

185182
const baseEventIds = events
186-
.filter(isExistingInstance)
183+
.filter(isInstance)
187184
.map((e) => new ObjectId(e.recurrence?.eventId));
188185

189186
const baseEvents = await mongoService.event
@@ -192,7 +189,7 @@ class EventService {
192189

193190
return events
194191
.map((event) => {
195-
if (isExistingInstance(event)) {
192+
if (isInstance(event)) {
196193
const baseEvent = baseEvents.find(
197194
({ _id }) => _id.toString() === event.recurrence?.eventId,
198195
);
@@ -242,9 +239,7 @@ class EventService {
242239
);
243240
}
244241

245-
const isInstance = isExistingInstance(event);
246-
247-
if (isInstance) {
242+
if (isInstance(event)) {
248243
const baseEvent = await mongoService.event.findOne({
249244
user: userId,
250245
_id: new ObjectId(event.recurrence?.eventId),

packages/backend/src/sync/controllers/sync.controller.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { EVENT_CHANGED } from "@core/constants/websocket.constants";
77
import { Status } from "@core/errors/status.codes";
88
import { Resource_Sync } from "@core/types/sync.types";
99
import { Schema_User } from "@core/types/user.types";
10-
import { isBase, isExistingInstance } from "@core/util/event/event.util";
10+
import { isBase, isInstance } from "@core/util/event/event.util";
1111
import { BaseDriver } from "@backend/__tests__/drivers/base.driver";
1212
import { SyncControllerDriver } from "@backend/__tests__/drivers/sync.controller.driver";
1313
import { SyncDriver } from "@backend/__tests__/drivers/sync.driver";
@@ -150,7 +150,7 @@ describe("SyncController", () => {
150150
});
151151

152152
const baseEvent = currentEventsInDb.find(isBase)!;
153-
const firstInstance = currentEventsInDb.find(isExistingInstance)!;
153+
const firstInstance = currentEventsInDb.find(isInstance)!;
154154

155155
expect(baseEvent).toBeDefined();
156156
expect(firstInstance).toBeDefined();
@@ -190,7 +190,7 @@ describe("SyncController", () => {
190190
expect(baseEvents[0]?.title).toBe("Recurrence");
191191

192192
// Verify we have the correct instance
193-
const instanceEvents = currentEventsInDb.filter(isExistingInstance);
193+
const instanceEvents = currentEventsInDb.filter(isInstance);
194194

195195
expect(instanceEvents).toHaveLength(3);
196196

@@ -217,7 +217,7 @@ describe("SyncController", () => {
217217
});
218218

219219
// Get all instance events
220-
const instances = currentEventsInDb.filter(isExistingInstance);
220+
const instances = currentEventsInDb.filter(isInstance);
221221

222222
// For each instance event, verify there are no duplicates
223223
const eventIds = new Set<string>();
@@ -241,7 +241,7 @@ describe("SyncController", () => {
241241
});
242242

243243
const regularEvents = currentEventsInDb.filter(
244-
(e) => !isBase(e) && !isExistingInstance(e),
244+
(e) => !isBase(e) && !isInstance(e),
245245
);
246246

247247
expect(regularEvents).toHaveLength(1);

packages/backend/src/sync/services/import/sync.import.full.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isBase, isExistingInstance } from "@core/util/event/event.util";
1+
import { isBase, isInstance } from "@core/util/event/event.util";
22
import { UtilDriver } from "@backend/__tests__/drivers/util.driver";
33
import {
44
getCategorizedEventsInDb,
@@ -30,7 +30,7 @@ describe("SyncImport: Full", () => {
3030
});
3131

3232
const baseEvent = currentEventsInDb.find(isBase)!;
33-
const firstInstance = currentEventsInDb.find(isExistingInstance)!;
33+
const firstInstance = currentEventsInDb.find(isInstance)!;
3434

3535
expect(baseEvent).toBeDefined();
3636
expect(firstInstance).toBeDefined();
@@ -75,7 +75,7 @@ describe("SyncImport: Full", () => {
7575
expect(baseEvents[0]?.title).toBe("Recurrence");
7676

7777
// Verify we have the correct instance
78-
const instanceEvents = currentEventsInDb.filter(isExistingInstance);
78+
const instanceEvents = currentEventsInDb.filter(isInstance);
7979

8080
expect(instanceEvents).toHaveLength(3);
8181

@@ -108,7 +108,7 @@ describe("SyncImport: Full", () => {
108108
});
109109

110110
// Get all instance events
111-
const instances = currentEventsInDb.filter(isExistingInstance);
111+
const instances = currentEventsInDb.filter(isInstance);
112112

113113
// For each instance event, verify there are no duplicates
114114
const eventIds = new Set<string>();

packages/backend/src/sync/services/import/sync.import.util.test.ts

Lines changed: 1 addition & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
1-
import { ObjectId } from "mongodb";
21
import { gcalEvents } from "@core/__mocks__/v1/events/gcal/gcal.event";
3-
import { Event_Core } from "@core/types/event.types";
42
import { gSchema$Event } from "@core/types/gcal";
53
import dayjs from "@core/util/date/dayjs";
6-
import {
7-
createMockBaseEvent,
8-
createMockInstance,
9-
} from "@core/util/test/ccal.event.factory";
10-
import { generateGcalId } from "@backend/__tests__/mocks.gcal/factories/gcal.event.factory";
114
import { cancelledEventsIds } from "@backend/common/services/gcal/gcal.utils";
12-
import {
13-
assignIdsToEvents,
14-
organizeGcalEventsByType,
15-
} from "@backend/sync/services/import/sync.import.util";
5+
import { organizeGcalEventsByType } from "@backend/sync/services/import/sync.import.util";
166
import { syncExpired, syncExpiresSoon } from "@backend/sync/util/sync.util";
177

188
describe("categorizeGcalEvents", () => {
@@ -96,61 +86,3 @@ describe("Sync Expiry Checks", () => {
9686
expect(expiresSoon).toBe(false);
9787
});
9888
});
99-
100-
const getEventsWithoutIds = () => {
101-
// Create a base event with a Google ID
102-
const baseEvent = createMockBaseEvent();
103-
const baseGEventId = generateGcalId();
104-
105-
baseEvent.gEventId = baseGEventId;
106-
107-
delete (baseEvent as Partial<typeof baseEvent>)._id;
108-
109-
const gEventId = baseEvent.gEventId as string;
110-
111-
// Create instances that reference the base event's Google ID
112-
const instance1 = createMockInstance(baseGEventId, gEventId, {});
113-
const instance2 = createMockInstance(baseGEventId, gEventId, {});
114-
115-
// delete _ids to simulate a newly mapped event
116-
delete (instance1 as Partial<typeof instance1>)._id;
117-
delete (instance1 as Partial<typeof instance1>).recurrence;
118-
delete (instance2 as Partial<typeof instance2>)._id;
119-
delete (instance2 as Partial<typeof instance2>).recurrence;
120-
121-
const eventsWithoutIds = [baseEvent, instance1, instance2];
122-
123-
return eventsWithoutIds;
124-
};
125-
126-
describe("assignIdsToEvents", () => {
127-
it("assigns Mongo ObjectIds to events", () => {
128-
const eventsWithoutIds = getEventsWithoutIds() as Event_Core[];
129-
130-
const events = assignIdsToEvents(eventsWithoutIds);
131-
132-
// Verify all events have ObjectIds
133-
for (const event of events) {
134-
expect(event?._id).toBeDefined();
135-
expect(event._id).toBeInstanceOf(ObjectId);
136-
}
137-
});
138-
139-
it("links instances to base events", () => {
140-
const eventsWithoutIds = getEventsWithoutIds() as Event_Core[];
141-
142-
const events = assignIdsToEvents(eventsWithoutIds);
143-
144-
const baseEvent = events.find((event) => event.recurrence?.rule);
145-
146-
expect(baseEvent).toBeDefined();
147-
148-
const instances = events.filter((event) => event.recurrence?.eventId);
149-
150-
expect(instances).toHaveLength(2);
151-
152-
for (const instance of instances) {
153-
expect(instance.recurrence?.eventId).toBe(baseEvent?._id?.toString());
154-
}
155-
});
156-
});

packages/backend/src/sync/services/import/sync.import.util.ts

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,7 @@
1-
import { AnyBulkWriteOperation, ObjectId } from "mongodb";
2-
import {
3-
Event_Core,
4-
RecurrenceWithoutId,
5-
Schema_Event_Core,
6-
Schema_Event_Regular,
7-
WithoutCompassId,
8-
} from "@core/types/event.types";
1+
import { AnyBulkWriteOperation } from "mongodb";
2+
import { Schema_Event_Core } from "@core/types/event.types";
93
import { gSchema$Event } from "@core/types/gcal";
10-
import {
11-
isBase,
12-
isInstanceWithoutId,
13-
isRegularEvent,
14-
} from "@core/util/event/event.util";
154
import { cancelledEventsIds } from "@backend/common/services/gcal/gcal.utils";
16-
import { Event_Core_WithObjectId } from "@backend/sync/sync.types";
175

186
export const assembleEventOperations = (
197
userId: string,
@@ -81,42 +69,3 @@ export const organizeGcalEventsByType = (events: gSchema$Event[]) => {
8169
};
8270
return categorized;
8371
};
84-
85-
/**
86-
* Assigns IDs to events
87-
* will map instance events to their base events
88-
* @param events - The events to assign IDs
89-
* @returns The events with IDs assigned
90-
*/
91-
export const assignIdsToEvents = (
92-
events: Array<RecurrenceWithoutId | WithoutCompassId<Schema_Event_Regular>>,
93-
): Array<Event_Core_WithObjectId> => {
94-
const idMaps = new Map<string, ObjectId>();
95-
96-
return events.map((e) => {
97-
const event = e as Event_Core;
98-
const isBaseEvent = isBase(event);
99-
const isInstance = isInstanceWithoutId(event);
100-
const isRegular = isRegularEvent(event);
101-
const baseEventId = event?.gRecurringEventId ?? (event?.gEventId as string);
102-
103-
if (!idMaps.get(baseEventId)) idMaps.set(baseEventId, new ObjectId());
104-
105-
const baseEventObjectId = idMaps.get(baseEventId)!;
106-
107-
const result = {
108-
...event,
109-
_id: isBaseEvent ? baseEventObjectId : new ObjectId(),
110-
};
111-
112-
if (!isRegular) {
113-
if (isInstance) {
114-
result.recurrence = { eventId: baseEventObjectId.toString() };
115-
} else if (event.recurrence) {
116-
result.recurrence = event.recurrence;
117-
}
118-
}
119-
120-
return result;
121-
});
122-
};

packages/backend/src/sync/services/sync/__tests__/gcal.sync.processor.test.util.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import dayjs from "@core/util/date/dayjs";
1111
import {
1212
categorizeEvents,
1313
isBase,
14-
isExistingInstance,
14+
isInstance,
1515
} from "@core/util/event/event.util";
1616
import { getEventsInDb } from "@backend/__tests__/helpers/mock.db.queries";
1717
import { State_AfterGcalImport } from "@backend/__tests__/helpers/mock.events.init";
@@ -147,7 +147,7 @@ export const noInstancesAfterSplitDate = async (
147147
const splitDate = new Date(splitDateStr);
148148
const futureInstances = events.filter(
149149
(e) =>
150-
isExistingInstance(e as unknown as Schema_Event) &&
150+
isInstance(e as unknown as Schema_Event) &&
151151
new Date(e.startDate) > splitDate,
152152
);
153153
expect(futureInstances).toHaveLength(0);

0 commit comments

Comments
 (0)